From c3ff717676cc0d128a0f945a741a4fc86e91f4f3 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 25 Sep 2023 08:03:19 -0700 Subject: [PATCH] fix: pass keycrypter to AnyBasicKeyChain to fix wallet decryption bug * update AuthenticationKeyChainGroupTest Signed-off-by: HashEngineering --- .../java/org/bitcoinj/wallet/AnyKeyChainGroup.java | 5 +++-- .../wallet/AuthenticationKeyChainGroup.java | 13 ++++++++++++- .../AuthenticationGroupExtension.java | 3 ++- .../AuthenticationKeyChainGroupTest.java | 10 ++++++++++ 4 files changed, 27 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 2e2198985..d0535ea6b 100644 --- a/core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java +++ b/core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java @@ -224,7 +224,7 @@ protected AnyKeyChainGroup(NetworkParameters params, @Nullable AnyBasicKeyChain @Nullable EnumMap currentKeys, @Nullable KeyCrypter crypter, KeyFactory keyFactory) { this.params = params; - this.basic = basicKeyChain == null ? new AnyBasicKeyChain(keyFactory) : basicKeyChain; + this.basic = basicKeyChain == null ? new AnyBasicKeyChain(crypter, keyFactory) : basicKeyChain; this.keyFactory = keyFactory; if (chains != null) { if (lookaheadSize > -1) @@ -686,8 +686,9 @@ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { AnyBasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey); List newChains = new ArrayList<>(); if (chains != null) { - for (AnyDeterministicKeyChain chain : chains) + for (AnyDeterministicKeyChain chain : chains) { newChains.add(chain.toEncrypted(keyCrypter, aesKey)); + } } // Code below this point must be exception safe. diff --git a/core/src/main/java/org/bitcoinj/wallet/AuthenticationKeyChainGroup.java b/core/src/main/java/org/bitcoinj/wallet/AuthenticationKeyChainGroup.java index 115b75d00..b6b01b230 100644 --- a/core/src/main/java/org/bitcoinj/wallet/AuthenticationKeyChainGroup.java +++ b/core/src/main/java/org/bitcoinj/wallet/AuthenticationKeyChainGroup.java @@ -50,6 +50,7 @@ public static class Builder { private final List chains = new LinkedList<>(); private int lookaheadSize = -1, lookaheadThreshold = -1; private KeyFactory keyFactory; + private KeyCrypter keyCrypter; private Builder(NetworkParameters params, KeyChainGroupStructure structure) { this.params = params; @@ -132,8 +133,18 @@ public AuthenticationKeyChainGroup.Builder lookaheadThreshold(int lookaheadThres return this; } + /** + * Set the keyCrypter. + * @param keyCrypter to use + */ + public AuthenticationKeyChainGroup.Builder keyCrypter(KeyCrypter keyCrypter) { + this.keyCrypter = keyCrypter; + return this; + } + + public AuthenticationKeyChainGroup build() { - return new AuthenticationKeyChainGroup(params, null, chains, lookaheadSize, lookaheadThreshold, null, null, keyFactory); + return new AuthenticationKeyChainGroup(params, null, chains, lookaheadSize, lookaheadThreshold, null, keyCrypter, keyFactory); } } diff --git a/core/src/main/java/org/bitcoinj/wallet/authentication/AuthenticationGroupExtension.java b/core/src/main/java/org/bitcoinj/wallet/authentication/AuthenticationGroupExtension.java index 36d4d09b7..d63192b10 100644 --- a/core/src/main/java/org/bitcoinj/wallet/authentication/AuthenticationGroupExtension.java +++ b/core/src/main/java/org/bitcoinj/wallet/authentication/AuthenticationGroupExtension.java @@ -92,7 +92,7 @@ public class AuthenticationGroupExtension extends AbstractKeyChainGroupExtension public static String EXTENSION_ID = "org.dashj.wallet.authentication"; private static final Logger log = LoggerFactory.getLogger(AuthenticationGroupExtension.class); - private final AuthenticationKeyChainGroup keyChainGroup; + private AuthenticationKeyChainGroup keyChainGroup; private final HashMap keyUsage = Maps.newHashMap(); private final CopyOnWriteArrayList> creditFundingListeners @@ -293,6 +293,7 @@ public void deserializeWalletExtension(Wallet containingWallet, byte[] data) thr KeyCrypter keyCrypter = wallet.getKeyCrypter(); AuthenticationKeyChainFactory factory = new AuthenticationKeyChainFactory(); Protos.AuthenticationGroupExtension walletExtension = Protos.AuthenticationGroupExtension.parseFrom(data); + keyChainGroup = AuthenticationKeyChainGroup.authenticationBuilder(wallet.getParams()).keyCrypter(keyCrypter).build(); // extended chains for (Protos.ExtendedKeyChain extendedKeyChain : walletExtension.getAuthenticationKeyChainsList()) { diff --git a/core/src/test/java/org/bitcoinj/wallet/authentication/AuthenticationKeyChainGroupTest.java b/core/src/test/java/org/bitcoinj/wallet/authentication/AuthenticationKeyChainGroupTest.java index 9f62f1ba4..4ba1fd5ab 100644 --- a/core/src/test/java/org/bitcoinj/wallet/authentication/AuthenticationKeyChainGroupTest.java +++ b/core/src/test/java/org/bitcoinj/wallet/authentication/AuthenticationKeyChainGroupTest.java @@ -201,4 +201,14 @@ public void serializationTest() throws UnreadableWalletException { assertEquals(authenticationGroupExtension.currentKey(AuthenticationKeyChain.KeyChainType.MASTERNODE_PLATFORM_OPERATOR), authenticationGroupExtensionCopy.currentKey(AuthenticationKeyChain.KeyChainType.MASTERNODE_PLATFORM_OPERATOR)); } + + @Test + public void encryptionTest() throws UnreadableWalletException { + wallet.encrypt("hello"); + Protos.Wallet protos = new WalletProtobufSerializer().walletToProto(wallet); + AuthenticationGroupExtension authenticationGroupExtensionCopy = new AuthenticationGroupExtension(wallet.getParams()); + Wallet walletCopy = new WalletProtobufSerializer().readWallet(PARAMS, new WalletExtension[]{authenticationGroupExtensionCopy}, protos); + //walletCopy.encrypt("hello"); + walletCopy.decrypt("hello"); + } }