diff options
Diffstat (limited to 'src/rpc/misc.cpp')
-rw-r--r-- | src/rpc/misc.cpp | 171 |
1 files changed, 58 insertions, 113 deletions
diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 3bcad16316..3f89996e61 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -15,6 +15,7 @@ #include <netbase.h> #include <rpc/blockchain.h> #include <rpc/server.h> +#include <rpc/util.h> #include <timedata.h> #include <util.h> #include <utilstrencodings.h> @@ -85,11 +86,11 @@ public: UniValue operator()(const CKeyID &keyID) const { UniValue obj(UniValue::VOBJ); CPubKey vchPubKey; - obj.push_back(Pair("isscript", false)); - obj.push_back(Pair("iswitness", false)); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", false); if (pwallet && pwallet->GetPubKey(keyID, vchPubKey)) { - obj.push_back(Pair("pubkey", HexStr(vchPubKey))); - obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed())); + obj.pushKV("pubkey", HexStr(vchPubKey)); + obj.pushKV("iscompressed", vchPubKey.IsCompressed()); } return obj; } @@ -97,8 +98,8 @@ public: UniValue operator()(const CScriptID &scriptID) const { UniValue obj(UniValue::VOBJ); CScript subscript; - obj.push_back(Pair("isscript", true)); - obj.push_back(Pair("iswitness", false)); + obj.pushKV("isscript", true); + obj.pushKV("iswitness", false); if (pwallet && pwallet->GetCScript(scriptID, subscript)) { ProcessSubScript(subscript, obj, true); } @@ -109,12 +110,12 @@ public: { UniValue obj(UniValue::VOBJ); CPubKey pubkey; - obj.push_back(Pair("isscript", false)); - obj.push_back(Pair("iswitness", true)); - obj.push_back(Pair("witness_version", 0)); - obj.push_back(Pair("witness_program", HexStr(id.begin(), id.end()))); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", 0); + obj.pushKV("witness_program", HexStr(id.begin(), id.end())); if (pwallet && pwallet->GetPubKey(CKeyID(id), pubkey)) { - obj.push_back(Pair("pubkey", HexStr(pubkey))); + obj.pushKV("pubkey", HexStr(pubkey)); } return obj; } @@ -123,10 +124,10 @@ public: { UniValue obj(UniValue::VOBJ); CScript subscript; - obj.push_back(Pair("isscript", true)); - obj.push_back(Pair("iswitness", true)); - obj.push_back(Pair("witness_version", 0)); - obj.push_back(Pair("witness_program", HexStr(id.begin(), id.end()))); + obj.pushKV("isscript", true); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", 0); + obj.pushKV("witness_program", HexStr(id.begin(), id.end())); CRIPEMD160 hasher; uint160 hash; hasher.Write(id.begin(), 32).Finalize(hash.begin()); @@ -140,9 +141,9 @@ public: { UniValue obj(UniValue::VOBJ); CScript subscript; - obj.push_back(Pair("iswitness", true)); - obj.push_back(Pair("witness_version", (int)id.version)); - obj.push_back(Pair("witness_program", HexStr(id.program, id.program + id.length))); + obj.pushKV("iswitness", true); + obj.pushKV("witness_version", (int)id.version); + obj.pushKV("witness_program", HexStr(id.program, id.program + id.length)); return obj; } }; @@ -205,23 +206,23 @@ UniValue validateaddress(const JSONRPCRequest& request) bool isValid = IsValidDestination(dest); UniValue ret(UniValue::VOBJ); - ret.push_back(Pair("isvalid", isValid)); + ret.pushKV("isvalid", isValid); if (isValid) { std::string currentAddress = EncodeDestination(dest); - ret.push_back(Pair("address", currentAddress)); + ret.pushKV("address", currentAddress); CScript scriptPubKey = GetScriptForDestination(dest); - ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); + ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())); #ifdef ENABLE_WALLET isminetype mine = pwallet ? IsMine(*pwallet, dest) : ISMINE_NO; - ret.push_back(Pair("ismine", bool(mine & ISMINE_SPENDABLE))); - ret.push_back(Pair("iswatchonly", bool(mine & ISMINE_WATCH_ONLY))); + ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE)); + ret.pushKV("iswatchonly", bool(mine & ISMINE_WATCH_ONLY)); UniValue detail = boost::apply_visitor(DescribeAddressVisitor(pwallet), dest); ret.pushKVs(detail); if (pwallet && pwallet->mapAddressBook.count(dest)) { - ret.push_back(Pair("account", pwallet->mapAddressBook[dest].name)); + ret.pushKV("account", pwallet->mapAddressBook[dest].name); } if (pwallet) { const CKeyMetadata* meta = nullptr; @@ -239,10 +240,10 @@ UniValue validateaddress(const JSONRPCRequest& request) } } if (meta) { - ret.push_back(Pair("timestamp", meta->nCreateTime)); + ret.pushKV("timestamp", meta->nCreateTime); if (!meta->hdKeypath.empty()) { - ret.push_back(Pair("hdkeypath", meta->hdKeypath)); - ret.push_back(Pair("hdmasterkeyid", meta->hdMasterKeyID.GetHex())); + ret.pushKV("hdkeypath", meta->hdKeypath); + ret.pushKV("hdmasterkeyid", meta->hdMasterKeyID.GetHex()); } } } @@ -254,88 +255,18 @@ UniValue validateaddress(const JSONRPCRequest& request) // Needed even with !ENABLE_WALLET, to pass (ignored) pointers around class CWallet; -/** - * Used by addmultisigaddress / createmultisig: - */ -CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& params) -{ - int nRequired = params[0].get_int(); - const UniValue& keys = params[1].get_array(); - - // Gather public keys - if (nRequired < 1) - throw std::runtime_error("a multisignature address must require at least one key to redeem"); - if ((int)keys.size() < nRequired) - throw std::runtime_error( - strprintf("not enough keys supplied " - "(got %u keys, but need at least %d to redeem)", keys.size(), nRequired)); - if (keys.size() > 16) - throw std::runtime_error("Number of addresses involved in the multisignature address creation > 16\nReduce the number"); - std::vector<CPubKey> pubkeys; - pubkeys.resize(keys.size()); - for (unsigned int i = 0; i < keys.size(); i++) - { - const std::string& ks = keys[i].get_str(); -#ifdef ENABLE_WALLET - // Case 1: Bitcoin address and we have full public key: - CTxDestination dest = DecodeDestination(ks); - if (pwallet && IsValidDestination(dest)) { - CKeyID key = GetKeyForDestination(*pwallet, dest); - if (key.IsNull()) { - throw std::runtime_error(strprintf("%s does not refer to a key", ks)); - } - CPubKey vchPubKey; - if (!pwallet->GetPubKey(key, vchPubKey)) { - throw std::runtime_error(strprintf("no full public key for address %s", ks)); - } - if (!vchPubKey.IsFullyValid()) - throw std::runtime_error(" Invalid public key: "+ks); - pubkeys[i] = vchPubKey; - } - - // Case 2: hex public key - else -#endif - if (IsHex(ks)) - { - CPubKey vchPubKey(ParseHex(ks)); - if (!vchPubKey.IsFullyValid()) - throw std::runtime_error(" Invalid public key: "+ks); - pubkeys[i] = vchPubKey; - } - else - { - throw std::runtime_error(" Invalid public key: "+ks); - } - } - CScript result = GetScriptForMultisig(nRequired, pubkeys); - - if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) - throw std::runtime_error( - strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE)); - - return result; -} - UniValue createmultisig(const JSONRPCRequest& request) { -#ifdef ENABLE_WALLET - CWallet * const pwallet = GetWalletForJSONRPCRequest(request); -#else - CWallet * const pwallet = nullptr; -#endif - if (request.fHelp || request.params.size() < 2 || request.params.size() > 2) { std::string msg = "createmultisig nrequired [\"key\",...]\n" "\nCreates a multi-signature address with n signature of m keys required.\n" "It returns a json object with the address and redeemScript.\n" - "\nArguments:\n" - "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n" - "2. \"keys\" (string, required) A json array of keys which are bitcoin addresses or hex-encoded public keys\n" + "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n" + "2. \"keys\" (string, required) A json array of hex-encoded public keys\n" " [\n" - " \"key\" (string) bitcoin address or hex-encoded public key\n" + " \"key\" (string) The hex-encoded public key\n" " ,...\n" " ]\n" @@ -346,21 +277,35 @@ UniValue createmultisig(const JSONRPCRequest& request) "}\n" "\nExamples:\n" - "\nCreate a multisig address from 2 addresses\n" - + HelpExampleCli("createmultisig", "2 \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") + + "\nCreate a multisig address from 2 public keys\n" + + HelpExampleCli("createmultisig", "2 \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"") + "\nAs a json rpc call\n" - + HelpExampleRpc("createmultisig", "2, \"[\\\"16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") + + HelpExampleRpc("createmultisig", "2, \"[\\\"03789ed0bb717d88f7d321a368d905e7430207ebbd82bd342cf11ae157a7ace5fd\\\",\\\"03dbc6764b8884a92e871274b87583e6d5c2a58819473e17e107ef3f6aa5a61626\\\"]\"") ; throw std::runtime_error(msg); } + int required = request.params[0].get_int(); + + // Get the public keys + const UniValue& keys = request.params[1].get_array(); + std::vector<CPubKey> pubkeys; + for (unsigned int i = 0; i < keys.size(); ++i) { + if (IsHex(keys[i].get_str()) && (keys[i].get_str().length() == 66 || keys[i].get_str().length() == 130)) { + pubkeys.push_back(HexToPubKey(keys[i].get_str())); + } else { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Invalid public key: %s\nNote that from v0.16, createmultisig no longer accepts addresses." + " Users must use addmultisigaddress to create multisig addresses with addresses known to the wallet.", keys[i].get_str())); + } + } + // Construct using pay-to-script-hash: - CScript inner = _createmultisig_redeemScript(pwallet, request.params); + CScript inner = CreateMultisigRedeemscript(required, pubkeys); CScriptID innerID(inner); UniValue result(UniValue::VOBJ); - result.push_back(Pair("address", EncodeDestination(innerID))); - result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end()))); + result.pushKV("address", EncodeDestination(innerID)); + result.pushKV("redeemScript", HexStr(inner.begin(), inner.end())); return result; } @@ -494,12 +439,12 @@ static UniValue RPCLockedMemoryInfo() { LockedPool::Stats stats = LockedPoolManager::Instance().stats(); UniValue obj(UniValue::VOBJ); - obj.push_back(Pair("used", uint64_t(stats.used))); - obj.push_back(Pair("free", uint64_t(stats.free))); - obj.push_back(Pair("total", uint64_t(stats.total))); - obj.push_back(Pair("locked", uint64_t(stats.locked))); - obj.push_back(Pair("chunks_used", uint64_t(stats.chunks_used))); - obj.push_back(Pair("chunks_free", uint64_t(stats.chunks_free))); + obj.pushKV("used", uint64_t(stats.used)); + obj.pushKV("free", uint64_t(stats.free)); + obj.pushKV("total", uint64_t(stats.total)); + obj.pushKV("locked", uint64_t(stats.locked)); + obj.pushKV("chunks_used", uint64_t(stats.chunks_used)); + obj.pushKV("chunks_free", uint64_t(stats.chunks_free)); return obj; } @@ -556,7 +501,7 @@ UniValue getmemoryinfo(const JSONRPCRequest& request) std::string mode = request.params[0].isNull() ? "stats" : request.params[0].get_str(); if (mode == "stats") { UniValue obj(UniValue::VOBJ); - obj.push_back(Pair("locked", RPCLockedMemoryInfo())); + obj.pushKV("locked", RPCLockedMemoryInfo()); return obj; } else if (mode == "mallocinfo") { #ifdef HAVE_MALLOC_INFO |