@@ -1011,6 +1011,85 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp)
1011
1011
return CBitcoinAddress (innerID).ToString ();
1012
1012
}
1013
1013
1014
+ class Witnessifier : public boost ::static_visitor<bool >
1015
+ {
1016
+ public:
1017
+ CScriptID result;
1018
+
1019
+ bool operator ()(const CNoDestination &dest) const { return false ; }
1020
+
1021
+ bool operator ()(const CKeyID &keyID) {
1022
+ CPubKey pubkey;
1023
+ if (pwalletMain && pwalletMain->GetPubKey (keyID, pubkey)) {
1024
+ CScript basescript;
1025
+ basescript << ToByteVector (pubkey) << OP_CHECKSIG;
1026
+ CScript witscript = GetScriptForWitness (basescript);
1027
+ pwalletMain->AddCScript (witscript);
1028
+ result = CScriptID (witscript);
1029
+ return true ;
1030
+ }
1031
+ return false ;
1032
+ }
1033
+
1034
+ bool operator ()(const CScriptID &scriptID) {
1035
+ CScript subscript;
1036
+ if (pwalletMain && pwalletMain->GetCScript (scriptID, subscript)) {
1037
+ int witnessversion;
1038
+ std::vector<unsigned char > witprog;
1039
+ if (subscript.IsWitnessProgram (witnessversion, witprog)) {
1040
+ result = scriptID;
1041
+ return true ;
1042
+ }
1043
+ CScript witscript = GetScriptForWitness (subscript);
1044
+ pwalletMain->AddCScript (witscript);
1045
+ result = CScriptID (witscript);
1046
+ return true ;
1047
+ }
1048
+ return false ;
1049
+ }
1050
+ };
1051
+
1052
+ UniValue addwitnessaddress (const UniValue& params, bool fHelp )
1053
+ {
1054
+ if (!EnsureWalletIsAvailable (fHelp ))
1055
+ return NullUniValue;
1056
+
1057
+ if (fHelp || params.size () < 1 || params.size () > 1 )
1058
+ {
1059
+ string msg = " addwitnessaddress \" address\"\n "
1060
+ " \n Add a witness address for a script (with pubkey or redeemscript known).\n "
1061
+ " It returns the witness script.\n "
1062
+
1063
+ " \n Arguments:\n "
1064
+ " 1. \" address\" (string, required) An address known to the wallet\n "
1065
+
1066
+ " \n Result:\n "
1067
+ " \" witnessaddress\" , (string) The value of the new address (P2SH of witness script).\n "
1068
+ " }\n "
1069
+ ;
1070
+ throw runtime_error (msg);
1071
+ }
1072
+
1073
+ {
1074
+ LOCK (cs_main);
1075
+ if (!IsWitnessEnabled (chainActive.Tip (), Params ().GetConsensus ())) {
1076
+ throw JSONRPCError (RPC_WALLET_ERROR, " Segregated witness not enabled on network" );
1077
+ }
1078
+ }
1079
+
1080
+ CBitcoinAddress address (params[0 ].get_str ());
1081
+ if (!address.IsValid ())
1082
+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address" );
1083
+
1084
+ Witnessifier w;
1085
+ CTxDestination dest = address.Get ();
1086
+ bool ret = boost::apply_visitor (w, dest);
1087
+ if (!ret) {
1088
+ throw JSONRPCError (RPC_WALLET_ERROR, " Public key or redeemscript not known to wallet" );
1089
+ }
1090
+
1091
+ return CBitcoinAddress (w.result ).ToString ();
1092
+ }
1014
1093
1015
1094
struct tallyitem
1016
1095
{
@@ -2491,6 +2570,7 @@ static const CRPCCommand commands[] =
2491
2570
{ " hidden" , " resendwallettransactions" , &resendwallettransactions, true },
2492
2571
{ " wallet" , " abandontransaction" , &abandontransaction, false },
2493
2572
{ " wallet" , " addmultisigaddress" , &addmultisigaddress, true },
2573
+ { " wallet" , " addwitnessaddress" , &addwitnessaddress, true },
2494
2574
{ " wallet" , " backupwallet" , &backupwallet, true },
2495
2575
{ " wallet" , " dumpprivkey" , &dumpprivkey, true },
2496
2576
{ " wallet" , " dumpwallet" , &dumpwallet, true },
0 commit comments