@@ -33,6 +33,7 @@ import 'package:cw_zano/zano_wallet_api.dart';
3333import 'package:cw_zano/zano_wallet_exceptions.dart' ;
3434import 'package:cw_zano/zano_wallet_service.dart' ;
3535import 'package:cw_zano/api/model/balance.dart' ;
36+ import 'package:cw_zano/bip39_seed.dart' ;
3637
3738import 'package:mobx/mobx.dart' ;
3839
@@ -132,14 +133,27 @@ abstract class ZanoWalletBase
132133 final wallet = ZanoWallet (credentials.walletInfo! , await credentials.walletInfo! .getDerivationInfo (), credentials.password! );
133134 await wallet.initWallet ();
134135 final path = await pathForWallet (name: credentials.name, type: credentials.walletInfo! .type);
135- final createWalletResult = await wallet.createWallet (path, credentials.password! );
136+ final strength = credentials.seedPhraseLength == 24 ? 256 : 128 ;
137+ final providedMnemonic =
138+ credentials is ZanoNewWalletCredentials ? credentials.mnemonic : null ;
139+ final mnemonic = providedMnemonic ?? generateBip39Mnemonic (strength: strength);
140+ final passphrase = credentials.passphrase ?? '' ;
141+ final secretDerivation = getSecretDerivationFromBip39 (mnemonic, passphrase: passphrase);
142+ final creationTimestamp = DateTime .now ().millisecondsSinceEpoch ~ / 1000 ;
143+ final createWalletResult = await wallet.restoreWalletFromDerivations (
144+ path,
145+ credentials.password! ,
146+ secretDerivation,
147+ creationTimestamp: creationTimestamp,
148+ );
136149 await wallet.initWallet ();
137- await wallet.parseCreateWalletResult (createWalletResult );
150+ await wallet.setBip39Secrets (mnemonic : mnemonic, creationTimestamp : creationTimestamp );
138151 if (credentials.passphrase != null ) {
139152 await wallet.setPassphrase (credentials.passphrase! );
140- wallet.seed = await createWalletResult.seed (wallet);
141- wallet.passphrase = await wallet.getPassphrase ();
142153 }
154+ await wallet.parseCreateWalletResult (createWalletResult);
155+ wallet.seed = mnemonic;
156+ wallet.passphrase = await wallet.getPassphrase ();
143157 await wallet.init (createWalletResult.wi.address);
144158 return wallet;
145159 }
@@ -149,15 +163,33 @@ abstract class ZanoWalletBase
149163 final wallet = ZanoWallet (credentials.walletInfo! , await credentials.walletInfo! .getDerivationInfo (), credentials.password! );
150164 await wallet.initWallet ();
151165 final path = await pathForWallet (name: credentials.name, type: credentials.walletInfo! .type);
152- final createWalletResult = await wallet.restoreWalletFromSeed (
153- path, credentials.password! , credentials.mnemonic, credentials.passphrase);
166+ final passphrase = credentials.passphrase ?? '' ;
167+ final CreateWalletResult createWalletResult;
168+ if (isBip39Seed (credentials.mnemonic)) {
169+ final secretDerivation =
170+ getSecretDerivationFromBip39 (credentials.mnemonic, passphrase: passphrase);
171+ final creationTimestamp = (credentials.height ?? 0 ) > 0 ? credentials.height! : 0 ;
172+ createWalletResult = await wallet.restoreWalletFromDerivations (
173+ path,
174+ credentials.password! ,
175+ secretDerivation,
176+ creationTimestamp: creationTimestamp,
177+ );
178+ await wallet.setBip39Secrets (
179+ mnemonic: credentials.mnemonic,
180+ creationTimestamp: creationTimestamp,
181+ );
182+ } else {
183+ createWalletResult = await wallet.restoreWalletFromSeed (
184+ path, credentials.password! , credentials.mnemonic, credentials.passphrase);
185+ }
154186 await wallet.initWallet ();
155- await wallet.parseCreateWalletResult (createWalletResult);
156187 if (credentials.passphrase != null ) {
157188 await wallet.setPassphrase (credentials.passphrase! );
158- wallet.seed = await createWalletResult.seed (wallet);
159- wallet.passphrase = await wallet.getPassphrase ();
160189 }
190+ await wallet.parseCreateWalletResult (createWalletResult);
191+ wallet.seed = isBip39Seed (credentials.mnemonic) ? credentials.mnemonic : await createWalletResult.seed (wallet);
192+ wallet.passphrase = await wallet.getPassphrase ();
161193 await wallet.init (createWalletResult.wi.address);
162194 return wallet;
163195 }
@@ -210,6 +242,19 @@ abstract class ZanoWalletBase
210242 transactionHistory.addMany (transactions);
211243 await transactionHistory.save ();
212244 }
245+ await _ensureBip39DerivationInfo ();
246+ }
247+
248+ Future <void > _ensureBip39DerivationInfo () async {
249+ if (await getBip39Mnemonic () == null ) {
250+ return ;
251+ }
252+ final di = await walletInfo.getDerivationInfo ();
253+ if (di.derivationType == DerivationType .bip39) {
254+ return ;
255+ }
256+ di.derivationType = DerivationType .bip39;
257+ await di.save ();
213258 }
214259
215260 @override
@@ -305,12 +350,13 @@ abstract class ZanoWalletBase
305350 Future <Map <String , ZanoTransactionInfo >> fetchTransactions () async {
306351 try {
307352 final transfers = < Transfer > [];
353+ var offset = 0 ;
308354 late GetRecentTxsAndInfoResult result;
309355 do {
310- result = await getRecentTxsAndInfo (offset: 0 , count: _txChunkSize);
311- // _lastTxIndex += result.transfers.length;
356+ result = await getRecentTxsAndInfo (offset: offset, count: _txChunkSize);
312357 transfers.addAll (result.transfers);
313- } while (result.lastItemIndex + 1 < result.totalTransfers);
358+ offset = result.lastItemIndex + 1 ;
359+ } while (offset < result.totalTransfers);
314360 return Transfer .makeMap (transfers, zanoAssets, currentDaemonHeight);
315361 } catch (e) {
316362 printV ((e.toString ()));
0 commit comments