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 { class BitcoinNewWalletCredentials extends WalletCredentials {
BitcoinNewWalletCredentials( BitcoinNewWalletCredentials(
{required String name, WalletInfo? walletInfo, this.derivationType, this.derivationPath}) {required String name,
: super(name: name, walletInfo: walletInfo); WalletInfo? walletInfo,
DerivationType? derivationType; DerivationType? derivationType,
String? derivationPath; String? derivationPath})
: super(
name: name,
walletInfo: walletInfo,
derivationInfo: DerivationInfo(
derivationType: derivationType,
derivationPath: derivationPath,
),
);
} }
class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials { class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
@ -15,8 +23,8 @@ class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials {
required String password, required String password,
required this.mnemonic, required this.mnemonic,
WalletInfo? walletInfo, WalletInfo? walletInfo,
DerivationType? derivationType, required DerivationType derivationType,
String? derivationPath, required String derivationPath,
}) : super( }) : super(
name: name, name: name,
password: password, password: password,

View file

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

View file

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

View file

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

View file

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

View file

@ -166,6 +166,9 @@ Future<void> defaultSettingsMigration(
await changeNanoCurrentPowNodeToDefault( await changeNanoCurrentPowNodeToDefault(
sharedPreferences: sharedPreferences, nodes: powNodes); sharedPreferences: sharedPreferences, nodes: powNodes);
break; break;
case 23:
// TODO: await updateBtcAndNanoWalletInfos()
break;
default: default:
break; break;

View file

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

View file

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

View file

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

View file

@ -47,6 +47,13 @@ abstract class WalletCreationVMBase with Store {
name = await generateName(); name = await generateName();
} }
if (options == null || options["derivationInfo"] == null) {
if (options == null) {
options = {};
}
options["derivationInfo"] = getDefaultDerivation();
}
walletCreationService.checkIfExists(name); walletCreationService.checkIfExists(name);
final dirPath = await pathForWalletDir(name: name, type: type); final dirPath = await pathForWalletDir(name: name, type: type);
final path = await pathForWallet(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(); WalletCredentials getCredentials(dynamic options) => throw UnimplementedError();
Future<WalletBase> process(WalletCredentials credentials) => throw UnimplementedError(); Future<WalletBase> process(WalletCredentials credentials) => throw UnimplementedError();

View file

@ -83,12 +83,17 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
name: name, name: name,
mnemonic: seed, mnemonic: seed,
password: password, password: password,
derivationType: derivationInfo?.derivationType, derivationType: derivationInfo!.derivationType!,
derivationPath: derivationInfo?.derivationPath, derivationPath: derivationInfo.derivationPath!,
); );
case WalletType.litecoin: case WalletType.litecoin:
return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials( return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials(
name: name, mnemonic: seed, password: password); name: name,
mnemonic: seed,
password: password,
derivationType: derivationInfo!.derivationType!,
derivationPath: derivationInfo.derivationPath!,
);
case WalletType.haven: case WalletType.haven:
return haven!.createHavenRestoreWalletFromSeedCredentials( return haven!.createHavenRestoreWalletFromSeedCredentials(
name: name, height: height, mnemonic: seed, password: password); name: name, height: height, mnemonic: seed, password: password);
@ -100,7 +105,7 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
name: name, name: name,
mnemonic: seed, mnemonic: seed,
password: password, password: password,
derivationType: derivationInfo?.derivationType, derivationType: derivationInfo!.derivationType!,
); );
default: default:
break; break;
@ -232,22 +237,6 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
return [DerivationType.def]; 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 @override
Future<WalletBase> process(WalletCredentials credentials) async { Future<WalletBase> process(WalletCredentials credentials) async {
if (mode == WalletRestoreMode.keys) { if (mode == WalletRestoreMode.keys) {

View file

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