@@ -2,16 +2,14 @@ import 'dart:async';
2
2
import 'dart:convert' ;
3
3
import 'dart:isolate' ;
4
4
5
- import 'package:bip39/bip39.dart' as bip39;
6
5
import 'package:bitcoin_base/bitcoin_base.dart' ;
7
6
import 'package:blockchain_utils/blockchain_utils.dart' ;
8
7
import 'package:cw_bitcoin/bitcoin_address_record.dart' ;
9
- import 'package:cw_bitcoin/bitcoin_mnemonic.dart' ;
10
8
import 'package:cw_bitcoin/psbt_transaction_builder.dart' ;
11
9
import 'package:cw_bitcoin/bitcoin_transaction_priority.dart' ;
12
10
import 'package:cw_bitcoin/bitcoin_unspent.dart' ;
13
11
import 'package:cw_bitcoin/electrum_transaction_info.dart' ;
14
- // import 'package:cw_bitcoin/electrum_wallet_addresses.dart';
12
+ import 'package:cw_bitcoin/electrum_wallet_addresses.dart' ;
15
13
import 'package:cw_core/encryption_file_utils.dart' ;
16
14
import 'package:cw_bitcoin/electrum_derivations.dart' ;
17
15
import 'package:cw_bitcoin/bitcoin_wallet_addresses.dart' ;
@@ -60,6 +58,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
60
58
int initialSilentAddressIndex = 0 ,
61
59
bool ? alwaysScan,
62
60
required bool mempoolAPIEnabled,
61
+ super .hdWallets,
63
62
}) : super (
64
63
mnemonic: mnemonic,
65
64
passphrase: passphrase,
@@ -88,9 +87,9 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
88
87
initialChangeAddressIndex: initialChangeAddressIndex,
89
88
initialSilentAddresses: initialSilentAddresses,
90
89
initialSilentAddressIndex: initialSilentAddressIndex,
91
- bip32: bip32,
92
90
network: networkParam ?? network,
93
91
isHardwareWallet: walletInfo.isHardwareWallet,
92
+ hdWallets: hdWallets,
94
93
);
95
94
96
95
autorun ((_) {
@@ -116,15 +115,49 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
116
115
required bool mempoolAPIEnabled,
117
116
}) async {
118
117
late List <int > seedBytes;
118
+ final Map <CWBitcoinDerivationType , Bip32Slip10Secp256k1 > hdWallets = {};
119
119
120
- switch ( walletInfo.derivationInfo ? .derivationType ) {
121
- case DerivationType .bip39:
120
+ for ( final derivation in walletInfo.derivations ?? < DerivationInfo > [] ) {
121
+ if (derivation.derivationType == DerivationType .bip39) {
122
122
seedBytes = Bip39SeedGenerator .generateFromString (mnemonic, passphrase);
123
+ hdWallets[CWBitcoinDerivationType .bip39] = Bip32Slip10Secp256k1 .fromSeed (seedBytes);
124
+ hdWallets[CWBitcoinDerivationType .old] = hdWallets[CWBitcoinDerivationType .bip39]! ;
125
+
126
+ try {
127
+ hdWallets[CWBitcoinDerivationType .old] = Bip32Slip10Secp256k1 .fromSeed (
128
+ seedBytes,
129
+ ElectrumWalletBase .getKeyNetVersion (network ?? BitcoinNetwork .mainnet),
130
+ ).derivePath (
131
+ _hardenedDerivationPath (derivation.derivationPath ?? electrum_path),
132
+ ) as Bip32Slip10Secp256k1 ;
133
+ } catch (e) {
134
+ print ("bip39 seed error: $e " );
135
+ }
123
136
break ;
124
- case DerivationType .electrum:
125
- default :
126
- seedBytes = ElectrumV2SeedGenerator .generateFromString (mnemonic, passphrase);
137
+ } else {
138
+ try {
139
+ seedBytes = ElectrumV2SeedGenerator .generateFromString (mnemonic, passphrase);
140
+ hdWallets[CWBitcoinDerivationType .electrum] = Bip32Slip10Secp256k1 .fromSeed (seedBytes);
141
+ } catch (e) {
142
+ print ("electrum_v2 seed error: $e " );
143
+
144
+ try {
145
+ seedBytes = ElectrumV1SeedGenerator (mnemonic).generate ();
146
+ hdWallets[CWBitcoinDerivationType .electrum] = Bip32Slip10Secp256k1 .fromSeed (seedBytes);
147
+ } catch (e) {
148
+ print ("electrum_v1 seed error: $e " );
149
+ }
150
+ }
151
+
152
+ try {
153
+ hdWallets[CWBitcoinDerivationType .old] = Bip32Slip10Secp256k1 .fromSeed (
154
+ seedBytes,
155
+ ).derivePath (
156
+ _hardenedDerivationPath (derivation.derivationPath ?? electrum_path),
157
+ ) as Bip32Slip10Secp256k1 ;
158
+ } catch (_) {}
127
159
break ;
160
+ }
128
161
}
129
162
130
163
return BitcoinWallet (
@@ -144,6 +177,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
144
177
addressPageType: addressPageType,
145
178
networkParam: network,
146
179
mempoolAPIEnabled: mempoolAPIEnabled,
180
+ hdWallets: hdWallets,
147
181
);
148
182
}
149
183
@@ -200,21 +234,52 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
200
234
walletInfo.derivationInfo! .derivationType ?? = snp? .derivationType ?? DerivationType .electrum;
201
235
202
236
List <int >? seedBytes = null ;
237
+ final Map <CWBitcoinDerivationType , Bip32Slip10Secp256k1 > hdWallets = {};
203
238
final mnemonic = keysData.mnemonic;
204
239
final passphrase = keysData.passphrase;
205
240
206
241
if (mnemonic != null ) {
207
- switch (walletInfo.derivationInfo! .derivationType) {
208
- case DerivationType .electrum:
209
- seedBytes = await mnemonicToSeedBytes (mnemonic, passphrase: passphrase ?? "" );
242
+ for (final derivation in walletInfo.derivations ?? < DerivationInfo > []) {
243
+ if (derivation.derivationType == DerivationType .bip39) {
244
+ seedBytes = Bip39SeedGenerator .generateFromString (mnemonic, passphrase);
245
+ hdWallets[CWBitcoinDerivationType .bip39] = Bip32Slip10Secp256k1 .fromSeed (seedBytes);
246
+ hdWallets[CWBitcoinDerivationType .old] = hdWallets[CWBitcoinDerivationType .bip39]! ;
247
+
248
+ try {
249
+ hdWallets[CWBitcoinDerivationType .old] = Bip32Slip10Secp256k1 .fromSeed (
250
+ seedBytes,
251
+ ElectrumWalletBase .getKeyNetVersion (network),
252
+ ).derivePath (
253
+ _hardenedDerivationPath (derivation.derivationPath ?? electrum_path),
254
+ ) as Bip32Slip10Secp256k1 ;
255
+ } catch (e) {
256
+ print ("bip39 seed error: $e " );
257
+ }
210
258
break ;
211
- case DerivationType .bip39:
212
- default :
213
- seedBytes = await bip39.mnemonicToSeed (
214
- mnemonic,
215
- passphrase: passphrase ?? '' ,
216
- );
259
+ } else {
260
+ try {
261
+ seedBytes = ElectrumV2SeedGenerator .generateFromString (mnemonic, passphrase);
262
+ hdWallets[CWBitcoinDerivationType .electrum] = Bip32Slip10Secp256k1 .fromSeed (seedBytes);
263
+ } catch (e) {
264
+ print ("electrum_v2 seed error: $e " );
265
+
266
+ try {
267
+ seedBytes = ElectrumV1SeedGenerator (mnemonic).generate ();
268
+ hdWallets[CWBitcoinDerivationType .electrum] =
269
+ Bip32Slip10Secp256k1 .fromSeed (seedBytes);
270
+ } catch (e) {
271
+ print ("electrum_v1 seed error: $e " );
272
+ }
273
+ }
274
+
275
+ try {
276
+ hdWallets[CWBitcoinDerivationType .old] =
277
+ Bip32Slip10Secp256k1 .fromSeed (seedBytes! ).derivePath (
278
+ _hardenedDerivationPath (derivation.derivationPath ?? electrum_path),
279
+ ) as Bip32Slip10Secp256k1 ;
280
+ } catch (_) {}
217
281
break ;
282
+ }
218
283
}
219
284
}
220
285
@@ -237,6 +302,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
237
302
networkParam: network,
238
303
alwaysScan: alwaysScan,
239
304
mempoolAPIEnabled: mempoolAPIEnabled,
305
+ hdWallets: hdWallets,
240
306
);
241
307
}
242
308
@@ -784,6 +850,9 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store {
784
850
super .syncStatusReaction (syncStatus);
785
851
}
786
852
}
853
+
854
+ static String _hardenedDerivationPath (String derivationPath) =>
855
+ derivationPath.substring (0 , derivationPath.lastIndexOf ("'" ) + 1 );
787
856
}
788
857
789
858
Future <void > startRefresh (ScanData scanData) async {
0 commit comments