Bitcoin ABC 0.30.5
P2P Digital Currency
standard.cpp
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#include <script/standard.h>
7
8#include <script/script.h>
9
10#include <string>
11
12typedef std::vector<uint8_t> valtype;
13
16 : BaseHash(static_cast<uint160>(in)) {}
17
20 : BaseHash(static_cast<uint160>(in)) {}
21
22PKHash::PKHash(const CPubKey &pubkey) : BaseHash(pubkey.GetID()) {}
23PKHash::PKHash(const CKeyID &pubkey_id) : BaseHash(pubkey_id) {}
24
25CKeyID ToKeyID(const PKHash &key_hash) {
26 return CKeyID{static_cast<uint160>(key_hash)};
27}
28
30 switch (t) {
32 return "nonstandard";
34 return "pubkey";
36 return "pubkeyhash";
38 return "scripthash";
40 return "multisig";
42 return "nulldata";
43 } // no default case, so the compiler can warn about missing cases
44 assert(false);
45}
46
47static bool MatchPayToPubkey(const CScript &script, valtype &pubkey) {
48 if (script.size() == CPubKey::SIZE + 2 && script[0] == CPubKey::SIZE &&
49 script.back() == OP_CHECKSIG) {
50 pubkey =
51 valtype(script.begin() + 1, script.begin() + CPubKey::SIZE + 1);
52 return CPubKey::ValidSize(pubkey);
53 }
54 if (script.size() == CPubKey::COMPRESSED_SIZE + 2 &&
55 script[0] == CPubKey::COMPRESSED_SIZE && script.back() == OP_CHECKSIG) {
56 pubkey = valtype(script.begin() + 1,
57 script.begin() + CPubKey::COMPRESSED_SIZE + 1);
58 return CPubKey::ValidSize(pubkey);
59 }
60 return false;
61}
62
63static bool MatchPayToPubkeyHash(const CScript &script, valtype &pubkeyhash) {
64 if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 &&
65 script[2] == 20 && script[23] == OP_EQUALVERIFY &&
66 script[24] == OP_CHECKSIG) {
67 pubkeyhash = valtype(script.begin() + 3, script.begin() + 23);
68 return true;
69 }
70 return false;
71}
72
74static constexpr bool IsSmallInteger(opcodetype opcode) {
75 return opcode >= OP_1 && opcode <= OP_16;
76}
77
78static bool MatchMultisig(const CScript &script, unsigned int &required,
79 std::vector<valtype> &pubkeys) {
80 opcodetype opcode;
81 valtype data;
82 CScript::const_iterator it = script.begin();
83 if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) {
84 return false;
85 }
86
87 if (!script.GetOp(it, opcode, data) || !IsSmallInteger(opcode)) {
88 return false;
89 }
90 required = CScript::DecodeOP_N(opcode);
91 while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
92 if (opcode < 0 || opcode > OP_PUSHDATA4 ||
93 !CheckMinimalPush(data, opcode)) {
94 return false;
95 }
96 pubkeys.emplace_back(std::move(data));
97 }
98 if (!IsSmallInteger(opcode)) {
99 return false;
100 }
101 unsigned int keys = CScript::DecodeOP_N(opcode);
102 if (pubkeys.size() != keys || keys < required) {
103 return false;
104 }
105 return (it + 1 == script.end());
106}
107
108TxoutType Solver(const CScript &scriptPubKey,
109 std::vector<std::vector<uint8_t>> &vSolutionsRet) {
110 vSolutionsRet.clear();
111
112 // Shortcut for pay-to-script-hash, which are more constrained than the
113 // other types:
114 // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
115 if (scriptPubKey.IsPayToScriptHash()) {
116 std::vector<uint8_t> hashBytes(scriptPubKey.begin() + 2,
117 scriptPubKey.begin() + 22);
118 vSolutionsRet.push_back(hashBytes);
120 }
121
122 // Provably prunable, data-carrying output
123 //
124 // So long as script passes the IsUnspendable() test and all but the first
125 // byte passes the IsPushOnly() test we don't care what exactly is in the
126 // script.
127 if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN &&
128 scriptPubKey.IsPushOnly(scriptPubKey.begin() + 1)) {
130 }
131
132 std::vector<uint8_t> data;
133 if (MatchPayToPubkey(scriptPubKey, data)) {
134 vSolutionsRet.push_back(std::move(data));
135 return TxoutType::PUBKEY;
136 }
137
138 if (MatchPayToPubkeyHash(scriptPubKey, data)) {
139 vSolutionsRet.push_back(std::move(data));
141 }
142
143 unsigned int required;
144 std::vector<std::vector<uint8_t>> keys;
145 if (MatchMultisig(scriptPubKey, required, keys)) {
146 // safe as required is in range 1..16
147 vSolutionsRet.push_back({static_cast<uint8_t>(required)});
148 vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
149 // safe as size is in range 1..16
150 vSolutionsRet.push_back({static_cast<uint8_t>(keys.size())});
151 return TxoutType::MULTISIG;
152 }
153
154 vSolutionsRet.clear();
156}
157
158bool ExtractDestination(const CScript &scriptPubKey,
159 CTxDestination &addressRet) {
160 std::vector<valtype> vSolutions;
161 TxoutType whichType = Solver(scriptPubKey, vSolutions);
162
163 if (whichType == TxoutType::PUBKEY) {
164 CPubKey pubKey(vSolutions[0]);
165 if (!pubKey.IsValid()) {
166 return false;
167 }
168
169 addressRet = PKHash(pubKey);
170 return true;
171 }
172 if (whichType == TxoutType::PUBKEYHASH) {
173 addressRet = PKHash(uint160(vSolutions[0]));
174 return true;
175 }
176 if (whichType == TxoutType::SCRIPTHASH) {
177 addressRet = ScriptHash(uint160(vSolutions[0]));
178 return true;
179 }
180 // Multisig txns have more than one address...
181 return false;
182}
183
184bool ExtractDestinations(const CScript &scriptPubKey, TxoutType &typeRet,
185 std::vector<CTxDestination> &addressRet,
186 int &nRequiredRet) {
187 addressRet.clear();
188 std::vector<valtype> vSolutions;
189 typeRet = Solver(scriptPubKey, vSolutions);
190 if (typeRet == TxoutType::NONSTANDARD) {
191 return false;
192 } else if (typeRet == TxoutType::NULL_DATA) {
193 // This is data, not addresses
194 return false;
195 }
196
197 if (typeRet == TxoutType::MULTISIG) {
198 nRequiredRet = vSolutions.front()[0];
199 for (size_t i = 1; i < vSolutions.size() - 1; i++) {
200 CPubKey pubKey(vSolutions[i]);
201 if (!pubKey.IsValid()) {
202 continue;
203 }
204
205 CTxDestination address = PKHash(pubKey);
206 addressRet.push_back(address);
207 }
208
209 if (addressRet.empty()) {
210 return false;
211 }
212 } else {
213 nRequiredRet = 1;
214 CTxDestination address;
215 if (!ExtractDestination(scriptPubKey, address)) {
216 return false;
217 }
218 addressRet.push_back(address);
219 }
220
221 return true;
222}
223
224namespace {
225class CScriptVisitor {
226public:
227 CScript operator()(const CNoDestination &dest) const { return CScript(); }
228
229 CScript operator()(const PKHash &keyID) const {
230 return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID)
232 }
233
234 CScript operator()(const ScriptHash &scriptID) const {
235 return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
236 }
237};
238} // namespace
239
241 return std::visit(CScriptVisitor(), dest);
242}
243
245 return CScript() << std::vector<uint8_t>(pubKey.begin(), pubKey.end())
246 << OP_CHECKSIG;
247}
248
249CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey> &keys) {
250 CScript script;
251
252 script << CScript::EncodeOP_N(nRequired);
253 for (const CPubKey &key : keys) {
254 script << ToByteVector(key);
255 }
256 script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
257 return script;
258}
259
261 return dest.index() != 0;
262}
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
An encapsulated public key.
Definition: pubkey.h:31
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:37
const uint8_t * end() const
Definition: pubkey.h:101
bool IsValid() const
Definition: pubkey.h:147
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:36
static bool ValidSize(const std::vector< uint8_t > &vch)
Definition: pubkey.h:70
const uint8_t * begin() const
Definition: pubkey.h:100
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:404
bool IsPayToScriptHash() const
Definition: script.cpp:373
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:512
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
Definition: script.h:502
static opcodetype EncodeOP_N(int n)
Definition: script.h:520
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:24
CScriptID()
Definition: standard.h:26
size_type size() const
Definition: prevector.h:394
T & back()
Definition: prevector.h:547
iterator begin()
Definition: prevector.h:398
iterator end()
Definition: prevector.h:400
160-bit opaque blob.
Definition: uint256.h:117
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
bool CheckMinimalPush(const std::vector< uint8_t > &data, opcodetype opcode)
Check whether the given stack element data would be minimally pushed using the given opcode.
Definition: script.cpp:268
opcodetype
Script opcodes.
Definition: script.h:47
@ OP_PUSHDATA4
Definition: script.h:53
@ OP_CHECKMULTISIG
Definition: script.h:165
@ OP_CHECKSIG
Definition: script.h:163
@ OP_16
Definition: script.h:72
@ OP_EQUAL
Definition: script.h:119
@ OP_DUP
Definition: script.h:98
@ OP_HASH160
Definition: script.h:160
@ OP_1
Definition: script.h:56
@ OP_RETURN
Definition: script.h:84
@ OP_EQUALVERIFY
Definition: script.h:120
std::vector< uint8_t > ToByteVector(const T &in)
Definition: script.h:42
std::vector< uint8_t > valtype
Definition: sigencoding.h:16
std::vector< uint8_t > valtype
Definition: standard.cpp:12
static bool MatchPayToPubkeyHash(const CScript &script, valtype &pubkeyhash)
Definition: standard.cpp:63
static constexpr bool IsSmallInteger(opcodetype opcode)
Test for "small positive integer" script opcodes - OP_1 through OP_16.
Definition: standard.cpp:74
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:249
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:158
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:108
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
Definition: standard.cpp:29
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:244
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:184
static bool MatchPayToPubkey(const CScript &script, valtype &pubkey)
Definition: standard.cpp:47
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:260
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:240
static bool MatchMultisig(const CScript &script, unsigned int &required, std::vector< valtype > &pubkeys)
Definition: standard.cpp:78
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:25
TxoutType
Definition: standard.h:38
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
PKHash()
Definition: standard.h:60
ScriptHash()
Definition: standard.h:68
assert(!tx.IsCoinBase())