Bitcoin ABC  0.24.10
P2P Digital Currency
standard.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_SCRIPT_STANDARD_H
7 #define BITCOIN_SCRIPT_STANDARD_H
8 
9 #include <amount.h>
10 #include <pubkey.h>
11 #include <script/script_flags.h>
12 #include <uint256.h>
13 
14 #include <boost/variant.hpp>
15 
16 #include <string>
17 
18 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
19 
20 class CKeyID;
21 class CScript;
22 struct ScriptHash;
23 
24 template <typename HashType> class BaseHash {
25 protected:
26  HashType m_hash;
27 
28 public:
29  BaseHash() : m_hash() {}
30  BaseHash(const HashType &in) : m_hash(in) {}
31 
32  uint8_t *begin() { return m_hash.begin(); }
33 
34  const uint8_t *begin() const { return m_hash.begin(); }
35 
36  uint8_t *end() { return m_hash.end(); }
37 
38  const uint8_t *end() const { return m_hash.end(); }
39 
40  operator std::vector<uint8_t>() const {
41  return std::vector<uint8_t>{m_hash.begin(), m_hash.end()};
42  }
43 
44  std::string ToString() const { return m_hash.ToString(); }
45 
46  bool operator==(const BaseHash<HashType> &other) const noexcept {
47  return m_hash == other.m_hash;
48  }
49 
50  bool operator!=(const BaseHash<HashType> &other) const noexcept {
51  return !(m_hash == other.m_hash);
52  }
53 
54  bool operator<(const BaseHash<HashType> &other) const noexcept {
55  return m_hash < other.m_hash;
56  }
57 
58  size_t size() const { return m_hash.size(); }
59 
60  uint8_t *data() { return m_hash.data(); }
61  const uint8_t *data() const { return m_hash.data(); }
62 };
63 
65 class CScriptID : public BaseHash<uint160> {
66 public:
68  explicit CScriptID(const CScript &in);
69  explicit CScriptID(const uint160 &in) : BaseHash(in) {}
70  explicit CScriptID(const ScriptHash &in);
71 };
72 
77 static const unsigned int MAX_OP_RETURN_RELAY = 223;
78 
83 extern bool fAcceptDatacarrier;
84 
85 enum class TxoutType {
87  // 'standard' transaction types:
88  PUBKEY,
89  PUBKEYHASH,
90  SCRIPTHASH,
91  MULTISIG,
92  // unspendable OP_RETURN script that carries data
93  NULL_DATA,
94 };
95 
97 public:
98  friend bool operator==(const CNoDestination &a, const CNoDestination &b) {
99  return true;
100  }
101  friend bool operator<(const CNoDestination &a, const CNoDestination &b) {
102  return true;
103  }
104 };
105 
106 struct PKHash : public BaseHash<uint160> {
107  PKHash() : BaseHash() {}
108  explicit PKHash(const uint160 &hash) : BaseHash(hash) {}
109  explicit PKHash(const CPubKey &pubkey);
110  explicit PKHash(const CKeyID &pubkey_id);
111 };
112 CKeyID ToKeyID(const PKHash &key_hash);
113 
114 struct ScriptHash : public BaseHash<uint160> {
116  // This doesn't do what you'd expect.
117  // Use ScriptHash(GetScriptForDestination(...)) instead.
118  explicit ScriptHash(const PKHash &hash) = delete;
119 
120  explicit ScriptHash(const uint160 &hash) : BaseHash(hash) {}
121  explicit ScriptHash(const CScript &script);
122  explicit ScriptHash(const CScriptID &script);
123 };
124 
132 typedef boost::variant<CNoDestination, PKHash, ScriptHash> CTxDestination;
133 
135 bool IsValidDestination(const CTxDestination &dest);
136 
138 std::string GetTxnOutputType(TxoutType t);
139 
151 TxoutType Solver(const CScript &scriptPubKey,
152  std::vector<std::vector<uint8_t>> &vSolutionsRet);
153 
160 bool ExtractDestination(const CScript &scriptPubKey,
161  CTxDestination &addressRet);
162 
174 bool ExtractDestinations(const CScript &scriptPubKey, TxoutType &typeRet,
175  std::vector<CTxDestination> &addressRet,
176  int &nRequiredRet);
177 
184 
186 CScript GetScriptForRawPubKey(const CPubKey &pubkey);
187 
189 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey> &keys);
190 
191 #endif // BITCOIN_SCRIPT_STANDARD_H
BaseHash::operator==
bool operator==(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:46
ScriptHash::ScriptHash
ScriptHash()
Definition: standard.h:115
TxoutType
TxoutType
Definition: standard.h:85
TxoutType::NONSTANDARD
@ NONSTANDARD
uint256.h
BaseHash::data
uint8_t * data()
Definition: standard.h:60
script_flags.h
BaseHash::m_hash
HashType m_hash
Definition: standard.h:26
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
CNoDestination::operator==
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:98
pubkey.h
BaseHash
Definition: standard.h:24
GetTxnOutputType
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:32
ToKeyID
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:28
GetScriptForRawPubKey
CScript GetScriptForRawPubKey(const CPubKey &pubkey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:247
GetScriptForMultisig
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:252
TxoutType::PUBKEY
@ PUBKEY
Solver
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:111
CNoDestination::operator<
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: standard.h:101
ScriptHash::ScriptHash
ScriptHash(const uint160 &hash)
Definition: standard.h:120
TxoutType::SCRIPTHASH
@ SCRIPTHASH
BaseHash::ToString
std::string ToString() const
Definition: standard.h:44
BaseHash::operator!=
bool operator!=(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:50
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
PKHash::PKHash
PKHash(const uint160 &hash)
Definition: standard.h:108
BaseHash::BaseHash
BaseHash(const HashType &in)
Definition: standard.h:30
CNoDestination
Definition: standard.h:96
TxoutType::NULL_DATA
@ NULL_DATA
TxoutType::PUBKEYHASH
@ PUBKEYHASH
PKHash
Definition: standard.h:106
uint160
160-bit opaque blob.
Definition: uint256.h:115
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
MAX_OP_RETURN_RELAY
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:77
CScriptID::CScriptID
CScriptID(const uint160 &in)
Definition: standard.h:69
BaseHash::BaseHash
BaseHash()
Definition: standard.h:29
BaseHash::size
size_t size() const
Definition: standard.h:58
BaseHash::end
const uint8_t * end() const
Definition: standard.h:38
BaseHash::operator<
bool operator<(const BaseHash< HashType > &other) const noexcept
Definition: standard.h:54
PKHash::PKHash
PKHash()
Definition: standard.h:107
TxoutType::MULTISIG
@ MULTISIG
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:263
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:243
BaseHash::end
uint8_t * end()
Definition: standard.h:36
ExtractDestinations
bool ExtractDestinations(const CScript &scriptPubKey, TxoutType &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Parse a standard scriptPubKey with one or more destination addresses.
Definition: standard.cpp:187
BaseHash::begin
const uint8_t * begin() const
Definition: standard.h:34
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:65
CScriptID::CScriptID
CScriptID()
Definition: standard.h:67
amount.h
BaseHash::begin
uint8_t * begin()
Definition: standard.h:32
BaseHash::data
const uint8_t * data() const
Definition: standard.h:61
CTxDestination
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:132
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:161
ScriptHash
Definition: standard.h:114
fAcceptDatacarrier
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:15
DEFAULT_ACCEPT_DATACARRIER
static const bool DEFAULT_ACCEPT_DATACARRIER
Definition: standard.h:18