From 03c8cf59273ed9b0003e2530ac9d77a4214da27f Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Sat, 23 Aug 2014 20:39:55 +0200 Subject: [PATCH] Wallet: ban usage of wallet.importKey with deterministic keys. --- core/src/main/java/com/google/bitcoin/core/Wallet.java | 10 ++++++++++ .../test/java/com/google/bitcoin/core/WalletTest.java | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index 9a92ca0126f..ea48b6c55d8 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -572,6 +572,8 @@ public int addKeys(List keys) { public int importKeys(final List keys) { lock.lock(); try { + // API usage check. + checkNoDeterministicKeys(keys); int result = keychain.importKeys(keys); saveNow(); return result; @@ -580,6 +582,13 @@ public int importKeys(final List keys) { } } + private void checkNoDeterministicKeys(List 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 keys, CharSequence password) { lock.lock(); @@ -595,6 +604,7 @@ public int importKeysAndEncrypt(final List keys, CharSequence password) { public int importKeysAndEncrypt(final List keys, KeyParameter aesKey) { lock.lock(); try { + checkNoDeterministicKeys(keys); return keychain.importKeysAndEncrypt(keys, aesKey); } finally { lock.unlock(); diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index 907d72b37e0..e08653abd6f 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -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.