diff --git a/cw_bitcoin/lib/bitcoin_wallet.dart b/cw_bitcoin/lib/bitcoin_wallet.dart index 1d29307ca..5ccc3ef33 100644 --- a/cw_bitcoin/lib/bitcoin_wallet.dart +++ b/cw_bitcoin/lib/bitcoin_wallet.dart @@ -33,33 +33,32 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { Map? 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; diff --git a/cw_bitcoin/lib/electrum_derivations.dart b/cw_bitcoin/lib/electrum_derivations.dart index e2450fc5f..78b51eb20 100644 --- a/cw_bitcoin/lib/electrum_derivations.dart +++ b/cw_bitcoin/lib/electrum_derivations.dart @@ -4,7 +4,7 @@ Map> electrum_derivations = { DerivationType.electrum: [ DerivationInfo( derivationType: DerivationType.electrum, - derivationPath: "m/0'/0", + derivationPath: "m/0'", description: "Electrum", scriptType: "p2wpkh", ), diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 8342e4816..b184cbb6a 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -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 = [], diff --git a/cw_bitcoin/lib/electrum_wallet_snapshot.dart b/cw_bitcoin/lib/electrum_wallet_snapshot.dart index 218792e3c..6cbd70314 100644 --- a/cw_bitcoin/lib/electrum_wallet_snapshot.dart +++ b/cw_bitcoin/lib/electrum_wallet_snapshot.dart @@ -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 = { diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index d2379d5a5..153c939e2 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -33,23 +33,27 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { Map? initialRegularAddressIndex, Map? 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((_) { diff --git a/lib/bitcoin/cw_bitcoin.dart b/lib/bitcoin/cw_bitcoin.dart index 7ae01df1c..5cfaae9d6 100644 --- a/lib/bitcoin/cw_bitcoin.dart +++ b/lib/bitcoin/cw_bitcoin.dart @@ -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) { diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index 5c9c29a16..7aa83ab52 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -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;