Skip to content

Commit

Permalink
Support BIP173 in addwitnessaddress
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Sep 28, 2017
1 parent c091b99 commit e278f12
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "logging", 0, "include" },
{ "logging", 1, "exclude" },
{ "disconnectnode", 1, "nodeid" },
{ "addwitnessaddress", 1, "p2sh" },
// Echo with conversion (For testing only)
{ "echojson", 0, "arg0" },
{ "echojson", 1, "arg1" },
Expand Down
59 changes: 45 additions & 14 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,9 +1154,10 @@ class Witnessifier : public boost::static_visitor<bool>
{
public:
CWallet * const pwallet;
CScriptID result;
CTxDestination result;
bool already_witness;

explicit Witnessifier(CWallet *_pwallet) : pwallet(_pwallet) {}
explicit Witnessifier(CWallet *_pwallet) : pwallet(_pwallet), already_witness(false) {}

bool operator()(const CKeyID &keyID) {
if (pwallet) {
Expand All @@ -1170,9 +1171,7 @@ class Witnessifier : public boost::static_visitor<bool>
!VerifyScript(sigs.scriptSig, witscript, &sigs.scriptWitness, MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, DummySignatureCreator(pwallet).Checker())) {
return false;
}
pwallet->AddCScript(witscript);
result = CScriptID(witscript);
return true;
return ExtractDestination(witscript, result);
}
return false;
}
Expand All @@ -1183,7 +1182,8 @@ class Witnessifier : public boost::static_visitor<bool>
int witnessversion;
std::vector<unsigned char> witprog;
if (subscript.IsWitnessProgram(witnessversion, witprog)) {
result = scriptID;
ExtractDestination(subscript, result);
already_witness = true;
return true;
}
CScript witscript = GetScriptForWitness(subscript);
Expand All @@ -1195,13 +1195,25 @@ class Witnessifier : public boost::static_visitor<bool>
!VerifyScript(sigs.scriptSig, witscript, &sigs.scriptWitness, MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, DummySignatureCreator(pwallet).Checker())) {
return false;
}
pwallet->AddCScript(witscript);
result = CScriptID(witscript);
return true;
return ExtractDestination(witscript, result);
}
return false;
}

bool operator()(const WitnessV0KeyHash& id)
{
already_witness = true;
result = id;
return true;
}

bool operator()(const WitnessV0ScriptHash& id)
{
already_witness = true;
result = id;
return true;
}

template<typename T>
bool operator()(const T& dest) { return false; }
};
Expand All @@ -1213,17 +1225,18 @@ UniValue addwitnessaddress(const JSONRPCRequest& request)
return NullUniValue;
}

if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
{
std::string msg = "addwitnessaddress \"address\"\n"
std::string msg = "addwitnessaddress \"address\" ( p2sh )\n"
"\nAdd a witness address for a script (with pubkey or redeemscript known).\n"
"It returns the witness script.\n"

"\nArguments:\n"
"1. \"address\" (string, required) An address known to the wallet\n"
"2. p2sh (bool, optional, default=true) Embed inside P2SH\n"

"\nResult:\n"
"\"witnessaddress\", (string) The value of the new address (P2SH of witness script).\n"
"\"witnessaddress\", (string) The value of the new address (P2SH or BIP173).\n"
"}\n"
;
throw std::runtime_error(msg);
Expand All @@ -1241,13 +1254,31 @@ UniValue addwitnessaddress(const JSONRPCRequest& request)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
}

bool p2sh = true;
if (!request.params[1].isNull()) {
p2sh = request.params[1].get_bool();
}

Witnessifier w(pwallet);
bool ret = boost::apply_visitor(w, dest);
if (!ret) {
throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet, or the key is uncompressed");
}

pwallet->SetAddressBook(w.result, "", "receive");
CScript witprogram = GetScriptForDestination(w.result);

if (p2sh) {
w.result = CScriptID(witprogram);
}

if (w.already_witness) {
if (!(dest == w.result)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot convert between witness address types");
}
} else {
pwallet->AddCScript(witprogram);
pwallet->SetAddressBook(w.result, "", "receive");
}

return EncodeDestination(w.result);
}
Expand Down Expand Up @@ -3200,7 +3231,7 @@ static const CRPCCommand commands[] =
{ "wallet", "abandontransaction", &abandontransaction, {"txid"} },
{ "wallet", "abortrescan", &abortrescan, {} },
{ "wallet", "addmultisigaddress", &addmultisigaddress, {"nrequired","keys","account"} },
{ "wallet", "addwitnessaddress", &addwitnessaddress, {"address"} },
{ "wallet", "addwitnessaddress", &addwitnessaddress, {"address","p2sh"} },
{ "wallet", "backupwallet", &backupwallet, {"destination"} },
{ "wallet", "bumpfee", &bumpfee, {"txid", "options"} },
{ "wallet", "dumpprivkey", &dumpprivkey, {"address"} },
Expand Down

0 comments on commit e278f12

Please sign in to comment.