Research
Timechain
Search Donate
Sponsors
Cake Wallet, private Bitcoin and Monero walletSideShift.ai, no-signup crypto exchangeOrangeRock, non-custodial bitcoin spot and perps tradingBraiins Hashpower, buy and sell Bitcoin hashrateeCash.com
Bitcoin v0.1.0 · 2009-01-08

script.h

597 lines · plain text · MIT license (license.txt)
1// Copyright (c) 2009 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,138139140    // multi-byte opcodes141    OP_SINGLEBYTE_END = 0xF0,142    OP_DOUBLEBYTE_BEGIN = 0xF000,143144    // template matching params145    OP_PUBKEY,146    OP_PUBKEYHASH,147148149150    OP_INVALIDOPCODE = 0xFFFF,151};152153154155156157158159160inline const char* GetOpName(opcodetype opcode)161{162    switch (opcode)163    {164    // push value165    case OP_0                      : return "0";166    case OP_PUSHDATA1              : return "OP_PUSHDATA1";167    case OP_PUSHDATA2              : return "OP_PUSHDATA2";168    case OP_PUSHDATA4              : return "OP_PUSHDATA4";169    case OP_1NEGATE                : return "-1";170    case OP_RESERVED               : return "OP_RESERVED";171    case OP_1                      : return "1";172    case OP_2                      : return "2";173    case OP_3                      : return "3";174    case OP_4                      : return "4";175    case OP_5                      : return "5";176    case OP_6                      : return "6";177    case OP_7                      : return "7";178    case OP_8                      : return "8";179    case OP_9                      : return "9";180    case OP_10                     : return "10";181    case OP_11                     : return "11";182    case OP_12                     : return "12";183    case OP_13                     : return "13";184    case OP_14                     : return "14";185    case OP_15                     : return "15";186    case OP_16                     : return "16";187188    // control189    case OP_NOP                    : return "OP_NOP";190    case OP_VER                    : return "OP_VER";191    case OP_IF                     : return "OP_IF";192    case OP_NOTIF                  : return "OP_NOTIF";193    case OP_VERIF                  : return "OP_VERIF";194    case OP_VERNOTIF               : return "OP_VERNOTIF";195    case OP_ELSE                   : return "OP_ELSE";196    case OP_ENDIF                  : return "OP_ENDIF";197    case OP_VERIFY                 : return "OP_VERIFY";198    case OP_RETURN                 : return "OP_RETURN";199200    // stack ops201    case OP_TOALTSTACK             : return "OP_TOALTSTACK";202    case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";203    case OP_2DROP                  : return "OP_2DROP";204    case OP_2DUP                   : return "OP_2DUP";205    case OP_3DUP                   : return "OP_3DUP";206    case OP_2OVER                  : return "OP_2OVER";207    case OP_2ROT                   : return "OP_2ROT";208    case OP_2SWAP                  : return "OP_2SWAP";209    case OP_IFDUP                  : return "OP_IFDUP";210    case OP_DEPTH                  : return "OP_DEPTH";211    case OP_DROP                   : return "OP_DROP";212    case OP_DUP                    : return "OP_DUP";213    case OP_NIP                    : return "OP_NIP";214    case OP_OVER                   : return "OP_OVER";215    case OP_PICK                   : return "OP_PICK";216    case OP_ROLL                   : return "OP_ROLL";217    case OP_ROT                    : return "OP_ROT";218    case OP_SWAP                   : return "OP_SWAP";219    case OP_TUCK                   : return "OP_TUCK";220221    // splice ops222    case OP_CAT                    : return "OP_CAT";223    case OP_SUBSTR                 : return "OP_SUBSTR";224    case OP_LEFT                   : return "OP_LEFT";225    case OP_RIGHT                  : return "OP_RIGHT";226    case OP_SIZE                   : return "OP_SIZE";227228    // bit logic229    case OP_INVERT                 : return "OP_INVERT";230    case OP_AND                    : return "OP_AND";231    case OP_OR                     : return "OP_OR";232    case OP_XOR                    : return "OP_XOR";233    case OP_EQUAL                  : return "OP_EQUAL";234    case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";235    case OP_RESERVED1              : return "OP_RESERVED1";236    case OP_RESERVED2              : return "OP_RESERVED2";237238    // numeric239    case OP_1ADD                   : return "OP_1ADD";240    case OP_1SUB                   : return "OP_1SUB";241    case OP_2MUL                   : return "OP_2MUL";242    case OP_2DIV                   : return "OP_2DIV";243    case OP_NEGATE                 : return "OP_NEGATE";244    case OP_ABS                    : return "OP_ABS";245    case OP_NOT                    : return "OP_NOT";246    case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";247    case OP_ADD                    : return "OP_ADD";248    case OP_SUB                    : return "OP_SUB";249    case OP_MUL                    : return "OP_MUL";250    case OP_DIV                    : return "OP_DIV";251    case OP_MOD                    : return "OP_MOD";252    case OP_LSHIFT                 : return "OP_LSHIFT";253    case OP_RSHIFT                 : return "OP_RSHIFT";254    case OP_BOOLAND                : return "OP_BOOLAND";255    case OP_BOOLOR                 : return "OP_BOOLOR";256    case OP_NUMEQUAL               : return "OP_NUMEQUAL";257    case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";258    case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";259    case OP_LESSTHAN               : return "OP_LESSTHAN";260    case OP_GREATERTHAN            : return "OP_GREATERTHAN";261    case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";262    case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";263    case OP_MIN                    : return "OP_MIN";264    case OP_MAX                    : return "OP_MAX";265    case OP_WITHIN                 : return "OP_WITHIN";266267    // crypto268    case OP_RIPEMD160              : return "OP_RIPEMD160";269    case OP_SHA1                   : return "OP_SHA1";270    case OP_SHA256                 : return "OP_SHA256";271    case OP_HASH160                : return "OP_HASH160";272    case OP_HASH256                : return "OP_HASH256";273    case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";274    case OP_CHECKSIG               : return "OP_CHECKSIG";275    case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";276    case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";277    case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";278279280281    // multi-byte opcodes282    case OP_SINGLEBYTE_END         : return "OP_SINGLEBYTE_END";283    case OP_DOUBLEBYTE_BEGIN       : return "OP_DOUBLEBYTE_BEGIN";284    case OP_PUBKEY                 : return "OP_PUBKEY";285    case OP_PUBKEYHASH             : return "OP_PUBKEYHASH";286287288289    case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";290    default:291        return "UNKNOWN_OPCODE";292    }293};294295296297298inline string ValueString(const vector<unsigned char>& vch)299{300    if (vch.size() <= 4)301        return strprintf("%d", CBigNum(vch).getint());302    else303        return HexNumStr(vch.begin(), vch.end());304        //return string("(") + HexStr(vch.begin(), vch.end()) + string(")");305}306307inline string StackString(const vector<vector<unsigned char> >& vStack)308{309    string str;310    foreach(const vector<unsigned char>& vch, vStack)311    {312        if (!str.empty())313            str += " ";314        str += ValueString(vch);315    }316    return str;317}318319320321322323324325326327class CScript : public vector<unsigned char>328{329protected:330    CScript& push_int64(int64 n)331    {332        if (n == -1 || (n >= 1 && n <= 16))333        {334            push_back(n + (OP_1 - 1));335        }336        else337        {338            CBigNum bn(n);339            *this << bn.getvch();340        }341        return (*this);342    }343344    CScript& push_uint64(uint64 n)345    {346        if (n == -1 || (n >= 1 && n <= 16))347        {348            push_back(n + (OP_1 - 1));349        }350        else351        {352            CBigNum bn(n);353            *this << bn.getvch();354        }355        return (*this);356    }357358public:359    CScript() { }360    CScript(const CScript& b) : vector<unsigned char>(b.begin(), b.end()) { }361    CScript(const_iterator pbegin, const_iterator pend) : vector<unsigned char>(pbegin, pend) { }362#ifndef _MSC_VER363    CScript(const unsigned char* pbegin, const unsigned char* pend) : vector<unsigned char>(pbegin, pend) { }364#endif365366    CScript& operator+=(const CScript& b)367    {368        insert(end(), b.begin(), b.end());369        return *this;370    }371372    friend CScript operator+(const CScript& a, const CScript& b)373    {374        CScript ret = a;375        ret += b;376        return (ret);377    }378379380    explicit CScript(char b)           { operator<<(b); }381    explicit CScript(short b)          { operator<<(b); }382    explicit CScript(int b)            { operator<<(b); }383    explicit CScript(long b)           { operator<<(b); }384    explicit CScript(int64 b)          { operator<<(b); }385    explicit CScript(unsigned char b)  { operator<<(b); }386    explicit CScript(unsigned int b)   { operator<<(b); }387    explicit CScript(unsigned short b) { operator<<(b); }388    explicit CScript(unsigned long b)  { operator<<(b); }389    explicit CScript(uint64 b)         { operator<<(b); }390391    explicit CScript(opcodetype b)     { operator<<(b); }392    explicit CScript(const uint256& b) { operator<<(b); }393    explicit CScript(const CBigNum& b) { operator<<(b); }394    explicit CScript(const vector<unsigned char>& b) { operator<<(b); }395396397    CScript& operator<<(char b)           { return (push_int64(b)); }398    CScript& operator<<(short b)          { return (push_int64(b)); }399    CScript& operator<<(int b)            { return (push_int64(b)); }400    CScript& operator<<(long b)           { return (push_int64(b)); }401    CScript& operator<<(int64 b)          { return (push_int64(b)); }402    CScript& operator<<(unsigned char b)  { return (push_uint64(b)); }403    CScript& operator<<(unsigned int b)   { return (push_uint64(b)); }404    CScript& operator<<(unsigned short b) { return (push_uint64(b)); }405    CScript& operator<<(unsigned long b)  { return (push_uint64(b)); }406    CScript& operator<<(uint64 b)         { return (push_uint64(b)); }407408    CScript& operator<<(opcodetype opcode)409    {410        if (opcode <= OP_SINGLEBYTE_END)411        {412            insert(end(), (unsigned char)opcode);413        }414        else415        {416            assert(opcode >= OP_DOUBLEBYTE_BEGIN);417            insert(end(), (unsigned char)(opcode >> 8));418            insert(end(), (unsigned char)(opcode & 0xFF));419        }420        return (*this);421    }422423    CScript& operator<<(const uint160& b)424    {425        insert(end(), sizeof(b));426        insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));427        return (*this);428    }429430    CScript& operator<<(const uint256& b)431    {432        insert(end(), sizeof(b));433        insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));434        return (*this);435    }436437    CScript& operator<<(const CBigNum& b)438    {439        *this << b.getvch();440        return (*this);441    }442443    CScript& operator<<(const vector<unsigned char>& b)444    {445        if (b.size() < OP_PUSHDATA1)446        {447            insert(end(), (unsigned char)b.size());448        }449        else if (b.size() <= 0xff)450        {451            insert(end(), OP_PUSHDATA1);452            insert(end(), (unsigned char)b.size());453        }454        else455        {456            insert(end(), OP_PUSHDATA2);457            unsigned short nSize = b.size();458            insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));459        }460        insert(end(), b.begin(), b.end());461        return (*this);462    }463464    CScript& operator<<(const CScript& b)465    {466        // I'm not sure if this should push the script or concatenate scripts.467        // If there's ever a use for pushing a script onto a script, delete this member fn468        assert(("warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate", false));469        return (*this);470    }471472473    bool GetOp(iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet)474    {475         // This is why people hate C++476         const_iterator pc2 = pc;477         bool fRet = GetOp(pc2, opcodeRet, vchRet);478         pc = begin() + (pc2 - begin());479         return fRet;480    }481482    bool GetOp(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet) const483    {484        opcodeRet = OP_INVALIDOPCODE;485        vchRet.clear();486        if (pc >= end())487            return false;488489        // Read instruction490        unsigned int opcode = *pc++;491        if (opcode >= OP_SINGLEBYTE_END)492        {493            if (pc + 1 > end())494                return false;495            opcode <<= 8;496            opcode |= *pc++;497        }498499        // Immediate operand500        if (opcode <= OP_PUSHDATA4)501        {502            unsigned int nSize = opcode;503            if (opcode == OP_PUSHDATA1)504            {505                if (pc + 1 > end())506                    return false;507                nSize = *pc++;508            }509            else if (opcode == OP_PUSHDATA2)510            {511                if (pc + 2 > end())512                    return false;513                nSize = 0;514                memcpy(&nSize, &pc[0], 2);515                pc += 2;516            }517            else if (opcode == OP_PUSHDATA4)518            {519                if (pc + 4 > end())520                    return false;521                memcpy(&nSize, &pc[0], 4);522                pc += 4;523            }524            if (pc + nSize > end())525                return false;526            vchRet.assign(pc, pc + nSize);527            pc += nSize;528        }529530        opcodeRet = (opcodetype)opcode;531        return true;532    }533534535    void FindAndDelete(const CScript& b)536    {537        iterator pc = begin();538        opcodetype opcode;539        vector<unsigned char> vchPushValue;540        int count = 0;541        do542        {543            while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)544            {545                erase(pc, pc + b.size());546                count++;547            }548        }549        while (GetOp(pc, opcode, vchPushValue));550        //printf("FindAndDeleted deleted %d items\n", count); /// debug551    }552553554    void PrintHex() const555    {556        printf("CScript(%s)\n", HexStr(begin(), end()).c_str());557    }558559    string ToString() const560    {561        string str;562        opcodetype opcode;563        vector<unsigned char> vch;564        const_iterator it = begin();565        while (GetOp(it, opcode, vch))566        {567            if (!str.empty())568                str += " ";569            if (opcode <= OP_PUSHDATA4)570                str += ValueString(vch);571            else572                str += GetOpName(opcode);573        }574        return str;575    }576577    void print() const578    {579        printf("%s\n", ToString().c_str());580    }581};582583584585586587588589590bool EvalScript(const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType=0,591                vector<vector<unsigned char> >* pvStackRet=NULL);592uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);593bool IsMine(const CScript& scriptPubKey);594bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector<unsigned char>& vchPubKeyRet);595bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);596bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());597bool 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.

0:000:00