File indexing completed on 2020-06-25 15:45:43
0001
0002
0003
0004
0005 #ifndef H_BITCOIN_SCRIPT
0006 #define H_BITCOIN_SCRIPT
0007
0008 #include "base58.h"
0009 #include "keystore.h"
0010
0011 #include <string>
0012 #include <vector>
0013
0014 #include <boost/foreach.hpp>
0015
0016 class CTransaction;
0017
0018 enum
0019 {
0020 SIGHASH_ALL = 1,
0021 SIGHASH_NONE = 2,
0022 SIGHASH_SINGLE = 3,
0023 SIGHASH_ANYONECANPAY = 0x80,
0024 };
0025
0026
0027
0028 enum opcodetype
0029 {
0030
0031 OP_0=0,
0032 OP_FALSE=OP_0,
0033 OP_PUSHDATA1=76,
0034 OP_PUSHDATA2,
0035 OP_PUSHDATA4,
0036 OP_1NEGATE,
0037 OP_RESERVED,
0038 OP_1,
0039 OP_TRUE=OP_1,
0040 OP_2,
0041 OP_3,
0042 OP_4,
0043 OP_5,
0044 OP_6,
0045 OP_7,
0046 OP_8,
0047 OP_9,
0048 OP_10,
0049 OP_11,
0050 OP_12,
0051 OP_13,
0052 OP_14,
0053 OP_15,
0054 OP_16,
0055
0056
0057 OP_NOP,
0058 OP_VER,
0059 OP_IF,
0060 OP_NOTIF,
0061 OP_VERIF,
0062 OP_VERNOTIF,
0063 OP_ELSE,
0064 OP_ENDIF,
0065 OP_VERIFY,
0066 OP_RETURN,
0067
0068
0069 OP_TOALTSTACK,
0070 OP_FROMALTSTACK,
0071 OP_2DROP,
0072 OP_2DUP,
0073 OP_3DUP,
0074 OP_2OVER,
0075 OP_2ROT,
0076 OP_2SWAP,
0077 OP_IFDUP,
0078 OP_DEPTH,
0079 OP_DROP,
0080 OP_DUP,
0081 OP_NIP,
0082 OP_OVER,
0083 OP_PICK,
0084 OP_ROLL,
0085 OP_ROT,
0086 OP_SWAP,
0087 OP_TUCK,
0088
0089
0090 OP_CAT,
0091 OP_SUBSTR,
0092 OP_LEFT,
0093 OP_RIGHT,
0094 OP_SIZE,
0095
0096
0097 OP_INVERT,
0098 OP_AND,
0099 OP_OR,
0100 OP_XOR,
0101 OP_EQUAL,
0102 OP_EQUALVERIFY,
0103 OP_RESERVED1,
0104 OP_RESERVED2,
0105
0106
0107 OP_1ADD,
0108 OP_1SUB,
0109 OP_2MUL,
0110 OP_2DIV,
0111 OP_NEGATE,
0112 OP_ABS,
0113 OP_NOT,
0114 OP_0NOTEQUAL,
0115
0116 OP_ADD,
0117 OP_SUB,
0118 OP_MUL,
0119 OP_DIV,
0120 OP_MOD,
0121 OP_LSHIFT,
0122 OP_RSHIFT,
0123
0124 OP_BOOLAND,
0125 OP_BOOLOR,
0126 OP_NUMEQUAL,
0127 OP_NUMEQUALVERIFY,
0128 OP_NUMNOTEQUAL,
0129 OP_LESSTHAN,
0130 OP_GREATERTHAN,
0131 OP_LESSTHANOREQUAL,
0132 OP_GREATERTHANOREQUAL,
0133 OP_MIN,
0134 OP_MAX,
0135
0136 OP_WITHIN,
0137
0138
0139 OP_RIPEMD160,
0140 OP_SHA1,
0141 OP_SHA256,
0142 OP_HASH160,
0143 OP_HASH256,
0144 OP_CODESEPARATOR,
0145 OP_CHECKSIG,
0146 OP_CHECKSIGVERIFY,
0147 OP_CHECKMULTISIG,
0148 OP_CHECKMULTISIGVERIFY,
0149
0150
0151 OP_NOP1,
0152 OP_NOP2,
0153 OP_NOP3,
0154 OP_NOP4,
0155 OP_NOP5,
0156 OP_NOP6,
0157 OP_NOP7,
0158 OP_NOP8,
0159 OP_NOP9,
0160 OP_NOP10,
0161
0162
0163
0164
0165 OP_PUBKEYHASH = 0xfd,
0166 OP_PUBKEY = 0xfe,
0167
0168 OP_INVALIDOPCODE = 0xff,
0169 };
0170
0171
0172
0173
0174
0175
0176
0177
0178 inline const char* GetOpName(opcodetype opcode)
0179 {
0180 switch (opcode)
0181 {
0182
0183 case OP_0 : return "0";
0184 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
0185 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
0186 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
0187 case OP_1NEGATE : return "-1";
0188 case OP_RESERVED : return "OP_RESERVED";
0189 case OP_1 : return "1";
0190 case OP_2 : return "2";
0191 case OP_3 : return "3";
0192 case OP_4 : return "4";
0193 case OP_5 : return "5";
0194 case OP_6 : return "6";
0195 case OP_7 : return "7";
0196 case OP_8 : return "8";
0197 case OP_9 : return "9";
0198 case OP_10 : return "10";
0199 case OP_11 : return "11";
0200 case OP_12 : return "12";
0201 case OP_13 : return "13";
0202 case OP_14 : return "14";
0203 case OP_15 : return "15";
0204 case OP_16 : return "16";
0205
0206
0207 case OP_NOP : return "OP_NOP";
0208 case OP_VER : return "OP_VER";
0209 case OP_IF : return "OP_IF";
0210 case OP_NOTIF : return "OP_NOTIF";
0211 case OP_VERIF : return "OP_VERIF";
0212 case OP_VERNOTIF : return "OP_VERNOTIF";
0213 case OP_ELSE : return "OP_ELSE";
0214 case OP_ENDIF : return "OP_ENDIF";
0215 case OP_VERIFY : return "OP_VERIFY";
0216 case OP_RETURN : return "OP_RETURN";
0217
0218
0219 case OP_TOALTSTACK : return "OP_TOALTSTACK";
0220 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
0221 case OP_2DROP : return "OP_2DROP";
0222 case OP_2DUP : return "OP_2DUP";
0223 case OP_3DUP : return "OP_3DUP";
0224 case OP_2OVER : return "OP_2OVER";
0225 case OP_2ROT : return "OP_2ROT";
0226 case OP_2SWAP : return "OP_2SWAP";
0227 case OP_IFDUP : return "OP_IFDUP";
0228 case OP_DEPTH : return "OP_DEPTH";
0229 case OP_DROP : return "OP_DROP";
0230 case OP_DUP : return "OP_DUP";
0231 case OP_NIP : return "OP_NIP";
0232 case OP_OVER : return "OP_OVER";
0233 case OP_PICK : return "OP_PICK";
0234 case OP_ROLL : return "OP_ROLL";
0235 case OP_ROT : return "OP_ROT";
0236 case OP_SWAP : return "OP_SWAP";
0237 case OP_TUCK : return "OP_TUCK";
0238
0239
0240 case OP_CAT : return "OP_CAT";
0241 case OP_SUBSTR : return "OP_SUBSTR";
0242 case OP_LEFT : return "OP_LEFT";
0243 case OP_RIGHT : return "OP_RIGHT";
0244 case OP_SIZE : return "OP_SIZE";
0245
0246
0247 case OP_INVERT : return "OP_INVERT";
0248 case OP_AND : return "OP_AND";
0249 case OP_OR : return "OP_OR";
0250 case OP_XOR : return "OP_XOR";
0251 case OP_EQUAL : return "OP_EQUAL";
0252 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
0253 case OP_RESERVED1 : return "OP_RESERVED1";
0254 case OP_RESERVED2 : return "OP_RESERVED2";
0255
0256
0257 case OP_1ADD : return "OP_1ADD";
0258 case OP_1SUB : return "OP_1SUB";
0259 case OP_2MUL : return "OP_2MUL";
0260 case OP_2DIV : return "OP_2DIV";
0261 case OP_NEGATE : return "OP_NEGATE";
0262 case OP_ABS : return "OP_ABS";
0263 case OP_NOT : return "OP_NOT";
0264 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
0265 case OP_ADD : return "OP_ADD";
0266 case OP_SUB : return "OP_SUB";
0267 case OP_MUL : return "OP_MUL";
0268 case OP_DIV : return "OP_DIV";
0269 case OP_MOD : return "OP_MOD";
0270 case OP_LSHIFT : return "OP_LSHIFT";
0271 case OP_RSHIFT : return "OP_RSHIFT";
0272 case OP_BOOLAND : return "OP_BOOLAND";
0273 case OP_BOOLOR : return "OP_BOOLOR";
0274 case OP_NUMEQUAL : return "OP_NUMEQUAL";
0275 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
0276 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
0277 case OP_LESSTHAN : return "OP_LESSTHAN";
0278 case OP_GREATERTHAN : return "OP_GREATERTHAN";
0279 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
0280 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
0281 case OP_MIN : return "OP_MIN";
0282 case OP_MAX : return "OP_MAX";
0283 case OP_WITHIN : return "OP_WITHIN";
0284
0285
0286 case OP_RIPEMD160 : return "OP_RIPEMD160";
0287 case OP_SHA1 : return "OP_SHA1";
0288 case OP_SHA256 : return "OP_SHA256";
0289 case OP_HASH160 : return "OP_HASH160";
0290 case OP_HASH256 : return "OP_HASH256";
0291 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
0292 case OP_CHECKSIG : return "OP_CHECKSIG";
0293 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
0294 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
0295 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
0296
0297
0298 case OP_NOP1 : return "OP_NOP1";
0299 case OP_NOP2 : return "OP_NOP2";
0300 case OP_NOP3 : return "OP_NOP3";
0301 case OP_NOP4 : return "OP_NOP4";
0302 case OP_NOP5 : return "OP_NOP5";
0303 case OP_NOP6 : return "OP_NOP6";
0304 case OP_NOP7 : return "OP_NOP7";
0305 case OP_NOP8 : return "OP_NOP8";
0306 case OP_NOP9 : return "OP_NOP9";
0307 case OP_NOP10 : return "OP_NOP10";
0308
0309
0310
0311
0312 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
0313 case OP_PUBKEY : return "OP_PUBKEY";
0314
0315 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
0316 default:
0317 return "OP_UNKNOWN";
0318 }
0319 };
0320
0321
0322
0323
0324 inline std::string ValueString(const std::vector<unsigned char>& vch)
0325 {
0326 if (vch.size() <= 4)
0327 return strprintf("%d", CBigNum(vch).getint());
0328 else
0329 return HexStr(vch);
0330 }
0331
0332 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
0333 {
0334 std::string str;
0335 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
0336 {
0337 if (!str.empty())
0338 str += " ";
0339 str += ValueString(vch);
0340 }
0341 return str;
0342 }
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352 class CScript : public std::vector<unsigned char>
0353 {
0354 protected:
0355 CScript& push_int64(int64 n)
0356 {
0357 if (n == -1 || (n >= 1 && n <= 16))
0358 {
0359 push_back(n + (OP_1 - 1));
0360 }
0361 else
0362 {
0363 CBigNum bn(n);
0364 *this << bn.getvch();
0365 }
0366 return *this;
0367 }
0368
0369 CScript& push_uint64(uint64 n)
0370 {
0371 if (n >= 1 && n <= 16)
0372 {
0373 push_back(n + (OP_1 - 1));
0374 }
0375 else
0376 {
0377 CBigNum bn(n);
0378 *this << bn.getvch();
0379 }
0380 return *this;
0381 }
0382
0383 public:
0384 CScript() { }
0385 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
0386 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
0387 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
0388
0389 CScript& operator+=(const CScript& b)
0390 {
0391 insert(end(), b.begin(), b.end());
0392 return *this;
0393 }
0394
0395 friend CScript operator+(const CScript& a, const CScript& b)
0396 {
0397 CScript ret = a;
0398 ret += b;
0399 return ret;
0400 }
0401
0402
0403 explicit CScript(char b) { operator<<(b); }
0404 explicit CScript(short b) { operator<<(b); }
0405 explicit CScript(int b) { operator<<(b); }
0406 explicit CScript(long b) { operator<<(b); }
0407 explicit CScript(int64 b) { operator<<(b); }
0408 explicit CScript(unsigned char b) { operator<<(b); }
0409 explicit CScript(unsigned int b) { operator<<(b); }
0410 explicit CScript(unsigned short b) { operator<<(b); }
0411 explicit CScript(unsigned long b) { operator<<(b); }
0412 explicit CScript(uint64 b) { operator<<(b); }
0413
0414 explicit CScript(opcodetype b) { operator<<(b); }
0415 explicit CScript(const uint256& b) { operator<<(b); }
0416 explicit CScript(const CBigNum& b) { operator<<(b); }
0417 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
0418
0419
0420 CScript& operator<<(char b) { return push_int64(b); }
0421 CScript& operator<<(short b) { return push_int64(b); }
0422 CScript& operator<<(int b) { return push_int64(b); }
0423 CScript& operator<<(long b) { return push_int64(b); }
0424 CScript& operator<<(int64 b) { return push_int64(b); }
0425 CScript& operator<<(unsigned char b) { return push_uint64(b); }
0426 CScript& operator<<(unsigned int b) { return push_uint64(b); }
0427 CScript& operator<<(unsigned short b) { return push_uint64(b); }
0428 CScript& operator<<(unsigned long b) { return push_uint64(b); }
0429 CScript& operator<<(uint64 b) { return push_uint64(b); }
0430
0431 CScript& operator<<(opcodetype opcode)
0432 {
0433 if (opcode < 0 || opcode > 0xff)
0434 throw std::runtime_error("CScript::operator<<() : invalid opcode");
0435 insert(end(), (unsigned char)opcode);
0436 return *this;
0437 }
0438
0439 CScript& operator<<(const uint160& b)
0440 {
0441 insert(end(), sizeof(b));
0442 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
0443 return *this;
0444 }
0445
0446 CScript& operator<<(const uint256& b)
0447 {
0448 insert(end(), sizeof(b));
0449 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
0450 return *this;
0451 }
0452
0453 CScript& operator<<(const CBigNum& b)
0454 {
0455 *this << b.getvch();
0456 return *this;
0457 }
0458
0459 CScript& operator<<(const std::vector<unsigned char>& b)
0460 {
0461 if (b.size() < OP_PUSHDATA1)
0462 {
0463 insert(end(), (unsigned char)b.size());
0464 }
0465 else if (b.size() <= 0xff)
0466 {
0467 insert(end(), OP_PUSHDATA1);
0468 insert(end(), (unsigned char)b.size());
0469 }
0470 else if (b.size() <= 0xffff)
0471 {
0472 insert(end(), OP_PUSHDATA2);
0473 unsigned short nSize = b.size();
0474 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
0475 }
0476 else
0477 {
0478 insert(end(), OP_PUSHDATA4);
0479 unsigned int nSize = b.size();
0480 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
0481 }
0482 insert(end(), b.begin(), b.end());
0483 return *this;
0484 }
0485
0486 CScript& operator<<(const CScript& b)
0487 {
0488
0489
0490 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
0491 return *this;
0492 }
0493
0494
0495 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
0496 {
0497
0498 const_iterator pc2 = pc;
0499 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
0500 pc = begin() + (pc2 - begin());
0501 return fRet;
0502 }
0503
0504 bool GetOp(iterator& pc, opcodetype& opcodeRet)
0505 {
0506 const_iterator pc2 = pc;
0507 bool fRet = GetOp2(pc2, opcodeRet, NULL);
0508 pc = begin() + (pc2 - begin());
0509 return fRet;
0510 }
0511
0512 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
0513 {
0514 return GetOp2(pc, opcodeRet, &vchRet);
0515 }
0516
0517 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
0518 {
0519 return GetOp2(pc, opcodeRet, NULL);
0520 }
0521
0522 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
0523 {
0524 opcodeRet = OP_INVALIDOPCODE;
0525 if (pvchRet)
0526 pvchRet->clear();
0527 if (pc >= end())
0528 return false;
0529
0530
0531 if (end() - pc < 1)
0532 return false;
0533 unsigned int opcode = *pc++;
0534
0535
0536 if (opcode <= OP_PUSHDATA4)
0537 {
0538 unsigned int nSize;
0539 if (opcode < OP_PUSHDATA1)
0540 {
0541 nSize = opcode;
0542 }
0543 else if (opcode == OP_PUSHDATA1)
0544 {
0545 if (end() - pc < 1)
0546 return false;
0547 nSize = *pc++;
0548 }
0549 else if (opcode == OP_PUSHDATA2)
0550 {
0551 if (end() - pc < 2)
0552 return false;
0553 nSize = 0;
0554 memcpy(&nSize, &pc[0], 2);
0555 pc += 2;
0556 }
0557 else if (opcode == OP_PUSHDATA4)
0558 {
0559 if (end() - pc < 4)
0560 return false;
0561 memcpy(&nSize, &pc[0], 4);
0562 pc += 4;
0563 }
0564 if (end() - pc < nSize)
0565 return false;
0566 if (pvchRet)
0567 pvchRet->assign(pc, pc + nSize);
0568 pc += nSize;
0569 }
0570
0571 opcodeRet = (opcodetype)opcode;
0572 return true;
0573 }
0574
0575
0576 void FindAndDelete(const CScript& b)
0577 {
0578 if (b.empty())
0579 return;
0580 iterator pc = begin();
0581 opcodetype opcode;
0582 do
0583 {
0584 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
0585 erase(pc, pc + b.size());
0586 }
0587 while (GetOp(pc, opcode));
0588 }
0589
0590
0591 int GetSigOpCount() const
0592 {
0593 int n = 0;
0594 const_iterator pc = begin();
0595 while (pc < end())
0596 {
0597 opcodetype opcode;
0598 if (!GetOp(pc, opcode))
0599 break;
0600 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
0601 n++;
0602 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
0603 n += 20;
0604 }
0605 return n;
0606 }
0607
0608
0609 bool IsPushOnly() const
0610 {
0611 if (size() > 200)
0612 return false;
0613 const_iterator pc = begin();
0614 while (pc < end())
0615 {
0616 opcodetype opcode;
0617 if (!GetOp(pc, opcode))
0618 return false;
0619 if (opcode > OP_16)
0620 return false;
0621 }
0622 return true;
0623 }
0624
0625
0626 CBitcoinAddress GetBitcoinAddress() const
0627 {
0628 opcodetype opcode;
0629 std::vector<unsigned char> vch;
0630 CScript::const_iterator pc = begin();
0631 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
0632 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
0633 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
0634 uint160 hash160 = uint160(vch);
0635 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
0636 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
0637 if (pc != end()) return 0;
0638 return CBitcoinAddress(hash160);
0639 }
0640
0641 void SetBitcoinAddress(const CBitcoinAddress& address)
0642 {
0643 this->clear();
0644 *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
0645 }
0646
0647 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
0648 {
0649 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
0650 }
0651
0652
0653 void PrintHex() const
0654 {
0655 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
0656 }
0657
0658 std::string ToString() const
0659 {
0660 std::string str;
0661 opcodetype opcode;
0662 std::vector<unsigned char> vch;
0663 const_iterator pc = begin();
0664 while (pc < end())
0665 {
0666 if (!str.empty())
0667 str += " ";
0668 if (!GetOp(pc, opcode, vch))
0669 {
0670 str += "[error]";
0671 return str;
0672 }
0673 if (0 <= opcode && opcode <= OP_PUSHDATA4)
0674 str += ValueString(vch);
0675 else
0676 str += GetOpName(opcode);
0677 }
0678 return str;
0679 }
0680
0681 void print() const
0682 {
0683 printf("%s\n", ToString().c_str());
0684 }
0685 };
0686
0687
0688
0689
0690
0691
0692
0693 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
0694
0695 bool IsStandard(const CScript& scriptPubKey);
0696 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
0697 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
0698 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
0699 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);
0700
0701 #endif