keep the open wallet encrypted while discovering accounts to add
What changed, and why it matters
This change adjusts how Sparrow Wallet handles wallet encryption when adding or discovering new accounts. Previously, the main open wallet was decrypted before deciding what to do next, which could leave it decrypted during a potentially lengthy account-discovery network operation. Now the wallet stays encrypted during discovery; only a temporary copy is decrypted for that task. This reduces the window during which sensitive wallet key material sits unencrypted in memory while the user is not actively interacting with it.
Treat as a hardening improvement rather than an urgent vulnerability. Review whether other long-running background services (e.g., transaction history refresh, server synchronization) similarly decrypt the master wallet and could adopt the copy-and-decrypt pattern. Verify that Wallet.copy() produces a sufficiently independent object so that decrypting the copy does not decrypt the original via shared references.
Security signals we found
Reduction of plaintext key material lifetime in memory
Defense-in-depth against memory-exposure or cold-boot style attacks during long-running background operations
No cryptographic primitive changes; change is operational/lifecycle only
Failure path now clears key instead of decrypting and re-encrypting with empty list
Evidence from the diff
In SettingsController.addAccounts(), the original code called masterWallet.decrypt(key) unconditionally before branching on keystore source or discoverAccounts. The patch moves decryption into each branch: immediate decryption only for hardware/software-wallet sourced keystores and for direct account addition, while the discoverAccounts branch now creates a copy of the wallet (Wallet discoveryWallet = masterWallet.copy()), decrypts only the copy, and runs ElectrumServer.AccountDiscoveryService against that copy. On success it then decrypts masterWallet briefly to add and re-encrypt the discovered accounts; on failure it clears the key and skips decrypting masterWallet. This keeps the in-memory master wallet encrypted during the network-bound discovery phase.
Changed components
src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.javaaddAccounts() methodAccountDiscoveryService invocation pathInspect captured patch +8 / −4
### src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java
@@ -696,9 +696,8 @@ private void addAccounts(Wallet masterWallet, List<StandardAccount> standardAcco
EventManager.get().post(new StorageEvent(walletId, TimedEvent.Action.END, "Done"));
ECKey encryptionFullKey = keyDerivationService.getValue();
Key key = new Key(encryptionFullKey.getPrivKeyBytes(), walletForm.getStorage().getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2);
- masterWallet.decrypt(key);
-
if(masterWallet.getKeystores().stream().anyMatch(ks -> ks.getSource() != KeystoreSource.SW_SEED)) {
+ masterWallet.decrypt(key);
try {
for(StandardAccount standardAccount : standardAccounts) {
Wallet childWallet = masterWallet.addChildWallet(standardAccount);
@@ -710,8 +709,12 @@ private void addAccounts(Wallet masterWallet, List<StandardAccount> standardAcco
key.clear();
}
} else if(discoverAccounts) {
- ElectrumServer.AccountDiscoveryService accountDiscoveryService = new ElectrumServer.AccountDiscoveryService(masterWallet, standardAccounts);
+ //Discovery can take a while, during which the open wallet stays encrypted
+ Wallet discoveryWallet = masterWallet.copy();
+ discoveryWallet.decrypt(key);
+ ElectrumServer.AccountDiscoveryService accountDiscoveryService = new ElectrumServer.AccountDiscoveryService(discoveryWallet, standardAccounts);
accountDiscoveryService.setOnSucceeded(event -> {
+ masterWallet.decrypt(key);
addAndEncryptAccounts(masterWallet, accountDiscoveryService.getValue(), key);
if(accountDiscoveryService.getValue().isEmpty()) {
AppServices.showAlertDialog("No Accounts Found", "No new accounts with existing transactions were found. " +
@@ -721,11 +724,12 @@ private void addAccounts(Wallet masterWallet, List<StandardAccount> standardAcco
});
accountDiscoveryService.setOnFailed(event -> {
log.error("Failed to discover accounts", event.getSource().getException());
- addAndEncryptAccounts(masterWallet, Collections.emptyList(), key);
+ key.clear();
AppServices.showErrorDialog("Failed to discover accounts", event.getSource().getException().getMessage());
});
accountDiscoveryService.start();
} else {
+ masterWallet.decrypt(key);
addAndEncryptAccounts(masterWallet, standardAccounts, key);
}
});Why this scored 43/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.