additional refactor improvements

This commit is contained in:
fosse 2023-10-09 17:19:58 -04:00
parent d1f421a760
commit bb422ed224
12 changed files with 108 additions and 80 deletions

View file

@ -3,10 +3,18 @@ import 'package:cw_core/wallet_info.dart';
class BitcoinNewWalletCredentials extends WalletCredentials {
BitcoinNewWalletCredentials(
{required String name, WalletInfo? walletInfo, this.derivationType, this.derivationPath})
: super(name: name, walletInfo: walletInfo);
DerivationType? derivationType;
String? derivationPath;
{required String name,
WalletInfo? walletInfo,
DerivationType? derivationType,
String? derivationPath})
: super(
name: name,
walletInfo: walletInfo,
derivationInfo: DerivationInfo(
derivationType: derivationType,
derivationPath: derivationPath,
),
);
}
class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
@ -15,8 +23,8 @@ class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
required String password,
required this.mnemonic,
WalletInfo? walletInfo,
DerivationType? derivationType,
String? derivationPath,
required DerivationType derivationType,
required String derivationPath,
}) : super(
name: name,
password: password,

View file

@ -46,12 +46,9 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
@override
Future<BitcoinWallet> create(BitcoinNewWalletCredentials credentials) async {
// set default if not present:
DerivationType derivationType = credentials.derivationType ?? DerivationType.electrum2;
String derivationPath = credentials.derivationPath ?? "m/0'/1";
// only set if not in the walletInfo already:
credentials.walletInfo!.derivationInfo ??=
DerivationInfo(derivationType: derivationType, derivationPath: derivationPath);
// set the walletInfo's derivationInfo if not present:
credentials.walletInfo!.derivationInfo ??= credentials.derivationInfo;
final wallet = await BitcoinWalletBase.create(
mnemonic: await generateElectrumMnemonic(strength: 132),

View file

@ -19,15 +19,13 @@ class NanoRestoreWalletFromSeedCredentials extends WalletCredentials {
required this.mnemonic,
int height = 0,
String? password,
DerivationType? derivationType,
required DerivationType derivationType,
}) : super(
name: name,
password: password,
height: height,
derivationInfo: DerivationInfo(
derivationType: derivationType,
height: height,
));
name: name,
password: password,
height: height,
derivationInfo: DerivationInfo(derivationType: derivationType),
);
final String mnemonic;
}
@ -41,10 +39,13 @@ class NanoRestoreWalletFromKeysCredentials extends WalletCredentials {
NanoRestoreWalletFromKeysCredentials({
required String name,
required String password,
required DerivationType derivationType,
required this.seedKey,
this.derivationType,
}) : super(name: name, password: password);
}) : super(
name: name,
password: password,
derivationInfo: DerivationInfo(derivationType: derivationType),
);
final String seedKey;
final DerivationType? derivationType;
}

View file

@ -32,7 +32,8 @@ class NanoWalletService extends WalletService<NanoNewWalletCredentials,
String mnemonic = NanoUtil.seedToMnemonic(seedKey);
// set default if not present:
DerivationType derivationType = credentials.derivationInfo?.derivationType ?? DerivationType.nano;
DerivationType derivationType =
credentials.derivationInfo?.derivationType ?? DerivationType.nano;
credentials.walletInfo!.derivationInfo ??= DerivationInfo(derivationType: derivationType);
final wallet = NanoWallet(
@ -88,8 +89,8 @@ class NanoWalletService extends WalletService<NanoNewWalletCredentials,
}
}
DerivationType derivationType = credentials.derivationType ?? DerivationType.nano;
credentials.walletInfo!.derivationInfo ??= DerivationInfo(derivationType: derivationType);
// set the walletInfo's derivationInfo if not present:
credentials.walletInfo!.derivationInfo ??= credentials.derivationInfo;
String? mnemonic;

View file

