qa: Restore bitcoin-util-test py2 compatibility
[bitcoinplatinum.git] / src / script / standard.cpp
blob9570b8c8d9a77e8edb72573284665fa629e1c3f9
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.
6 #include "script/standard.h"
8 #include "pubkey.h"
9 #include "script/script.h"
10 #include "util.h"
11 #include "utilstrencodings.h"
14 typedef std::vector<unsigned char> valtype;
16 bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
17 unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
19 CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
21 const char* GetTxnOutputType(txnouttype t)
23 switch (t)
25 case TX_NONSTANDARD: return "nonstandard";
26 case TX_PUBKEY: return "pubkey";
27 case TX_PUBKEYHASH: return "pubkeyhash";
28 case TX_SCRIPTHASH: return "scripthash";
29 case TX_MULTISIG: return "multisig";
30 case TX_NULL_DATA: return "nulldata";
31 case TX_WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
32 case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
34 return nullptr;
37 /**
38 * Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
40 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet)
42 // Templates
43 static std::multimap<txnouttype, CScript> mTemplates;
44 if (mTemplates.empty())
46 // Standard tx, sender provides pubkey, receiver adds signature
47 mTemplates.insert(std::make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
49 // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
50 mTemplates.insert(std::make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
52 // Sender provides N pubkeys, receivers provides M signatures
53 mTemplates.insert(std::make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
56 vSolutionsRet.clear();
58 // Shortcut for pay-to-script-hash, which are more constrained than the other types:
59 // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
60 if (scriptPubKey.IsPayToScriptHash())
62 typeRet = TX_SCRIPTHASH;
63 std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
64 vSolutionsRet.push_back(hashBytes);
65 return true;
68 int witnessversion;
69 std::vector<unsigned char> witnessprogram;
70 if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
71 if (witnessversion == 0 && witnessprogram.size() == 20) {
72 typeRet = TX_WITNESS_V0_KEYHASH;
73 vSolutionsRet.push_back(witnessprogram);
74 return true;
76 if (witnessversion == 0 && witnessprogram.size() == 32) {
77 typeRet = TX_WITNESS_V0_SCRIPTHASH;
78 vSolutionsRet.push_back(witnessprogram);
79 return true;
81 return false;
84 // Provably prunable, data-carrying output
86 // So long as script passes the IsUnspendable() test and all but the first
87 // byte passes the IsPushOnly() test we don't care what exactly is in the
88 // script.
89 if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
90 typeRet = TX_NULL_DATA;
91 return true;
94 // Scan templates
95 const CScript& script1 = scriptPubKey;
96 for (const std::pair<txnouttype, CScript>& tplate : mTemplates)
98 const CScript& script2 = tplate.second;
99 vSolutionsRet.clear();
101 opcodetype opcode1, opcode2;
102 std::vector<unsigned char> vch1, vch2;
104 // Compare
105 CScript::const_iterator pc1 = script1.begin();
106 CScript::const_iterator pc2 = script2.begin();
107 while (true)
109 if (pc1 == script1.end() && pc2 == script2.end())
111 // Found a match
112 typeRet = tplate.first;
113 if (typeRet == TX_MULTISIG)
115 // Additional checks for TX_MULTISIG:
116 unsigned char m = vSolutionsRet.front()[0];
117 unsigned char n = vSolutionsRet.back()[0];
118 if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
119 return false;
121 return true;
123 if (!script1.GetOp(pc1, opcode1, vch1))
124 break;
125 if (!script2.GetOp(pc2, opcode2, vch2))
126 break;
128 // Template matching opcodes:
129 if (opcode2 == OP_PUBKEYS)
131 while (vch1.size() >= 33 && vch1.size() <= 65)
133 vSolutionsRet.push_back(vch1);
134 if (!script1.GetOp(pc1, opcode1, vch1))
135 break;
137 if (!script2.GetOp(pc2, opcode2, vch2))
138 break;
139 // Normal situation is to fall through
140 // to other if/else statements
143 if (opcode2 == OP_PUBKEY)
145 if (vch1.size() < 33 || vch1.size() > 65)
146 break;
147 vSolutionsRet.push_back(vch1);
149 else if (opcode2 == OP_PUBKEYHASH)
151 if (vch1.size() != sizeof(uint160))
152 break;
153 vSolutionsRet.push_back(vch1);
155 else if (opcode2 == OP_SMALLINTEGER)
156 { // Single-byte small integer pushed onto vSolutions
157 if (opcode1 == OP_0 ||
158 (opcode1 >= OP_1 && opcode1 <= OP_16))
160 char n = (char)CScript::DecodeOP_N(opcode1);
161 vSolutionsRet.push_back(valtype(1, n));
163 else
164 break;
166 else if (opcode1 != opcode2 || vch1 != vch2)
168 // Others must match exactly
169 break;
174 vSolutionsRet.clear();
175 typeRet = TX_NONSTANDARD;
176 return false;
179 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
181 std::vector<valtype> vSolutions;
182 txnouttype whichType;
183 if (!Solver(scriptPubKey, whichType, vSolutions))
184 return false;
186 if (whichType == TX_PUBKEY)
188 CPubKey pubKey(vSolutions[0]);
189 if (!pubKey.IsValid())
190 return false;
192 addressRet = pubKey.GetID();
193 return true;
195 else if (whichType == TX_PUBKEYHASH)
197 addressRet = CKeyID(uint160(vSolutions[0]));
198 return true;
200 else if (whichType == TX_SCRIPTHASH)
202 addressRet = CScriptID(uint160(vSolutions[0]));
203 return true;
205 // Multisig txns have more than one address...
206 return false;
209 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
211 addressRet.clear();
212 typeRet = TX_NONSTANDARD;
213 std::vector<valtype> vSolutions;
214 if (!Solver(scriptPubKey, typeRet, vSolutions))
215 return false;
216 if (typeRet == TX_NULL_DATA){
217 // This is data, not addresses
218 return false;
221 if (typeRet == TX_MULTISIG)
223 nRequiredRet = vSolutions.front()[0];
224 for (unsigned int i = 1; i < vSolutions.size()-1; i++)
226 CPubKey pubKey(vSolutions[i]);
227 if (!pubKey.IsValid())
228 continue;
230 CTxDestination address = pubKey.GetID();
231 addressRet.push_back(address);
234 if (addressRet.empty())
235 return false;
237 else
239 nRequiredRet = 1;
240 CTxDestination address;
241 if (!ExtractDestination(scriptPubKey, address))
242 return false;
243 addressRet.push_back(address);
246 return true;
249 namespace
251 class CScriptVisitor : public boost::static_visitor<bool>
253 private:
254 CScript *script;
255 public:
256 CScriptVisitor(CScript *scriptin) { script = scriptin; }
258 bool operator()(const CNoDestination &dest) const {
259 script->clear();
260 return false;
263 bool operator()(const CKeyID &keyID) const {
264 script->clear();
265 *script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
266 return true;
269 bool operator()(const CScriptID &scriptID) const {
270 script->clear();
271 *script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
272 return true;
275 } // namespace
277 CScript GetScriptForDestination(const CTxDestination& dest)
279 CScript script;
281 boost::apply_visitor(CScriptVisitor(&script), dest);
282 return script;
285 CScript GetScriptForRawPubKey(const CPubKey& pubKey)
287 return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
290 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
292 CScript script;
294 script << CScript::EncodeOP_N(nRequired);
295 for (const CPubKey& key : keys)
296 script << ToByteVector(key);
297 script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
298 return script;
301 CScript GetScriptForWitness(const CScript& redeemscript)
303 CScript ret;
305 txnouttype typ;
306 std::vector<std::vector<unsigned char> > vSolutions;
307 if (Solver(redeemscript, typ, vSolutions)) {
308 if (typ == TX_PUBKEY) {
309 unsigned char h160[20];
310 CHash160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160);
311 ret << OP_0 << std::vector<unsigned char>(&h160[0], &h160[20]);
312 return ret;
313 } else if (typ == TX_PUBKEYHASH) {
314 ret << OP_0 << vSolutions[0];
315 return ret;
318 uint256 hash;
319 CSHA256().Write(&redeemscript[0], redeemscript.size()).Finalize(hash.begin());
320 ret << OP_0 << ToByteVector(hash);
321 return ret;