fix(cw_monero): keys nullcheck handling (#2338)

add default values to monero wallet keys functions in order to prevent null check crashes

cyan committed Jun 27, 2025 at 14:06 UTC 900304d40532e9226439a38387853519299f670a
1 file changed +10 -5
cw_monero/lib/api/wallet.dart
+10 -5
@@ -116,12 +116,17 @@ String getSeedLegacy(String? language) {
116 }
117
118 String getPassphrase() {
119 - return currentWallet!.getCacheAttribute(key: "cakewallet.passphrase");
119 + return currentWallet?.getCacheAttribute(key: "cakewallet.passphrase") ?? "";
120 }
121
122 Map<int, Map<int, Map<int, String>>> addressCache = {};
123
124 String getAddress({int accountIndex = 0, int addressIndex = 0}) {
125 + // this is a workaround for when we switch the wallet pointer,
126 + // it should never reach UI but should be good enough to prevent gray screen
127 + // or other errors because of forced null check.
128 + if (currentWallet == null) return "<wallet not ready ($accountIndex:$addressIndex)>";
129 +
130 // printV("getaddress: ${accountIndex}/${addressIndex}: ${monero.Wallet_numSubaddresses(wptr!, accountIndex: accountIndex)}: ${monero.Wallet_address(wptr!, accountIndex: accountIndex, addressIndex: addressIndex)}");
131 // this could be a while loop, but I'm in favor of making it if to not cause freezes
132 if (currentWallet!.numSubaddresses(accountIndex: accountIndex)-1 < addressIndex) {
@@ -272,13 +277,13 @@ void closeCurrentWallet() {
277 currentWallet!.stop();
278 }
279
275 -String getSecretViewKey() => currentWallet!.secretViewKey();
280 +String getSecretViewKey() => currentWallet?.secretViewKey() ?? "";
281
277 -String getPublicViewKey() => currentWallet!.publicViewKey();
282 +String getPublicViewKey() => currentWallet?.publicViewKey() ?? "";
283
279 -String getSecretSpendKey() => currentWallet!.secretSpendKey();
284 +String getSecretSpendKey() => currentWallet?.secretSpendKey() ?? "";
285
281 -String getPublicSpendKey() => currentWallet!.publicSpendKey();
286 +String getPublicSpendKey() => currentWallet?.publicSpendKey() ?? "";
287
288 class SyncListener {
289 SyncListener(this.onNewBlock, this.onNewTransaction)