Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #11415: [RPC] Disallow using addresses in createmultisig
1df206f Disallow using addresses in createmultisig (Andrew Chow) Pull request description: This PR should be the last part of #7965. This PR makes createmultisig only accept public keys and marks the old functionality of accepting addresses as deprecated. It also splits `_createmultisig_redeemscript` into two functions, `_createmultisig_getpubkeys` and `_createmultisig_getaddr_pubkeys`. `_createmultisig_getpubkeys` retrieves public keys from the RPC parameters and `_createmultisig_getaddr_pubkeys` retrieves addresses' public keys from the wallet. `_createmultisig_getaddr_pubkeys` requires the wallet and is only used by `addwitnessaddress` (except when `createmultisig` is used in deprecated mode). `addwitnessaddress`'s API is also changed. Instead of returning just an address, it now returns the same thing as `createmultisig`: a JSON object with two fields, address and redeemscript. Tree-SHA512: a5796e41935ad5e47d8165ff996a8b20d5112b5fc1a06a6d3c7f5513c13e7628a4fd37ec30fde05d8b15abfed51bc250710140f6834b13f64d0a0e47a3817969
- Loading branch information
Showing
15 changed files
with
204 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2017 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <base58.h> | ||
#include <keystore.h> | ||
#include <pubkey.h> | ||
#include <rpc/protocol.h> | ||
#include <rpc/util.h> | ||
#include <tinyformat.h> | ||
#include <utilstrencodings.h> | ||
|
||
// Converts a hex string to a public key if possible | ||
CPubKey HexToPubKey(const std::string& hex_in) | ||
{ | ||
if (!IsHex(hex_in)) { | ||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in); | ||
} | ||
CPubKey vchPubKey(ParseHex(hex_in)); | ||
if (!vchPubKey.IsFullyValid()) { | ||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in); | ||
} | ||
return vchPubKey; | ||
} | ||
|
||
// Retrieves a public key for an address from the given CKeyStore | ||
CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in) | ||
{ | ||
CTxDestination dest = DecodeDestination(addr_in); | ||
if (!IsValidDestination(dest)) { | ||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in); | ||
} | ||
CKeyID key = GetKeyForDestination(*keystore, dest); | ||
if (key.IsNull()) { | ||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("%s does not refer to a key", addr_in)); | ||
} | ||
CPubKey vchPubKey; | ||
if (!keystore->GetPubKey(key, vchPubKey)) { | ||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in)); | ||
} | ||
if (!vchPubKey.IsFullyValid()) { | ||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key"); | ||
} | ||
return vchPubKey; | ||
} | ||
|
||
// Creates a multisig redeemscript from a given list of public keys and number required. | ||
CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey>& pubkeys) | ||
{ | ||
// Gather public keys | ||
if (required < 1) { | ||
throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem"); | ||
} | ||
if ((int)pubkeys.size() < required) { | ||
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required)); | ||
} | ||
if (pubkeys.size() > 16) { | ||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Number of keys involved in the multisignature address creation > 16\nReduce the number"); | ||
} | ||
|
||
CScript result = GetScriptForMultisig(required, pubkeys); | ||
|
||
if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) { | ||
throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE))); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2017 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_RPC_UTIL_H | ||
#define BITCOIN_RPC_UTIL_H | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
class CKeyStore; | ||
class CPubKey; | ||
class CScript; | ||
|
||
CPubKey HexToPubKey(const std::string& hex_in); | ||
CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in); | ||
CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey>& pubkeys); | ||
|
||
#endif // BITCOIN_RPC_UTIL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.