Skip to content

Commit

Permalink
fix: pass keycrypter to AnyBasicKeyChain to fix wallet decryption bug
Browse files Browse the repository at this point in the history
* update AuthenticationKeyChainGroupTest

Signed-off-by: HashEngineering <[email protected]>
  • Loading branch information
HashEngineering committed Sep 25, 2023
1 parent aa01975 commit c3ff717
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions core/src/main/java/org/bitcoinj/wallet/AnyKeyChainGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected AnyKeyChainGroup(NetworkParameters params, @Nullable AnyBasicKeyChain
@Nullable EnumMap<KeyChain.KeyPurpose, IDeterministicKey> 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)
Expand Down Expand Up @@ -686,8 +686,9 @@ public void encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) {
AnyBasicKeyChain newBasic = basic.toEncrypted(keyCrypter, aesKey);
List<AnyDeterministicKeyChain> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class Builder {
private final List<AnyDeterministicKeyChain> chains = new LinkedList<>();
private int lookaheadSize = -1, lookaheadThreshold = -1;
private KeyFactory keyFactory;
private KeyCrypter keyCrypter;

private Builder(NetworkParameters params, KeyChainGroupStructure structure) {
this.params = params;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IKey, AuthenticationKeyUsage> keyUsage = Maps.newHashMap();

private final CopyOnWriteArrayList<ListenerRegistration<CreditFundingTransactionEventListener>> creditFundingListeners
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit c3ff717

Please sign in to comment.