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";
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
)
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
);
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
);
76 if (witnessversion
== 0 && witnessprogram
.size() == 32) {
77 typeRet
= TX_WITNESS_V0_SCRIPTHASH
;
78 vSolutionsRet
.push_back(witnessprogram
);
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
89 if (scriptPubKey
.size() >= 1 && scriptPubKey
[0] == OP_RETURN
&& scriptPubKey
.IsPushOnly(scriptPubKey
.begin()+1)) {
90 typeRet
= TX_NULL_DATA
;
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
;
105 CScript::const_iterator pc1
= script1
.begin();
106 CScript::const_iterator pc2
= script2
.begin();
109 if (pc1
== script1
.end() && pc2
== script2
.end())
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
)
123 if (!script1
.GetOp(pc1
, opcode1
, vch1
))
125 if (!script2
.GetOp(pc2
, opcode2
, vch2
))
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
))
137 if (!script2
.GetOp(pc2
, opcode2
, vch2
))
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)
147 vSolutionsRet
.push_back(vch1
);
149 else if (opcode2
== OP_PUBKEYHASH
)
151 if (vch1
.size() != sizeof(uint160
))
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
));
166 else if (opcode1
!= opcode2
|| vch1
!= vch2
)
168 // Others must match exactly
174 vSolutionsRet
.clear();
175 typeRet
= TX_NONSTANDARD
;
179 bool ExtractDestination(const CScript
& scriptPubKey
, CTxDestination
& addressRet
)
181 std::vector
<valtype
> vSolutions
;
182 txnouttype whichType
;
183 if (!Solver(scriptPubKey
, whichType
, vSolutions
))
186 if (whichType
== TX_PUBKEY
)
188 CPubKey
pubKey(vSolutions
[0]);
189 if (!pubKey
.IsValid())
192 addressRet
= pubKey
.GetID();
195 else if (whichType
== TX_PUBKEYHASH
)
197 addressRet
= CKeyID(uint160(vSolutions
[0]));
200 else if (whichType
== TX_SCRIPTHASH
)
202 addressRet
= CScriptID(uint160(vSolutions
[0]));
205 // Multisig txns have more than one address...
209 bool ExtractDestinations(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<CTxDestination
>& addressRet
, int& nRequiredRet
)
212 typeRet
= TX_NONSTANDARD
;
213 std::vector
<valtype
> vSolutions
;
214 if (!Solver(scriptPubKey
, typeRet
, vSolutions
))
216 if (typeRet
== TX_NULL_DATA
){
217 // This is data, not addresses
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())
230 CTxDestination address
= pubKey
.GetID();
231 addressRet
.push_back(address
);
234 if (addressRet
.empty())
240 CTxDestination address
;
241 if (!ExtractDestination(scriptPubKey
, address
))
243 addressRet
.push_back(address
);
251 class CScriptVisitor
: public boost::static_visitor
<bool>
256 CScriptVisitor(CScript
*scriptin
) { script
= scriptin
; }
258 bool operator()(const CNoDestination
&dest
) const {
263 bool operator()(const CKeyID
&keyID
) const {
265 *script
<< OP_DUP
<< OP_HASH160
<< ToByteVector(keyID
) << OP_EQUALVERIFY
<< OP_CHECKSIG
;
269 bool operator()(const CScriptID
&scriptID
) const {
271 *script
<< OP_HASH160
<< ToByteVector(scriptID
) << OP_EQUAL
;
277 CScript
GetScriptForDestination(const CTxDestination
& dest
)
281 boost::apply_visitor(CScriptVisitor(&script
), dest
);
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
)
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
;
301 CScript
GetScriptForWitness(const CScript
& redeemscript
)
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]);
313 } else if (typ
== TX_PUBKEYHASH
) {
314 ret
<< OP_0
<< vSolutions
[0];
319 CSHA256().Write(&redeemscript
[0], redeemscript
.size()).Finalize(hash
.begin());
320 ret
<< OP_0
<< ToByteVector(hash
);