Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 80 additions & 52 deletions lib/wallets/wallet/impl/xelis_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import '../intermediate/lib_xelis_wallet.dart';
import '../wallet.dart';

class XelisWallet extends LibXelisWallet {
Completer<void>? _initCompleter;

XelisWallet(CryptoCurrencyNetwork network) : super(Xelis(network));
// ==================== Overrides ============================================

Expand Down Expand Up @@ -65,6 +67,8 @@ class XelisWallet extends LibXelisWallet {
);

libXelisWallet = wallet;

await _finishInit();
}

Future<void> _createNewWallet() async {
Expand Down Expand Up @@ -97,68 +101,93 @@ class XelisWallet extends LibXelisWallet {
);

libXelisWallet = wallet;

await _finishInit();
}

Future<void> _existingWallet() async {
print("EXISTING");
Logging.instance.i("Xelis: opening existing wallet");
final tablePath = await getPrecomputedTablesPath();
final tableState = await getTableState();
final xelisDir = await StackFileSystem.applicationXelisDirectory();
final String name = walletId;
final String directory = xelisDir.path;
final password = await secureStorageInterface.read(
key: Wallet.mnemonicPassphraseKey(walletId: info.walletId),
);

libXelisWallet = await x_wallet.openXelisWallet(
name: name,
directory: directory,
password: password!,
network: cryptoCurrency.network.xelisNetwork,
precomputedTablesPath: tablePath,
l1Low: tableState.currentSize.isLow,
);

await _finishInit();
}

Future<void> _finishInit() async {
if (await isTableUpgradeAvailable()) {
unawaited(updateTablesToDesiredSize());
}

final newReceivingAddress =
await getCurrentReceivingAddress() ??
Address(
walletId: walletId,
derivationIndex: 0,
derivationPath: null,
value: libXelisWallet!.getAddressStr(),
publicKey: [],
type: AddressType.xelis,
subType: AddressSubType.receiving,
);

await mainDB.updateOrPutAddresses([newReceivingAddress]);

if (info.cachedReceivingAddress != newReceivingAddress.value) {
await info.updateReceivingAddress(
newAddress: newReceivingAddress.value,
isar: mainDB.isar,
);
}
}

@override
Future<void> init({bool? isRestore}) async {
Logging.instance.d("Xelis: init");

if (libXelisWallet == null) {
if (isRestore == true) {
await _restoreWallet();
} else {
final bool walletExists = await LibXelisWallet.checkWalletExists(walletId);
if (!walletExists) {
await _createNewWallet();
} else {
Logging.instance.i("Xelis: opening existing wallet");
final tablePath = await getPrecomputedTablesPath();
final tableState = await getTableState();
final xelisDir = await StackFileSystem.applicationXelisDirectory();
final String name = walletId;
final String directory = xelisDir.path;
final password = await secureStorageInterface.read(
key: Wallet.mnemonicPassphraseKey(walletId: info.walletId),
);

libXelisWallet = await x_wallet.openXelisWallet(
name: name,
directory: directory,
password: password!,
network: cryptoCurrency.network.xelisNetwork,
precomputedTablesPath: tablePath,
l1Low: tableState.currentSize.isLow,
);
}
}
if (_initCompleter != null) {
await _initCompleter!.future;
return super.init();
}

if (await isTableUpgradeAvailable()) {
unawaited(updateTablesToDesiredSize());
}
_initCompleter = Completer<void>();

final newReceivingAddress =
await getCurrentReceivingAddress() ??
Address(
walletId: walletId,
derivationIndex: 0,
derivationPath: null,
value: libXelisWallet!.getAddressStr(),
publicKey: [],
type: AddressType.xelis,
subType: AddressSubType.receiving,
);

await mainDB.updateOrPutAddresses([newReceivingAddress]);
try {
final bool walletExists = await LibXelisWallet.checkWalletExists(walletId);

if (info.cachedReceivingAddress != newReceivingAddress.value) {
await info.updateReceivingAddress(
newAddress: newReceivingAddress.value,
isar: mainDB.isar,
);
if (libXelisWallet == null) {
if (isRestore == true) {
await _restoreWallet();
} else {
if (!walletExists) {
await _createNewWallet();
} else {
await _existingWallet();
}
}
}
_initCompleter!.complete();
} catch (e) {
_initCompleter!.completeError(e);
rethrow;
}

return await super.init();
return super.init();
}

@override
Expand Down Expand Up @@ -191,7 +220,6 @@ class XelisWallet extends LibXelisWallet {

@override
Future<bool> pingCheck() async {
checkInitialized();
try {
await libXelisWallet!.getDaemonInfo();
await handleOnline();
Expand Down Expand Up @@ -247,7 +275,7 @@ class XelisWallet extends LibXelisWallet {
final Map<String, dynamic> nodeInfo =
(json.decode(infoString) as Map).cast();

pruningHeight = int.parse(nodeInfo['pruned_topoheight'].toString());
pruningHeight = int.tryParse(nodeInfo['pruned_topoheight']?.toString() ?? '0') ?? 0;
return int.parse(nodeInfo['topoheight'].toString());
}

Expand Down
6 changes: 2 additions & 4 deletions lib/wallets/wallet/intermediate/lib_xelis_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ enum XelisTableSize {
}
}

enum XelisWalletOpenType { create, restore }

class XelisTableState {
final bool isGenerating;
final XelisTableSize currentSize;
Expand Down Expand Up @@ -330,7 +328,7 @@ abstract class LibXelisWallet<T extends ElectrumCurrency>
}

@override
Future<void> open({XelisWalletOpenType? openType}) async {
Future<void> open() async {
try {
await connect();
} catch (e) {
Expand Down Expand Up @@ -439,4 +437,4 @@ extension XelisTableManagement on LibXelisWallet {
}
});
}
}
}
Loading