encrypt account keystores when a password is set together with a change needing a refresh, and edit an account as a child of the master wallet copy
What changed, and why it matters
This commit fixes a bug in Sparrow Wallet's settings form. When a user changed wallet settings that required a full refresh (like the gap limit) at the same time as adding a wallet password, the software could fail to properly encrypt the private key data for child accounts. The fix ensures account 'keystores' are copied and encrypted consistently with the master wallet, and adds tests to confirm the behavior.
Treat this as a security fix and include it in the next release. Users who set or changed a wallet password while simultaneously changing settings that force a wallet refresh (e.g., gap limit) should verify that all account keystores are reported as encrypted in the wallet settings. Review related settings-change paths for similar missed encryption events.
Security signals we found
Incorrect encryption state propagation for child account keystores during settings refresh
Missing security event emission on refresh-necessary code path
Wallet copy semantics changed to keep account copies attached to master wallet copy
Added regression tests for encryption of account keystores on password set
Evidence from the diff
The patch refactors SettingsWalletForm so that when editing an account wallet, the working copy is created as a child of a copied master wallet rather than as an isolated account copy. It also ensures that when a full wallet refresh is triggered alongside a password change, the KeystoreEncryptionChangedEvent is still posted for all affected master and child wallet keystores. Previously, the early refresh path could bypass the encryption notification logic. A new unit test verifies that adding a password together with a gap-limit change encrypts account keystores and emits the expected event.
Changed components
com.sparrowwallet.sparrow.wallet.SettingsWalletFormWallet keystore encryption handlingMaster/child wallet account settings editingEventManager KeystoreEncryptionChangedEvent dispatchInspect captured patch +170 / −22
### src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java
@@ -27,8 +27,7 @@ public class SettingsWalletForm extends WalletForm {
public SettingsWalletForm(Storage storage, Wallet currentWallet, WalletForm appWalletForm) {
super(storage, currentWallet);
- this.walletCopy = currentWallet.copy();
- this.walletCopy.setMasterWallet(walletCopy.isMasterWallet() ? null : walletCopy.getMasterWallet().copy());
+ this.walletCopy = copyWallet(currentWallet);
this.appWalletForm = appWalletForm;
disposeRefreshNodes();
}
@@ -40,9 +39,31 @@ public Wallet getWallet() {
@Override
public void setWallet(Wallet wallet) {
+ //An account replaced in settings takes the place of the one in the master copy, so that changes made to the master copy's accounts reach it
+ if(!wallet.isMasterWallet() && walletCopy != null && !walletCopy.isMasterWallet() && wallet.getMasterWallet() == walletCopy.getMasterWallet()) {
+ List<Wallet> childWallets = wallet.getMasterWallet().getChildWallets();
+ for(int i = 0; i < childWallets.size(); i++) {
+ if(childWallets.get(i) == walletCopy) {
+ childWallets.set(i, wallet);
+ }
+ }
+ }
+
this.walletCopy = wallet;
}
+ //An account is copied as the child of a copy of its master wallet, rather than as a copy of its own, so that a change made to the master copy and
+ //its accounts, such as encrypting them all, also applies to it
+ private static Wallet copyWallet(Wallet wallet) {
+ if(wallet.isMasterWallet()) {
+ return wallet.copy();
+ }
+
+ Wallet masterWalletCopy = wallet.getMasterWallet().copy();
+ int childIndex = wallet.getMasterWallet().getChildWallets().indexOf(wallet);
+ return masterWalletCopy.getChildWallets().get(childIndex);
+ }
+
public WalletForm getAppWalletForm() {
return appWalletForm;
}
@@ -64,8 +85,7 @@ public void setLocked(boolean locked) {
@Override
public void revert() {
- this.walletCopy = super.getWallet().copy();
- this.walletCopy.setMasterWallet(walletCopy.isMasterWallet() ? null : walletCopy.getMasterWallet().copy());
+ this.walletCopy = copyWallet(super.getWallet());
}
@Override
@@ -103,6 +123,9 @@ public void saveAndRefresh() throws IOException, StorageException {
save();
EventManager.get().post(new WalletAddressesChangedEvent(wallet, addressChange ? null : pastWallet, getWalletId()));
+
+ //A password changed along with a change needing a refresh must still reach the keystores the save above does not write
+ postEncryptionChangedKeystores();
} else {
List<Keystore> labelChangedKeystores = getLabelChangedKeystores(wallet, walletCopy);
if(!labelChangedKeystores.isEmpty()) {
@@ -113,22 +136,7 @@ public void saveAndRefresh() throws IOException, StorageException {
EventManager.get().post(new WalletWatchLastChangedEvent(wallet, pastWallet, getWalletId(), walletCopy.getWatchLast()));
}
- Wallet masterWallet = getMasterWallet();
- Wallet masterWalletCopy = walletCopy.isMasterWallet() ? walletCopy : walletCopy.getMasterWallet();
- List<Keystore> encryptionChangedKeystores = getEncryptionChangedKeystores(masterWallet, masterWalletCopy);
- if(!encryptionChangedKeystores.isEmpty()) {
- EventManager.get().post(new KeystoreEncryptionChangedEvent(masterWallet, masterWallet.copy(), getStorage().getWalletId(masterWallet), encryptionChangedKeystores));
- }
-
- for(Wallet childWallet : masterWallet.getChildWallets()) {
- Wallet childWalletCopy = masterWalletCopy.getChildWallet(childWallet.getName());
- if(childWalletCopy != null) {
- List<Keystore> childEncryptionChangedKeystores = getEncryptionChangedKeystores(childWallet, childWalletCopy);
- if(!childEncryptionChangedKeystores.isEmpty()) {
- EventManager.get().post(new KeystoreEncryptionChangedEvent(childWallet, childWallet.copy(), getStorage().getWalletId(childWallet), childEncryptionChangedKeystores));
- }
- }
- }
+ List<Keystore> encryptionChangedKeystores = postEncryptionChangedKeystores();
if(labelChangedKeystores.isEmpty() && encryptionChangedKeystores.isEmpty()) {
//Can only be a wallet password change on a wallet without private keys
@@ -137,6 +145,29 @@ public void saveAndRefresh() throws IOException, StorageException {
}
}
+ //Posts the keystores of the master wallet and its accounts whose encryption the copy changed, returning those of the master wallet
+ private List<Keystore> postEncryptionChangedKeystores() {
+ Wallet masterWallet = getMasterWallet();
+ Wallet masterWalletCopy = walletCopy.isMasterWallet() ? walletCopy : walletCopy.getMasterWallet();
+ List<Keystore> encryptionChangedKeystores = getEncryptionChangedKeystores(masterWallet, masterWalletCopy);
+ if(!encryptionChangedKeystores.isEmpty()) {
+ EventManager.get().post(new KeystoreEncryptionChangedEvent(masterWallet, masterWallet.copy(), getStorage().getWalletId(masterWallet), encryptionChangedKeystores));
+ }
+
+ for(Wallet childWallet : masterWallet.getChildWallets()) {
+ Wallet childWalletCopy = masterWalletCopy.getChildWallet(childWallet.getName());
+ if(childWalletCopy != null) {
+ List<Keystore> childEncryptionChangedKeystores = getEncryptionChangedKeystores(childWallet, childWalletCopy);
+ if(!childEncryptionChangedKeystores.isEmpty()) {
+ EventManager.get().post(new KeystoreEncryptionChangedEvent(childWallet, childWallet.copy(), getStorage().getWalletId(childWallet),
+ childEncryptionChangedKeystores));
+ }
+ }
+ }
+
+ return encryptionChangedKeystores;
+ }
+
//Returns true for any change, other than a keystore label change, to trigger a full wallet refresh
//Even though this is not strictly necessary for some changes, it it better to refresh on saving so background transaction history updates on the old wallet have no effect/are not lost
private boolean isRefreshNecessary(Wallet original, Wallet changed) {
@@ -244,7 +275,8 @@ private List<Keystore> getLabelChangedKeystores(Wallet original, Wallet changed)
private List<Keystore> getEncryptionChangedKeystores(Wallet original, Wallet changed) {
List<Keystore> changedKeystores = new ArrayList<>();
- for(int i = 0; i < original.getKeystores().size(); i++) {
+ //A wallet whose keystores were replaced may have a different number of them
+ for(int i = 0; i < Math.min(original.getKeystores().size(), changed.getKeystores().size()); i++) {
Keystore originalKeystore = original.getKeystores().get(i);
Keystore changedKeystore = changed.getKeystores().get(i);
@@ -277,7 +309,7 @@ public void childWalletSaved(Wallet childWallet) {
if(childWalletCopy == null) {
childWalletCopy = childWallet.copy();
childWalletCopy.setMasterWallet(masterWalletCopy);
- masterWalletCopy.getChildWallets().add(childWallet.copy());
+ masterWalletCopy.getChildWallets().add(childWalletCopy);
}
}
### src/test/java/com/sparrowwallet/sparrow/wallet/SettingsWalletFormTest.java
@@ -0,0 +1,116 @@
+package com.sparrowwallet.sparrow.wallet;
+
+import com.google.common.eventbus.Subscribe;
+import com.sparrowwallet.drongo.Utils;
+import com.sparrowwallet.drongo.crypto.Argon2KeyDeriver;
+import com.sparrowwallet.drongo.crypto.Key;
+import com.sparrowwallet.drongo.policy.Policy;
+import com.sparrowwallet.drongo.policy.PolicyType;
+import com.sparrowwallet.drongo.protocol.ScriptType;
+import com.sparrowwallet.drongo.wallet.DeterministicSeed;
+import com.sparrowwallet.drongo.wallet.Keystore;
+import com.sparrowwallet.drongo.wallet.StandardAccount;
+import com.sparrowwallet.drongo.wallet.Wallet;
+import com.sparrowwallet.sparrow.EventManager;
+import com.sparrowwallet.sparrow.SparrowWallet;
+import com.sparrowwallet.sparrow.event.KeystoreEncryptionChangedEvent;
+import com.sparrowwallet.sparrow.io.PersistenceType;
+import com.sparrowwallet.sparrow.io.Storage;
+import org.junit.jupiter.api.*;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+public class SettingsWalletFormTest {
+ @TempDir
+ private static Path tempHome;
+
+ private final List<KeystoreEncryptionChangedEvent> encryptionEvents = new ArrayList<>();
+
+ @BeforeAll
+ public static void setUpAll() {
+ System.setProperty(SparrowWallet.APP_HOME_PROPERTY, tempHome.toString());
+ }
+
+ @AfterAll
+ public static void tearDownAll() {
+ System.clearProperty(SparrowWallet.APP_HOME_PROPERTY);
+ }
+
+ @BeforeEach
+ public void setUp() {
+ EventManager.get().register(this);
+ }
+
+ @AfterEach
+ public void tearDown() {
+ EventManager.get().unregister(this);
+ }
+
+ @Subscribe
+ public void keystoreEncryptionChanged(KeystoreEncryptionChangedEvent event) {
+ encryptionEvents.add(event);
+ }
+
+ private static Wallet seedWallet() throws Exception {
+ Wallet wallet = new Wallet("settings");
+ wallet.setPolicyType(PolicyType.SINGLE_HD);
+ wallet.setScriptType(ScriptType.P2WPKH);
+ DeterministicSeed seed = new DeterministicSeed(Utils.hexToBytes(String.format("%032x", 3)), "", 0);
+ wallet.getKeystores().add(Keystore.fromSeed(seed, PolicyType.SINGLE_HD, ScriptType.P2WPKH.getDefaultDerivation()));
+ wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE_HD, ScriptType.P2WPKH, wallet.getKeystores(), null));
+ return wallet;
+ }
+
+ private Storage savedStorage(Wallet masterWallet) throws Exception {
+ Storage storage = new Storage(PersistenceType.DB, new File(tempHome.toFile(), "settings-" + System.nanoTime() + ".mv.db"));
+ storage.setEncryptionPubKey(Storage.NO_PASSWORD_KEY);
+ storage.saveWallet(masterWallet);
+ for(Wallet childWallet : masterWallet.getChildWallets()) {
+ storage.saveWallet(childWallet);
+ }
+ return storage;
+ }
+
+ @Test
+ public void accountCopyIsChildOfMasterCopy() throws Exception {
+ Wallet masterWallet = seedWallet();
+ Wallet account = masterWallet.addChildWallet(StandardAccount.ACCOUNT_1);
+ Storage storage = savedStorage(masterWallet);
+ SettingsWalletForm form = new SettingsWalletForm(storage, account, null);
+
+ Wallet accountCopy = form.getWallet();
+ Assertions.assertNotSame(account, accountCopy);
+ Assertions.assertTrue(accountCopy.getMasterWallet().getChildWallets().stream().anyMatch(child -> child == accountCopy));
+
+ Wallet replacement = accountCopy.copy();
+ replacement.setMasterWallet(accountCopy.getMasterWallet());
+ form.setWallet(replacement);
+ Assertions.assertTrue(replacement.getMasterWallet().getChildWallets().stream().anyMatch(child -> child == replacement));
+ Assertions.assertTrue(replacement.getMasterWallet().getChildWallets().stream().noneMatch(child -> child == accountCopy));
+ storage.closeAndWait();
+ }
+
+ @Test
+ public void passwordAddedWithGapLimitChangeEncryptsAccounts() throws Exception {
+ Wallet masterWallet = seedWallet();
+ Wallet account = masterWallet.addChildWallet(StandardAccount.ACCOUNT_1);
+ Storage storage = savedStorage(masterWallet);
+ SettingsWalletForm form = new SettingsWalletForm(storage, masterWallet, null);
+
+ Wallet masterCopy = form.getWallet();
+ Key key = new Argon2KeyDeriver().deriveKey("pass");
+ masterCopy.encrypt(key);
+ masterCopy.getChildWallets().forEach(child -> child.encrypt(key));
+ form.gapLimitChanged(masterCopy.getGapLimit() + 5);
+
+ form.saveAndRefresh();
+
+ Assertions.assertTrue(encryptionEvents.stream().anyMatch(event -> event.getWallet() == account), "the account's keystores were not re-encrypted");
+ Assertions.assertTrue(account.getKeystores().getFirst().getSeed().isEncrypted());
+ storage.closeAndWait();
+ }
+}Why this scored 57/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.