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