derivation fixes

This commit is contained in:
fosse 2023-09-12 10:33:15 -04:00
parent cb390cb4eb
commit e0dd53fa23
4 changed files with 26 additions and 21 deletions

View file

@ -10,16 +10,10 @@ Map<DerivationType, List<DerivationInfo>> bitcoin_derivations = {
),
],
DerivationType.bip39: [
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/44'/0'/0'/0/0",
description: "Standard BIP44",
script_type: "p2pkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/44'/0'/0'",
description: "Standard BIP44 legacy",
description: "Standard BIP44",
script_type: "p2pkh",
),
DerivationInfo(
@ -28,12 +22,6 @@ Map<DerivationType, List<DerivationInfo>> bitcoin_derivations = {
description: "Standard BIP49 compatibility segwit",
script_type: "p2wpkh-p2sh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/0'/0/0",
description: "Standard BIP84",
script_type: "p2wpkh",
),
DerivationInfo(
derivationType: DerivationType.bip39,
derivationPath: "m/84'/0'/0'",

View file

@ -38,6 +38,9 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
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)
walletAddresses = BitcoinWalletAddresses(walletInfo,
electrumClient: electrumClient,
initialAddresses: initialAddresses,
@ -45,8 +48,8 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
initialChangeAddressIndex: initialChangeAddressIndex,
mainHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
.derivePath(walletInfo.derivationPath!),
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType)
.derivePath(walletInfo.derivationPath!),
sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType).derivePath(
walletInfo.derivationPath!.substring(0, walletInfo.derivationPath!.length - 1) + "1"),
networkType: networkType);
}
@ -59,7 +62,6 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
ElectrumBalance? initialBalance,
int initialRegularAddressIndex = 0,
int initialChangeAddressIndex = 0}) async {
late Uint8List seedBytes;
switch (walletInfo.derivationType) {

View file

@ -24,6 +24,16 @@ import 'package:cw_bitcoin/bitcoin_derivations.dart';
import 'package:bip32/bip32.dart' as bip32;
import 'package:bip39/bip39.dart' as bip39;
int countOccurrences(String str, String charToCount) {
int count = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] == charToCount) {
count++;
}
}
return count;
}
class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
BitcoinRestoreWalletFromSeedCredentials, BitcoinRestoreWalletFromWIFCredentials> {
BitcoinWalletService(this.walletInfoSource, this.unspentCoinsInfoSource);
@ -141,7 +151,14 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
for (DerivationInfo dInfo in bitcoin_derivations[dType]!) {
try {
var node = bip32.BIP32.fromSeed(seedBytes);
node = node.derivePath(dInfo.derivationPath!);
String derivationPath = dInfo.derivationPath!;
int derivationDepth = countOccurrences(derivationPath, "/");
if (derivationDepth == 3) {
derivationPath += "/0/0";
dInfo.derivationPath = dInfo.derivationPath! + "/0";
}
node = node.derivePath(derivationPath);
String? address;
switch (dInfo.script_type) {

View file

@ -17,8 +17,6 @@ enum DerivationType {
@HiveField(3)
bip39,
@HiveField(4)
electrum1,
@HiveField(5)
electrum2,
}
@ -36,8 +34,8 @@ class DerivationInfo {
String balance;
String address;
int height;
final DerivationType derivationType;
final String? derivationPath;
DerivationType derivationType;
String? derivationPath;
final String? script_type;
final String? description;
}