diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index d55cc68dc0309..b70e8805b06db 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -74,6 +74,8 @@ std::string DecodeDumpString(const std::string &str) { return ret.str(); } +void ParseWIFPrivKey(const std::string strSecret, CKey& key, CPubKey& pubkey); + UniValue importprivkey(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) @@ -117,16 +119,9 @@ UniValue importprivkey(const UniValue& params, bool fHelp) if (fRescan && fPruneMode) throw JSONRPCError(RPC_WALLET_ERROR, "Rescan is disabled in pruned mode"); - CBitcoinSecret vchSecret; - bool fGood = vchSecret.SetString(strSecret); - - if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding"); - - CKey key = vchSecret.GetKey(); - if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range"); - - CPubKey pubkey = key.GetPubKey(); - assert(key.VerifyPubKey(pubkey)); + CKey key; + CPubKey pubkey; + ParseWIFPrivKey(strSecret, key, pubkey); CKeyID vchAddress = pubkey.GetID(); { pwalletMain->MarkDirty(); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b4831ad795093..33ae019808f0b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -55,6 +55,22 @@ void EnsureWalletIsUnlocked() throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first."); } +void ParseWIFPrivKey(const std::string strSecret, CKey& key, CPubKey& pubkey) +{ + CBitcoinSecret vchSecret; + if (!vchSecret.SetString(strSecret)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding"); + } + + key = vchSecret.GetKey(); + if (!key.IsValid()) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range"); + } + + pubkey = key.GetPubKey(); + assert(key.VerifyPubKey(pubkey)); +} + void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry) { int confirms = wtx.GetDepthInMainChain();