Skip to content

Commit 723a03d

Browse files
committed
Move createmultisig from rpcwallet to rpcmisc
Enables it in --disable-wallet compiles.
1 parent 452955f commit 723a03d

File tree

3 files changed

+104
-99
lines changed

3 files changed

+104
-99
lines changed

src/rpcmisc.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,105 @@ Value validateaddress(const Array& params, bool fHelp)
171171
return ret;
172172
}
173173

174+
//
175+
// Used by addmultisigaddress / createmultisig:
176+
//
177+
CScript _createmultisig(const Array& params)
178+
{
179+
int nRequired = params[0].get_int();
180+
const Array& keys = params[1].get_array();
181+
182+
// Gather public keys
183+
if (nRequired < 1)
184+
throw runtime_error("a multisignature address must require at least one key to redeem");
185+
if ((int)keys.size() < nRequired)
186+
throw runtime_error(
187+
strprintf("not enough keys supplied "
188+
"(got %"PRIszu" keys, but need at least %d to redeem)", keys.size(), nRequired));
189+
std::vector<CPubKey> pubkeys;
190+
pubkeys.resize(keys.size());
191+
for (unsigned int i = 0; i < keys.size(); i++)
192+
{
193+
const std::string& ks = keys[i].get_str();
194+
#ifdef ENABLE_WALLET
195+
// Case 1: Bitcoin address and we have full public key:
196+
CBitcoinAddress address(ks);
197+
if (pwalletMain && address.IsValid())
198+
{
199+
CKeyID keyID;
200+
if (!address.GetKeyID(keyID))
201+
throw runtime_error(
202+
strprintf("%s does not refer to a key",ks.c_str()));
203+
CPubKey vchPubKey;
204+
if (!pwalletMain->GetPubKey(keyID, vchPubKey))
205+
throw runtime_error(
206+
strprintf("no full public key for address %s",ks.c_str()));
207+
if (!vchPubKey.IsFullyValid())
208+
throw runtime_error(" Invalid public key: "+ks);
209+
pubkeys[i] = vchPubKey;
210+
}
211+
212+
// Case 2: hex public key
213+
else
214+
#endif
215+
if (IsHex(ks))
216+
{
217+
CPubKey vchPubKey(ParseHex(ks));
218+
if (!vchPubKey.IsFullyValid())
219+
throw runtime_error(" Invalid public key: "+ks);
220+
pubkeys[i] = vchPubKey;
221+
}
222+
else
223+
{
224+
throw runtime_error(" Invalid public key: "+ks);
225+
}
226+
}
227+
CScript result;
228+
result.SetMultisig(nRequired, pubkeys);
229+
return result;
230+
}
231+
232+
Value createmultisig(const Array& params, bool fHelp)
233+
{
234+
if (fHelp || params.size() < 2 || params.size() > 2)
235+
{
236+
string msg = "createmultisig nrequired [\"key\",...]\n"
237+
"\nCreates a multi-signature address with n signature of m keys required.\n"
238+
"It returns a json object with the address and redeemScript.\n"
239+
240+
"\nArguments:\n"
241+
"1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
242+
"2. \"keys\" (string, required) A json array of keys which are bitcoin addresses or hex-encoded public keys\n"
243+
" [\n"
244+
" \"key\" (string) bitcoin address or hex-encoded public key\n"
245+
" ,...\n"
246+
" ]\n"
247+
248+
"\nResult:\n"
249+
"{\n"
250+
" \"address\":\"multisigaddress\", (string) The value of the new multisig address.\n"
251+
" \"redeemScript\":\"script\" (string) The string value of the hex-encoded redemption script.\n"
252+
"}\n"
253+
254+
"\nExamples:\n"
255+
"\nCreate a multisig address from 2 addresses\n"
256+
+ HelpExampleCli("createmultisig", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
257+
"\nAs a json rpc call\n"
258+
+ HelpExampleRpc("icreatemultisig", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
259+
;
260+
throw runtime_error(msg);
261+
}
262+
263+
// Construct using pay-to-script-hash:
264+
CScript inner = _createmultisig(params);
265+
CScriptID innerID = inner.GetID();
266+
CBitcoinAddress address(innerID);
267+
268+
Object result;
269+
result.push_back(Pair("address", address.ToString()));
270+
result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end())));
271+
272+
return result;
273+
}
274+
174275

src/rpcserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ static const CRPCCommand vRPCCommands[] =
253253
{ "getblocktemplate", &getblocktemplate, true, false, false },
254254
{ "submitblock", &submitblock, false, false, false },
255255
{ "validateaddress", &validateaddress, true, false, false },
256+
{ "createmultisig", &createmultisig, true, true , false },
256257

