Remove duplicate destination decoding
[bitcoinplatinum.git] / src / script / standard.cpp
blobb6e2232ab4420eef4349b99ba73f0c46ded2ab9b
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 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet)
39 // Templates
40 static std::multimap<txnouttype, CScript> mTemplates;
41 if (mTemplates.empty())
43 // Standard tx, sender provides pubkey, receiver adds signature
44 mTemplates.insert(std::make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
46 // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
47 mTemplates.insert(std::make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
49 // Sender provides N pubkeys, receivers provides M signatures
50 mTemplates.insert(std::make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
53 vSolutionsRet.clear();
55 // Shortcut for pay-to-script-hash, which are more constrained than the other types:
56 // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
57 if (scriptPubKey.IsPayToScriptHash())
59 typeRet = TX_SCRIPTHASH;
60 std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
61 vSolutionsRet.push_back(hashBytes);
62 return true;
65 int witnessversion;
66 std::vector<unsigned char> witnessprogram;
67 if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
68 if (witnessversion == 0 && witnessprogram.size() == 20) {
69 typeRet = TX_WITNESS_V0_KEYHASH;
70 vSolutionsRet.push_back(witnessprogram);
71 return true;
73 if (witnessversion == 0 && witnessprogram.size() == 32) {
74 typeRet = TX_WITNESS_V0_SCRIPTHASH;
75 vSolutionsRet.push_back(witnessprogram);
76 return true;
78 return false;
81 // Provably prunable, data-carrying output
83 // So long as script passes the IsUnspendable() test and all but the first
84 // byte passes the IsPushOnly() test we don't care what exactly is in the
85 // script.
86 if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
87 typeRet = TX_NULL_DATA;
88 return true;
91 // Scan templates
92 const CScript& script1 = scriptPubKey;
93 for (const std::pair<txnouttype, CScript>& tplate : mTemplates)
95 const CScript& script2 = tplate.second;
96 vSolutionsRet.clear();
98 opcodetype opcode1, opcode2;
99 std::vector<unsigned char> vch1, vch2;
101 // Compare
102 CScript::const_iterator pc1 = script1.begin();
103 CScript::const_iterator pc2 = script2.begin();
104 while (true)
106 if (pc1 == script1.end() && pc2 == script2.end())
108 // Found a match
109 typeRet = tplate.first;
110 if (typeRet == TX_MULTISIG)
112 // Additional checks for TX_MULTISIG:
113 unsigned char m = vSolutionsRet.front()[0];
114 unsigned char n = vSolutionsRet.back()[0];
115 if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
116 return false;
118 return true;
120 if (!script1.GetOp(pc1, opcode1, vch1))
121 break;
122 if (!script2.GetOp(pc2, opcode2, vch2))
123 break;
125 // Template matching opcodes:
126 if (opcode2 == OP_PUBKEYS)
128 while (vch1.size() >= 33 && vch1.size() <= 65)
130 vSolutionsRet.push_back(vch1);
131 if (!script1.GetOp(pc1, opcode1, vch1))
132 break;
134 if (!script2.GetOp(pc2, opcode2, vch2))
135 break;
136 // Normal situation is to fall through
137 // to other if/else statements
140 if (opcode2 == OP_PUBKEY)
142 if (vch1.size() < 33 || vch1.size() > 65)
143 break;
144 vSolutionsRet.push_back(vch1);
146 else if (opcode2 == OP_PUBKEYHASH)
148 if (vch1.size() != sizeof(uint160))
149 break;
150 vSolutionsRet.push_back(vch1);
152 else if (opcode2 == OP_SMALLINTEGER)
153 { // Single-byte small integer pushed onto vSolutions
154 if (opcode1 == OP_0 ||
155 (opcode1 >= OP_1 && opcode1 <= OP_16))
157 char n = (char)CScript::DecodeOP_N(opcode1);
158 vSolutionsRet.push_back(valtype(1, n));
160 else
161 break;
163 else if (opcode1 != opcode2 || vch1 != vch2)
165 // Others must match exactly
166 break;
171 vSolutionsRet.clear();
172 typeRet = TX_NONSTANDARD;
173 return false;
176 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
178 std::vector<valtype> vSolutions;
179 txnouttype whichType;
180 if (!Solver(scriptPubKey, whichType, vSolutions))
181 return false;
183 if (whichType == TX_PUBKEY)
185 CPubKey pubKey(vSolutions[0]);
186 if (!pubKey.IsValid())
187 return false;
189 addressRet = pubKey.GetID();
190 return true;
192 else if (whichType == TX_PUBKEYHASH)
194 addressRet = CKeyID(uint160(vSolutions[0]));
195 return true;
197 else if (whichType == TX_SCRIPTHASH)
199 addressRet = CScriptID(uint160(vSolutions[0]));
200 return true;
202 // Multisig txns have more than one address...
203 return false;
206 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
208 addressRet.clear();
209 typeRet = TX_NONSTANDARD;
210 std::vector<valtype> vSolutions;
211 if (!Solver(scriptPubKey, typeRet, vSolutions))
212 return false;
213 if (typeRet == TX_NULL_DATA){
214 // This is data, not addresses
215 return false;
218 if (typeRet == TX_MULTISIG)
220 nRequiredRet = vSolutions.front()[0];
221 for (unsigned int i = 1; i < vSolutions.size()-1; i++)
223 CPubKey pubKey(vSolutions[i]);
224 if (!pubKey.IsValid())
225 continue;
227 CTxDestination address = pubKey.GetID();
228 addressRet.push_back(address);
231 if (addressRet.empty())
232 return false;
234 else
236 nRequiredRet = 1;
237 CTxDestination address;
238 if (!ExtractDestination(scriptPubKey, address))
239 return false;
240 addressRet.push_back(address);
243 return true;
246 namespace
248 class CScriptVisitor : public boost::static_visitor<bool>
250 private:
251 CScript *script;
252 public:
253 explicit CScriptVisitor(CScript *scriptin) { script = scriptin; }
255 bool operator()(const CNoDestination &dest) const {
256 script->clear();
257 return false;
260 bool operator()(const CKeyID &keyID) const {
261 script->clear();
262 *script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
263 return true;
266 bool operator()(const CScriptID &scriptID) const {
267 script->clear();
268 *script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
269 return true;
272 } // namespace
274 CScript GetScriptForDestination(const CTxDestination& dest)
276 CScript script;
278 boost::apply_visitor(CScriptVisitor(&script), dest);
279 return script;
282 CScript GetScriptForRawPubKey(const CPubKey& pubKey)
284 return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
287 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
289 CScript script;
291 script << CScript::EncodeOP_N(nRequired);
292 for (const CPubKey& key : keys)
293 script << ToByteVector(key);
294 script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
295 return script;
298 CScript GetScriptForWitness(const CScript& redeemscript)
300 CScript ret;
302 txnouttype typ;
303 std::vector<std::vector<unsigned char> > vSolutions;
304 if (Solver(redeemscript, typ, vSolutions)) {
305 if (typ == TX_PUBKEY) {
306 unsigned char h160[20];
307 CHash160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160);
308 ret << OP_0 << std::vector<unsigned char>(&h160[0], &h160[20]);
309 return ret;
310 } else if (typ == TX_PUBKEYHASH) {
311 ret << OP_0 << vSolutions[0];
312 return ret;
315 uint256 hash;
316 CSHA256().Write(&redeemscript[0], redeemscript.size()).Finalize(hash.begin());
317 ret << OP_0 << ToByteVector(hash);
318 return ret;
321 bool IsValidDestination(const CTxDestination& dest) {
322 return dest.which() != 0;