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

cyan committed Sep 15, 2024 at 17:48 UTC 417de3669c4e37720b5a4a7f6d4484d2770cc10f
2 files changed +53 -3
cw_monero/lib/api/wallet_manager.dart
+27 -1
@@ -123,7 +123,7 @@ void restoreWalletFromKeysSync(
123 int nettype = 0,
124 int restoreHeight = 0}) {
125 txhistory = null;
126 - final newWptr = spendKey != ""
126 + var newWptr = (spendKey != "")
127 ? monero.WalletManager_createDeterministicWalletFromSpendKey(
128 wmPtr,
129 path: path,
@@ -149,6 +149,32 @@ void restoreWalletFromKeysSync(
149 message: monero.Wallet_errorString(newWptr));
150 }
151
152 + // CW-712 - Try to restore deterministic wallet first, if the view key doesn't
153 + // match the view key provided
154 + if (spendKey != "") {
155 + final viewKeyRestored = monero.Wallet_secretViewKey(newWptr);
156 + if (viewKey != viewKeyRestored && viewKey != "") {
157 + monero.WalletManager_closeWallet(wmPtr, newWptr, false);
158 + File(path).deleteSync();
159 + File(path+".keys").deleteSync();
160 + newWptr = monero.WalletManager_createWalletFromKeys(
161 + wmPtr,
162 + path: path,
163 + password: password,
164 + restoreHeight: restoreHeight,
165 + addressString: address,
166 + viewKeyString: viewKey,
167 + spendKeyString: spendKey,
168 + nettype: 0,
169 + );
170 + final status = monero.Wallet_status(newWptr);
171 + if (status != 0) {
172 + throw WalletRestoreFromKeysException(
173 + message: monero.Wallet_errorString(newWptr));
174 + }
175 + }
176 + }
177 +
178 wptr = newWptr;
179
180 openedWalletsByPath[path] = wptr!;
cw_wownero/lib/api/wallet_manager.dart
+26 -2
@@ -140,7 +140,7 @@ void restoreWalletFromKeysSync(
140 int nettype = 0,
141 int restoreHeight = 0}) {
142 txhistory = null;
143 - final newWptr = spendKey != ""
143 + var newWptr = (spendKey != "")
144 ? wownero.WalletManager_createDeterministicWalletFromSpendKey(
145 wmPtr,
146 path: path,
@@ -165,7 +165,31 @@ void restoreWalletFromKeysSync(
165 throw WalletRestoreFromKeysException(
166 message: wownero.Wallet_errorString(newWptr));
167 }
168 -
168 + // CW-712 - Try to restore deterministic wallet first, if the view key doesn't
169 + // match the view key provided
170 + if (spendKey != "") {
171 + final viewKeyRestored = wownero.Wallet_secretViewKey(newWptr);
172 + if (viewKey != viewKeyRestored && viewKey != "") {
173 + wownero.WalletManager_closeWallet(wmPtr, newWptr, false);
174 + File(path).deleteSync();
175 + File(path+".keys").deleteSync();
176 + newWptr = wownero.WalletManager_createWalletFromKeys(
177 + wmPtr,
178 + path: path,
179 + password: password,
180 + restoreHeight: restoreHeight,
181 + addressString: address,
182 + viewKeyString: viewKey,
183 + spendKeyString: spendKey,
184 + nettype: 0,
185 + );
186 + final status = wownero.Wallet_status(newWptr);
187 + if (status != 0) {
188 + throw WalletRestoreFromKeysException(
189 + message: wownero.Wallet_errorString(newWptr));
190 + }
191 + }
192 + }
193 wptr = newWptr;
194
195 openedWalletsByPath[path] = wptr!;