derive node addresses rather than using stored addresses when importing a sparrow wallet file
What changed, and why it matters
This commit changes how Sparrow Wallet imports its own wallet files. Previously, when loading a wallet backup, Sparrow trusted the address data stored inside the file. Now it clears those stored addresses and re-derives them from the wallet's cryptographic keys. This prevents a tampered or crafted wallet file from sneaking in bogus Bitcoin addresses that could trick the user into sending funds to the wrong place. The change also adds tests to make sure addresses are always recalculated and that user labels on addresses are preserved.
Treat this as a security-hardening fix and include it in the next release. Users who imported Sparrow wallet files with older versions should verify that receive addresses match expected derived addresses before receiving funds. Review the drongo submodule diff for completeness, as the actual address derivation logic lives there.
Security signals we found
Tampered serialized addresses in wallet backup files are no longer trusted during import
Addresses are re-derived deterministically from wallet keys/seeds after import
Child wallets and all purpose/address nodes are covered by the clearing logic
New regression tests explicitly check address derivation behavior on import
Submodule bump suggests related library-level changes in drongo
Evidence from the diff
The patch modifies Sparrow.java’s importWallet() to iterate over every imported wallet (root plus child wallets) and every purpose/address node, calling setAddress(null) on each node. This forces the wallet to re-derive addresses from the stored seed/keys rather than using serialized address objects. The drongo submodule is bumped, likely containing supporting changes in the underlying wallet library. New unit tests verify that imported addresses match freshly derived addresses for both root and child wallets, and that node labels survive import even though addresses are recalculated.
Changed components
src/main/java/com/sparrowwallet/sparrow/io/Sparrow.javadrongo submoduleWallet import/backup functionalityWalletNode address handlingInspect captured patch +68 / −1
### drongo
@@ -1 +1 @@
-Subproject commit 3ce42353d76323f1d8c36c32d87e342fdd21572c
+Subproject commit 4d85e20bad80d8f25a8e422d9d1ed8308f7e01b7
### src/main/java/com/sparrowwallet/sparrow/io/Sparrow.java
@@ -4,6 +4,7 @@
import com.sparrowwallet.drongo.wallet.MnemonicException;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletModel;
+import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.AppServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,6 +16,7 @@
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Map;
+import java.util.stream.Stream;
public class Sparrow implements WalletImport, WalletExport {
private static final Logger log = LoggerFactory.getLogger(Sparrow.class);
@@ -123,6 +125,15 @@ public Wallet importWallet(InputStream inputStream, String password) throws Impo
storage.restorePublicKeysFromSeed(childWallet, null);
}
+ for(Wallet importedWallet : Stream.concat(Stream.of(wallet), wallet.getChildWallets().stream()).toList()) {
+ for(WalletNode purposeNode : importedWallet.getPurposeNodes()) {
+ purposeNode.setAddress(null);
+ for(WalletNode addressNode : purposeNode.getChildren()) {
+ addressNode.setAddress(null);
+ }
+ }
+ }
+
return wallet;
} catch(IOException | StorageException | MnemonicException e) {
throw new ImportException("Error importing Sparrow wallet", e);
### src/test/java/com/sparrowwallet/sparrow/io/SparrowTest.java
@@ -2,6 +2,7 @@
import com.sparrowwallet.drongo.ExtendedKey;
import com.sparrowwallet.drongo.KeyDerivation;
+import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.crypto.Argon2KeyDeriver;
import com.sparrowwallet.drongo.crypto.ECKey;
import com.sparrowwallet.drongo.crypto.EncryptionType;
@@ -12,8 +13,10 @@
import com.sparrowwallet.drongo.wallet.DeterministicSeed;
import com.sparrowwallet.drongo.wallet.Keystore;
import com.sparrowwallet.drongo.wallet.KeystoreSource;
+import com.sparrowwallet.drongo.wallet.StandardAccount;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletModel;
+import com.sparrowwallet.drongo.wallet.WalletNode;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -135,4 +138,57 @@ public void watchOnlyWalletImports() throws Exception {
Assertions.assertEquals(ExtendedKey.fromDescriptor(TEST_XPUB), imported.getKeystores().getFirst().getExtendedPublicKey());
Assertions.assertDoesNotThrow(imported::checkWallet);
}
+
+ @Test
+ public void importDerivesNodeAddresses() throws Exception {
+ Wallet wallet = createWatchOnlyWallet();
+ for(KeyPurpose keyPurpose : KeyPurpose.DEFAULT_PURPOSES) {
+ wallet.getFreshNode(keyPurpose).setAddress(ScriptType.P2WPKH.getAddress(new byte[20]));
+ }
+ File walletFile = saveWallet(wallet, null);
+
+ Wallet imported = importWallet(walletFile, null);
+ for(KeyPurpose keyPurpose : KeyPurpose.DEFAULT_PURPOSES) {
+ WalletNode node = imported.getFreshNode(keyPurpose);
+ Assertions.assertEquals(imported.getAddress(node), node.getAddress());
+ }
+ }
+
+ @Test
+ public void importDerivesChildWalletNodeAddresses() throws Exception {
+ Wallet wallet = createSeedWallet();
+ Wallet childWallet = wallet.addChildWallet(StandardAccount.ACCOUNT_1);
+ childWallet.getFreshNode(KeyPurpose.RECEIVE).setAddress(ScriptType.P2WPKH.getAddress(new byte[20]));
+
+ Storage storage = new Storage(PersistenceType.DB, tempDir.resolve(wallet.getName() + "." + PersistenceType.DB.getExtension()).toFile());
+ storage.setEncryptionPubKey(Storage.NO_PASSWORD_KEY);
+ try {
+ storage.saveWallet(wallet);
+ storage.saveWallet(childWallet);
+ } finally {
+ storage.closeAndWait();
+ }
+ File walletFile = storage.getWalletFile();
+
+ Wallet imported = importWallet(walletFile, null);
+ Assertions.assertEquals(1, imported.getChildWallets().size());
+ Wallet importedChild = imported.getChildWallets().getFirst();
+ WalletNode node = importedChild.getFreshNode(KeyPurpose.RECEIVE);
+ Assertions.assertEquals(importedChild.getAddress(node), node.getAddress());
+ }
+
+ @Test
+ public void importPreservesNodeLabels() throws Exception {
+ Wallet wallet = createWatchOnlyWallet();
+ WalletNode node = wallet.getFreshNode(KeyPurpose.RECEIVE);
+ node.setLabel("Invoice 1");
+ String expectedAddress = node.getAddress().toString();
+ File walletFile = saveWallet(wallet, null);
+
+ Wallet imported = importWallet(walletFile, null);
+ WalletNode importedNode = imported.getNode(KeyPurpose.RECEIVE).getChildren().stream().filter(n -> n.getIndex() == node.getIndex()).findFirst().orElseThrow();
+ Assertions.assertEquals(node.getDerivationPath(), importedNode.getDerivationPath());
+ Assertions.assertEquals(expectedAddress, importedNode.getAddress().toString());
+ Assertions.assertEquals("Invoice 1", importedNode.getLabel());
+ }
}Why this scored 59/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.