BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
standard.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <pubkey.h>
10 #include <script/script.h>
11 #include <util.h>
12 #include <utilstrencodings.h>
13 
14 
15 typedef std::vector<unsigned char> valtype;
16 
17 bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
18 unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
19 
20 CScriptID::CScriptID(const CScript& in) : uint160(Hash360(in.begin(), in.end())) {}
21 
23 {
24  CSHA3().Write(in.data(), in.size()).Finalize(begin());
25 }
26 
28 {
29  switch (t)
30  {
31  case TX_NONSTANDARD: return "nonstandard";
32  case TX_PUBKEY: return "pubkey";
33  case TX_PUBKEYHASH: return "pubkeyhash";
34  case TX_SCRIPTHASH: return "scripthash";
35  case TX_MULTISIG: return "multisig";
36  case TX_NULL_DATA: return "nulldata";
37  case TX_WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
38  case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
39  case TX_WITNESS_UNKNOWN: return "witness_unknown";
40  }
41  return nullptr;
42 }
43 
44 static bool MatchPayToPubkey(const CScript& script, valtype& pubkey)
45 {
46  if (script.size() == CPubKey::PUBLIC_KEY_SIZE + 2 && script[0] == CPubKey::PUBLIC_KEY_SIZE && script.back() == OP_CHECKSIG) {
47  pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::PUBLIC_KEY_SIZE + 1);
48  return CPubKey::ValidSize(pubkey);
49  }
50  if (script.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 2 && script[0] == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE && script.back() == OP_CHECKSIG) {
51  pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1);
52  return CPubKey::ValidSize(pubkey);
53  }
54  return false;
55 }
56 
57 static bool MatchPayToPubkeyHash(const CScript& script, valtype& pubkeyhash)
58 {
59  if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH360 && script[2] == 20 && script[23] == OP_EQUALVERIFY && script[24] == OP_CHECKSIG) {
60  pubkeyhash = valtype(script.begin () + 3, script.begin() + 23);
61  return true;
62  }
63  return false;
64 }
65 
67 static constexpr bool IsSmallInteger(opcodetype opcode)
68 {
69  return opcode >= OP_1 && opcode <= OP_16;
70 }
71 
72 static bool MatchMultisig(const CScript& script, unsigned int& required, std::vector<valtype>& pubkeys)
73 {
74  opcodetype opcode;
75  valtype data;
76  CScript::const_iterator it = script.begin();
77  if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
78 
79  if (!script.GetOp(it, opcode, data) || !IsSmallInteger(opcode)) return false;
80  required = CScript::DecodeOP_N(opcode);
81  while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
82  pubkeys.emplace_back(std::move(data));
83  }
84  if (!IsSmallInteger(opcode)) return false;
85  unsigned int keys = CScript::DecodeOP_N(opcode);
86  if (pubkeys.size() != keys || keys < required) return false;
87  return (it + 1 == script.end());
88 }
89 
90 txnouttype Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
91 {
92  vSolutionsRet.clear();
93 
94  // Shortcut for pay-to-script-hash, which are more constrained than the other types:
95  // it is always OP_HASH360 20 [20 byte hash] OP_EQUAL
96  if (scriptPubKey.IsPayToScriptHash())
97  {
98  std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
99  vSolutionsRet.push_back(hashBytes);
100  return TX_SCRIPTHASH;
101  }
102 
103  int witnessversion;
104  std::vector<unsigned char> witnessprogram;
105  if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
106  if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
107  vSolutionsRet.push_back(witnessprogram);
108  return TX_WITNESS_V0_KEYHASH;
109  }
110  if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
111  vSolutionsRet.push_back(witnessprogram);
113  }
114  if (witnessversion != 0) {
115  vSolutionsRet.push_back(std::vector<unsigned char>{(unsigned char)witnessversion});
116  vSolutionsRet.push_back(std::move(witnessprogram));
117  return TX_WITNESS_UNKNOWN;
118  }
119  return TX_NONSTANDARD;
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 && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
128  return TX_NULL_DATA;
129  }
130 
131  std::vector<unsigned char> data;
132  if (MatchPayToPubkey(scriptPubKey, data)) {
133  vSolutionsRet.push_back(std::move(data));
134  return TX_PUBKEY;
135  }
136 
137  if (MatchPayToPubkeyHash(scriptPubKey, data)) {
138  vSolutionsRet.push_back(std::move(data));
139  return TX_PUBKEYHASH;
140  }
141 
142  unsigned int required;
143  std::vector<std::vector<unsigned char>> keys;
144  if (MatchMultisig(scriptPubKey, required, keys)) {
145  vSolutionsRet.push_back({static_cast<unsigned char>(required)}); // safe as required is in range 1..16
146  vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
147  vSolutionsRet.push_back({static_cast<unsigned char>(keys.size())}); // safe as size is in range 1..16
148  return TX_MULTISIG;
149  }
150 
151  vSolutionsRet.clear();
152  return TX_NONSTANDARD;
153 }
154 
155 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
156 {
157  std::vector<valtype> vSolutions;
158  txnouttype whichType = Solver(scriptPubKey, vSolutions);
159 
160  if (whichType == TX_PUBKEY) {
161  CPubKey pubKey(vSolutions[0]);
162  if (!pubKey.IsValid())
163  return false;
164 
165  addressRet = pubKey.GetID();
166  return true;
167  }
168  else if (whichType == TX_PUBKEYHASH)
169  {
170  addressRet = CKeyID(uint160(vSolutions[0]));
171  return true;
172  }
173  else if (whichType == TX_SCRIPTHASH)
174  {
175  addressRet = CScriptID(uint160(vSolutions[0]));
176  return true;
177  } else if (whichType == TX_WITNESS_V0_KEYHASH) {
178  WitnessV0KeyHash hash;
179  std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
180  addressRet = hash;
181  return true;
182  } else if (whichType == TX_WITNESS_V0_SCRIPTHASH) {
183  WitnessV0ScriptHash hash;
184  std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
185  addressRet = hash;
186  return true;
187  } else if (whichType == TX_WITNESS_UNKNOWN) {
188  WitnessUnknown unk;
189  unk.version = vSolutions[0][0];
190  std::copy(vSolutions[1].begin(), vSolutions[1].end(), unk.program);
191  unk.length = vSolutions[1].size();
192  addressRet = unk;
193  return true;
194  }
195  // Multisig txns have more than one address...
196  return false;
197 }
198 
199 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
200 {
201  addressRet.clear();
202  std::vector<valtype> vSolutions;
203  typeRet = Solver(scriptPubKey, vSolutions);
204  if (typeRet == TX_NONSTANDARD) {
205  return false;
206  } else if (typeRet == TX_NULL_DATA) {
207  // This is data, not addresses
208  return false;
209  }
210 
211  if (typeRet == TX_MULTISIG)
212  {
213  nRequiredRet = vSolutions.front()[0];
214  for (unsigned int i = 1; i < vSolutions.size()-1; i++)
215  {
216  CPubKey pubKey(vSolutions[i]);
217  if (!pubKey.IsValid())
218  continue;
219 
220  CTxDestination address = pubKey.GetID();
221  addressRet.push_back(address);
222  }
223 
224  if (addressRet.empty())
225  return false;
226  }
227  else
228  {
229  nRequiredRet = 1;
230  CTxDestination address;
231  if (!ExtractDestination(scriptPubKey, address))
232  return false;
233  addressRet.push_back(address);
234  }
235 
236  return true;
237 }
238 
239 namespace
240 {
241 class CScriptVisitor : public boost::static_visitor<bool>
242 {
243 private:
244  CScript *script;
245 public:
246  explicit CScriptVisitor(CScript *scriptin) { script = scriptin; }
247 
248  bool operator()(const CNoDestination &dest) const {
249  script->clear();
250  return false;
251  }
252 
253  bool operator()(const CKeyID &keyID) const {
254  script->clear();
255  *script << OP_DUP << OP_HASH360 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
256  return true;
257  }
258 
259  bool operator()(const CScriptID &scriptID) const {
260  script->clear();
261  *script << OP_HASH360 << ToByteVector(scriptID) << OP_EQUAL;
262  return true;
263  }
264 
265  bool operator()(const WitnessV0KeyHash& id) const
266  {
267  script->clear();
268  *script << OP_0 << ToByteVector(id);
269  return true;
270  }
271 
272  bool operator()(const WitnessV0ScriptHash& id) const
273  {
274  script->clear();
275  *script << OP_0 << ToByteVector(id);
276  return true;
277  }
278 
279  bool operator()(const WitnessUnknown& id) const
280  {
281  script->clear();
282  *script << CScript::EncodeOP_N(id.version) << std::vector<unsigned char>(id.program, id.program + id.length);
283  return true;
284  }
285 };
286 } // namespace
287 
289 {
290  CScript script;
291 
292  boost::apply_visitor(CScriptVisitor(&script), dest);
293  return script;
294 }
295 
297 {
298  return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
299 }
300 
301 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
302 {
303  CScript script;
304 
305  script << CScript::EncodeOP_N(nRequired);
306  for (const CPubKey& key : keys)
307  script << ToByteVector(key);
308  script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
309  return script;
310 }
311 
312 CScript GetScriptForWitness(const CScript& redeemscript)
313 {
314  std::vector<std::vector<unsigned char> > vSolutions;
315  txnouttype typ = Solver(redeemscript, vSolutions);
316  if (typ == TX_PUBKEY) {
317  return GetScriptForDestination(WitnessV0KeyHash(Hash360(vSolutions[0].begin(), vSolutions[0].end())));
318  } else if (typ == TX_PUBKEYHASH) {
319  return GetScriptForDestination(WitnessV0KeyHash(vSolutions[0]));
320  }
321  return GetScriptForDestination(WitnessV0ScriptHash(redeemscript));
322 }
323 
325  return dest.which() != 0;
326 }
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:503
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:155
unspendable OP_RETURN script that carries data
Definition: standard.h:64
T & back()
Definition: prevector.h:444
unsigned char program[40]
Definition: standard.h:96
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:491
bool IsPayToScriptHash() const
Definition: script.cpp:197
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
Definition: pubkey.h:36
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:324
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:17
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:296
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:90
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:216
unsigned int version
Definition: standard.h:94
CScriptID()
Definition: standard.h:25
unsigned int length
Definition: standard.h:95
unsigned char * begin()
Definition: uint256.h:56
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:155
const unsigned char * begin() const
Definition: pubkey.h:111
Definition: script.h:74
value_type * data()
Definition: prevector.h:519
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
Definition: pubkey.h:37
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:199
static bool ValidSize(const std::vector< unsigned char > &vch)
Definition: pubkey.h:74
iterator end()
Definition: prevector.h:303
opcodetype
Script opcodes.
Definition: script.h:48
Only for Witness versions not already defined above.
Definition: standard.h:67
const unsigned char * end() const
Definition: pubkey.h:112
boost::variant< CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:123
CTxDestination subtype to encode any future Witness version.
Definition: standard.h:92
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:232
bool IsValid() const
Definition: pubkey.h:171
An encapsulated public key.
Definition: pubkey.h:30
Definition: script.h:58
std::vector< unsigned char > valtype
Definition: standard.cpp:15
const char * GetTxnOutputType(txnouttype t)
Get the name of a txnouttype as a C string, or nullptr if unknown.
Definition: standard.cpp:27
auto end
Definition: rpcwallet.cpp:1068
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:288
A hasher class for SHA3-256.
Definition: sha3.h:14
txnouttype
Definition: standard.h:56
static opcodetype EncodeOP_N(int n)
Definition: script.h:510
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:384
A reference to a CKey: the Hash360 of its serialized public key.
Definition: pubkey.h:20
CSHA3 & Write(const unsigned char *data, size_t len)
Definition: sha3.cpp:30
160-bit opaque blob.
Definition: uint256.h:111
std::vector< unsigned char > valtype
Definition: interpreter.cpp:15
iterator begin()
Definition: prevector.h:301
A reference to a CScript: the Hash360 of its serialization (see script.h)
Definition: standard.h:22
size_type size() const
Definition: prevector.h:293
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:301
uint160 Hash360(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:142
Definition: script.h:51
void clear()
Definition: script.h:554
CScript GetScriptForWitness(const CScript &redeemscript)
Generate a pay-to-witness script for the given redeem script.
Definition: standard.cpp:312
unsigned nMaxDatacarrierBytes
Maximum size of TX_NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:18
Definition: script.h:100
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:42