From 14806a3ea5f48866de14cd8c936e9aa4176180a5 Mon Sep 17 00:00:00 2001 From: Oscar Guindzberg Date: Wed, 7 Oct 2020 16:27:39 -0300 Subject: [PATCH] key chains: Keep listeners when decrypting/encrypting --- .../main/java/org/bitcoinj/wallet/BasicKeyChain.java | 12 +++++++++++- .../org/bitcoinj/wallet/DeterministicKeyChain.java | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java index bbbe72f0abc..453eb5d77d0 100644 --- a/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java @@ -409,7 +409,11 @@ public void addEventListener(KeyChainEventListener listener) { @Override public void addEventListener(KeyChainEventListener listener, Executor executor) { - listeners.add(new ListenerRegistration<>(listener, executor)); + addEventListener(new ListenerRegistration<>(listener, executor)); + } + + public void addEventListener(ListenerRegistration listener) { + listeners.add(listener); } @Override @@ -476,6 +480,9 @@ public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) { throw new KeyCrypterException("The key " + key.toString() + " cannot be successfully decrypted after encryption so aborting wallet encryption."); encrypted.importKeyLocked(encryptedKey); } + for (ListenerRegistration listener : listeners) { + encrypted.listeners.add(listener); + } return encrypted; } finally { lock.unlock(); @@ -501,6 +508,9 @@ public BasicKeyChain toDecrypted(KeyParameter aesKey) { for (ECKey key : hashToKeys.values()) { decrypted.importKeyLocked(key.decrypt(aesKey)); } + for (ListenerRegistration listener : listeners) { + decrypted.listeners.add(listener); + } return decrypted; } finally { lock.unlock(); diff --git a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java index 96b0df75110..7828581ca4f 100644 --- a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java @@ -22,6 +22,7 @@ import org.bitcoinj.core.Utils; import org.bitcoinj.crypto.*; import org.bitcoinj.script.Script; +import org.bitcoinj.utils.ListenerRegistration; import org.bitcoinj.utils.Threading; import org.bitcoinj.wallet.listeners.KeyChainEventListener; @@ -428,6 +429,9 @@ protected DeterministicKeyChain(KeyCrypter crypter, KeyParameter aesKey, Determi hierarchy.putKey(key); basicKeyChain.importKey(key); } + for (ListenerRegistration listener : chain.basicKeyChain.getListeners()) { + basicKeyChain.addEventListener(listener); + } } public ImmutableList getAccountPath() { @@ -1030,6 +1034,9 @@ public DeterministicKeyChain toDecrypted(KeyParameter aesKey) { } chain.issuedExternalKeys = issuedExternalKeys; chain.issuedInternalKeys = issuedInternalKeys; + for (ListenerRegistration listener : basicKeyChain.getListeners()) { + chain.basicKeyChain.addEventListener(listener); + } return chain; }