Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <base58.h>
6 #include <keystore.h>
7 #include <pubkey.h>
8 #include <rpc/protocol.h>
9 #include <rpc/util.h>
10 #include <tinyformat.h>
11 #include <utilstrencodings.h>
12 
13 // Converts a hex string to a public key if possible
14 CPubKey HexToPubKey(const std::string& hex_in)
15 {
16  if (!IsHex(hex_in)) {
17  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
18  }
19  CPubKey vchPubKey(ParseHex(hex_in));
20  if (!vchPubKey.IsFullyValid()) {
21  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid public key: " + hex_in);
22  }
23  return vchPubKey;
24 }
25 
26 // Retrieves a public key for an address from the given CKeyStore
27 CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in)
28 {
29  CTxDestination dest = DecodeDestination(addr_in);
30  if (!IsValidDestination(dest)) {
31  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address: " + addr_in);
32  }
33  const CKeyID *keyID = boost::get<CKeyID>(&dest);
34  if (!keyID) {
35  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("%s does not refer to a key", addr_in));
36  }
37  CPubKey vchPubKey;
38  if (!keystore->GetPubKey(*keyID, vchPubKey)) {
39  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("no full public key for address %s", addr_in));
40  }
41  if (!vchPubKey.IsFullyValid()) {
42  throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet contains an invalid public key");
43  }
44  return vchPubKey;
45 }
46 
47 // Creates a multisig redeemscript from a given list of public keys and number required.
48 CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey>& pubkeys)
49 {
50  // Gather public keys
51  if (required < 1) {
52  throw JSONRPCError(RPC_INVALID_PARAMETER, "a multisignature address must require at least one key to redeem");
53  }
54  if ((int)pubkeys.size() < required) {
55  throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("not enough keys supplied (got %u keys, but need at least %d to redeem)", pubkeys.size(), required));
56  }
57  if (pubkeys.size() > 16) {
58  throw JSONRPCError(RPC_INVALID_PARAMETER, "Number of keys involved in the multisignature address creation > 16\nReduce the number");
59  }
60 
61  CScript result = GetScriptForMultisig(required, pubkeys);
62 
63  if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) {
64  throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE)));
65  }
66 
67  return result;
68 }
CScript CreateMultisigRedeemscript(const int required, const std::vector< CPubKey > &pubkeys)
Definition: util.cpp:48
CPubKey AddrToPubKey(CKeyStore *const keystore, const std::string &addr_in)
Definition: util.cpp:27
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:80
#define strprintf
Definition: tinyformat.h:1066
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:281
CTxDestination DecodeDestination(const std::string &str)
Definition: base58.cpp:336
CPubKey HexToPubKey(const std::string &hex_in)
Definition: util.cpp:14
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
Invalid, missing or duplicate parameter.
Definition: protocol.h:53
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
An encapsulated public key.
Definition: pubkey.h:30
bool IsHex(const std::string &str)
Invalid address or key.
Definition: protocol.h:51
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const =0
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
A virtual base class for key stores.
Definition: keystore.h:19
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
size_type size() const
Definition: prevector.h:310
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:269
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:54
std::vector< unsigned char > ParseHex(const char *psz)
Released under the MIT license