stack_wallet/lib/utilities/flutter_secure_storage_interface.dart

218 lines
4.9 KiB
Dart
Raw Normal View History

2022-08-26 08:11:35 +00:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2022-11-09 22:43:26 +00:00
import 'package:isar/isar.dart';
import 'package:stack_wallet_backup/secure_storage.dart';
2022-08-26 08:11:35 +00:00
abstract class FlutterSecureStorageInterface {
Future<void> write({
required String key,
required String? value,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
});
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
});
Future<void> delete({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
});
}
2022-11-09 22:43:26 +00:00
class DesktopPWStore {
final StorageCryptoHandler handler;
late final Isar isar;
DesktopPWStore(this.handler);
Future<void> init() async {}
Future<String?> read({
required String key,
}) async {
// final String encryptedString =
return "";
}
Future<void> write({
required String key,
required String? value,
}) async {
return;
}
Future<void> delete({
required String key,
}) async {
return;
}
}
/// all *Options params ignored on desktop
2022-08-26 08:11:35 +00:00
class SecureStorageWrapper implements FlutterSecureStorageInterface {
2022-11-09 22:43:26 +00:00
final dynamic _store;
final bool _isDesktop;
2022-08-26 08:11:35 +00:00
2022-11-09 22:43:26 +00:00
const SecureStorageWrapper({
required dynamic store,
required bool isDesktop,
}) : assert(isDesktop
? store is DesktopPWStore
: store is FlutterSecureStorage),
_store = store,
_isDesktop = isDesktop;
2022-08-26 08:11:35 +00:00
@override
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
2022-11-09 22:43:26 +00:00
}) async {
if (_isDesktop) {
return await (_store as DesktopPWStore).read(key: key);
} else {
return await (_store as FlutterSecureStorage).read(
key: key,
iOptions: iOptions,
aOptions: aOptions,
lOptions: lOptions,
webOptions: webOptions,
mOptions: mOptions,
wOptions: wOptions,
);
}
2022-08-26 08:11:35 +00:00
}
@override
Future<void> write({
required String key,
required String? value,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
2022-11-09 22:43:26 +00:00
}) async {
if (_isDesktop) {
return await (_store as DesktopPWStore).write(key: key, value: value);
} else {
return await (_store as FlutterSecureStorage).write(
key: key,
value: value,
iOptions: iOptions,
aOptions: aOptions,
lOptions: lOptions,
webOptions: webOptions,
mOptions: mOptions,
wOptions: wOptions,
);
}
2022-08-26 08:11:35 +00:00
}
@override
Future<void> delete({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
2022-11-09 22:43:26 +00:00
if (_isDesktop) {
return (_store as DesktopPWStore).delete(key: key);
} else {
return await (_store as FlutterSecureStorage).delete(
key: key,
iOptions: iOptions,
aOptions: aOptions,
lOptions: lOptions,
webOptions: webOptions,
mOptions: mOptions,
wOptions: wOptions,
);
}
2022-08-26 08:11:35 +00:00
}
}
// Mock class for testing purposes
class FakeSecureStorage implements FlutterSecureStorageInterface {
final Map<String, String?> _store = {};
int _interactions = 0;
int get interactions => _interactions;
int _writes = 0;
int get writes => _writes;
int _reads = 0;
int get reads => _reads;
int _deletes = 0;
int get deletes => _deletes;
@override
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
_interactions++;
_reads++;
return _store[key];
}
@override
Future<void> write({
required String key,
required String? value,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
_interactions++;
_writes++;
_store[key] = value;
}
@override
Future<void> delete({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
_interactions++;
_deletes++;
_store.remove(key);
}
}