Skip to content

Commit

Permalink
Add witness address RPCs (using P2SH)
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Dec 30, 2015
1 parent ad8c0c6 commit d33b80c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/rpcmisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,39 @@ UniValue createmultisig(const UniValue& params, bool fHelp)
return result;
}

UniValue createwitnessaddress(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 1)
{
string msg = "createwitnessaddress \"script\"\n"
"\nCreates a witness address for a particular script.\n"
"It returns a json object with the address and witness script.\n"

"\nArguments:\n"
"1. \"script\" (string, required) A hex encoded script\n"

"\nResult:\n"
"{\n"
" \"address\":\"multisigaddress\", (string) The value of the new address (P2SH of witness script).\n"
" \"witnessScript\":\"script\" (string) The string value of the hex-encoded witness script.\n"
"}\n"
;
throw runtime_error(msg);
}

std::vector<unsigned char> code = ParseHex(params[0].get_str());
CScript script(code.begin(), code.end());
CScript witscript = GetScriptForWitness(script);
CScriptID witscriptid(witscript);
CBitcoinAddress address(witscriptid);

UniValue result(UniValue::VOBJ);
result.push_back(Pair("address", address.ToString()));
result.push_back(Pair("witnessScript", HexStr(witscript.begin(), witscript.end())));

return result;
}

UniValue verifymessage(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 3)
Expand Down
2 changes: 2 additions & 0 deletions src/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ static const CRPCCommand vRPCCommands[] =

/* Utility functions */
{ "util", "createmultisig", &createmultisig, true },
{ "util", "createwitnessaddress", &createwitnessaddress, true },
{ "util", "validateaddress", &validateaddress, true }, /* uses wallet if enabled */
{ "util", "verifymessage", &verifymessage, true },
{ "util", "estimatefee", &estimatefee, true },
Expand All @@ -333,6 +334,7 @@ static const CRPCCommand vRPCCommands[] =
#ifdef ENABLE_WALLET
/* Wallet */
{ "wallet", "addmultisigaddress", &addmultisigaddress, true },
{ "wallet", "addwitnessaddress", &addwitnessaddress, true },
{ "wallet", "backupwallet", &backupwallet, true },
{ "wallet", "dumpprivkey", &dumpprivkey, true },
{ "wallet", "dumpwallet", &dumpwallet, true },
Expand Down
2 changes: 2 additions & 0 deletions src/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ extern UniValue movecmd(const UniValue& params, bool fHelp);
extern UniValue sendfrom(const UniValue& params, bool fHelp);
extern UniValue sendmany(const UniValue& params, bool fHelp);
extern UniValue addmultisigaddress(const UniValue& params, bool fHelp);
extern UniValue addwitnessaddress(const UniValue& params, bool fHelp);
extern UniValue createmultisig(const UniValue& params, bool fHelp);
extern UniValue createwitnessaddress(const UniValue& params, bool fHelp);
extern UniValue listreceivedbyaddress(const UniValue& params, bool fHelp);
extern UniValue listreceivedbyaccount(const UniValue& params, bool fHelp);
extern UniValue listtransactions(const UniValue& params, bool fHelp);
Expand Down
29 changes: 29 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,35 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp)
return CBitcoinAddress(innerID).ToString();
}

UniValue addwitnessaddress(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 1)
{
string msg = "createwitnessaddress \"script\"\n"
"\nAdd a witness address for a particular script to the wallet.\n"
"It returns a json object with the address and witness script.\n"

"\nArguments:\n"
"1. \"script\" (string, required) A hex encoded script\n"

"\nResult:\n"
"\"address\":\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
"}\n"
;
throw runtime_error(msg);
}

std::vector<unsigned char> code = ParseHex(params[0].get_str());
CScript script(code.begin(), code.end());
CScript witscript = GetScriptForWitness(script);
CScriptID witscriptid(witscript);
CBitcoinAddress address(witscriptid);

pwalletMain->AddCScript(script);
pwalletMain->AddCScript(witscript);
UniValue result(UniValue::VOBJ);
return address.ToString();
}

struct tallyitem
{
Expand Down

0 comments on commit d33b80c

Please sign in to comment.