From 143da5669ffc9b3cf85dd1cc86cdf494e8a49b4b Mon Sep 17 00:00:00 2001 From: Matthew Leon Date: Thu, 28 Mar 2019 22:23:58 -0400 Subject: [PATCH] KeyChainGroup: Be smarter about encryption order. This partly reverts commit 4ed5b0e3b06c18040d83be30df32e7be5b778b51. (cherry picked from commit 5570b0b47639e4a1210ca317a69a0c49549404cc) --- .../src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java | 6 ++++-- core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) 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; } /**