Skip to content

Commit

Permalink
Wallet/RPC: Abstract common WIF privkey parsing into ParseWIFPrivKey …
Browse files Browse the repository at this point in the history
…function

Github-Pull: bitcoin#9152
Rebased-From: 4437a5f875da68032f9539e35679219f316cc851
  • Loading branch information
luke-jr committed Dec 10, 2016
1 parent 155e1cf commit f73bc4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f73bc4f

Please sign in to comment.