Skip to content

Commit

Permalink
Wallet: ban usage of wallet.importKey with deterministic keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehearn committed Aug 23, 2014
1 parent fcdd011 commit 03c8cf5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/java/com/google/bitcoin/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ public int addKeys(List<ECKey> keys) {
public int importKeys(final List<ECKey> keys) {
lock.lock();
try {
// API usage check.
checkNoDeterministicKeys(keys);
int result = keychain.importKeys(keys);
saveNow();
return result;
Expand All @@ -580,6 +582,13 @@ public int importKeys(final List<ECKey> keys) {
}
}

private void checkNoDeterministicKeys(List<ECKey> keys) {
// Watch out for someone doing wallet.importKey(wallet.freshReceiveKey()); or equivalent: we never tested this.
for (ECKey key : keys)
if (key instanceof DeterministicKey)
throw new IllegalArgumentException("Cannot import HD keys back into the wallet");
}

/** Takes a list of keys and a password, then encrypts and imports them in one step using the current keycrypter. */
public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) {
lock.lock();
Expand All @@ -595,6 +604,7 @@ public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) {
public int importKeysAndEncrypt(final List<ECKey> keys, KeyParameter aesKey) {
lock.lock();
try {
checkNoDeterministicKeys(keys);
return keychain.importKeysAndEncrypt(keys, aesKey);
} finally {
lock.unlock();
Expand Down
5 changes: 5 additions & 0 deletions core/src/test/java/com/google/bitcoin/core/WalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,11 @@ public void keyRotationHD() throws Exception {
assertNotEquals(watchKey1, watchKey2);
}

@Test(expected = IllegalArgumentException.class)
public void importOfHDKeyForbidden() throws Exception {
wallet.importKey(wallet.freshReceiveKey());
}

//@Test //- this test is slow, disable for now.
public void fragmentedReKeying() throws Exception {
// Send lots of small coins and check the fee is correct.
Expand Down

0 comments on commit 03c8cf5

Please sign in to comment.