@@ -19,6 +19,8 @@ import '../../../services/event_bus/events/global/wallet_sync_status_changed_eve
1919import '../../../services/event_bus/global_event_bus.dart' ;
2020import '../../../utilities/amount/amount.dart' ;
2121import '../../../utilities/logger.dart' ;
22+ import '../../../utilities/stack_file_system.dart' ;
23+
2224import '../../crypto_currency/crypto_currency.dart' ;
2325import '../../models/tx_data.dart' ;
2426import '../intermediate/lib_xelis_wallet.dart' ;
@@ -31,29 +33,129 @@ class XelisWallet extends LibXelisWallet {
3133 @override
3234 int get isarTransactionVersion => 2 ;
3335
36+ Future <void > _restoreWallet () async {
37+ final tablePath = await getPrecomputedTablesPath ();
38+ final tableState = await getTableState ();
39+ final xelisDir = await StackFileSystem .applicationXelisDirectory ();
40+ final String name = walletId;
41+ final String directory = xelisDir.path;
42+ final password = await secureStorageInterface.read (
43+ key: Wallet .mnemonicPassphraseKey (walletId: info.walletId),
44+ );
45+
46+ final mnemonic = await getMnemonic ();
47+ final seedLength = mnemonic.trim ().split (" " ).length;
48+
49+ invalidSeedLengthCheck (seedLength);
50+
51+ Logging .instance.i ("Xelis: recovering wallet" );
52+ final wallet = await x_wallet.createXelisWallet (
53+ name: name,
54+ directory: directory,
55+ password: password! ,
56+ seed: mnemonic.trim (),
57+ network: cryptoCurrency.network.xelisNetwork,
58+ precomputedTablesPath: tablePath,
59+ l1Low: tableState.currentSize.isLow,
60+ );
61+
62+ await secureStorageInterface.write (
63+ key: Wallet .mnemonicKey (walletId: walletId),
64+ value: mnemonic.trim (),
65+ );
66+
67+ libXelisWallet = wallet;
68+ }
69+
3470 Future <void > _createNewWallet () async {
71+ final tablePath = await getPrecomputedTablesPath ();
72+ final tableState = await getTableState ();
73+ final xelisDir = await StackFileSystem .applicationXelisDirectory ();
74+ final String name = walletId;
75+ final String directory = xelisDir.path;
3576 final String password = generatePassword ();
3677
3778 Logging .instance.d ("Xelis: storing password" );
3879 await secureStorageInterface.write (
3980 key: Wallet .mnemonicPassphraseKey (walletId: info.walletId),
4081 value: password,
4182 );
83+
84+ final wallet = await x_wallet.createXelisWallet (
85+ name: name,
86+ directory: directory,
87+ password: password! ,
88+ network: cryptoCurrency.network.xelisNetwork,
89+ precomputedTablesPath: tablePath,
90+ l1Low: tableState.currentSize.isLow,
91+ );
92+
93+ final mnemonic = await wallet.getSeed ();
94+ await secureStorageInterface.write (
95+ key: Wallet .mnemonicKey (walletId: walletId),
96+ value: mnemonic.trim (),
97+ );
98+
99+ libXelisWallet = wallet;
42100 }
43101
44102 @override
45103 Future <void > init ({bool ? isRestore}) async {
46104 Logging .instance.d ("Xelis: init" );
47105
48- if (isRestore == true ) {
49- await super .init ();
50- return await open (openType: XelisWalletOpenType .restore);
51- }
106+ if (libXelisWallet == null ) {
107+ if (isRestore == true ) {
108+ await _restoreWallet ();
109+ } else {
110+ final bool walletExists = await LibXelisWallet .checkWalletExists (walletId);
111+ if (! walletExists) {
112+ await _createNewWallet ();
113+ } else {
114+ Logging .instance.i ("Xelis: opening existing wallet" );
115+ final tablePath = await getPrecomputedTablesPath ();
116+ final tableState = await getTableState ();
117+ final xelisDir = await StackFileSystem .applicationXelisDirectory ();
118+ final String name = walletId;
119+ final String directory = xelisDir.path;
120+ final password = await secureStorageInterface.read (
121+ key: Wallet .mnemonicPassphraseKey (walletId: info.walletId),
122+ );
52123
53- final bool walletExists = await LibXelisWallet .checkWalletExists (walletId);
54- if (! walletExists) {
55- await _createNewWallet ();
56- await open (openType: XelisWalletOpenType .create);
124+ libXelisWallet = await x_wallet.openXelisWallet (
125+ name: name,
126+ directory: directory,
127+ password: password! ,
128+ network: cryptoCurrency.network.xelisNetwork,
129+ precomputedTablesPath: tablePath,
130+ l1Low: tableState.currentSize.isLow,
131+ );
132+ }
133+ }
134+
135+ if (await isTableUpgradeAvailable ()) {
136+ unawaited (updateTablesToDesiredSize ());
137+ }
138+
139+ final newReceivingAddress =
140+ await getCurrentReceivingAddress () ??
141+ Address (
142+ walletId: walletId,
143+ derivationIndex: 0 ,
144+ derivationPath: null ,
145+ value: libXelisWallet! .getAddressStr (),
146+ publicKey: [],
147+ type: AddressType .xelis,
148+ subType: AddressSubType .receiving,
149+ );
150+
151+ await mainDB.updateOrPutAddresses ([newReceivingAddress]);
152+
153+ if (info.cachedReceivingAddress != newReceivingAddress.value) {
154+ await info.updateReceivingAddress (
155+ newAddress: newReceivingAddress.value,
156+ isar: mainDB.isar,
157+ );
158+ }
57159 }
58160
59161 return await super .init ();
@@ -97,7 +199,7 @@ class XelisWallet extends LibXelisWallet {
97199 } catch (_) {
98200 await handleOffline ();
99201 return false ;
100- }
202+ }
101203 }
102204
103205 final _balanceUpdateMutex = Mutex ();
0 commit comments