mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
CW-599-Extract-Secure-Storage (#1353)
* feat: Modify app to depend on secure storage abstraction instead of the direct package * chore: Revert command * Update configure.dart [skip ci] * Update configure.dart * Fix conflicts * clean up and fixes * minor fix --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
co-authored by
Omar Hatem
parent
c35929f28e
commit
bfb78eded9
@@ -1,10 +1,10 @@
|
||||
import 'dart:io' show Directory, File, Platform;
|
||||
import 'package:cake_wallet/bitcoin/bitcoin.dart';
|
||||
import 'package:cake_wallet/core/secure_storage.dart';
|
||||
import 'package:cake_wallet/entities/exchange_api_mode.dart';
|
||||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
import 'package:cw_core/pathForWallet.dart';
|
||||
import 'package:cake_wallet/entities/secret_store_key.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -42,7 +42,7 @@ const newCakeWalletBitcoinUri = 'btc-electrum.cakewallet.com:50002';
|
||||
Future<void> defaultSettingsMigration(
|
||||
{required int version,
|
||||
required SharedPreferences sharedPreferences,
|
||||
required FlutterSecureStorage secureStorage,
|
||||
required SecureStorage secureStorage,
|
||||
required Box<Node> nodes,
|
||||
required Box<Node> powNodes,
|
||||
required Box<WalletInfo> walletInfoSource,
|
||||
@@ -485,7 +485,7 @@ Node? getTronDefaultNode({required Box<Node> nodes}) {
|
||||
|
||||
Future<void> insecureStorageMigration({
|
||||
required SharedPreferences sharedPreferences,
|
||||
required FlutterSecureStorage secureStorage,
|
||||
required SecureStorage secureStorage,
|
||||
}) async {
|
||||
bool? allowBiometricalAuthentication =
|
||||
sharedPreferences.getBool(SecureKey.allowBiometricalAuthenticationKey);
|
||||
@@ -559,7 +559,7 @@ Future<void> insecureStorageMigration({
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> rewriteSecureStoragePin({required FlutterSecureStorage secureStorage}) async {
|
||||
Future<void> rewriteSecureStoragePin({required SecureStorage secureStorage}) async {
|
||||
// the bug only affects ios/mac:
|
||||
if (!Platform.isIOS && !Platform.isMacOS) {
|
||||
return;
|
||||
@@ -585,8 +585,9 @@ Future<void> rewriteSecureStoragePin({required FlutterSecureStorage secureStorag
|
||||
await secureStorage.write(
|
||||
key: keyForPinCode,
|
||||
value: encodedPin,
|
||||
iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock),
|
||||
mOptions: MacOsOptions(accessibility: KeychainAccessibility.first_unlock),
|
||||
// TODO: find a way to add those with the generated secure storage
|
||||
// iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock),
|
||||
// mOptions: MacOsOptions(accessibility: KeychainAccessibility.first_unlock),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -720,7 +721,7 @@ Future<void> updateDisplayModes(SharedPreferences sharedPreferences) async {
|
||||
await sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey, balanceDisplayMode);
|
||||
}
|
||||
|
||||
Future<void> generateBackupPassword(FlutterSecureStorage secureStorage) async {
|
||||
Future<void> generateBackupPassword(SecureStorage secureStorage) async {
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.backupPassword);
|
||||
|
||||
if ((await secureStorage.read(key: key))?.isNotEmpty ?? false) {
|
||||
|
||||
@@ -2,7 +2,6 @@ import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'package:cake_wallet/core/secure_storage.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
@@ -11,8 +10,7 @@ import 'package:cake_wallet/entities/contact.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/encrypt.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
import 'package:cake_wallet/entities/ios_legacy_helper.dart'
|
||||
as ios_legacy_helper;
|
||||
import 'package:cake_wallet/entities/ios_legacy_helper.dart' as ios_legacy_helper;
|
||||
import 'package:cake_wallet/entities/preferences_key.dart';
|
||||
import 'package:cake_wallet/entities/secret_store_key.dart';
|
||||
import 'package:cw_core/wallet_info.dart';
|
||||
@@ -30,8 +28,8 @@ Future<void> migrate_android_v1() async {
|
||||
await android_migrate_wallets(appDocDir: appDocDir);
|
||||
}
|
||||
|
||||
Future<void> ios_migrate_v1(Box<WalletInfo> walletInfoSource,
|
||||
Box<Trade> tradeSource, Box<Contact> contactSource) async {
|
||||
Future<void> ios_migrate_v1(
|
||||
Box<WalletInfo> walletInfoSource, Box<Trade> tradeSource, Box<Contact> contactSource) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
if (prefs.getBool('ios_migration_v1_completed') ?? false) {
|
||||
@@ -67,10 +65,7 @@ Future<void> ios_migrate_user_defaults() async {
|
||||
if (activeCurrency != null) {
|
||||
final convertedCurrency = convertFiatLegacy(activeCurrency);
|
||||
|
||||
if (convertedCurrency != null) {
|
||||
await prefs.setString(
|
||||
'current_fiat_currency', convertedCurrency.serialize());
|
||||
}
|
||||
await prefs.setString('current_fiat_currency', convertedCurrency.serialize());
|
||||
}
|
||||
|
||||
//translate fee priority
|
||||
@@ -81,24 +76,21 @@ Future<void> ios_migrate_user_defaults() async {
|
||||
}
|
||||
|
||||
//translate current balance mode
|
||||
final currentBalanceMode =
|
||||
await ios_legacy_helper.getInt('display_balance_mode');
|
||||
final currentBalanceMode = await ios_legacy_helper.getInt('display_balance_mode');
|
||||
if (currentBalanceMode != null) {
|
||||
await prefs.setInt('current_balance_display_mode', currentBalanceMode);
|
||||
}
|
||||
|
||||
//translate should save recipient address
|
||||
final shouldSave =
|
||||
await ios_legacy_helper.getBool('should_save_recipient_address');
|
||||
|
||||
final shouldSave = await ios_legacy_helper.getBool('should_save_recipient_address');
|
||||
|
||||
if (shouldSave != null) {
|
||||
await prefs.setBool('save_recipient_address', shouldSave);
|
||||
}
|
||||
|
||||
//translate biometric
|
||||
final biometricOn =
|
||||
await ios_legacy_helper.getBool('biometric_authentication_on');
|
||||
|
||||
final biometricOn = await ios_legacy_helper.getBool('biometric_authentication_on');
|
||||
|
||||
if (biometricOn != null) {
|
||||
await prefs.setBool('allow_biometrical_authentication', biometricOn);
|
||||
}
|
||||
@@ -137,9 +129,8 @@ Future<void> ios_migrate_pin() async {
|
||||
return;
|
||||
}
|
||||
|
||||
final flutterSecureStorage = FlutterSecureStorage();
|
||||
final pinPassword = await flutterSecureStorage.read(
|
||||
key: 'pin_password', iOptions: IOSOptions());
|
||||
final flutterSecureStorage = secureStorageShared;
|
||||
final pinPassword = await flutterSecureStorage.readNoIOptions(key: 'pin_password');
|
||||
// No pin
|
||||
if (pinPassword == null) {
|
||||
await prefs.setBool('ios_migration_pin_completed', true);
|
||||
@@ -148,7 +139,7 @@ Future<void> ios_migrate_pin() async {
|
||||
|
||||
final key = generateStoreKeyFor(key: SecretStoreKey.pinCodePassword);
|
||||
final encodedPassword = encodedPinCode(pin: pinPassword);
|
||||
await writeSecureStorage(flutterSecureStorage, key: key, value: encodedPassword);
|
||||
await flutterSecureStorage.write(key: key, value: encodedPassword);
|
||||
|
||||
await prefs.setBool('ios_migration_pin_completed', true);
|
||||
}
|
||||
@@ -161,7 +152,7 @@ Future<void> ios_migrate_wallet_passwords() async {
|
||||
}
|
||||
|
||||
final appDocDir = await getApplicationDocumentsDirectory();
|
||||
final flutterSecureStorage = FlutterSecureStorage();
|
||||
final flutterSecureStorage = secureStorageShared;
|
||||
final keyService = KeyService(flutterSecureStorage);
|
||||
final walletsDir = Directory('${appDocDir.path}/wallets');
|
||||
final moneroWalletsDir = Directory('${walletsDir.path}/monero');
|
||||
@@ -176,10 +167,8 @@ Future<void> ios_migrate_wallet_passwords() async {
|
||||
if (item is Directory) {
|
||||
final name = item.path.split('/').last;
|
||||
final oldKey = 'wallet_monero_' + name + '_password';
|
||||
final password = await flutterSecureStorage.read(
|
||||
key: oldKey, iOptions: IOSOptions());
|
||||
await keyService.saveWalletPassword(
|
||||
walletName: name, password: password!);
|
||||
final password = await flutterSecureStorage.readNoIOptions(key: oldKey);
|
||||
await keyService.saveWalletPassword(walletName: name, password: password!);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
@@ -311,18 +300,14 @@ Future<void> ios_migrate_wallet_info(Box<WalletInfo> walletsInfoSource) async {
|
||||
return null;
|
||||
}
|
||||
|
||||
final config = json.decode(configFile.readAsStringSync())
|
||||
as Map<String, dynamic>;
|
||||
final isRecovery = config['isRecovery'] as bool ?? false;
|
||||
final config = json.decode(configFile.readAsStringSync()) as Map<String, dynamic>;
|
||||
final isRecovery = config['isRecovery'] as bool? ?? false;
|
||||
final dateAsDouble = config['date'] as double;
|
||||
final timestamp = dateAsDouble.toInt() * 1000;
|
||||
final date = DateTime.fromMillisecondsSinceEpoch(timestamp);
|
||||
final id = walletTypeToString(WalletType.monero).toLowerCase() +
|
||||
'_' +
|
||||
name;
|
||||
final exist = walletsInfoSource.values
|
||||
.firstWhereOrNull((el) => el.id == id) != null;
|
||||
|
||||
final id = walletTypeToString(WalletType.monero).toLowerCase() + '_' + name;
|
||||
final exist = walletsInfoSource.values.firstWhereOrNull((el) => el.id == id) != null;
|
||||
|
||||
if (exist) {
|
||||
return null;
|
||||
}
|
||||
@@ -373,12 +358,10 @@ Future<void> ios_migrate_trades_list(Box<Trade> tradeSource) async {
|
||||
}
|
||||
|
||||
final content = file.readAsBytesSync();
|
||||
final flutterSecureStorage = FlutterSecureStorage();
|
||||
final masterPassword = await flutterSecureStorage.read(
|
||||
key: 'master_password', iOptions: IOSOptions());
|
||||
final flutterSecureStorage = secureStorageShared;
|
||||
final masterPassword = await flutterSecureStorage.readNoIOptions(key: 'master_password');
|
||||
final key = masterPassword!.replaceAll('-', '');
|
||||
final decoded =
|
||||
await ios_legacy_helper.decrypt(content, key: key, salt: secrets.salt);
|
||||
final decoded = await ios_legacy_helper.decrypt(content, key: key, salt: secrets.salt);
|
||||
final decodedJson = json.decode(decoded) as List<dynamic>;
|
||||
final trades = decodedJson.map((dynamic el) {
|
||||
final elAsMap = el as Map<String, dynamic>;
|
||||
@@ -441,8 +424,7 @@ Future<void> ios_migrate_address_book(Box<Contact> contactSource) async {
|
||||
final address = _item["address"] as String;
|
||||
final name = _item["name"] as String;
|
||||
|
||||
return Contact(
|
||||
address: address, name: name, type: CryptoCurrency.fromString(type));
|
||||
return Contact(address: address, name: name, type: CryptoCurrency.fromString(type));
|
||||
});
|
||||
|
||||
await contactSource.addAll(contacts);
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import 'package:cake_wallet/core/secure_storage.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:cw_core/cake_hive.dart';
|
||||
|
||||
Future<List<int>> getEncryptionKey(
|
||||
{required String forKey, required FlutterSecureStorage secureStorage}) async {
|
||||
{required String forKey, required SecureStorage secureStorage}) async {
|
||||
final stringifiedKey = await secureStorage.read(key: 'transactionDescriptionsBoxKey');
|
||||
List<int> key;
|
||||
|
||||
@@ -11,7 +10,7 @@ Future<List<int>> getEncryptionKey(
|
||||
key = CakeHive.generateSecureKey();
|
||||
final keyStringified = key.join(',');
|
||||
String storageKey = 'transactionDescriptionsBoxKey';
|
||||
await writeSecureStorage(secureStorage, key: storageKey, value: keyStringified);
|
||||
await secureStorage.write(key: storageKey, value: keyStringified);
|
||||
} else {
|
||||
key = stringifiedKey.split(',').map((i) => int.parse(i)).toList();
|
||||
}
|
||||
|
||||
@@ -61,9 +61,9 @@ class PreferencesKey {
|
||||
static const defaultBananoRep = 'default_banano_representative';
|
||||
static const lookupsTwitter = 'looks_up_twitter';
|
||||
static const lookupsMastodon = 'looks_up_mastodon';
|
||||
static const lookupsYatService = 'looks_up_mastodon';
|
||||
static const lookupsUnstoppableDomains = 'looks_up_mastodon';
|
||||
static const lookupsOpenAlias = 'looks_up_mastodon';
|
||||
static const lookupsYatService = 'looks_up_yat';
|
||||
static const lookupsUnstoppableDomains = 'looks_up_unstoppable_domain';
|
||||
static const lookupsOpenAlias = 'looks_up_open_alias';
|
||||
static const lookupsENS = 'looks_up_ens';
|
||||
|
||||
static String moneroWalletUpdateV1Key(String name) =>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:cake_wallet/core/secure_storage.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
enum SecretStoreKey { moneroWalletPassword, pinCodePassword, backupPassword }
|
||||
@@ -66,7 +66,7 @@ class SecureKey {
|
||||
static const lastAuthTimeMilliseconds = 'last_auth_time_milliseconds';
|
||||
|
||||
static Future<int?> getInt({
|
||||
required FlutterSecureStorage secureStorage,
|
||||
required SecureStorage secureStorage,
|
||||
required SharedPreferences sharedPreferences,
|
||||
required String key,
|
||||
}) async {
|
||||
@@ -76,7 +76,7 @@ class SecureKey {
|
||||
}
|
||||
|
||||
static Future<bool?> getBool({
|
||||
required FlutterSecureStorage secureStorage,
|
||||
required SecureStorage secureStorage,
|
||||
required SharedPreferences sharedPreferences,
|
||||
required String key,
|
||||
}) async {
|
||||
@@ -91,7 +91,7 @@ class SecureKey {
|
||||
}
|
||||
|
||||
static Future<String?> getString({
|
||||
required FlutterSecureStorage secureStorage,
|
||||
required SecureStorage secureStorage,
|
||||
required SharedPreferences sharedPreferences,
|
||||
required String key,
|
||||
}) async {
|
||||
|
||||
Reference in New Issue
Block a user