Skip to content

Commit

Permalink
fix invalid wallet pointer getting set even when we throw (cake-tech#…
Browse files Browse the repository at this point in the history
…1556)

* fix invalid wallet pointer getting set even when we throw

* Hide "wallet seeds" field if there is no seeds (cases of restore from keys)

---------

Co-authored-by: OmarHatem <[email protected]>
  • Loading branch information
MrCyjaneK and OmarHatem28 authored Jul 26, 2024
1 parent d5fca19 commit eb8158e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
37 changes: 22 additions & 15 deletions cw_monero/lib/api/wallet_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ void createWalletSync(
required String language,
int nettype = 0}) {
txhistory = null;
wptr = monero.WalletManager_createWallet(wmPtr,
final newWptr = monero.WalletManager_createWallet(wmPtr,
path: path, password: password, language: language, networkType: 0);

final status = monero.Wallet_status(wptr!);
final status = monero.Wallet_status(newWptr);
if (status != 0) {
throw WalletCreationException(message: monero.Wallet_errorString(wptr!));
throw WalletCreationException(message: monero.Wallet_errorString(newWptr));
}
wptr = newWptr;
monero.Wallet_store(wptr!, path: path);
openedWalletsByPath[path] = wptr!;

Expand All @@ -57,7 +58,7 @@ void restoreWalletFromSeedSync(
int nettype = 0,
int restoreHeight = 0}) {
txhistory = null;
wptr = monero.WalletManager_recoveryWallet(
final newWptr = monero.WalletManager_recoveryWallet(
wmPtr,
path: path,
password: password,
Expand All @@ -67,12 +68,13 @@ void restoreWalletFromSeedSync(
networkType: 0,
);

final status = monero.Wallet_status(wptr!);
final status = monero.Wallet_status(newWptr);

if (status != 0) {
final error = monero.Wallet_errorString(wptr!);
final error = monero.Wallet_errorString(newWptr);
throw WalletRestoreFromSeedException(message: error);
}
wptr = newWptr;

openedWalletsByPath[path] = wptr!;
}
Expand All @@ -87,7 +89,7 @@ void restoreWalletFromKeysSync(
int nettype = 0,
int restoreHeight = 0}) {
txhistory = null;
wptr = monero.WalletManager_createWalletFromKeys(
final newWptr = monero.WalletManager_createWalletFromKeys(
wmPtr,
path: path,
password: password,
Expand All @@ -98,12 +100,14 @@ void restoreWalletFromKeysSync(
nettype: 0,
);

final status = monero.Wallet_status(wptr!);
final status = monero.Wallet_status(newWptr);
if (status != 0) {
throw WalletRestoreFromKeysException(
message: monero.Wallet_errorString(wptr!));
message: monero.Wallet_errorString(newWptr));
}

wptr = newWptr;

openedWalletsByPath[path] = wptr!;
}

Expand All @@ -128,7 +132,7 @@ void restoreWalletFromSpendKeySync(
// );

txhistory = null;
wptr = monero.WalletManager_createDeterministicWalletFromSpendKey(
final newWptr = monero.WalletManager_createDeterministicWalletFromSpendKey(
wmPtr,
path: path,
password: password,
Expand All @@ -138,14 +142,16 @@ void restoreWalletFromSpendKeySync(
restoreHeight: restoreHeight,
);

final status = monero.Wallet_status(wptr!);
final status = monero.Wallet_status(newWptr);

if (status != 0) {
final err = monero.Wallet_errorString(wptr!);
final err = monero.Wallet_errorString(newWptr);
print("err: $err");
throw WalletRestoreFromKeysException(message: err);
}

wptr = newWptr;

monero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed);

storeSync();
Expand Down Expand Up @@ -203,15 +209,16 @@ void loadWallet(
});
}
txhistory = null;
wptr = monero.WalletManager_openWallet(wmPtr,
final newWptr = monero.WalletManager_openWallet(wmPtr,
path: path, password: password);
_lastOpenedWallet = path;
final status = monero.Wallet_status(wptr!);
final status = monero.Wallet_status(newWptr);
if (status != 0) {
final err = monero.Wallet_errorString(wptr!);
final err = monero.Wallet_errorString(newWptr);
print(err);
throw WalletOpeningException(message: err);
}
wptr = newWptr;
openedWalletsByPath[path] = wptr!;
}
}
Expand Down
41 changes: 25 additions & 16 deletions cw_wownero/lib/api/wallet_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ void createWalletSync(
required String language,
int nettype = 0}) {
txhistory = null;
wptr = wownero.WalletManager_createWallet(wmPtr,
final newWptr = wownero.WalletManager_createWallet(wmPtr,
path: path, password: password, language: language, networkType: 0);

final status = wownero.Wallet_status(wptr!);
final status = wownero.Wallet_status(newWptr);
if (status != 0) {
throw WalletCreationException(message: wownero.Wallet_errorString(wptr!));
throw WalletCreationException(message: wownero.Wallet_errorString(newWptr));
}
wptr = newWptr;
wownero.Wallet_store(wptr!, path: path);
openedWalletsByPath[path] = wptr!;

Expand All @@ -56,9 +57,10 @@ void restoreWalletFromSeedSync(
required String seed,
int nettype = 0,
int restoreHeight = 0}) {
var newWptr;
if (seed.split(" ").length == 14) {
txhistory = null;
wptr = wownero.WOWNERO_deprecated_restore14WordSeed(
newWptr = wownero.WOWNERO_deprecated_restore14WordSeed(
path: path,
password: password,
language: seed, // I KNOW - this is supposed to be called seed
Expand All @@ -70,7 +72,7 @@ void restoreWalletFromSeedSync(
);
} else {
txhistory = null;
wptr = wownero.WalletManager_recoveryWallet(
newWptr = wownero.WalletManager_recoveryWallet(
wmPtr,
path: path,
password: password,
Expand All @@ -81,13 +83,15 @@ void restoreWalletFromSeedSync(
);
}

final status = wownero.Wallet_status(wptr!);
final status = wownero.Wallet_status(newWptr);

if (status != 0) {
final error = wownero.Wallet_errorString(wptr!);
final error = wownero.Wallet_errorString(newWptr);
throw WalletRestoreFromSeedException(message: error);
}

wptr = newWptr;

openedWalletsByPath[path] = wptr!;
}

Expand All @@ -101,7 +105,7 @@ void restoreWalletFromKeysSync(
int nettype = 0,
int restoreHeight = 0}) {
txhistory = null;
wptr = wownero.WalletManager_createWalletFromKeys(
final newWptr = wownero.WalletManager_createWalletFromKeys(
wmPtr,
path: path,
password: password,
Expand All @@ -112,12 +116,14 @@ void restoreWalletFromKeysSync(
nettype: 0,
);

final status = wownero.Wallet_status(wptr!);
final status = wownero.Wallet_status(newWptr);
if (status != 0) {
throw WalletRestoreFromKeysException(
message: wownero.Wallet_errorString(wptr!));
message: wownero.Wallet_errorString(newWptr));
}

wptr = newWptr;

openedWalletsByPath[path] = wptr!;
}

Expand All @@ -142,7 +148,7 @@ void restoreWalletFromSpendKeySync(
// );

txhistory = null;
wptr = wownero.WalletManager_createDeterministicWalletFromSpendKey(
final newWptr = wownero.WalletManager_createDeterministicWalletFromSpendKey(
wmPtr,
path: path,
password: password,
Expand All @@ -152,14 +158,16 @@ void restoreWalletFromSpendKeySync(
restoreHeight: restoreHeight,
);

final status = wownero.Wallet_status(wptr!);
final status = wownero.Wallet_status(newWptr);

if (status != 0) {
final err = wownero.Wallet_errorString(wptr!);
final err = wownero.Wallet_errorString(newWptr);
print("err: $err");
throw WalletRestoreFromKeysException(message: err);
}

wptr = newWptr;

wownero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed);

storeSync();
Expand Down Expand Up @@ -217,15 +225,16 @@ void loadWallet(
});
}
txhistory = null;
wptr = wownero.WalletManager_openWallet(wmPtr,
final newWptr = wownero.WalletManager_openWallet(wmPtr,
path: path, password: password);
_lastOpenedWallet = path;
final status = wownero.Wallet_status(wptr!);
final status = wownero.Wallet_status(newWptr);
if (status != 0) {
final err = wownero.Wallet_errorString(wptr!);
final err = wownero.Wallet_errorString(newWptr);
print(err);
throw WalletOpeningException(message: err);
}
wptr = newWptr;
openedWalletsByPath[path] = wptr!;
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/view_model/wallet_keys_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ abstract class WalletKeysViewModelBase with Store {
StandartListItem(
title: S.current.view_key_private,
value: keys['privateViewKey']!),
StandartListItem(
title: S.current.wallet_seed, value: _appStore.wallet!.seed!),
if (_appStore.wallet!.seed!.isNotEmpty)
StandartListItem(title: S.current.wallet_seed, value: _appStore.wallet!.seed!),
]);

if (_appStore.wallet?.seed != null &&
Expand Down Expand Up @@ -123,8 +123,8 @@ abstract class WalletKeysViewModelBase with Store {
StandartListItem(
title: S.current.view_key_private,
value: keys['privateViewKey']!),
StandartListItem(
title: S.current.wallet_seed, value: _appStore.wallet!.seed!),
if (_appStore.wallet!.seed!.isNotEmpty)
StandartListItem(title: S.current.wallet_seed, value: _appStore.wallet!.seed!),
]);
}

Expand All @@ -147,8 +147,8 @@ abstract class WalletKeysViewModelBase with Store {
StandartListItem(
title: S.current.view_key_private,
value: keys['privateViewKey']!),
StandartListItem(
title: S.current.wallet_seed, value: _appStore.wallet!.seed!),
if (_appStore.wallet!.seed!.isNotEmpty)
StandartListItem(title: S.current.wallet_seed, value: _appStore.wallet!.seed!),
]);

if (_appStore.wallet?.seed != null &&
Expand Down

0 comments on commit eb8158e

Please sign in to comment.