mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-03 09:29:48 +00:00
refactor to use one less /0 working on both electrum and bip39 derivations
This commit is contained in:
parent
fcb9f0d77e
commit
765689c551
7 changed files with 49 additions and 54 deletions
|
@ -33,33 +33,32 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
|||
Map<String, int>? initialChangeAddressIndex,
|
||||
String? passphrase,
|
||||
}) : super(
|
||||
mnemonic: mnemonic,
|
||||
passphrase: passphrase,
|
||||
password: password,
|
||||
walletInfo: walletInfo,
|
||||
unspentCoinsInfo: unspentCoinsInfo,
|
||||
networkType: networkParam == null
|
||||
? bitcoin.bitcoin
|
||||
: networkParam == BitcoinNetwork.mainnet
|
||||
? bitcoin.bitcoin
|
||||
: bitcoin.testnet,
|
||||
initialAddresses: initialAddresses,
|
||||
initialBalance: initialBalance,
|
||||
seedBytes: seedBytes,
|
||||
currency: CryptoCurrency.btc) {
|
||||
// in a standard BIP44 wallet, mainHd derivation path = m/84'/0'/0'/0 (account 0, index unspecified here)
|
||||
// the sideHd derivation path = m/84'/0'/0'/1 (account 1, index unspecified here)
|
||||
String derivationPath = walletInfo.derivationInfo!.derivationPath!;
|
||||
String sideDerivationPath = derivationPath.substring(0, derivationPath.length - 1) + "1";
|
||||
final hd = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
|
||||
mnemonic: mnemonic,
|
||||
passphrase: passphrase,
|
||||
password: password,
|
||||
walletInfo: walletInfo,
|
||||
unspentCoinsInfo: unspentCoinsInfo,
|
||||
networkType: networkParam == null
|
||||
? bitcoin.bitcoin
|
||||
: networkParam == BitcoinNetwork.mainnet
|
||||
? bitcoin.bitcoin
|
||||
: bitcoin.testnet,
|
||||
initialAddresses: initialAddresses,
|
||||
initialBalance: initialBalance,
|
||||
seedBytes: seedBytes,
|
||||
currency: CryptoCurrency.btc,
|
||||
) {
|
||||
String derivationPath = walletInfo.derivationInfo!.derivationPath! + "/0";
|
||||
String sideDerivationPath = walletInfo.derivationInfo!.derivationPath! + "/1";
|
||||
final hd2 = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
|
||||
walletAddresses = BitcoinWalletAddresses(
|
||||
walletInfo,
|
||||
electrumClient: electrumClient,
|
||||
initialAddresses: initialAddresses,
|
||||
initialRegularAddressIndex: initialRegularAddressIndex,
|
||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||
mainHd: hd.derivePath(derivationPath),
|
||||
sideHd: hd.derivePath(sideDerivationPath),
|
||||
mainHd: hd2.derivePath(derivationPath),
|
||||
sideHd: hd2.derivePath(sideDerivationPath),
|
||||
network: networkParam ?? network,
|
||||
);
|
||||
autorun((_) {
|
||||
|
@ -127,7 +126,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
|
|||
);
|
||||
|
||||
// set the default if not present:
|
||||
walletInfo.derivationInfo!.derivationPath = snp.derivationPath ?? "m/0'/1";
|
||||
walletInfo.derivationInfo!.derivationPath = snp.derivationPath ?? "m/0'";
|
||||
|
||||
late Uint8List seedBytes;
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ Map<DerivationType, List<DerivationInfo>> electrum_derivations = {
|
|||
DerivationType.electrum: [
|
||||
DerivationInfo(
|
||||
derivationType: DerivationType.electrum,
|
||||
derivationPath: "m/0'/0",
|
||||
derivationPath: "m/0'",
|
||||
description: "Electrum",
|
||||
scriptType: "p2wpkh",
|
||||
),
|
||||
|
|
|
@ -63,7 +63,7 @@ abstract class ElectrumWalletBase
|
|||
: hd = currency == CryptoCurrency.bch
|
||||
? bitcoinCashHDWallet(seedBytes)
|
||||
: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
|
||||
.derivePath(walletInfo.derivationInfo?.derivationPath ?? "m/0'/0"),
|
||||
.derivePath(walletInfo.derivationInfo?.derivationPath ?? "m/0'"),
|
||||
syncStatus = NotConnectedSyncStatus(),
|
||||
_password = password,
|
||||
_feeRates = <int>[],
|
||||
|
|
|
@ -56,7 +56,7 @@ class ElectrumWalletSnapshot {
|
|||
|
||||
final derivationType =
|
||||
DerivationType.values[(data['derivationTypeIndex'] as int?) ?? DerivationType.electrum.index];
|
||||
final derivationPath = data['derivationPath'] as String? ?? "m/0'/0";
|
||||
final derivationPath = data['derivationPath'] as String? ?? "m/0'";
|
||||
|
||||
try {
|
||||
regularAddressIndexByType = {
|
||||
|
|
|
@ -33,23 +33,27 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
|
|||
Map<String, int>? initialRegularAddressIndex,
|
||||
Map<String, int>? initialChangeAddressIndex,
|
||||
}) : super(
|
||||
mnemonic: mnemonic,
|
||||
password: password,
|
||||
walletInfo: walletInfo,
|
||||
unspentCoinsInfo: unspentCoinsInfo,
|
||||
networkType: litecoinNetwork,
|
||||
initialAddresses: initialAddresses,
|
||||
initialBalance: initialBalance,
|
||||
seedBytes: seedBytes,
|
||||
currency: CryptoCurrency.ltc) {
|
||||
mnemonic: mnemonic,
|
||||
password: password,
|
||||
walletInfo: walletInfo,
|
||||
unspentCoinsInfo: unspentCoinsInfo,
|
||||
networkType: litecoinNetwork,
|
||||
initialAddresses: initialAddresses,
|
||||
initialBalance: initialBalance,
|
||||
seedBytes: seedBytes,
|
||||
currency: CryptoCurrency.ltc,
|
||||
) {
|
||||
String derivationPath = walletInfo.derivationInfo!.derivationPath! + "/0";
|
||||
String sideDerivationPath = walletInfo.derivationInfo!.derivationPath! + "/1";
|
||||
final hd2 = bitcoin.HDWallet.fromSeed(seedBytes, network: networkType);
|
||||
walletAddresses = LitecoinWalletAddresses(
|
||||
walletInfo,
|
||||
electrumClient: electrumClient,
|
||||
initialAddresses: initialAddresses,
|
||||
initialRegularAddressIndex: initialRegularAddressIndex,
|
||||
initialChangeAddressIndex: initialChangeAddressIndex,
|
||||
mainHd: hd,
|
||||
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType).derivePath("m/0'/1"),
|
||||
mainHd: hd2.derivePath(derivationPath),
|
||||
sideHd: hd2.derivePath(sideDerivationPath),
|
||||
network: network,
|
||||
);
|
||||
autorun((_) {
|
||||
|
|
|
@ -292,7 +292,7 @@ class CWBitcoin extends Bitcoin {
|
|||
return [
|
||||
DerivationInfo(
|
||||
derivationType: DerivationType.electrum,
|
||||
derivationPath: "m/0'/0",
|
||||
derivationPath: "m/0'",
|
||||
description: "Electrum",
|
||||
scriptType: "p2wpkh",
|
||||
)
|
||||
|
@ -333,27 +333,19 @@ class CWBitcoin extends Bitcoin {
|
|||
scriptType: dInfo.scriptType,
|
||||
);
|
||||
|
||||
String derivationPath = dInfoCopy.derivationPath!;
|
||||
int derivationDepth = _countOccurrences(derivationPath, "/");
|
||||
|
||||
// the correct derivation depth is dependant on the derivation type:
|
||||
// the derivation paths defined in electrum_derivations are at the ROOT level, i.e.:
|
||||
// electrum's format doesn't specify subaddresses, just subaccounts:
|
||||
|
||||
// for BIP44
|
||||
if (derivationDepth == 3) {
|
||||
// we add "/0/0" so that we generate account 0, index 0 and correctly get balance
|
||||
derivationPath += "/0/0";
|
||||
// we don't support sub-ACCOUNTS in bitcoin like we do monero, and so the path dInfoCopy
|
||||
// expects should be ACCOUNT 0, index unspecified:
|
||||
dInfoCopy.derivationPath = dInfoCopy.derivationPath! + "/0";
|
||||
String rootPath = dInfoCopy.derivationPath!;
|
||||
int depth = _countOccurrences(rootPath, "/");
|
||||
String pathForIndex0 = rootPath;
|
||||
if (depth == 3) {
|
||||
pathForIndex0 = rootPath + "/0/0";
|
||||
} else {
|
||||
pathForIndex0 = rootPath + "/0";
|
||||
}
|
||||
|
||||
// var hd = bip32.BIP32.fromSeed(seedBytes).derivePath(derivationPath);
|
||||
final hd = btc.HDWallet.fromSeed(
|
||||
seedBytes,
|
||||
network: networkType,
|
||||
).derivePath(derivationPath);
|
||||
).derivePath(pathForIndex0);
|
||||
|
||||
String? address;
|
||||
switch (dInfoCopy.scriptType) {
|
||||
|
|
|
@ -99,7 +99,7 @@ abstract class WalletCreationVMBase with Store {
|
|||
case WalletType.litecoin:
|
||||
return DerivationInfo(
|
||||
derivationType: DerivationType.electrum,
|
||||
derivationPath: "m/0'/0",
|
||||
derivationPath: "m/0'",
|
||||
);
|
||||
default:
|
||||
return null;
|
||||
|
|
Loading…
Reference in a new issue