Fix hardware derivation path with accounts bigger than zero (#3203)

Omar Hatem committed May 6, 2026 at 23:22 UTC 813aa348bcf04cfa11a6e75454763ee7418b3c6e
1 file changed +13 -2
cw_bitcoin/lib/electrum_wallet.dart
+13 -2
@@ -132,10 +132,11 @@ abstract class ElectrumWalletBase
132 }
133 } else if (canDeriveFromSeed) {
134 final coinType = _coinTypeFor(currency);
135 + final accountIndex = _parseAccountIndex(derivationInfo.derivationPath);
136
137 for (final type in supportedTypes) {
138 final purpose = _purposeForType(type);
138 - final accountPath = "m/$purpose'/$coinType'/0'";
139 + final accountPath = "m/$purpose'/$coinType'/$accountIndex'";
140
141 mainHdByType[type] = _masterHD!.derivePath("$accountPath/0") as Bip32Slip10Secp256k1;
142 sideHdByType[type] = _masterHD!.derivePath("$accountPath/1") as Bip32Slip10Secp256k1;
@@ -197,7 +198,8 @@ abstract class ElectrumWalletBase
198
199 final coinType = _coinTypeFor(currency);
200 final purpose = _purposeForType(record.type);
200 - return "m/$purpose'/$coinType'/0'";
201 + final accountIndex = _parseAccountIndex(derivationInfo.derivationPath);
202 + return "m/$purpose'/$coinType'/$accountIndex'";
203 }
204
205 List<BitcoinAddressType> supportedAddressTypes(WalletType type) {
@@ -259,6 +261,15 @@ abstract class ElectrumWalletBase
261 static int estimatedTransactionSize(int inputsCount, int outputsCounts) =>
262 inputsCount * 68 + outputsCounts * 34 + 10;
263
264 + // Parses the account index from a BIP-44/49/84/86 derivation path.
265 + // e.g. "m/84'/0'/1'" → 1. Returns 0 for unrecognised formats.
266 + static int _parseAccountIndex(String? derivationPath) {
267 + if (derivationPath == null) return 0;
268 + final parts = derivationPath.split('/');
269 + if (parts.length < 4) return 0;
270 + return int.tryParse(parts[3].replaceAll("'", "")) ?? 0;
271 + }
272 +
273 static Bip32KeyNetVersions? getKeyNetVersion(BasedUtxoNetwork network,
274 [HardwareWalletType? hardwareWalletType]) {
275 switch (network) {