3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 06:44:16 +00:00

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

View File

@ -2370,6 +2370,11 @@ public class WalletTest extends TestWithWallet {
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.