Skip to content

Commit

Permalink
KeyChainGroup: Fix currentKey() and currentAddress() still vends old …
Browse files Browse the repository at this point in the history
…key/address after new chain was activated.
  • Loading branch information
Andreas Schildbach committed Feb 18, 2019
1 parent 9f4f74c commit 497f716
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ public void addAndActivateHDChain(DeterministicKeyChain chain) {
if (lookaheadThreshold >= 0)
chain.setLookaheadThreshold(lookaheadThreshold);
chains.add(chain);
currentKeys.clear();
currentAddresses.clear();
}

/**
Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/org/bitcoinj/wallet/KeyChainGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,27 @@ public void constructFromSeed() throws Exception {
assertEquals(key1, key2);
}

@Test
public void addAndActivateHDChain_freshCurrentAddress() {
DeterministicSeed seed = new DeterministicSeed(ENTROPY, "", 0);
DeterministicKeyChain chain1 = DeterministicKeyChain.builder().seed(seed)
.accountPath(DeterministicKeyChain.ACCOUNT_ZERO_PATH).outputScriptType(Script.ScriptType.P2PKH).build();
group = KeyChainGroup.builder(MAINNET).addChain(chain1).build();
assertEquals("1M5T5k9yKtGWRtWYMjQtGx3K2sshrABzCT", group.currentAddress(KeyPurpose.RECEIVE_FUNDS).toString());

final DeterministicKeyChain chain2 = DeterministicKeyChain.builder().seed(seed)
.accountPath(DeterministicKeyChain.ACCOUNT_ONE_PATH).outputScriptType(Script.ScriptType.P2PKH).build();
group.addAndActivateHDChain(chain2);
assertEquals("1JLnjJEXcyByAaW6sqSxNvGiiSEWRhdvPb", group.currentAddress(KeyPurpose.RECEIVE_FUNDS).toString());

final DeterministicKeyChain chain3 = DeterministicKeyChain.builder().seed(seed)
.accountPath(DeterministicKeyChain.BIP44_ACCOUNT_ZERO_PATH).outputScriptType(Script.ScriptType.P2WPKH)
.build();
group.addAndActivateHDChain(chain3);
assertEquals("bc1q5fa84aghxd6uzk5g2ywkppmzlut5d77vg8cd20",
group.currentAddress(KeyPurpose.RECEIVE_FUNDS).toString());
}

@Test(expected = DeterministicUpgradeRequiredException.class)
public void deterministicUpgradeRequired() throws Exception {
// Check that if we try to use HD features in a KCG that only has random keys, we get an exception.
Expand Down

0 comments on commit 497f716

Please sign in to comment.