diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 83165326a..a7aee6f17 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -1042,6 +1042,18 @@ public class Wallet extends BaseTaggableObject } } + /** + * Locates a keypair from the wallet given the corresponding address. + * @return ECKey or null if no such key was found. + */ + public ECKey findKeyFromAddress(Address address) { + final ScriptType scriptType = address.getOutputScriptType(); + if (scriptType == ScriptType.P2PKH || scriptType == ScriptType.P2WPKH) + return findKeyFromPubHash(address.getHash()); + else + return null; + } + /** * Locates a keypair from the basicKeyChain given the raw public key bytes. * @return ECKey or null if no such key was found. diff --git a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java index 39dea122a..8588f98e7 100644 --- a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java +++ b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java @@ -1493,7 +1493,7 @@ public class WalletTool { } else { try { Address address = LegacyAddress.fromBase58(wallet.getParams(), addr); - key = wallet.findKeyFromPubHash(address.getHash()); + key = wallet.findKeyFromAddress(address); } catch (AddressFormatException e) { System.err.println(addr + " does not parse as a Bitcoin address of the right network parameters."); return;