3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Adjust the behaviour of the current key mechanism to be more like what gui wallets really need.

This commit is contained in:
Mike Hearn 2014-07-15 20:54:28 +02:00
parent 96ee76e506
commit a5e4d046df
2 changed files with 22 additions and 11 deletions

View File

@ -220,8 +220,12 @@ public class KeyChainGroup {
throw new UnsupportedOperationException("Key is not suitable to receive coins for married keychains." + throw new UnsupportedOperationException("Key is not suitable to receive coins for married keychains." +
" Use freshAddress to get P2SH address instead"); " Use freshAddress to get P2SH address instead");
} }
final DeterministicKey current = currentKeys.get(purpose); DeterministicKey current = currentKeys.get(purpose);
return current != null ? current : freshKey(purpose); if (current == null) {
current = freshKey(purpose);
currentKeys.put(purpose, current);
}
return current;
} }
/** /**
@ -231,7 +235,11 @@ public class KeyChainGroup {
DeterministicKeyChain chain = getActiveKeyChain(); DeterministicKeyChain chain = getActiveKeyChain();
if (isMarried(chain)) { if (isMarried(chain)) {
Address current = currentAddresses.get(purpose); Address current = currentAddresses.get(purpose);
return current != null ? current : freshAddress(purpose); if (current == null) {
current = freshAddress(purpose);
currentAddresses.put(purpose, current);
}
return current;
} else { } else {
return currentKey(purpose).toAddress(params); return currentKey(purpose).toAddress(params);
} }
@ -271,10 +279,7 @@ public class KeyChainGroup {
throw new UnsupportedOperationException("Key is not suitable to receive coins for married keychains." + throw new UnsupportedOperationException("Key is not suitable to receive coins for married keychains." +
" Use freshAddress to get P2SH address instead"); " Use freshAddress to get P2SH address instead");
} }
return chain.getKeys(purpose, numberOfKeys); // Always returns the next key along the key chain.
List<DeterministicKey> keys = chain.getKeys(purpose, numberOfKeys); // Always returns the next key along the key chain.
currentKeys.put(purpose, keys.get(keys.size() - 1));
return keys;
} }
/** /**
@ -453,9 +458,10 @@ public class KeyChainGroup {
/** If the given key is "current", advance the current key to a new one. */ /** If the given key is "current", advance the current key to a new one. */
private void markKeyAsUsed(DeterministicKey key) { private void markKeyAsUsed(DeterministicKey key) {
for (Map.Entry<KeyChain.KeyPurpose, DeterministicKey> entry : currentKeys.entrySet()) { for (Map.Entry<KeyChain.KeyPurpose, DeterministicKey> entry : currentKeys.entrySet()) {
if (entry.getValue().equals(key)) { if (entry.getValue() != null && entry.getValue().equals(key)) {
log.info("Marking key as used: {}", key); log.info("Marking key as used: {}", key);
freshKey(entry.getKey()); currentKeys.put(entry.getKey(), null);
return;
} }
} }
} }

View File

@ -86,10 +86,15 @@ public class KeyChainGroupTest {
assertNotEquals(r1, r3); assertNotEquals(r1, r3);
ECKey c2 = group.freshKey(KeyChain.KeyPurpose.CHANGE); ECKey c2 = group.freshKey(KeyChain.KeyPurpose.CHANGE);
assertNotEquals(r3, c2); assertNotEquals(r3, c2);
// Current key has not moved and will not under marked as used.
ECKey r4 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); ECKey r4 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
assertEquals(r3, r4); assertEquals(r2, r4);
ECKey c3 = group.currentKey(KeyChain.KeyPurpose.CHANGE); ECKey c3 = group.currentKey(KeyChain.KeyPurpose.CHANGE);
assertEquals(c2, c3); assertEquals(c1, c3);
// Mark as used. Current key is now different.
group.markPubKeyAsUsed(r4.getPubKey());
ECKey r5 = group.currentKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
assertNotEquals(r4, r5);
} }
@Test @Test