1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef H_BITCOIN_SCRIPT
6 #define H_BITCOIN_SCRIPT
11 #include <boost/foreach.hpp>
12 #include <boost/variant.hpp>
19 /** Signature hash types/flags */
25 SIGHASH_ANYONECANPAY
= 0x80,
32 // 'standard' transaction types:
39 class CNoDestination
{
41 friend bool operator==(const CNoDestination
&a
, const CNoDestination
&b
) { return true; }
42 friend bool operator<(const CNoDestination
&a
, const CNoDestination
&b
) { return true; }
45 /** A txout script template with a specific destination. It is either:
46 * * CNoDestination: no destination set
47 * * CKeyID: TX_PUBKEYHASH destination
48 * * CScriptID: TX_SCRIPTHASH destination
49 * A CTxDestination is the internal data type encoded in a CBitcoinAddress
51 typedef boost::variant
<CNoDestination
, CKeyID
, CScriptID
> CTxDestination
;
53 const char* GetTxnOutputType(txnouttype t
);
98 OP_FROMALTSTACK
= 0x6c,
130 OP_EQUALVERIFY
= 0x88,
155 OP_NUMEQUALVERIFY
= 0x9d,
156 OP_NUMNOTEQUAL
= 0x9e,
158 OP_GREATERTHAN
= 0xa0,
159 OP_LESSTHANOREQUAL
= 0xa1,
160 OP_GREATERTHANOREQUAL
= 0xa2,
172 OP_CODESEPARATOR
= 0xab,
174 OP_CHECKSIGVERIFY
= 0xad,
175 OP_CHECKMULTISIG
= 0xae,
176 OP_CHECKMULTISIGVERIFY
= 0xaf,
192 // template matching params
193 OP_SMALLINTEGER
= 0xfa,
195 OP_PUBKEYHASH
= 0xfd,
198 OP_INVALIDOPCODE
= 0xff,
201 const char* GetOpName(opcodetype opcode
);
205 inline std::string
ValueString(const std::vector
<unsigned char>& vch
)
208 return strprintf("%d", CBigNum(vch
).getint());
213 inline std::string
StackString(const std::vector
<std::vector
<unsigned char> >& vStack
)
216 BOOST_FOREACH(const std::vector
<unsigned char>& vch
, vStack
)
220 str
+= ValueString(vch
);
232 /** Serialized script, used inside transaction inputs and outputs */
233 class CScript
: public std::vector
<unsigned char>
236 CScript
& push_int64(int64 n
)
238 if (n
== -1 || (n
>= 1 && n
<= 16))
240 push_back(n
+ (OP_1
- 1));
245 *this << bn
.getvch();
250 CScript
& push_uint64(uint64 n
)
252 if (n
>= 1 && n
<= 16)
254 push_back(n
+ (OP_1
- 1));
259 *this << bn
.getvch();
266 CScript(const CScript
& b
) : std::vector
<unsigned char>(b
.begin(), b
.end()) { }
267 CScript(const_iterator pbegin
, const_iterator pend
) : std::vector
<unsigned char>(pbegin
, pend
) { }
269 CScript(const unsigned char* pbegin
, const unsigned char* pend
) : std::vector
<unsigned char>(pbegin
, pend
) { }
272 CScript
& operator+=(const CScript
& b
)
274 insert(end(), b
.begin(), b
.end());
278 friend CScript
operator+(const CScript
& a
, const CScript
& b
)
286 //explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
287 explicit CScript(signed char b
) { operator<<(b
); }
288 explicit CScript(short b
) { operator<<(b
); }
289 explicit CScript(int b
) { operator<<(b
); }
290 explicit CScript(long b
) { operator<<(b
); }
291 explicit CScript(int64 b
) { operator<<(b
); }
292 explicit CScript(unsigned char b
) { operator<<(b
); }
293 explicit CScript(unsigned int b
) { operator<<(b
); }
294 explicit CScript(unsigned short b
) { operator<<(b
); }
295 explicit CScript(unsigned long b
) { operator<<(b
); }
296 explicit CScript(uint64 b
) { operator<<(b
); }
298 explicit CScript(opcodetype b
) { operator<<(b
); }
299 explicit CScript(const uint256
& b
) { operator<<(b
); }
300 explicit CScript(const CBigNum
& b
) { operator<<(b
); }
301 explicit CScript(const std::vector
<unsigned char>& b
) { operator<<(b
); }
304 //CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
305 CScript
& operator<<(signed char b
) { return push_int64(b
); }
306 CScript
& operator<<(short b
) { return push_int64(b
); }
307 CScript
& operator<<(int b
) { return push_int64(b
); }
308 CScript
& operator<<(long b
) { return push_int64(b
); }
309 CScript
& operator<<(int64 b
) { return push_int64(b
); }
310 CScript
& operator<<(unsigned char b
) { return push_uint64(b
); }
311 CScript
& operator<<(unsigned int b
) { return push_uint64(b
); }
312 CScript
& operator<<(unsigned short b
) { return push_uint64(b
); }
313 CScript
& operator<<(unsigned long b
) { return push_uint64(b
); }
314 CScript
& operator<<(uint64 b
) { return push_uint64(b
); }
316 CScript
& operator<<(opcodetype opcode
)
318 if (opcode
< 0 || opcode
> 0xff)
319 throw std::runtime_error("CScript::operator<<() : invalid opcode");
320 insert(end(), (unsigned char)opcode
);
324 CScript
& operator<<(const uint160
& b
)
326 insert(end(), sizeof(b
));
327 insert(end(), (unsigned char*)&b
, (unsigned char*)&b
+ sizeof(b
));
331 CScript
& operator<<(const uint256
& b
)
333 insert(end(), sizeof(b
));
334 insert(end(), (unsigned char*)&b
, (unsigned char*)&b
+ sizeof(b
));
338 CScript
& operator<<(const CPubKey
& key
)
340 std::vector
<unsigned char> vchKey
= key
.Raw();
341 return (*this) << vchKey
;
344 CScript
& operator<<(const CBigNum
& b
)
350 CScript
& operator<<(const std::vector
<unsigned char>& b
)
352 if (b
.size() < OP_PUSHDATA1
)
354 insert(end(), (unsigned char)b
.size());
356 else if (b
.size() <= 0xff)
358 insert(end(), OP_PUSHDATA1
);
359 insert(end(), (unsigned char)b
.size());
361 else if (b
.size() <= 0xffff)
363 insert(end(), OP_PUSHDATA2
);
364 unsigned short nSize
= b
.size();
365 insert(end(), (unsigned char*)&nSize
, (unsigned char*)&nSize
+ sizeof(nSize
));
369 insert(end(), OP_PUSHDATA4
);
370 unsigned int nSize
= b
.size();
371 insert(end(), (unsigned char*)&nSize
, (unsigned char*)&nSize
+ sizeof(nSize
));
373 insert(end(), b
.begin(), b
.end());
377 CScript
& operator<<(const CScript
& b
)
379 // I'm not sure if this should push the script or concatenate scripts.
380 // If there's ever a use for pushing a script onto a script, delete this member fn
381 assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
386 bool GetOp(iterator
& pc
, opcodetype
& opcodeRet
, std::vector
<unsigned char>& vchRet
)
388 // Wrapper so it can be called with either iterator or const_iterator
389 const_iterator pc2
= pc
;
390 bool fRet
= GetOp2(pc2
, opcodeRet
, &vchRet
);
391 pc
= begin() + (pc2
- begin());
395 bool GetOp(iterator
& pc
, opcodetype
& opcodeRet
)
397 const_iterator pc2
= pc
;
398 bool fRet
= GetOp2(pc2
, opcodeRet
, NULL
);
399 pc
= begin() + (pc2
- begin());
403 bool GetOp(const_iterator
& pc
, opcodetype
& opcodeRet
, std::vector
<unsigned char>& vchRet
) const
405 return GetOp2(pc
, opcodeRet
, &vchRet
);
408 bool GetOp(const_iterator
& pc
, opcodetype
& opcodeRet
) const
410 return GetOp2(pc
, opcodeRet
, NULL
);
413 bool GetOp2(const_iterator
& pc
, opcodetype
& opcodeRet
, std::vector
<unsigned char>* pvchRet
) const
415 opcodeRet
= OP_INVALIDOPCODE
;
424 unsigned int opcode
= *pc
++;
427 if (opcode
<= OP_PUSHDATA4
)
430 if (opcode
< OP_PUSHDATA1
)
434 else if (opcode
== OP_PUSHDATA1
)
440 else if (opcode
== OP_PUSHDATA2
)
445 memcpy(&nSize
, &pc
[0], 2);
448 else if (opcode
== OP_PUSHDATA4
)
452 memcpy(&nSize
, &pc
[0], 4);
455 if (end() - pc
< 0 || (unsigned int)(end() - pc
) < nSize
)
458 pvchRet
->assign(pc
, pc
+ nSize
);
462 opcodeRet
= (opcodetype
)opcode
;
466 // Encode/decode small integers:
467 static int DecodeOP_N(opcodetype opcode
)
471 assert(opcode
>= OP_1
&& opcode
<= OP_16
);
472 return (int)opcode
- (int)(OP_1
- 1);
474 static opcodetype
EncodeOP_N(int n
)
476 assert(n
>= 0 && n
<= 16);
479 return (opcodetype
)(OP_1
+n
-1);
482 int FindAndDelete(const CScript
& b
)
487 iterator pc
= begin();
491 while (end() - pc
>= (long)b
.size() && memcmp(&pc
[0], &b
[0], b
.size()) == 0)
493 erase(pc
, pc
+ b
.size());
497 while (GetOp(pc
, opcode
));
500 int Find(opcodetype op
) const
504 for (const_iterator pc
= begin(); pc
!= end() && GetOp(pc
, opcode
);)
510 // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
511 // as 20 sigops. With pay-to-script-hash, that changed:
512 // CHECKMULTISIGs serialized in scriptSigs are
513 // counted more accurately, assuming they are of the form
514 // ... OP_N CHECKMULTISIG ...
515 unsigned int GetSigOpCount(bool fAccurate
) const;
517 // Accurately count sigOps, including sigOps in
518 // pay-to-script-hash transactions:
519 unsigned int GetSigOpCount(const CScript
& scriptSig
) const;
521 bool IsPayToScriptHash() const;
523 // Called by CTransaction::IsStandard
524 bool IsPushOnly() const
526 const_iterator pc
= begin();
530 if (!GetOp(pc
, opcode
))
539 void SetDestination(const CTxDestination
& address
);
540 void SetMultisig(int nRequired
, const std::vector
<CKey
>& keys
);
543 void PrintHex() const
545 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
548 std::string
ToString() const
552 std::vector
<unsigned char> vch
;
553 const_iterator pc
= begin();
558 if (!GetOp(pc
, opcode
, vch
))
563 if (0 <= opcode
&& opcode
<= OP_PUSHDATA4
)
564 str
+= ValueString(vch
);
566 str
+= GetOpName(opcode
);
573 printf("%s\n", ToString().c_str());
576 CScriptID
GetID() const
578 return CScriptID(Hash160(*this));
586 bool EvalScript(std::vector
<std::vector
<unsigned char> >& stack
, const CScript
& script
, const CTransaction
& txTo
, unsigned int nIn
, int nHashType
);
587 bool Solver(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<std::vector
<unsigned char> >& vSolutionsRet
);
588 int ScriptSigArgsExpected(txnouttype t
, const std::vector
<std::vector
<unsigned char> >& vSolutions
);
589 bool IsStandard(const CScript
& scriptPubKey
);
590 bool IsMine(const CKeyStore
& keystore
, const CScript
& scriptPubKey
);
591 bool IsMine(const CKeyStore
& keystore
, const CTxDestination
&dest
);
592 bool ExtractDestination(const CScript
& scriptPubKey
, CTxDestination
& addressRet
);
593 bool ExtractDestinations(const CScript
& scriptPubKey
, txnouttype
& typeRet
, std::vector
<CTxDestination
>& addressRet
, int& nRequiredRet
);
594 bool SignSignature(const CKeyStore
& keystore
, const CScript
& fromPubKey
, CTransaction
& txTo
, unsigned int nIn
, int nHashType
=SIGHASH_ALL
);
595 bool SignSignature(const CKeyStore
& keystore
, const CTransaction
& txFrom
, CTransaction
& txTo
, unsigned int nIn
, int nHashType
=SIGHASH_ALL
);
596 bool VerifyScript(const CScript
& scriptSig
, const CScript
& scriptPubKey
, const CTransaction
& txTo
, unsigned int nIn
,
597 bool fValidatePayToScriptHash
, int nHashType
);
598 bool VerifySignature(const CTransaction
& txFrom
, const CTransaction
& txTo
, unsigned int nIn
, bool fValidatePayToScriptHash
, int nHashType
);
600 // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
601 // combine them intelligently and return the result.
602 CScript
CombineSignatures(CScript scriptPubKey
, const CTransaction
& txTo
, unsigned int nIn
, const CScript
& scriptSig1
, const CScript
& scriptSig2
);