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"
9 #include "script/script.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
)
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";
37 bool Solver(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<std::vector
<unsigned char> >& vSolutionsRet
)
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
);
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
);
73 if (witnessversion
== 0 && witnessprogram
.size() == 32) {
74 typeRet
= TX_WITNESS_V0_SCRIPTHASH
;
75 vSolutionsRet
.push_back(witnessprogram
);
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
86 if (scriptPubKey
.size() >= 1 && scriptPubKey
[0] == OP_RETURN
&& scriptPubKey
.IsPushOnly(scriptPubKey
.begin()+1)) {
87 typeRet
= TX_NULL_DATA
;
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
;
102 CScript::const_iterator pc1
= script1
.begin();
103 CScript::const_iterator pc2
= script2
.begin();
106 if (pc1
== script1
.end() && pc2
== script2
.end())
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
)
120 if (!script1
.GetOp(pc1
, opcode1
, vch1
))
122 if (!script2
.GetOp(pc2
, opcode2
, vch2
))
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
))
134 if (!script2
.GetOp(pc2
, opcode2
, vch2
))
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)
144 vSolutionsRet
.push_back(vch1
);
146 else if (opcode2
== OP_PUBKEYHASH
)
148 if (vch1
.size() != sizeof(uint160
))
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
));
163 else if (opcode1
!= opcode2
|| vch1
!= vch2
)
165 // Others must match exactly
171 vSolutionsRet
.clear();
172 typeRet
= TX_NONSTANDARD
;
176 bool ExtractDestination(const CScript
& scriptPubKey
, CTxDestination
& addressRet
)
178 std::vector
<valtype
> vSolutions
;
179 txnouttype whichType
;
180 if (!Solver(scriptPubKey
, whichType
, vSolutions
))
183 if (whichType
== TX_PUBKEY
)
185 CPubKey
pubKey(vSolutions
[0]);
186 if (!pubKey
.IsValid())
189 addressRet
= pubKey
.GetID();
192 else if (whichType
== TX_PUBKEYHASH
)
194 addressRet
= CKeyID(uint160(vSolutions
[0]));
197 else if (whichType
== TX_SCRIPTHASH
)
199 addressRet
= CScriptID(uint160(vSolutions
[0]));
202 // Multisig txns have more than one address...
206 bool ExtractDestinations(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<CTxDestination
>& addressRet
, int& nRequiredRet
)
209 typeRet
= TX_NONSTANDARD
;
210 std::vector
<valtype
> vSolutions
;
211 if (!Solver(scriptPubKey
, typeRet
, vSolutions
))
213 if (typeRet
== TX_NULL_DATA
){
214 // This is data, not addresses
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())
227 CTxDestination address
= pubKey
.GetID();
228 addressRet
.push_back(address
);
231 if (addressRet
.empty())
237 CTxDestination address
;
238 if (!ExtractDestination(scriptPubKey
, address
))
240 addressRet
.push_back(address
);
248 class CScriptVisitor
: public boost::static_visitor
<bool>
253 explicit CScriptVisitor(CScript
*scriptin
) { script
= scriptin
; }
255 bool operator()(const CNoDestination
&dest
) const {
260 bool operator()(const CKeyID
&keyID
) const {
262 *script
<< OP_DUP
<< OP_HASH160
<< ToByteVector(keyID
) << OP_EQUALVERIFY
<< OP_CHECKSIG
;
266 bool operator()(const CScriptID
&scriptID
) const {
268 *script
<< OP_HASH160
<< ToByteVector(scriptID
) << OP_EQUAL
;
274 CScript
GetScriptForDestination(const CTxDestination
& dest
)
278 boost::apply_visitor(CScriptVisitor(&script
), dest
);
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
)
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
;
298 CScript
GetScriptForWitness(const CScript
& redeemscript
)
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]);
310 } else if (typ
== TX_PUBKEYHASH
) {
311 ret
<< OP_0
<< vSolutions
[0];
316 CSHA256().Write(&redeemscript
[0], redeemscript
.size()).Finalize(hash
.begin());
317 ret
<< OP_0
<< ToByteVector(hash
);
321 bool IsValidDestination(const CTxDestination
& dest
) {
322 return dest
.which() != 0;