Skip to content
Merged
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
117 changes: 54 additions & 63 deletions cw_monero/lib/api/wallet_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ void createWallet(
currentWallet!.setCacheAttribute(key: "cakewallet.passphrase", value: passphrase);
currentWallet!.store(path: path);
openedWalletsByPath[path] = currentWallet!;
_lastOpenedWallet = path;
}

bool isWalletExist({required String path}) {
Expand Down Expand Up @@ -147,7 +146,6 @@ void restoreWalletFromSeedSync(
openedWalletsByPath[path] = currentWallet!;

currentWallet!.store(path: path);
_lastOpenedWallet = path;
}

void storePassphrase({required String path, required String passphrase}) {
Expand Down Expand Up @@ -220,7 +218,6 @@ void restoreWalletFromKeys(
currentWallet = newW;

openedWalletsByPath[path] = currentWallet!;
_lastOpenedWallet = path;
}

// English only, because normalization.
Expand Down Expand Up @@ -310,11 +307,8 @@ void restoreWalletFromSpendKeySync(
setupBackgroundSync(password, currentWallet!);

openedWalletsByPath[path] = currentWallet!;
_lastOpenedWallet = path;
}

String _lastOpenedWallet = "";

Future<void> restoreWalletFromHardwareWallet(
{required String path,
required String password,
Expand All @@ -339,7 +333,6 @@ Future<void> restoreWalletFromHardwareWallet(

currentWallet = newW;
currentWallet!.store(path: path);
_lastOpenedWallet = path;
openedWalletsByPath[path] = currentWallet!;
}

Expand All @@ -351,72 +344,70 @@ Future<void> loadWallet({required String path, required String password, int net
currentWallet = openedWalletsByPath[path]!;
return;
}
if (currentWallet == null || path != _lastOpenedWallet) {
if (currentWallet != null) {
final addr = currentWallet!.ffiAddress();
Isolate.run(() {
monero.Wallet_store(Pointer.fromAddress(addr));
});
}
txhistory = null;

/// Get the device type
/// 0: Software Wallet
/// 1: Ledger
/// 2: Trezor
var deviceType = 0;
if (currentWallet != null) {
final addr = currentWallet!.ffiAddress();
Isolate.run(() {
monero.Wallet_store(Pointer.fromAddress(addr));
});
}
txhistory = null;

if (Platform.isAndroid || Platform.isIOS) {
deviceType = wmPtr.queryWalletDevice(
keysFileName: "$path.keys",
password: password,
kdfRounds: 1,
);
final status = wmPtr.errorString();
if (status != "") {
printV("loadWallet:" + status);
// This is most likely closeWallet call leaking error. This is fine.
if (status.contains("failed to save file")) {
printV("loadWallet: error leaked: $status");
deviceType = 0;
} else {
throw WalletOpeningException(message: status);
}
/// Get the device type
/// 0: Software Wallet
/// 1: Ledger
/// 2: Trezor
var deviceType = 0;

if (Platform.isAndroid || Platform.isIOS) {
deviceType = wmPtr.queryWalletDevice(
keysFileName: "$path.keys",
password: password,
kdfRounds: 1,
);
final status = wmPtr.errorString();
if (status != "") {
printV("loadWallet:" + status);
// This is most likely closeWallet call leaking error. This is fine.
if (status.contains("failed to save file")) {
printV("loadWallet: error leaked: $status");
deviceType = 0;
} else {
throw WalletOpeningException(message: status);
}
} else {
deviceType = 0;
}
} else {
deviceType = 0;
}

if (deviceType == 1) {
if (gLedger == null) {
throw Exception("Tried to open a ledger wallet with no ledger connected");
}
enableLedgerExchange(gLedger!);
if (deviceType == 1) {
if (gLedger == null) {
throw Exception("Tried to open a ledger wallet with no ledger connected");
}
enableLedgerExchange(gLedger!);
}

final addr = wmPtr.ffiAddress();
final newWptrAddr = await Isolate.run(() {
return monero.WalletManager_openWallet(Pointer.fromAddress(addr),
path: path, password: password)
.address;
});

final newW = MoneroWallet(Pointer.fromAddress(newWptrAddr));
final addr = wmPtr.ffiAddress();
final newWptrAddr = await Isolate.run(() {
return monero.WalletManager_openWallet(Pointer.fromAddress(addr),
path: path, password: password)
.address;
});

int status = newW.status();
if (status != 0) {
final err = newW.errorString();
printV("loadWallet:" + err);
throw WalletOpeningException(message: err);
}
if (deviceType == 0) {
setupBackgroundSync(password, newW);
}
final newW = MoneroWallet(Pointer.fromAddress(newWptrAddr));

currentWallet = newW;
_lastOpenedWallet = path;
openedWalletsByPath[path] = currentWallet!;
int status = newW.status();
if (status != 0) {
final err = newW.errorString();
printV("loadWallet:" + err);
throw WalletOpeningException(message: err);
}
if (deviceType == 0) {
setupBackgroundSync(password, newW);
}

currentWallet = newW;
openedWalletsByPath[path] = currentWallet!;
}

void setupBackgroundSync(String password, Wallet2Wallet wallet) {
Expand Down
Loading