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