mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-08 20:09:24 +00:00
refactor + updates
This commit is contained in:
parent
cb3289c99b
commit
38e34ec648
6 changed files with 103 additions and 102 deletions
|
@ -47,10 +47,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
||||||
initialBalance: initialBalance,
|
initialBalance: initialBalance,
|
||||||
seedBytes: seedBytes,
|
seedBytes: seedBytes,
|
||||||
currency: CryptoCurrency.btc) {
|
currency: CryptoCurrency.btc) {
|
||||||
// in a standard BIP44 wallet, mainHd derivation path = m/84'/0'/0'/0 (account 0, index unspecified here)
|
String derivationPath = walletInfo.derivationInfo!.derivationPath! + "/0";
|
||||||
// the sideHd derivation path = m/84'/0'/0'/1 (account 1, index unspecified here)
|
String sideDerivationPath = walletInfo.derivationInfo!.derivationPath! + "/1";
|
||||||
String derivationPath = walletInfo.derivationInfo!.derivationPath!;
|
|
||||||
String sideDerivationPath = derivationPath.substring(0, derivationPath.length - 1) + "1";
|
|
||||||
final hd = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
|
final hd = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
|
||||||
walletAddresses = BitcoinWalletAddresses(
|
walletAddresses = BitcoinWalletAddresses(
|
||||||
walletInfo,
|
walletInfo,
|
||||||
|
|
|
@ -287,6 +287,18 @@ class CWBitcoin extends Bitcoin {
|
||||||
}) async {
|
}) async {
|
||||||
List<DerivationInfo> list = [];
|
List<DerivationInfo> list = [];
|
||||||
|
|
||||||
|
// var types = await compareDerivationMethods(mnemonic: mnemonic, node: node);
|
||||||
|
// if (types.length == 1 && types.first == DerivationType.electrum) {
|
||||||
|
// return [
|
||||||
|
// DerivationInfo(
|
||||||
|
// derivationType: DerivationType.electrum,
|
||||||
|
// derivationPath: "m/0'/0",
|
||||||
|
// description: "Electrum",
|
||||||
|
// scriptType: "p2wpkh",
|
||||||
|
// )
|
||||||
|
// ];
|
||||||
|
// }
|
||||||
|
|
||||||
final electrumClient = ElectrumClient();
|
final electrumClient = ElectrumClient();
|
||||||
await electrumClient.connectToUri(node.uri);
|
await electrumClient.connectToUri(node.uri);
|
||||||
|
|
||||||
|
@ -322,20 +334,18 @@ class CWBitcoin extends Bitcoin {
|
||||||
);
|
);
|
||||||
|
|
||||||
String rootPath = dInfoCopy.derivationPath!;
|
String rootPath = dInfoCopy.derivationPath!;
|
||||||
int rootDepth = _countOccurrences(rootPath, "/");
|
int depth = _countOccurrences(rootPath, "/");
|
||||||
String pathForAccount0Index0 = rootPath;
|
String pathForIndex0 = rootPath;
|
||||||
|
if (depth == 3) {
|
||||||
// for BIP44/BIP49, we need to specify the index 0 for the first address:
|
pathForIndex0 = rootPath + "/0/0";
|
||||||
if (rootDepth == 3) {
|
|
||||||
pathForAccount0Index0 += "/0/0";
|
|
||||||
} else {
|
} else {
|
||||||
pathForAccount0Index0 += "/0";
|
pathForIndex0 = rootPath + "/0";
|
||||||
}
|
}
|
||||||
|
|
||||||
final hd = btc.HDWallet.fromSeed(
|
final hd = btc.HDWallet.fromSeed(
|
||||||
seedBytes,
|
seedBytes,
|
||||||
network: networkType,
|
network: networkType,
|
||||||
).derivePath(pathForAccount0Index0);
|
).derivePath(pathForIndex0);
|
||||||
|
|
||||||
String? address;
|
String? address;
|
||||||
switch (dInfoCopy.scriptType) {
|
switch (dInfoCopy.scriptType) {
|
||||||
|
@ -359,7 +369,6 @@ class CWBitcoin extends Bitcoin {
|
||||||
dInfoCopy.balance = balance.entries.first.value.toString();
|
dInfoCopy.balance = balance.entries.first.value.toString();
|
||||||
dInfoCopy.address = address;
|
dInfoCopy.address = address;
|
||||||
dInfoCopy.transactionsCount = history.length;
|
dInfoCopy.transactionsCount = history.length;
|
||||||
dInfoCopy.derivationPath = pathForAccount0Index0;
|
|
||||||
|
|
||||||
list.add(dInfoCopy);
|
list.add(dInfoCopy);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -106,7 +106,6 @@ class CWNano extends Nano {
|
||||||
required String mnemonic,
|
required String mnemonic,
|
||||||
required DerivationType derivationType,
|
required DerivationType derivationType,
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
if (mnemonic.split(" ").length == 12) {
|
if (mnemonic.split(" ").length == 12) {
|
||||||
derivationType = DerivationType.bip39;
|
derivationType = DerivationType.bip39;
|
||||||
}
|
}
|
||||||
|
@ -126,7 +125,6 @@ class CWNano extends Nano {
|
||||||
required String seedKey,
|
required String seedKey,
|
||||||
required DerivationType derivationType,
|
required DerivationType derivationType,
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
if (seedKey.length == 128) {
|
if (seedKey.length == 128) {
|
||||||
derivationType = DerivationType.bip39;
|
derivationType = DerivationType.bip39;
|
||||||
}
|
}
|
||||||
|
@ -192,7 +190,6 @@ class CWNano extends Nano {
|
||||||
}
|
}
|
||||||
|
|
||||||
class CWNanoUtil extends NanoUtil {
|
class CWNanoUtil extends NanoUtil {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool isValidBip39Seed(String seed) {
|
bool isValidBip39Seed(String seed) {
|
||||||
return NanoDerivations.isValidBip39Seed(seed);
|
return NanoDerivations.isValidBip39Seed(seed);
|
||||||
|
@ -346,4 +343,54 @@ class CWNanoUtil extends NanoUtil {
|
||||||
return [DerivationType.nano, DerivationType.bip39];
|
return [DerivationType.nano, DerivationType.bip39];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<DerivationInfo>> getDerivationsFromMnemonic({
|
||||||
|
String? mnemonic,
|
||||||
|
String? seedKey,
|
||||||
|
required Node node,
|
||||||
|
}) async {
|
||||||
|
List<DerivationInfo> list = [];
|
||||||
|
|
||||||
|
List<DerivationType> possibleDerivationTypes = await compareDerivationMethods(
|
||||||
|
mnemonic: mnemonic,
|
||||||
|
privateKey: seedKey,
|
||||||
|
node: node,
|
||||||
|
);
|
||||||
|
if (possibleDerivationTypes.length == 1) {
|
||||||
|
return [DerivationInfo(derivationType: possibleDerivationTypes.first)];
|
||||||
|
}
|
||||||
|
|
||||||
|
AccountInfoResponse? bip39Info = await nanoUtil!.getInfoFromSeedOrMnemonic(
|
||||||
|
DerivationType.bip39,
|
||||||
|
mnemonic: mnemonic,
|
||||||
|
seedKey: seedKey,
|
||||||
|
node: node,
|
||||||
|
);
|
||||||
|
AccountInfoResponse? standardInfo = await nanoUtil!.getInfoFromSeedOrMnemonic(
|
||||||
|
DerivationType.nano,
|
||||||
|
mnemonic: mnemonic,
|
||||||
|
seedKey: seedKey,
|
||||||
|
node: node,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (standardInfo?.confirmationHeight != null && standardInfo!.confirmationHeight > 0) {
|
||||||
|
list.add(DerivationInfo(
|
||||||
|
derivationType: DerivationType.nano,
|
||||||
|
balance: nanoUtil!.getRawAsUsableString(standardInfo.balance, nanoUtil!.rawPerNano),
|
||||||
|
address: standardInfo.address!,
|
||||||
|
transactionsCount: standardInfo.confirmationHeight,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bip39Info?.confirmationHeight != null && bip39Info!.confirmationHeight > 0) {
|
||||||
|
list.add(DerivationInfo(
|
||||||
|
derivationType: DerivationType.bip39,
|
||||||
|
balance: nanoUtil!.getRawAsUsableString(bip39Info.balance, nanoUtil!.rawPerNano),
|
||||||
|
address: bip39Info.address!,
|
||||||
|
transactionsCount: bip39Info.confirmationHeight,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -355,49 +355,39 @@ class WalletRestorePage extends BasePage {
|
||||||
|
|
||||||
walletRestoreViewModel.state = IsExecutingState();
|
walletRestoreViewModel.state = IsExecutingState();
|
||||||
|
|
||||||
List<DerivationType> derivationTypes =
|
|
||||||
await walletRestoreViewModel.getDerivationTypes(_credentials());
|
|
||||||
DerivationInfo? dInfo;
|
DerivationInfo? dInfo;
|
||||||
|
|
||||||
if (derivationTypes.length > 1) {
|
// get info about the different derivations:
|
||||||
// push screen to choose the derivation type:
|
List<DerivationInfo> derivations =
|
||||||
List<DerivationInfo> derivations =
|
await walletRestoreViewModel.getDerivationInfo(_credentials());
|
||||||
await walletRestoreViewModel.getDerivationInfo(_credentials());
|
|
||||||
|
|
||||||
int derivationsWithHistory = 0;
|
int derivationsWithHistory = 0;
|
||||||
int derivationWithHistoryIndex = 0;
|
int derivationWithHistoryIndex = 0;
|
||||||
for (int i = 0; i < derivations.length; i++) {
|
for (int i = 0; i < derivations.length; i++) {
|
||||||
if (derivations[i].transactionsCount > 0) {
|
if (derivations[i].transactionsCount > 0) {
|
||||||
derivationsWithHistory++;
|
derivationsWithHistory++;
|
||||||
derivationWithHistoryIndex = i;
|
derivationWithHistoryIndex = i;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dInfo = await Navigator.of(context).pushNamed(Routes.restoreWalletChooseDerivation,
|
|
||||||
// arguments: derivations) as DerivationInfo?;
|
|
||||||
|
|
||||||
if (derivationsWithHistory > 1) {
|
|
||||||
dInfo = await Navigator.of(context).pushNamed(Routes.restoreWalletChooseDerivation,
|
|
||||||
arguments: derivations) as DerivationInfo?;
|
|
||||||
} else if (derivationsWithHistory == 1) {
|
|
||||||
dInfo = derivations[derivationWithHistoryIndex];
|
|
||||||
} else if (derivationsWithHistory == 0 && derivations.isNotEmpty) {
|
|
||||||
dInfo = DerivationInfo(
|
|
||||||
derivationType: DerivationType.bip39,
|
|
||||||
derivationPath: "m/84'/0'/0'/0",
|
|
||||||
description: "Standard BIP84 native segwit",
|
|
||||||
scriptType: "p2wpkh",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dInfo == null) {
|
|
||||||
walletRestoreViewModel.state = InitialExecutionState();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.derivationInfo = dInfo;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (derivationsWithHistory > 1) {
|
||||||
|
dInfo = await Navigator.of(context).pushNamed(
|
||||||
|
Routes.restoreWalletChooseDerivation,
|
||||||
|
arguments: derivations,
|
||||||
|
) as DerivationInfo?;
|
||||||
|
} else if (derivationsWithHistory == 1) {
|
||||||
|
dInfo = derivations[derivationWithHistoryIndex];
|
||||||
|
} else if (derivationsWithHistory == 0 && derivations.isNotEmpty) {
|
||||||
|
dInfo = derivations.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dInfo == null) {
|
||||||
|
walletRestoreViewModel.state = InitialExecutionState();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.derivationInfo = dInfo;
|
||||||
|
|
||||||
// get the default derivation for this wallet type:
|
// get the default derivation for this wallet type:
|
||||||
if (this.derivationInfo == null) {
|
if (this.derivationInfo == null) {
|
||||||
this.derivationInfo = walletRestoreViewModel.getDefaultDerivation();
|
this.derivationInfo = walletRestoreViewModel.getDefaultDerivation();
|
||||||
|
|
|
@ -212,65 +212,17 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
|
||||||
case WalletType.nano:
|
case WalletType.nano:
|
||||||
String? mnemonic = credentials['seed'] as String?;
|
String? mnemonic = credentials['seed'] as String?;
|
||||||
String? seedKey = credentials['private_key'] as String?;
|
String? seedKey = credentials['private_key'] as String?;
|
||||||
AccountInfoResponse? bip39Info = await nanoUtil!.getInfoFromSeedOrMnemonic(
|
return nanoUtil!.getDerivationsFromMnemonic(
|
||||||
DerivationType.bip39,
|
|
||||||
mnemonic: mnemonic,
|
|
||||||
seedKey: seedKey,
|
|
||||||
node: node);
|
|
||||||
AccountInfoResponse? standardInfo = await nanoUtil!.getInfoFromSeedOrMnemonic(
|
|
||||||
DerivationType.nano,
|
|
||||||
mnemonic: mnemonic,
|
mnemonic: mnemonic,
|
||||||
seedKey: seedKey,
|
seedKey: seedKey,
|
||||||
node: node,
|
node: node,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (standardInfo?.balance != null) {
|
|
||||||
list.add(DerivationInfo(
|
|
||||||
derivationType: DerivationType.nano,
|
|
||||||
balance: nanoUtil!.getRawAsUsableString(standardInfo!.balance, nanoUtil!.rawPerNano),
|
|
||||||
address: standardInfo.address!,
|
|
||||||
transactionsCount: standardInfo.confirmationHeight,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bip39Info?.balance != null) {
|
|
||||||
list.add(DerivationInfo(
|
|
||||||
derivationType: DerivationType.bip39,
|
|
||||||
balance: nanoUtil!.getRawAsUsableString(bip39Info!.balance, nanoUtil!.rawPerNano),
|
|
||||||
address: bip39Info.address!,
|
|
||||||
transactionsCount: bip39Info.confirmationHeight,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<DerivationType>> getDerivationTypes(dynamic options) async {
|
|
||||||
final seedKey = options['private_key'] as String?;
|
|
||||||
final mnemonic = options['seed'] as String?;
|
|
||||||
WalletType walletType = options['walletType'] as WalletType;
|
|
||||||
var appStore = getIt.get<AppStore>();
|
|
||||||
var node = appStore.settingsStore.getCurrentNode(walletType);
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case WalletType.bitcoin:
|
|
||||||
case WalletType.litecoin:
|
|
||||||
return bitcoin!.compareDerivationMethods(mnemonic: mnemonic!, node: node);
|
|
||||||
case WalletType.nano:
|
|
||||||
return nanoUtil!.compareDerivationMethods(
|
|
||||||
mnemonic: mnemonic,
|
|
||||||
privateKey: seedKey,
|
|
||||||
node: node,
|
|
||||||
);
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return [DerivationType.def];
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<WalletBase> process(WalletCredentials credentials) async {
|
Future<WalletBase> process(WalletCredentials credentials) async {
|
||||||
if (mode == WalletRestoreMode.keys) {
|
if (mode == WalletRestoreMode.keys) {
|
||||||
|
|
|
@ -914,6 +914,11 @@ abstract class NanoUtil {
|
||||||
String? privateKey,
|
String? privateKey,
|
||||||
required Node node,
|
required Node node,
|
||||||
});
|
});
|
||||||
|
Future<List<DerivationInfo>> getDerivationsFromMnemonic({
|
||||||
|
String? mnemonic,
|
||||||
|
String? seedKey,
|
||||||
|
required Node node,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue