Bitcoin v0.3.10 · 2010-08-15
script.h
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45class CTransaction;67enum8{9 SIGHASH_ALL = 1,10 SIGHASH_NONE = 2,11 SIGHASH_SINGLE = 3,12 SIGHASH_ANYONECANPAY = 0x80,13};14151617enum opcodetype18{19 // push value20 OP_0=0,21 OP_FALSE=OP_0,22 OP_PUSHDATA1=76,23 OP_PUSHDATA2,24 OP_PUSHDATA4,25 OP_1NEGATE,26 OP_RESERVED,27 OP_1,28 OP_TRUE=OP_1,29 OP_2,30 OP_3,31 OP_4,32 OP_5,33 OP_6,34 OP_7,35 OP_8,36 OP_9,37 OP_10,38 OP_11,39 OP_12,40 OP_13,41 OP_14,42 OP_15,43 OP_16,4445 // control46 OP_NOP,47 OP_VER,48 OP_IF,49 OP_NOTIF,50 OP_VERIF,51 OP_VERNOTIF,52 OP_ELSE,53 OP_ENDIF,54 OP_VERIFY,55 OP_RETURN,5657 // stack ops58 OP_TOALTSTACK,59 OP_FROMALTSTACK,60 OP_2DROP,61 OP_2DUP,62 OP_3DUP,63 OP_2OVER,64 OP_2ROT,65 OP_2SWAP,66 OP_IFDUP,67 OP_DEPTH,68 OP_DROP,69 OP_DUP,70 OP_NIP,71 OP_OVER,72 OP_PICK,73 OP_ROLL,74 OP_ROT,75 OP_SWAP,76 OP_TUCK,7778 // splice ops79 OP_CAT,80 OP_SUBSTR,81 OP_LEFT,82 OP_RIGHT,83 OP_SIZE,8485 // bit logic86 OP_INVERT,87 OP_AND,88 OP_OR,89 OP_XOR,90 OP_EQUAL,91 OP_EQUALVERIFY,92 OP_RESERVED1,93 OP_RESERVED2,9495 // numeric96 OP_1ADD,97 OP_1SUB,98 OP_2MUL,99 OP_2DIV,100 OP_NEGATE,101 OP_ABS,102 OP_NOT,103 OP_0NOTEQUAL,104105 OP_ADD,106 OP_SUB,107 OP_MUL,108 OP_DIV,109 OP_MOD,110 OP_LSHIFT,111 OP_RSHIFT,112113 OP_BOOLAND,114 OP_BOOLOR,115 OP_NUMEQUAL,116 OP_NUMEQUALVERIFY,117 OP_NUMNOTEQUAL,118 OP_LESSTHAN,119 OP_GREATERTHAN,120 OP_LESSTHANOREQUAL,121 OP_GREATERTHANOREQUAL,122 OP_MIN,123 OP_MAX,124125 OP_WITHIN,126127 // crypto128 OP_RIPEMD160,129 OP_SHA1,130 OP_SHA256,131 OP_HASH160,132 OP_HASH256,133 OP_CODESEPARATOR,134 OP_CHECKSIG,135 OP_CHECKSIGVERIFY,136 OP_CHECKMULTISIG,137 OP_CHECKMULTISIGVERIFY,138139 // expansion140 OP_NOP1,141 OP_NOP2,142 OP_NOP3,143 OP_NOP4,144 OP_NOP5,145 OP_NOP6,146 OP_NOP7,147 OP_NOP8,148 OP_NOP9,149 OP_NOP10,150151152153154 // multi-byte opcodes155 OP_SINGLEBYTE_END = 0xF0,156 OP_DOUBLEBYTE_BEGIN = 0xF000,157158 // template matching params159 OP_PUBKEY,160 OP_PUBKEYHASH,161162163164 OP_INVALIDOPCODE = 0xFFFF,165};166167168169170171172173174inline const char* GetOpName(opcodetype opcode)175{176 switch (opcode)177 {178 // push value179 case OP_0 : return "0";180 case OP_PUSHDATA1 : return "OP_PUSHDATA1";181 case OP_PUSHDATA2 : return "OP_PUSHDATA2";182 case OP_PUSHDATA4 : return "OP_PUSHDATA4";183 case OP_1NEGATE : return "-1";184 case OP_RESERVED : return "OP_RESERVED";185 case OP_1 : return "1";186 case OP_2 : return "2";187 case OP_3 : return "3";188 case OP_4 : return "4";189 case OP_5 : return "5";190 case OP_6 : return "6";191 case OP_7 : return "7";192 case OP_8 : return "8";193 case OP_9 : return "9";194 case OP_10 : return "10";195 case OP_11 : return "11";196 case OP_12 : return "12";197 case OP_13 : return "13";198 case OP_14 : return "14";199 case OP_15 : return "15";200 case OP_16 : return "16";201202 // control203 case OP_NOP : return "OP_NOP";204 case OP_VER : return "OP_VER";205 case OP_IF : return "OP_IF";206 case OP_NOTIF : return "OP_NOTIF";207 case OP_VERIF : return "OP_VERIF";208 case OP_VERNOTIF : return "OP_VERNOTIF";209 case OP_ELSE : return "OP_ELSE";210 case OP_ENDIF : return "OP_ENDIF";211 case OP_VERIFY : return "OP_VERIFY";212 case OP_RETURN : return "OP_RETURN";213214 // stack ops215 case OP_TOALTSTACK : return "OP_TOALTSTACK";216 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";217 case OP_2DROP : return "OP_2DROP";218 case OP_2DUP : return "OP_2DUP";219 case OP_3DUP : return "OP_3DUP";220 case OP_2OVER : return "OP_2OVER";221 case OP_2ROT : return "OP_2ROT";222 case OP_2SWAP : return "OP_2SWAP";223 case OP_IFDUP : return "OP_IFDUP";224 case OP_DEPTH : return "OP_DEPTH";225 case OP_DROP : return "OP_DROP";226 case OP_DUP : return "OP_DUP";227 case OP_NIP : return "OP_NIP";228 case OP_OVER : return "OP_OVER";229 case OP_PICK : return "OP_PICK";230 case OP_ROLL : return "OP_ROLL";231 case OP_ROT : return "OP_ROT";232 case OP_SWAP : return "OP_SWAP";233 case OP_TUCK : return "OP_TUCK";234235 // splice ops236 case OP_CAT : return "OP_CAT";237 case OP_SUBSTR : return "OP_SUBSTR";238 case OP_LEFT : return "OP_LEFT";239 case OP_RIGHT : return "OP_RIGHT";240 case OP_SIZE : return "OP_SIZE";241242 // bit logic243 case OP_INVERT : return "OP_INVERT";244 case OP_AND : return "OP_AND";245 case OP_OR : return "OP_OR";246 case OP_XOR : return "OP_XOR";247 case OP_EQUAL : return "OP_EQUAL";248 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";249 case OP_RESERVED1 : return "OP_RESERVED1";250 case OP_RESERVED2 : return "OP_RESERVED2";251252 // numeric253 case OP_1ADD : return "OP_1ADD";254 case OP_1SUB : return "OP_1SUB";255 case OP_2MUL : return "OP_2MUL";256 case OP_2DIV : return "OP_2DIV";257 case OP_NEGATE : return "OP_NEGATE";258 case OP_ABS : return "OP_ABS";259 case OP_NOT : return "OP_NOT";260 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";261 case OP_ADD : return "OP_ADD";262 case OP_SUB : return "OP_SUB";263 case OP_MUL : return "OP_MUL";264 case OP_DIV : return "OP_DIV";265 case OP_MOD : return "OP_MOD";266 case OP_LSHIFT : return "OP_LSHIFT";267 case OP_RSHIFT : return "OP_RSHIFT";268 case OP_BOOLAND : return "OP_BOOLAND";269 case OP_BOOLOR : return "OP_BOOLOR";270 case OP_NUMEQUAL : return "OP_NUMEQUAL";271 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";272 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";273 case OP_LESSTHAN : return "OP_LESSTHAN";274 case OP_GREATERTHAN : return "OP_GREATERTHAN";275 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";276 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";277 case OP_MIN : return "OP_MIN";278 case OP_MAX : return "OP_MAX";279 case OP_WITHIN : return "OP_WITHIN";280281 // crypto282 case OP_RIPEMD160 : return "OP_RIPEMD160";283 case OP_SHA1 : return "OP_SHA1";284 case OP_SHA256 : return "OP_SHA256";285 case OP_HASH160 : return "OP_HASH160";286 case OP_HASH256 : return "OP_HASH256";287 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";288 case OP_CHECKSIG : return "OP_CHECKSIG";289 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";290 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";291 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";292293 // expanson294 case OP_NOP1 : return "OP_NOP1";295 case OP_NOP2 : return "OP_NOP2";296 case OP_NOP3 : return "OP_NOP3";297 case OP_NOP4 : return "OP_NOP4";298 case OP_NOP5 : return "OP_NOP5";299 case OP_NOP6 : return "OP_NOP6";300 case OP_NOP7 : return "OP_NOP7";301 case OP_NOP8 : return "OP_NOP8";302 case OP_NOP9 : return "OP_NOP9";303 case OP_NOP10 : return "OP_NOP10";304305306307 // multi-byte opcodes308 case OP_SINGLEBYTE_END : return "OP_SINGLEBYTE_END";309 case OP_DOUBLEBYTE_BEGIN : return "OP_DOUBLEBYTE_BEGIN";310 case OP_PUBKEY : return "OP_PUBKEY";311 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";312313314 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";315 default:316 return "UNKNOWN_OPCODE";317 }318};319320321322323inline string ValueString(const vector<unsigned char>& vch)324{325 if (vch.size() <= 4)326 return strprintf("%d", CBigNum(vch).getint());327 else328 return HexNumStr(vch.begin(), vch.end());329 //return string("(") + HexStr(vch.begin(), vch.end()) + string(")");330}331332inline string StackString(const vector<vector<unsigned char> >& vStack)333{334 string str;335 foreach(const vector<unsigned char>& vch, vStack)336 {337 if (!str.empty())338 str += " ";339 str += ValueString(vch);340 }341 return str;342}343344345346347348349350351352class CScript : public vector<unsigned char>353{354protected:355 CScript& push_int64(int64 n)356 {357 if (n == -1 || (n >= 1 && n <= 16))358 {359 push_back(n + (OP_1 - 1));360 }361 else362 {363 CBigNum bn(n);364 *this << bn.getvch();365 }366 return (*this);367 }368369 CScript& push_uint64(uint64 n)370 {371 if (n == -1 || (n >= 1 && n <= 16))372 {373 push_back(n + (OP_1 - 1));374 }375 else376 {377 CBigNum bn(n);378 *this << bn.getvch();379 }380 return (*this);381 }382383public:384 CScript() { }385 CScript(const CScript& b) : vector<unsigned char>(b.begin(), b.end()) { }386 CScript(const_iterator pbegin, const_iterator pend) : vector<unsigned char>(pbegin, pend) { }387#ifndef _MSC_VER388 CScript(const unsigned char* pbegin, const unsigned char* pend) : vector<unsigned char>(pbegin, pend) { }389#endif390391 CScript& operator+=(const CScript& b)392 {393 insert(end(), b.begin(), b.end());394 return *this;395 }396397 friend CScript operator+(const CScript& a, const CScript& b)398 {399 CScript ret = a;400 ret += b;401 return (ret);402 }403404405 explicit CScript(char b) { operator<<(b); }406 explicit CScript(short b) { operator<<(b); }407 explicit CScript(int b) { operator<<(b); }408 explicit CScript(long b) { operator<<(b); }409 explicit CScript(int64 b) { operator<<(b); }410 explicit CScript(unsigned char b) { operator<<(b); }411 explicit CScript(unsigned int b) { operator<<(b); }412 explicit CScript(unsigned short b) { operator<<(b); }413 explicit CScript(unsigned long b) { operator<<(b); }414 explicit CScript(uint64 b) { operator<<(b); }415416 explicit CScript(opcodetype b) { operator<<(b); }417 explicit CScript(const uint256& b) { operator<<(b); }418 explicit CScript(const CBigNum& b) { operator<<(b); }419 explicit CScript(const vector<unsigned char>& b) { operator<<(b); }420421422 CScript& operator<<(char b) { return (push_int64(b)); }423 CScript& operator<<(short b) { return (push_int64(b)); }424 CScript& operator<<(int b) { return (push_int64(b)); }425 CScript& operator<<(long b) { return (push_int64(b)); }426 CScript& operator<<(int64 b) { return (push_int64(b)); }427 CScript& operator<<(unsigned char b) { return (push_uint64(b)); }428 CScript& operator<<(unsigned int b) { return (push_uint64(b)); }429 CScript& operator<<(unsigned short b) { return (push_uint64(b)); }430 CScript& operator<<(unsigned long b) { return (push_uint64(b)); }431 CScript& operator<<(uint64 b) { return (push_uint64(b)); }432433 CScript& operator<<(opcodetype opcode)434 {435 if (opcode <= OP_SINGLEBYTE_END)436 {437 insert(end(), (unsigned char)opcode);438 }439 else440 {441 assert(opcode >= OP_DOUBLEBYTE_BEGIN);442 insert(end(), (unsigned char)(opcode >> 8));443 insert(end(), (unsigned char)(opcode & 0xFF));444 }445 return (*this);446 }447448 CScript& operator<<(const uint160& b)449 {450 insert(end(), sizeof(b));451 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));452 return (*this);453 }454455 CScript& operator<<(const uint256& b)456 {457 insert(end(), sizeof(b));458 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));459 return (*this);460 }461462 CScript& operator<<(const CBigNum& b)463 {464 *this << b.getvch();465 return (*this);466 }467468 CScript& operator<<(const vector<unsigned char>& b)469 {470 if (b.size() < OP_PUSHDATA1)471 {472 insert(end(), (unsigned char)b.size());473 }474 else if (b.size() <= 0xff)475 {476 insert(end(), OP_PUSHDATA1);477 insert(end(), (unsigned char)b.size());478 }479 else480 {481 insert(end(), OP_PUSHDATA2);482 unsigned short nSize = b.size();483 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));484 }485 insert(end(), b.begin(), b.end());486 return (*this);487 }488489 CScript& operator<<(const CScript& b)490 {491 // I'm not sure if this should push the script or concatenate scripts.492 // If there's ever a use for pushing a script onto a script, delete this member fn493 assert(("warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate", false));494 return (*this);495 }496497498 bool GetOp(iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet)499 {500 // Wrapper so it can be called with either iterator or const_iterator501 const_iterator pc2 = pc;502 bool fRet = GetOp(pc2, opcodeRet, vchRet);503 pc = begin() + (pc2 - begin());504 return fRet;505 }506507 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet) const508 {509 opcodeRet = OP_INVALIDOPCODE;510 vchRet.clear();511 if (pc >= end())512 return false;513514 // Read instruction515 unsigned int opcode = *pc++;516 if (opcode >= OP_SINGLEBYTE_END)517 {518 if (pc + 1 > end())519 return false;520 opcode <<= 8;521 opcode |= *pc++;522 }523524 // Immediate operand525 if (opcode <= OP_PUSHDATA4)526 {527 unsigned int nSize = opcode;528 if (opcode == OP_PUSHDATA1)529 {530 if (pc + 1 > end())531 return false;532 nSize = *pc++;533 }534 else if (opcode == OP_PUSHDATA2)535 {536 if (pc + 2 > end())537 return false;538 nSize = 0;539 memcpy(&nSize, &pc[0], 2);540 pc += 2;541 }542 else if (opcode == OP_PUSHDATA4)543 {544 if (pc + 4 > end())545 return false;546 memcpy(&nSize, &pc[0], 4);547 pc += 4;548 }549 if (pc + nSize > end())550 return false;551 vchRet.assign(pc, pc + nSize);552 pc += nSize;553 }554555 opcodeRet = (opcodetype)opcode;556 return true;557 }558559560 void FindAndDelete(const CScript& b)561 {562 iterator pc = begin();563 opcodetype opcode;564 vector<unsigned char> vchPushValue;565 int count = 0;566 do567 {568 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)569 {570 erase(pc, pc + b.size());571 count++;572 }573 }574 while (GetOp(pc, opcode, vchPushValue));575 //printf("FindAndDeleted deleted %d items\n", count); /// debug576 }577578579 uint160 GetBitcoinAddressHash160() const580 {581 opcodetype opcode;582 vector<unsigned char> vch;583 CScript::const_iterator pc = begin();584 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;585 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;586 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;587 uint160 hash160 = uint160(vch);588 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;589 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;590 if (pc != end()) return 0;591 return hash160;592 }593594 string GetBitcoinAddress() const595 {596 uint160 hash160 = GetBitcoinAddressHash160();597 if (hash160 == 0)598 return "";599 return Hash160ToAddress(hash160);600 }601602 void SetBitcoinAddress(const uint160& hash160)603 {604 this->clear();605 *this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;606 }607608 void SetBitcoinAddress(const vector<unsigned char>& vchPubKey)609 {610 SetBitcoinAddress(Hash160(vchPubKey));611 }612613 bool SetBitcoinAddress(const string& strAddress)614 {615 this->clear();616 uint160 hash160;617 if (!AddressToHash160(strAddress, hash160))618 return false;619 SetBitcoinAddress(hash160);620 return true;621 }622623624 void PrintHex() const625 {626 printf("CScript(%s)\n", HexStr(begin(), end()).c_str());627 }628629 string ToString() const630 {631 string str;632 opcodetype opcode;633 vector<unsigned char> vch;634 const_iterator it = begin();635 while (GetOp(it, opcode, vch))636 {637 if (!str.empty())638 str += " ";639 if (opcode <= OP_PUSHDATA4)640 str += ValueString(vch);641 else642 str += GetOpName(opcode);643 }644 return str;645 }646647 void print() const648 {649 printf("%s\n", ToString().c_str());650 }651};652653654655656657658659660uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);661bool IsMine(const CScript& scriptPubKey);662bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector<unsigned char>& vchPubKeyRet);663bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);664bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());665bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);
Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.
