diff --git a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java index 23b98b4d..ca5174df 100644 --- a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java +++ b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java @@ -377,7 +377,7 @@ public class DeterministicKey extends ECKey { BigInteger privKey = findOrDeriveEncryptedPrivateKey(keyCrypter, aesKey); DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent); if (!Arrays.equals(key.getPubKey(), getPubKey())) - throw new KeyCrypterException("Provided AES key is wrong"); + throw new KeyCrypterException.PublicPrivateMismatch("Provided AES key is wrong"); if (parent == null) key.setCreationTimeSeconds(getCreationTimeSeconds()); return key; @@ -436,7 +436,7 @@ public class DeterministicKey extends ECKey { // If it's not, it means we tried decrypting with an invalid password and earlier checks e.g. for padding didn't // catch it. if (!downCursor.pub.equals(pub)) - throw new KeyCrypterException("Could not decrypt bytes"); + throw new KeyCrypterException.PublicPrivateMismatch("Could not decrypt bytes"); return checkNotNull(downCursor.priv); } diff --git a/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java b/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java index b5fe6a5c..5e8ae9af 100644 --- a/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java +++ b/core/src/main/java/org/bitcoinj/crypto/KeyCrypterException.java @@ -28,11 +28,25 @@ package org.bitcoinj.crypto; public class KeyCrypterException extends RuntimeException { private static final long serialVersionUID = -4441989608332681377L; - public KeyCrypterException(String s) { - super(s); + public KeyCrypterException(String message) { + super(message); } - public KeyCrypterException(String s, Throwable throwable) { - super(s, throwable); + public KeyCrypterException(String message, Throwable throwable) { + super(message, throwable); + } + + /** + * This exception is thrown when a private key or seed is decrypted, it doesn't match its public key any + * more. This likely means the wrong decryption key has been used. + */ + public static class PublicPrivateMismatch extends KeyCrypterException { + public PublicPrivateMismatch(String message) { + super(message); + } + + public PublicPrivateMismatch(String message, Throwable throwable) { + super(message, throwable); + } } } diff --git a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java index fe19679b..e60591f4 100644 --- a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java @@ -1014,7 +1014,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain { DeterministicKeyChain chain = makeKeyChainFromSeed(decSeed, getAccountPath(), outputScriptType); // Now double check that the keys match to catch the case where the key is wrong but padding didn't catch it. if (!chain.getWatchingKey().getPubKeyPoint().equals(getWatchingKey().getPubKeyPoint())) - throw new KeyCrypterException("Provided AES key is wrong"); + throw new KeyCrypterException.PublicPrivateMismatch("Provided AES key is wrong"); chain.lookaheadSize = lookaheadSize; // Now copy the (pubkey only) leaf keys across to avoid rederiving them. The private key bytes are missing // anyway so there's nothing to decrypt.