Skip to content

Commit d33b80c

Browse files
committed
Add witness address RPCs (using P2SH)
1 parent ad8c0c6 commit d33b80c

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

src/rpcmisc.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,39 @@ UniValue createmultisig(const UniValue& params, bool fHelp)
312312
return result;
313313
}
314314

315+
UniValue createwitnessaddress(const UniValue& params, bool fHelp)
316+
{
317+
if (fHelp || params.size() < 1 || params.size() > 1)
318+
{
319+
string msg = "createwitnessaddress \"script\"\n"
320+
"\nCreates a witness address for a particular script.\n"
321+
"It returns a json object with the address and witness script.\n"
322+
323+
"\nArguments:\n"
324+
"1. \"script\" (string, required) A hex encoded script\n"
325+
326+
"\nResult:\n"
327+
"{\n"
328+
" \"address\":\"multisigaddress\", (string) The value of the new address (P2SH of witness script).\n"
329+
" \"witnessScript\":\"script\" (string) The string value of the hex-encoded witness script.\n"
330+
"}\n"
331+
;
332+
throw runtime_error(msg);
333+
}
334+
335+
std::vector<unsigned char> code = ParseHex(params[0].get_str());
336+
CScript script(code.begin(), code.end());
337+
CScript witscript = GetScriptForWitness(script);
338+
CScriptID witscriptid(witscript);
339+
CBitcoinAddress address(witscriptid);
340+
341+
UniValue result(UniValue::VOBJ);
342+
result.push_back(Pair("address", address.ToString()));
343+
result.push_back(Pair("witnessScript", HexStr(witscript.begin(), witscript.end())));
344+
345+
return result;
346+
}
347+
315348
UniValue verifymessage(const UniValue& params, bool fHelp)
316349
{
317350
if (fHelp || params.size() != 3)

src/rpcserver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ static const CRPCCommand vRPCCommands[] =
315315

316316
/* Utility functions */
317317
{ "util", "createmultisig", &createmultisig, true },
318+
{ "util", "createwitnessaddress", &createwitnessaddress, true },
318319
{ "util", "validateaddress", &validateaddress, true }, /* uses wallet if enabled */
319320
{ "util", "verifymessage", &verifymessage, true },
320321
{ "util", "estimatefee", &estimatefee, true },
@@ -333,6 +334,7 @@ static const CRPCCommand vRPCCommands[] =
333334
#ifdef ENABLE_WALLET
334335
/* Wallet */
335336
{ "wallet", "addmultisigaddress", &addmultisigaddress, true },
337+
{ "wallet", "addwitnessaddress", &addwitnessaddress, true },
336338
{ "wallet", "backupwallet", &backupwallet, true },
337339
{ "wallet", "dumpprivkey", &dumpprivkey, true },
338340
{ "wallet", "dumpwallet", &dumpwallet, true },

src/rpcserver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ extern UniValue movecmd(const UniValue& params, bool fHelp);
213213
extern UniValue sendfrom(const UniValue& params, bool fHelp);
214214
extern UniValue sendmany(const UniValue& params, bool fHelp);
215215
extern UniValue addmultisigaddress(const UniValue& params, bool fHelp);
216+
extern UniValue addwitnessaddress(const UniValue& params, bool fHelp);
216217
extern UniValue createmultisig(const UniValue& params, bool fHelp);
218+
extern UniValue createwitnessaddress(const UniValue& params, bool fHelp);
217219
extern UniValue listreceivedbyaddress(const UniValue& params, bool fHelp);
218220
extern UniValue listreceivedbyaccount(const UniValue& params, bool fHelp);
219221
extern UniValue listtransactions(const UniValue& params, bool fHelp);

src/wallet/rpcwallet.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,35 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp)
10831083
return CBitcoinAddress(innerID).ToString();
10841084
}
10851085

1086+
UniValue addwitnessaddress(const UniValue& params, bool fHelp)
1087+
{
1088+
if (fHelp || params.size() < 1 || params.size() > 1)
1089+
{
1090+
string msg = "createwitnessaddress \"script\"\n"
1091+
"\nAdd a witness address for a particular script to the wallet.\n"
1092+
"It returns a json object with the address and witness script.\n"
1093+
1094+
"\nArguments:\n"
1095+
"1. \"script\" (string, required) A hex encoded script\n"
1096+
1097+
"\nResult:\n"
1098+
"\"address\":\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
1099+
"}\n"
1100+
;
1101+
throw runtime_error(msg);
1102+
}
1103+
1104+
std::vector<unsigned char> code = ParseHex(params[0].get_str());
1105+
CScript script(code.begin(), code.end());
1106+
CScript witscript = GetScriptForWitness(script);
1107+
CScriptID witscriptid(witscript);
1108+
CBitcoinAddress address(witscriptid);
1109+
1110+
pwalletMain->AddCScript(script);
1111+
pwalletMain->AddCScript(witscript);
1112+
UniValue result(UniValue::VOBJ);
1113+
return address.ToString();
1114+
}
10861115

10871116
struct tallyitem
10881117
{

0 commit comments

Comments
 (0)