257258
#ifdef ENABLE_WALLET
258259
/* Wallet */
@@ -278,7 +279,6 @@ static const CRPCCommand vRPCCommands[] =
278279
{ "sendfrom", &sendfrom, false, false, true },
279280
{ "sendmany", &sendmany, false, false, true },
280281
{ "addmultisigaddress", &addmultisigaddress, false, false, true },
281-
{ "createmultisig", &createmultisig, true, true , false },
282282
{ "gettransaction", &gettransaction, false, false, true },
283283
{ "listtransactions", &listtransactions, false, false, true },
284284
{ "listaddressgroupings", &listaddressgroupings, false, false, true },

src/rpcwallet.cpp

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -907,61 +907,8 @@ Value sendmany(const Array& params, bool fHelp)
907907
return wtx.GetHash().GetHex();
908908
}
909909

910-
//
911-
// Used by addmultisigaddress / createmultisig:
912-
//
913-
static CScript _createmultisig(const Array& params)
914-
{
915-
int nRequired = params[0].get_int();
916-
const Array& keys = params[1].get_array();
917-
918-
// Gather public keys
919-
if (nRequired < 1)
920-
throw runtime_error("a multisignature address must require at least one key to redeem");
921-
if ((int)keys.size() < nRequired)
922-
throw runtime_error(
923-
strprintf("not enough keys supplied "
924-
"(got %"PRIszu" keys, but need at least %d to redeem)", keys.size(), nRequired));
925-
std::vector<CPubKey> pubkeys;
926-
pubkeys.resize(keys.size());
927-
for (unsigned int i = 0; i < keys.size(); i++)
928-
{
929-
const std::string& ks = keys[i].get_str();
930-
931-
// Case 1: Bitcoin address and we have full public key:
932-
CBitcoinAddress address(ks);
933-
if (pwalletMain && address.IsValid())
934-
{
935-
CKeyID keyID;
936-
if (!address.GetKeyID(keyID))
937-
throw runtime_error(
938-
strprintf("%s does not refer to a key",ks.c_str()));
939-
CPubKey vchPubKey;
940-
if (!pwalletMain->GetPubKey(keyID, vchPubKey))
941-
throw runtime_error(
942-
strprintf("no full public key for address %s",ks.c_str()));
943-
if (!vchPubKey.IsFullyValid())
944-
throw runtime_error(" Invalid public key: "+ks);
945-
pubkeys[i] = vchPubKey;
946-
}
947-
948-
// Case 2: hex public key
949-
else if (IsHex(ks))
950-
{
951-
CPubKey vchPubKey(ParseHex(ks));
952-
if (!vchPubKey.IsFullyValid())
953-
throw runtime_error(" Invalid public key: "+ks);
954-
pubkeys[i] = vchPubKey;
955-
}
956-
else
957-
{
958-
throw runtime_error(" Invalid public key: "+ks);
959-
}
960-
}
961-
CScript result;
962-
result.SetMultisig(nRequired, pubkeys);
963-
return result;
964-
}
910+
// Defined in rpcmisc.cpp
911+
extern CScript _createmultisig(const Array& params);
965912

966913
Value addmultisigaddress(const Array& params, bool fHelp)
967914
{
@@ -1006,49 +953,6 @@ Value addmultisigaddress(const Array& params, bool fHelp)
1006953
return CBitcoinAddress(innerID).ToString();
1007954
}
1008955

1009-
Value createmultisig(const Array& params, bool fHelp)
1010-
{
1011-
if (fHelp || params.size() < 2 || params.size() > 2)
1012-
{
1013-
string msg = "createmultisig nrequired [\"key\",...]\n"
1014-
"\nCreates a multi-signature address with n signature of m keys required.\n"
1015-
"It returns a json object with the address and redeemScript.\n"
1016-
1017-
"\nArguments:\n"
1018-
"1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n"
1019-
"2. \"keys\" (string, required) A json array of keys which are bitcoin addresses or hex-encoded public keys\n"
1020-
" [\n"
1021-
" \"key\" (string) bitcoin address or hex-encoded public key\n"
1022-
" ,...\n"
1023-
" ]\n"
1024-
1025-
"\nResult:\n"
1026-
"{\n"
1027-
" \"address\":\"multisigaddress\", (string) The value of the new multisig address.\n"
1028-
" \"redeemScript\":\"script\" (string) The string value of the hex-encoded redemption script.\n"
1029-
"}\n"
1030-
1031-
"\nExamples:\n"
1032-
"\nCreate a multisig address from 2 addresses\n"
1033-
+ HelpExampleCli("createmultisig", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") +
1034-
"\nAs a json rpc call\n"
1035-
+ HelpExampleRpc("icreatemultisig", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"")
1036-
;
1037-
throw runtime_error(msg);
1038-
}
1039-
1040-
// Construct using pay-to-script-hash:
1041-
CScript inner = _createmultisig(params);
1042-
CScriptID innerID = inner.GetID();
1043-
CBitcoinAddress address(innerID);
1044-
1045-
Object result;
1046-
result.push_back(Pair("address", address.ToString()));
1047-
result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end())));
1048-
1049-
return result;
1050-
}
1051-
1052956

1053957
struct tallyitem
1054958
{

0 commit comments

Comments
 (0)