diff --git a/core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java b/core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java index 00eec8104..09cc45118 100644 --- a/core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java +++ b/core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java @@ -685,15 +685,17 @@ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { // This code must be exception safe. AnyBasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); - this.basic = newBasic; List newChains = new ArrayList<>(); if (chains != null) { for (AnyDeterministicKeyChain chain : chains) newChains.add(chain.toEncrypted(keyCrypter, aesKey)); + } + this.keyCrypter = keyCrypter; + this.basic = newBasic; + if (chains != null) { this.chains.clear(); this.chains.addAll(newChains); } - this.keyCrypter = keyCrypter; } /** diff --git a/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java b/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java index 5596df728..7ed2241f9 100644 --- a/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java +++ b/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java @@ -661,15 +661,17 @@ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { // This code must be exception safe. BasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); - this.basic = newBasic; List newChains = new ArrayList<>(); if (chains != null) { for (DeterministicKeyChain chain : chains) newChains.add(chain.toEncrypted(keyCrypter, aesKey)); + } + this.keyCrypter = keyCrypter; + this.basic = newBasic; + if (chains != null) { this.chains.clear(); this.chains.addAll(newChains); } - this.keyCrypter = keyCrypter; } /**