Wallet: ban usage of wallet.importKey with deterministic keys.

This commit is contained in:
Mike Hearn 2014-08-23 20:39:55 +02:00
parent fcdd0115c4
commit 03c8cf5927
2 changed files with 15 additions and 0 deletions

View file

@ -572,6 +572,8 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
public int importKeys(final List<ECKey> keys) { public int importKeys(final List<ECKey> keys) {
lock.lock(); lock.lock();
try { try {
// API usage check.
checkNoDeterministicKeys(keys);
int result = keychain.importKeys(keys); int result = keychain.importKeys(keys);
saveNow(); saveNow();
return result; return result;
@ -580,6 +582,13 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
} }
} }
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. */ /** 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) { public int importKeysAndEncrypt(final List<ECKey> keys, CharSequence password) {
lock.lock(); lock.lock();
@ -595,6 +604,7 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha
public int importKeysAndEncrypt(final List<ECKey> keys, KeyParameter aesKey) { public int importKeysAndEncrypt(final List<ECKey> keys, KeyParameter aesKey) {
lock.lock(); lock.lock();
try { try {
checkNoDeterministicKeys(keys);
return keychain.importKeysAndEncrypt(keys, aesKey); return keychain.importKeysAndEncrypt(keys, aesKey);
} finally { } finally {
lock.unlock(); lock.unlock();

View file

@ -2370,6 +2370,11 @@ public class WalletTest extends TestWithWallet {
assertNotEquals(watchKey1, watchKey2); assertNotEquals(watchKey1, watchKey2);
} }
@Test(expected = IllegalArgumentException.class)
public void importOfHDKeyForbidden() throws Exception {
wallet.importKey(wallet.freshReceiveKey());
}
//@Test //- this test is slow, disable for now. //@Test //- this test is slow, disable for now.
public void fragmentedReKeying() throws Exception { public void fragmentedReKeying() throws Exception {
// Send lots of small coins and check the fee is correct. // Send lots of small coins and check the fee is correct.