Skip to content

Commit

Permalink
key chains: Keep listeners when decrypting/encrypting
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarguindzberg committed Oct 8, 2020
1 parent 44ddbdc commit 14806a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyChainEventListener> listener) {
listeners.add(listener);
}

@Override
Expand Down Expand Up @@ -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<KeyChainEventListener> listener : listeners) {
encrypted.listeners.add(listener);
}
return encrypted;
} finally {
lock.unlock();
Expand All @@ -501,6 +508,9 @@ public BasicKeyChain toDecrypted(KeyParameter aesKey) {
for (ECKey key : hashToKeys.values()) {
decrypted.importKeyLocked(key.decrypt(aesKey));
}
for (ListenerRegistration<KeyChainEventListener> listener : listeners) {
decrypted.listeners.add(listener);
}
return decrypted;
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -428,6 +429,9 @@ protected DeterministicKeyChain(KeyCrypter crypter, KeyParameter aesKey, Determi
hierarchy.putKey(key);
basicKeyChain.importKey(key);
}
for (ListenerRegistration<KeyChainEventListener> listener : chain.basicKeyChain.getListeners()) {
basicKeyChain.addEventListener(listener);
}
}

public ImmutableList<ChildNumber> getAccountPath() {
Expand Down Expand Up @@ -1030,6 +1034,9 @@ public DeterministicKeyChain toDecrypted(KeyParameter aesKey) {
}
chain.issuedExternalKeys = issuedExternalKeys;
chain.issuedInternalKeys = issuedInternalKeys;
for (ListenerRegistration<KeyChainEventListener> listener : basicKeyChain.getListeners()) {
chain.basicKeyChain.addEventListener(listener);
}
return chain;
}

Expand Down

0 comments on commit 14806a3

Please sign in to comment.