Skip to content

Commit 22ff0b1

Browse files
Merge pull request #1101 from Tritonn204/xelis
Xelis init/open refactor
2 parents a503861 + e18a254 commit 22ff0b1

File tree

2 files changed

+120
-124
lines changed

2 files changed

+120
-124
lines changed

lib/wallets/wallet/impl/xelis_wallet.dart

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import '../../../services/event_bus/events/global/wallet_sync_status_changed_eve
1919
import '../../../services/event_bus/global_event_bus.dart';
2020
import '../../../utilities/amount/amount.dart';
2121
import '../../../utilities/logger.dart';
22+
import '../../../utilities/stack_file_system.dart';
23+
2224
import '../../crypto_currency/crypto_currency.dart';
2325
import '../../models/tx_data.dart';
2426
import '../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();

lib/wallets/wallet/intermediate/lib_xelis_wallet.dart

Lines changed: 9 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -331,119 +331,15 @@ abstract class LibXelisWallet<T extends ElectrumCurrency>
331331

332332
@override
333333
Future<void> open({XelisWalletOpenType? openType}) async {
334-
bool wasNull = false;
335-
336-
if (libXelisWallet == null) {
337-
wasNull = true;
338-
final tablePath = await getPrecomputedTablesPath();
339-
final tableState = await getTableState();
340-
final xelisDir = await StackFileSystem.applicationXelisDirectory();
341-
final String name = walletId;
342-
final String directory = xelisDir.path;
343-
final password = await secureStorageInterface.read(
344-
key: Wallet.mnemonicPassphraseKey(walletId: info.walletId),
345-
);
346-
347-
await LibXelisWallet._initMutex.protect(() async {
348-
try {
349-
libXelisWallet = await syncMutex.protect(() async {
350-
switch (openType) {
351-
case XelisWalletOpenType.create:
352-
Logging.instance.i("Xelis: creating new wallet");
353-
final wallet = await x_wallet.createXelisWallet(
354-
name: name,
355-
directory: directory,
356-
password: password!,
357-
network: cryptoCurrency.network.xelisNetwork,
358-
precomputedTablesPath: tablePath,
359-
l1Low: tableState.currentSize.isLow,
360-
);
361-
362-
final mnemonic = await wallet.getSeed();
363-
await secureStorageInterface.write(
364-
key: Wallet.mnemonicKey(walletId: walletId),
365-
value: mnemonic.trim(),
366-
);
367-
368-
return wallet;
369-
370-
case XelisWalletOpenType.restore:
371-
final mnemonic = await getMnemonic();
372-
final seedLength = mnemonic.trim().split(" ").length;
373-
374-
invalidSeedLengthCheck(seedLength);
375-
376-
Logging.instance.i("Xelis: recovering wallet");
377-
final wallet = await x_wallet.createXelisWallet(
378-
name: name,
379-
directory: directory,
380-
password: password!,
381-
seed: mnemonic.trim(),
382-
network: cryptoCurrency.network.xelisNetwork,
383-
precomputedTablesPath: tablePath,
384-
l1Low: tableState.currentSize.isLow,
385-
);
386-
387-
await secureStorageInterface.write(
388-
key: Wallet.mnemonicKey(walletId: walletId),
389-
value: mnemonic.trim(),
390-
);
391-
392-
return wallet;
393-
394-
case null:
395-
Logging.instance.i("Xelis: opening existing wallet");
396-
return await x_wallet.openXelisWallet(
397-
name: name,
398-
directory: directory,
399-
password: password!,
400-
network: cryptoCurrency.network.xelisNetwork,
401-
precomputedTablesPath: tablePath,
402-
l1Low: tableState.currentSize.isLow,
403-
);
404-
}
405-
});
406-
} catch (e, s) {
407-
Logging.instance.e(
408-
"Rethrowing failed $runtimeType open(openType: $openType)",
409-
error: e,
410-
stackTrace: s,
411-
);
412-
rethrow;
413-
}
414-
});
415-
416-
Logging.instance.i("Xelis: Checking for upgradability");
417-
if (await isTableUpgradeAvailable()) {
418-
Logging.instance.i("Xelis: Generating large tables in background");
419-
unawaited(updateTablesToDesiredSize());
420-
}
421-
}
422-
423-
final newReceivingAddress =
424-
await getCurrentReceivingAddress() ??
425-
Address(
426-
walletId: walletId,
427-
derivationIndex: 0,
428-
derivationPath: null,
429-
value: libXelisWallet!.getAddressStr(),
430-
publicKey: [],
431-
type: AddressType.xelis,
432-
subType: AddressSubType.receiving,
433-
);
434-
await mainDB.updateOrPutAddresses([newReceivingAddress]);
435-
436-
if (info.cachedReceivingAddress != newReceivingAddress.value) {
437-
await info.updateReceivingAddress(
438-
newAddress: newReceivingAddress.value,
439-
isar: mainDB.isar,
440-
);
441-
}
442-
443-
if (wasNull) {
334+
try {
444335
await connect();
336+
} catch (e) {
337+
// Logging.instance.log(
338+
// "Failed to start sync: $e",
339+
// level: LogLevel.Error,
340+
// );
341+
rethrow;
445342
}
446-
447343
unawaited(refresh());
448344
}
449345

@@ -457,10 +353,6 @@ abstract class LibXelisWallet<T extends ElectrumCurrency>
457353
_eventSubscription = null;
458354

459355
await libXelisWallet?.offlineMode();
460-
await libXelisWallet?.close();
461-
libXelisWallet?.dispose();
462-
libXelisWallet = null;
463-
464356
await super.exit();
465357
});
466358
}
@@ -511,6 +403,8 @@ extension XelisTableManagement on LibXelisWallet {
511403
await setTableState(state.copyWith(isGenerating: true));
512404

513405
try {
406+
Logging.instance.i("Xelis: Generating large tables in background");
407+
514408
final tablePath = await getPrecomputedTablesPath();
515409
await x_wallet.updateTables(
516410
precomputedTablesPath: tablePath,

0 commit comments

Comments
 (0)