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.