Fix: Prevent state loss in private key retrieval and improve empty keys handling#3375
Fix: Prevent state loss in private key retrieval and improve empty keys handling#3375
Conversation
…event state loss during async operations
…rove user feedback
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } catch (e) { | ||
| // Clear sensitive data on any error | ||
| _clearPrivateKeyData(); | ||
|
|
||
| // Log error for debugging | ||
| debugPrint('Failed to retrieve private keys: ${e.toString()}'); | ||
| fetchedKeys?.clear(); | ||
| fetchedKeys = null; | ||
|
|
||
| return false; // Failure |
There was a problem hiding this comment.
Clear cached keys after retrieval failure
The new flow moves private key assignment out of the password callback, but the catch block now only clears the local fetchedKeys variable and never calls _clearPrivateKeyData(). If a previous retrieval succeeded and a subsequent attempt fails (for example due to RPC/network error), _sdkPrivateKeys continues to hold the old keys in memory even though the operation failed. The prior implementation explicitly cleared _sdkPrivateKeyData on error to minimize exposure of sensitive data. Restoring that clear keeps the private key cache from persisting after a failed fetch.
Useful? React with 👍 / 👎.
|
Visit the preview URL for this PR (updated for commit c296597): https://walletrc--pull-3375-merge-739micho.web.app (expires Thu, 13 Nov 2025 14:21:59 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: f66a4ff03faa546f12f0ae5a841bd9eff2714dcc |
…ngs to streamline error handling
|
@codex Is it enough to do: or better: ? |
|
@cursor Is it enough to do: or better: ? |
|
Based on examining the code, you should use Here's why: The ContextLooking at the code flow in if (success && fetchedKeys != null) {
// Set the keys AFTER dialog closes to ensure state is preserved
_sdkPrivateKeys = fetchedKeys;
// Clear the local reference to minimize the number of places holding sensitive data
// Note: fetchedKeys is a reference to the same Map object, so we only set it to null
// to remove the extra reference, not clear() which would clear the data used by _sdkPrivateKeys
fetchedKeys = null;
// Private keys are ready, show the private keys screen
// ignore: use_build_context_synchronously
context.read<SecuritySettingsBloc>().add(const ShowPrivateKeysEvent());The variable return PrivateKeyShow(
privateKeys: _sdkPrivateKeys ?? <AssetId, List<PrivateKey>>{},
blockedAssetIds: blockedAssets,
);Why
|
|
Cursor Agent can help with this pull request. Just |
…te in security settings
|
I've carefully tested all the changes in this PR across all platforms. The latest web deployment is available at walletrc--pull-3375-merge-739micho.web.app and works fine. With @ca333’s approval to merge and @CharlVS’s approval of the PR, I’m going to merge it and prepare the release binaries. Others in the internal chat have also confirmed that this PR resolves all issues and that the TestFlight build on iOS works well. p.s. Thanks, everyone, for your hard work and dedication to this release. |
Summary
This PR fixes an issue where private keys were not displaying correctly on iOS (and potentially other platforms) due to state loss during async callback execution. It also adds proper handling for empty private keys scenarios with improved user feedback.
Problem
Issue 1: State Loss During Async Callback Execution
The private keys were being lost due to state management issues during async operations:
walletPasswordDialogWithLoadingshows a dialog and accepts anonPasswordValidatedcallbackWidgetsBinding.instance.addPostFrameCallback_sdkPrivateKeyswas being set directlywalletPasswordDialogWithLoadingreturned and we checked_sdkPrivateKeys, it was already null/emptyThe problem was that setting
_sdkPrivateKeysinside the callback didn't guarantee the state would persist when the dialog closed and the widget rebuilt.Issue 2: Missing Empty Keys Handling
When assets are still being activated and
getEnabledCoinsreturns an empty value, the app was not showing any error message to the user. The user had no feedback about why private keys were not available. Now the app displays a user-friendly message suggesting them to try again later.Solution
Fix 1: Defer State Update Until After Dialog Closes
The fix defers setting
_sdkPrivateKeysuntil after the dialog closes:fetchedKeys) during the async callback_sdkPrivateKeysafter the dialog closes and we confirm the operation succeededFix 2: Proper Empty Keys Handling
Added proper handling for empty private keys when assets are still being activated:
privateKeysis empty immediately after fetching from SDKisEmptyKeys) to distinguish between empty keys and actual errorsprivateKeysEmptyError: "No private keys found. Assets may need to be activated. Please try again later." (when keys are empty)privateKeyRetrievalFailed: Generic error message (for actual errors/exceptions)getEnabledCoinsreturned empty during asset activation, no error was shown to the user. Now users receive clear feedback suggesting them to try again later.