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"
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
)
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";
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
)
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
);
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
);
77 if (witnessversion
== 0 && witnessprogram
.size() == 32) {
78 typeRet
= TX_WITNESS_V0_SCRIPTHASH
;
79 vSolutionsRet
.push_back(witnessprogram
);
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
90 if (scriptPubKey
.size() >= 1 && scriptPubKey
[0] == OP_RETURN
&& scriptPubKey
.IsPushOnly(scriptPubKey
.begin()+1)) {
91 typeRet
= TX_NULL_DATA
;
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
;
106 CScript::const_iterator pc1
= script1
.begin();
107 CScript::const_iterator pc2
= script2
.begin();
110 if (pc1
== script1
.end() && pc2
== script2
.end())
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
)
124 if (!script1
.GetOp(pc1
, opcode1
, vch1
))
126 if (!script2
.GetOp(pc2
, opcode2
, vch2
))
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
))
138 if (!script2
.GetOp(pc2
, opcode2
, vch2
))
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)
148 vSolutionsRet
.push_back(vch1
);
150 else if (opcode2
== OP_PUBKEYHASH
)
152 if (vch1
.size() != sizeof(uint160
))
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
));
167 else if (opcode1
!= opcode2
|| vch1
!= vch2
)
169 // Others must match exactly
175 vSolutionsRet
.clear();
176 typeRet
= TX_NONSTANDARD
;
180 bool ExtractDestination(const CScript
& scriptPubKey
, CTxDestination
& addressRet
)
182 std::vector
<valtype
> vSolutions
;
183 txnouttype whichType
;
184 if (!Solver(scriptPubKey
, whichType
, vSolutions
))
187 if (whichType
== TX_PUBKEY
)
189 CPubKey
pubKey(vSolutions
[0]);
190 if (!pubKey
.IsValid())
193 addressRet
= pubKey
.GetID();
196 else if (whichType
== TX_PUBKEYHASH
)
198 addressRet
= CKeyID(uint160(vSolutions
[0]));
201 else if (whichType
== TX_SCRIPTHASH
)
203 addressRet
= CScriptID(uint160(vSolutions
[0]));
206 // Multisig txns have more than one address...
210 bool ExtractDestinations(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<CTxDestination
>& addressRet
, int& nRequiredRet
)
213 typeRet
= TX_NONSTANDARD
;
214 std::vector
<valtype
> vSolutions
;
215 if (!Solver(scriptPubKey
, typeRet
, vSolutions
))
217 if (typeRet
== TX_NULL_DATA
){
218 // This is data, not addresses
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())
231 CTxDestination address
= pubKey
.GetID();
232 addressRet
.push_back(address
);
235 if (addressRet
.empty())
241 CTxDestination address
;
242 if (!ExtractDestination(scriptPubKey
, address
))
244 addressRet
.push_back(address
);
252 class CScriptVisitor
: public boost::static_visitor
<bool>
257 CScriptVisitor(CScript
*scriptin
) { script
= scriptin
; }
259 bool operator()(const CNoDestination
&dest
) const {
264 bool operator()(const CKeyID
&keyID
) const {
266 *script
<< OP_DUP
<< OP_HASH160
<< ToByteVector(keyID
) << OP_EQUALVERIFY
<< OP_CHECKSIG
;
270 bool operator()(const CScriptID
&scriptID
) const {
272 *script
<< OP_HASH160
<< ToByteVector(scriptID
) << OP_EQUAL
;
278 CScript
GetScriptForDestination(const CTxDestination
& dest
)
282 boost::apply_visitor(CScriptVisitor(&script
), dest
);
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
)
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
;
302 CScript
GetScriptForWitness(const CScript
& redeemscript
)
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]);
314 } else if (typ
== TX_PUBKEYHASH
) {
315 ret
<< OP_0
<< vSolutions
[0];
320 CSHA256().Write(&redeemscript
[0], redeemscript
.size()).Finalize(hash
.begin());
321 ret
<< OP_0
<< ToByteVector(hash
);