Report [CANCELLED] instead of [DONE] when shut down during txdb upgrade
[bitcoinplatinum.git] / src / script / standard.cpp
blob8e08acf0c61b70dd07edd29b361fd102b7c94d05
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"
13 #include <boost/foreach.hpp>
15 typedef std::vector<unsigned char> valtype;
17 bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
18 unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
20 CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
22 const char* GetTxnOutputType(txnouttype t)
24 switch (t)
26 case TX_NONSTANDARD: return "nonstandard";
27 case TX_PUBKEY: return "pubkey";
28 case TX_PUBKEYHASH: return "pubkeyhash";
29 case TX_SCRIPTHASH: return "scripthash";
30 case TX_MULTISIG: return "multisig";
31 case TX_NULL_DATA: return "nulldata";
32 case TX_WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
33 case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
35 return NULL;
38 /**
39 * Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
41 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet)
43 // Templates
44 static std::multimap<txnouttype, CScript> mTemplates;
45 if (mTemplates.empty())
47 // Standard tx, sender provides pubkey, receiver adds signature
48 mTemplates.insert(std::make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
50 // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
51 mTemplates.insert(std::make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
53 // Sender provides N pubkeys, receivers provides M signatures
54 mTemplates.insert(std::make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
57 vSolutionsRet.clear();
59 // Shortcut for pay-to-script-hash, which are more constrained than the other types:
60 // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
61 if (scriptPubKey.IsPayToScriptHash())
63 typeRet = TX_SCRIPTHASH;
64 std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
65 vSolutionsRet.push_back(hashBytes);
66 return true;
69 int witnessversion;
70 std::vector<unsigned char> witnessprogram;
71 if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
72 if (witnessversion == 0 && witnessprogram.size() == 20) {
73 typeRet = TX_WITNESS_V0_KEYHASH;
74 vSolutionsRet.push_back(witnessprogram);
75 return true;
77 if (witnessversion == 0 && witnessprogram.size() == 32) {
78 typeRet = TX_WITNESS_V0_SCRIPTHASH;
79 vSolutionsRet.push_back(witnessprogram);
80 return true;
82 return false;
85 // Provably prunable, data-carrying output
87 // So long as script passes the IsUnspendable() test and all but the first
88 // byte passes the IsPushOnly() test we don't care what exactly is in the
89 // script.
90 if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
91 typeRet = TX_NULL_DATA;
92 return true;
95 // Scan templates
96 const CScript& script1 = scriptPubKey;
97 for (const std::pair<txnouttype, CScript>& tplate : mTemplates)
99 const CScript& script2 = tplate.second;
100 vSolutionsRet.clear();
102 opcodetype opcode1, opcode2;
103 std::vector<unsigned char> vch1, vch2;
105 // Compare
106 CScript::const_iterator pc1 = script1.begin();
107 CScript::const_iterator pc2 = script2.begin();
108 while (true)
110 if (pc1 == script1.end() && pc2 == script2.end())
112 // Found a match
113 typeRet = tplate.first;
114 if (typeRet == TX_MULTISIG)
116 // Additional checks for TX_MULTISIG:
117 unsigned char m = vSolutionsRet.front()[0];
118 unsigned char n = vSolutionsRet.back()[0];
119 if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
120 return false;
122 return true;
124 if (!script1.GetOp(pc1, opcode1, vch1))
125 break;
126 if (!script2.GetOp(pc2, opcode2, vch2))
127 break;
129 // Template matching opcodes:
130 if (opcode2 == OP_PUBKEYS)
132 while (vch1.size() >= 33 && vch1.size() <= 65)
134 vSolutionsRet.push_back(vch1);
135 if (!script1.GetOp(pc1, opcode1, vch1))
136 break;
138 if (!script2.GetOp(pc2, opcode2, vch2))
139 break;
140 // Normal situation is to fall through
141 // to other if/else statements
144 if (opcode2 == OP_PUBKEY)
146 if (vch1.size() < 33 || vch1.size() > 65)
147 break;
148 vSolutionsRet.push_back(vch1);
150 else if (opcode2 == OP_PUBKEYHASH)
152 if (vch1.size() != sizeof(uint160))
153 break;
154 vSolutionsRet.push_back(vch1);
156 else if (opcode2 == OP_SMALLINTEGER)
157 { // Single-byte small integer pushed onto vSolutions
158 if (opcode1 == OP_0 ||
159 (opcode1 >= OP_1 && opcode1 <= OP_16))
161 char n = (char)CScript::DecodeOP_N(opcode1);
162 vSolutionsRet.push_back(valtype(1, n));
164 else
165 break;
167 else if (opcode1 != opcode2 || vch1 != vch2)
169 // Others must match exactly
170 break;
175 vSolutionsRet.clear();
176 typeRet = TX_NONSTANDARD;
177 return false;
180 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
182 std::vector<valtype> vSolutions;
183 txnouttype whichType;
184 if (!Solver(scriptPubKey, whichType, vSolutions))
185 return false;
187 if (whichType == TX_PUBKEY)
189 CPubKey pubKey(vSolutions[0]);
190 if (!pubKey.IsValid())
191 return false;
193 addressRet = pubKey.GetID();
194 return true;
196 else if (whichType == TX_PUBKEYHASH)
198 addressRet = CKeyID(uint160(vSolutions[0]));
199 return true;
201 else if (whichType == TX_SCRIPTHASH)
203 addressRet = CScriptID(uint160(vSolutions[0]));
204 return true;
206 // Multisig txns have more than one address...
207 return false;
210 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
212 addressRet.clear();
213 typeRet = TX_NONSTANDARD;
214 std::vector<valtype> vSolutions;
215 if (!Solver(scriptPubKey, typeRet, vSolutions))
216 return false;
217 if (typeRet == TX_NULL_DATA){
218 // This is data, not addresses
219 return false;
222 if (typeRet == TX_MULTISIG)
224 nRequiredRet = vSolutions.front()[0];
225 for (unsigned int i = 1; i < vSolutions.size()-1; i++)
227 CPubKey pubKey(vSolutions[i]);
228 if (!pubKey.IsValid())
229 continue;
231 CTxDestination address = pubKey.GetID();
232 addressRet.push_back(address);
235 if (addressRet.empty())
236 return false;
238 else
240 nRequiredRet = 1;
241 CTxDestination address;
242 if (!ExtractDestination(scriptPubKey, address))
243 return false;
244 addressRet.push_back(address);
247 return true;
250 namespace
252 class CScriptVisitor : public boost::static_visitor<bool>
254 private:
255 CScript *script;
256 public:
257 CScriptVisitor(CScript *scriptin) { script = scriptin; }
259 bool operator()(const CNoDestination &dest) const {
260 script->clear();
261 return false;
264 bool operator()(const CKeyID &keyID) const {
265 script->clear();
266 *script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
267 return true;
270 bool operator()(const CScriptID &scriptID) const {
271 script->clear();
272 *script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
273 return true;
276 } // namespace
278 CScript GetScriptForDestination(const CTxDestination& dest)
280 CScript script;
282 boost::apply_visitor(CScriptVisitor(&script), dest);
283 return script;
286 CScript GetScriptForRawPubKey(const CPubKey& pubKey)
288 return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
291 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
293 CScript script;
295 script << CScript::EncodeOP_N(nRequired);
296 for (const CPubKey& key : keys)
297 script << ToByteVector(key);
298 script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
299 return script;
302 CScript GetScriptForWitness(const CScript& redeemscript)
304 CScript ret;
306 txnouttype typ;
307 std::vector<std::vector<unsigned char> > vSolutions;
308 if (Solver(redeemscript, typ, vSolutions)) {
309 if (typ == TX_PUBKEY) {
310 unsigned char h160[20];
311 CHash160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160);
312 ret << OP_0 << std::vector<unsigned char>(&h160[0], &h160[20]);
313 return ret;
314 } else if (typ == TX_PUBKEYHASH) {
315 ret << OP_0 << vSolutions[0];
316 return ret;
319 uint256 hash;
320 CSHA256().Write(&redeemscript[0], redeemscript.size()).Finalize(hash.begin());
321 ret << OP_0 << ToByteVector(hash);
322 return ret;