@ -1,17 +1,17 @@
part of 'bitcoin.dart';
class CWBitcoin extends Bitcoin {
@override
TransactionPriority getMediumTransactionPriority() => BitcoinTransactionPriority.medium;
@override
WalletCredentials createBitcoinRestoreWalletFromSeedCredentials(
{required String name,
required String mnemonic,
required String password,
DerivationType? derivationType,
String? derivationPath}) =>
WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({
required String name,
required String mnemonic,
required String password,
required DerivationType derivationType,
required String derivationPath,
}) =>
BitcoinRestoreWalletFromSeedCredentials(
name: name,
mnemonic: mnemonic,

View file

@ -49,7 +49,7 @@ Future<void> defaultSettingsMigration(
sharedPreferences.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null;
await _validateWalletInfoBoxData(walletInfoSource);
await sharedPreferences.setBool(PreferencesKey.isNewInstall, isNewInstall);
final currentVersion =
@ -166,6 +166,9 @@ Future<void> defaultSettingsMigration(
await changeNanoCurrentPowNodeToDefault(
sharedPreferences: sharedPreferences, nodes: powNodes);
break;
case 23:
// TODO: await updateBtcAndNanoWalletInfos()
break;
default:
break;

View file

@ -163,7 +163,7 @@ Future<void> initializeAppConfigs() async {
transactionDescriptions: transactionDescriptions,
secureStorage: secureStorage,
anonpayInvoiceInfo: anonpayInvoiceInfo,
initialMigrationVersion: 22);
initialMigrationVersion: 23);
}
Future<void> initialSetup(

View file

@ -104,15 +104,11 @@ class CWNano extends Nano {
required String name,
required String password,
required String mnemonic,
DerivationType? derivationType,
required DerivationType derivationType,
}) {
if (derivationType == null) {
// figure out the derivation type as best we can, otherwise set it to "unknown"
if (mnemonic.split(" ").length == 12) {
derivationType = DerivationType.bip39;
} else {
derivationType = DerivationType.unknown;
}
// figure out the derivation type as best we can, otherwise set it to "unknown"
if (mnemonic.split(" ").length == 12) {
derivationType = DerivationType.bip39;
}
return NanoRestoreWalletFromSeedCredentials(
@ -128,15 +124,11 @@ class CWNano extends Nano {
required String name,
required String password,
required String seedKey,
DerivationType? derivationType,
required DerivationType derivationType,
}) {
if (derivationType == null) {
// figure out the derivation type as best we can, otherwise set it to "unknown"
if (seedKey.length == 64) {
derivationType = DerivationType.nano;
} else {
derivationType = DerivationType.unknown;
}
// figure out the derivation type as best we can, otherwise set it to "unknown"
if (seedKey.length == 64) {
derivationType = DerivationType.nano;
}
return NanoRestoreWalletFromKeysCredentials(
@ -430,7 +422,8 @@ class CWNanoUtil extends NanoUtil {
AccountInfoResponse? accountInfo = await nanoClient.getAccountInfo(publicAddress);
if (accountInfo == null) {
accountInfo = AccountInfoResponse(frontier: "", balance: "0", representative: "", confirmationHeight: 0);
accountInfo = AccountInfoResponse(
frontier: "", balance: "0", representative: "", confirmationHeight: 0);
}
accountInfo.address = publicAddress;
return accountInfo;

View file

@ -26,8 +26,7 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store
spendKey = '',
wif = '',
address = '',
super(appStore, walletInfoSource, walletCreationService,
type: type, isRecovery: true);
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true);
@observable
int height;
@ -47,8 +46,10 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store
bool get hasRestorationHeight => type == WalletType.monero;
@override
WalletCredentials getCredentialsFromRestoredWallet(dynamic options, RestoredWallet restoreWallet) {
WalletCredentials getCredentialsFromRestoredWallet(
dynamic options, RestoredWallet restoreWallet) {
final password = generateWalletPassword();
DerivationInfo? derivationInfo = options["derivationInfo"] as DerivationInfo?;
switch (restoreWallet.restoreMode) {
case WalletRestoreMode.keys:
@ -83,7 +84,12 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store
case WalletType.bitcoin:
case WalletType.litecoin:
return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials(
name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password);
name: name,
mnemonic: restoreWallet.mnemonicSeed ?? '',
password: password,
derivationType: derivationInfo!.derivationType!,
derivationPath: derivationInfo.derivationPath!,
);
case WalletType.ethereum:
return ethereum!.createEthereumRestoreWalletFromSeedCredentials(
name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password);
@ -96,7 +102,8 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store
}
@override
Future<WalletBase> processFromRestoredWallet(WalletCredentials credentials, RestoredWallet restoreWallet) async {
Future<WalletBase> processFromRestoredWallet(
WalletCredentials credentials, RestoredWallet restoreWallet) async {
try {
switch (restoreWallet.restoreMode) {
case WalletRestoreMode.keys:

View file

@ -47,6 +47,13 @@ abstract class WalletCreationVMBase with Store {
name = await generateName();
}
if (options == null || options["derivationInfo"] == null) {
if (options == null) {
options = {};
}
options["derivationInfo"] = getDefaultDerivation();
}
walletCreationService.checkIfExists(name);
final dirPath = await pathForWalletDir(name: name, type: type);
final path = await pathForWallet(name: name, type: type);
@ -82,6 +89,22 @@ abstract class WalletCreationVMBase with Store {
}
}
DerivationInfo getDefaultDerivation() {
switch (this.type) {
case WalletType.nano:
return DerivationInfo(
derivationType: DerivationType.nano,
);
case WalletType.bitcoin:
case WalletType.litecoin:
default:
return DerivationInfo(
derivationType: DerivationType.electrum2,
derivationPath: "m/0'/1",
);
}
}
WalletCredentials getCredentials(dynamic options) => throw UnimplementedError();
Future<WalletBase> process(WalletCredentials credentials) => throw UnimplementedError();

View file

@ -83,12 +83,17 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
name: name,
mnemonic: seed,
password: password,
derivationType: derivationInfo?.derivationType,
derivationPath: derivationInfo?.derivationPath,
derivationType: derivationInfo!.derivationType!,
derivationPath: derivationInfo.derivationPath!,
);
case WalletType.litecoin:
return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials(
name: name, mnemonic: seed, password: password);
name: name,
mnemonic: seed,
password: password,
derivationType: derivationInfo!.derivationType!,
derivationPath: derivationInfo.derivationPath!,
);
case WalletType.haven:
return haven!.createHavenRestoreWalletFromSeedCredentials(
name: name, height: height, mnemonic: seed, password: password);
@ -100,7 +105,7 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
name: name,
mnemonic: seed,
password: password,
derivationType: derivationInfo?.derivationType,
derivationType: derivationInfo!.derivationType!,
);
default:
break;
@ -232,22 +237,6 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
return [DerivationType.def];
}
DerivationInfo getDefaultDerivation() {
switch (type) {
case WalletType.nano:
return DerivationInfo(
derivationType: DerivationType.nano,
);
case WalletType.bitcoin:
case WalletType.litecoin:
default:
return DerivationInfo(
derivationType: DerivationType.electrum2,
derivationPath: "m/0'/1",
);
}
}
@override
Future<WalletBase> process(WalletCredentials credentials) async {
if (mode == WalletRestoreMode.keys) {

View file

@ -80,7 +80,13 @@ import 'package:cw_bitcoin/script_hash.dart';
abstract class Bitcoin {
TransactionPriority getMediumTransactionPriority();
WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({required String name, required String mnemonic, required String password, DerivationType? derivationType, String? derivationPath});
WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({
required String name,
required String mnemonic,
required String password,
required DerivationType derivationType,
required String derivationPath,
});
WalletCredentials createBitcoinRestoreWalletFromWIFCredentials({required String name, required String password, required String wif, WalletInfo? walletInfo});
WalletCredentials createBitcoinNewWalletCredentials({required String name, WalletInfo? walletInfo});
List<String> getWordList();
@ -634,14 +640,14 @@ abstract class Nano {
required String name,
required String password,
required String mnemonic,
DerivationType? derivationType,
required DerivationType derivationType,
});
WalletCredentials createNanoRestoreWalletFromKeysCredentials({
required String name,
required String password,
required String seedKey,
DerivationType? derivationType,
required DerivationType derivationType,
});
List<String> getNanoWordList(String language);