@@ -1095,6 +1095,75 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp)
10951095 return CBitcoinAddress (innerID).ToString ();
10961096}
10971097
1098+ class Witnessifier : public boost ::static_visitor<bool >
1099+ {
1100+ public:
1101+ CScriptID result;
1102+
1103+ bool operator ()(const CNoDestination &dest) const { return false ; }
1104+
1105+ bool operator ()(const CKeyID &keyID) {
1106+ CPubKey pubkey;
1107+ if (pwalletMain && pwalletMain->GetPubKey (keyID, pubkey)) {
1108+ CScript basescript;
1109+ basescript << ToByteVector (pubkey) << OP_CHECKSIG;
1110+ CScript witscript = GetScriptForWitness (basescript);
1111+ pwalletMain->AddCScript (witscript);
1112+ result = CScriptID (witscript);
1113+ return true ;
1114+ }
1115+ return false ;
1116+ }
1117+
1118+ bool operator ()(const CScriptID &scriptID) {
1119+ CScript subscript;
1120+ if (pwalletMain && pwalletMain->GetCScript (scriptID, subscript)) {
1121+ int witnessversion;
1122+ std::vector<unsigned char > witprog;
1123+ if (subscript.IsWitnessProgram (witnessversion, witprog)) {
1124+ result = scriptID;
1125+ return true ;
1126+ }
1127+ CScript witscript = GetScriptForWitness (subscript);
1128+ pwalletMain->AddCScript (witscript);
1129+ result = CScriptID (witscript);
1130+ return true ;
1131+ }
1132+ return false ;
1133+ }
1134+ };
1135+
1136+ UniValue addwitnessaddress (const UniValue& params, bool fHelp )
1137+ {
1138+ if (fHelp || params.size () < 1 || params.size () > 1 )
1139+ {
1140+ string msg = " addwitnessaddress \" address\"\n "
1141+ " \n Add a witness address for a script (with pubkey or redeemscript known).\n "
1142+ " It returns the witness script.\n "
1143+
1144+ " \n Arguments:\n "
1145+ " 1. \" address\" (string, required) An address known to the wallet\n "
1146+
1147+ " \n Result:\n "
1148+ " \" witnessaddress\" , (string) The value of the new address (P2SH of witness script).\n "
1149+ " }\n "
1150+ ;
1151+ throw runtime_error (msg);
1152+ }
1153+
1154+ CBitcoinAddress address (params[0 ].get_str ());
1155+ if (!address.IsValid ())
1156+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address" );
1157+
1158+ Witnessifier w;
1159+ CTxDestination dest = address.Get ();
1160+ bool ret = boost::apply_visitor (w, dest);
1161+ if (!ret) {
1162+ throw JSONRPCError (RPC_WALLET_ERROR, " Public key or redeemscript not known to wallet" );
1163+ }
1164+
1165+ return CBitcoinAddress (w.result ).ToString ();
1166+ }
10981167
10991168struct tallyitem
11001169{
@@ -2555,6 +2624,7 @@ static const CRPCCommand commands[] =
25552624 { " hidden" , " resendwallettransactions" , &resendwallettransactions, true },
25562625 { " wallet" , " abandontransaction" , &abandontransaction, false },
25572626 { " wallet" , " addmultisigaddress" , &addmultisigaddress, true },
2627+ { " wallet" , " addwitnessaddress" , &addwitnessaddress, true },
25582628 { " wallet" , " backupwallet" , &backupwallet, true },
25592629 { " wallet" , " dumpprivkey" , &dumpprivkey, true },
25602630 { " wallet" , " dumpwallet" , &dumpwallet, true },
0 commit comments