From 417de3669c4e37720b5a4a7f6d4484d2770cc10f Mon Sep 17 00:00:00 2001 From: cyan Date: Sun, 15 Sep 2024 17:48:07 +0200 Subject: [PATCH] do not restore deterministic wallet if it's not one (#1676) * do not restore deterministic wallet if it's not one * [skip ci] update comment --- cw_monero/lib/api/wallet_manager.dart | 28 +++++++++++++++++++++++++- cw_wownero/lib/api/wallet_manager.dart | 28 ++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/cw_monero/lib/api/wallet_manager.dart b/cw_monero/lib/api/wallet_manager.dart index f06fe3e66..dca7586fb 100644 --- a/cw_monero/lib/api/wallet_manager.dart +++ b/cw_monero/lib/api/wallet_manager.dart @@ -123,7 +123,7 @@ void restoreWalletFromKeysSync( int nettype = 0, int restoreHeight = 0}) { txhistory = null; - final newWptr = spendKey != "" + var newWptr = (spendKey != "") ? monero.WalletManager_createDeterministicWalletFromSpendKey( wmPtr, path: path, @@ -149,6 +149,32 @@ void restoreWalletFromKeysSync( message: monero.Wallet_errorString(newWptr)); } + // CW-712 - Try to restore deterministic wallet first, if the view key doesn't + // match the view key provided + if (spendKey != "") { + final viewKeyRestored = monero.Wallet_secretViewKey(newWptr); + if (viewKey != viewKeyRestored && viewKey != "") { + monero.WalletManager_closeWallet(wmPtr, newWptr, false); + File(path).deleteSync(); + File(path+".keys").deleteSync(); + newWptr = monero.WalletManager_createWalletFromKeys( + wmPtr, + path: path, + password: password, + restoreHeight: restoreHeight, + addressString: address, + viewKeyString: viewKey, + spendKeyString: spendKey, + nettype: 0, + ); + final status = monero.Wallet_status(newWptr); + if (status != 0) { + throw WalletRestoreFromKeysException( + message: monero.Wallet_errorString(newWptr)); + } + } + } + wptr = newWptr; openedWalletsByPath[path] = wptr!; diff --git a/cw_wownero/lib/api/wallet_manager.dart b/cw_wownero/lib/api/wallet_manager.dart index 660433ba6..ed6d86823 100644 --- a/cw_wownero/lib/api/wallet_manager.dart +++ b/cw_wownero/lib/api/wallet_manager.dart @@ -140,7 +140,7 @@ void restoreWalletFromKeysSync( int nettype = 0, int restoreHeight = 0}) { txhistory = null; - final newWptr = spendKey != "" + var newWptr = (spendKey != "") ? wownero.WalletManager_createDeterministicWalletFromSpendKey( wmPtr, path: path, @@ -165,7 +165,31 @@ void restoreWalletFromKeysSync( throw WalletRestoreFromKeysException( message: wownero.Wallet_errorString(newWptr)); } - + // CW-712 - Try to restore deterministic wallet first, if the view key doesn't + // match the view key provided + if (spendKey != "") { + final viewKeyRestored = wownero.Wallet_secretViewKey(newWptr); + if (viewKey != viewKeyRestored && viewKey != "") { + wownero.WalletManager_closeWallet(wmPtr, newWptr, false); + File(path).deleteSync(); + File(path+".keys").deleteSync(); + newWptr = wownero.WalletManager_createWalletFromKeys( + wmPtr, + path: path, + password: password, + restoreHeight: restoreHeight, + addressString: address, + viewKeyString: viewKey, + spendKeyString: spendKey, + nettype: 0, + ); + final status = wownero.Wallet_status(newWptr); + if (status != 0) { + throw WalletRestoreFromKeysException( + message: wownero.Wallet_errorString(newWptr)); + } + } + } wptr = newWptr; openedWalletsByPath[path] = wptr!;