genesis 1
genesis 2
genesis 3
genesis 4
genesis 5 #ifndef H_BITCOIN_SCRIPT
genesis 6 #define H_BITCOIN_SCRIPT
genesis 7
genesis 8 #include "base58.h"
genesis 9 #include "keystore.h"
genesis 10
genesis 11 #include <string>
genesis 12 #include <vector>
genesis 13
genesis 14 #include <boost/foreach.hpp>
genesis 15
genesis 16 class CTransaction;
genesis 17
genesis 18 enum
genesis 19 {
genesis 20 SIGHASH_ALL = 1,
genesis 21 SIGHASH_NONE = 2,
genesis 22 SIGHASH_SINGLE = 3,
genesis 23 SIGHASH_ANYONECANPAY = 0x80,
genesis 24 };
genesis 25
genesis 26
genesis 27
genesis 28 enum opcodetype
genesis 29 {
genesis 30
genesis 31 OP_0=0,
genesis 32 OP_FALSE=OP_0,
genesis 33 OP_PUSHDATA1=76,
genesis 34 OP_PUSHDATA2,
genesis 35 OP_PUSHDATA4,
genesis 36 OP_1NEGATE,
genesis 37 OP_RESERVED,
genesis 38 OP_1,
genesis 39 OP_TRUE=OP_1,
genesis 40 OP_2,
genesis 41 OP_3,
genesis 42 OP_4,
genesis 43 OP_5,
genesis 44 OP_6,
genesis 45 OP_7,
genesis 46 OP_8,
genesis 47 OP_9,
genesis 48 OP_10,
genesis 49 OP_11,
genesis 50 OP_12,
genesis 51 OP_13,
genesis 52 OP_14,
genesis 53 OP_15,
genesis 54 OP_16,
genesis 55
genesis 56
genesis 57 OP_NOP,
genesis 58 OP_VER,
genesis 59 OP_IF,
genesis 60 OP_NOTIF,
genesis 61 OP_VERIF,
genesis 62 OP_VERNOTIF,
genesis 63 OP_ELSE,
genesis 64 OP_ENDIF,
genesis 65 OP_VERIFY,
genesis 66 OP_RETURN,
genesis 67
genesis 68
genesis 69 OP_TOALTSTACK,
genesis 70 OP_FROMALTSTACK,
genesis 71 OP_2DROP,
genesis 72 OP_2DUP,
genesis 73 OP_3DUP,
genesis 74 OP_2OVER,
genesis 75 OP_2ROT,
genesis 76 OP_2SWAP,
genesis 77 OP_IFDUP,
genesis 78 OP_DEPTH,
genesis 79 OP_DROP,
genesis 80 OP_DUP,
genesis 81 OP_NIP,
genesis 82 OP_OVER,
genesis 83 OP_PICK,
genesis 84 OP_ROLL,
genesis 85 OP_ROT,
genesis 86 OP_SWAP,
genesis 87 OP_TUCK,
genesis 88
genesis 89
genesis 90 OP_CAT,
genesis 91 OP_SUBSTR,
genesis 92 OP_LEFT,
genesis 93 OP_RIGHT,
genesis 94 OP_SIZE,
genesis 95
genesis 96
genesis 97 OP_INVERT,
genesis 98 OP_AND,
genesis 99 OP_OR,
genesis 100 OP_XOR,
genesis 101 OP_EQUAL,
genesis 102 OP_EQUALVERIFY,
genesis 103 OP_RESERVED1,
genesis 104 OP_RESERVED2,
genesis 105
genesis 106
genesis 107 OP_1ADD,
genesis 108 OP_1SUB,
genesis 109 OP_2MUL,
genesis 110 OP_2DIV,
genesis 111 OP_NEGATE,
genesis 112 OP_ABS,
genesis 113 OP_NOT,
genesis 114 OP_0NOTEQUAL,
genesis 115
genesis 116 OP_ADD,
genesis 117 OP_SUB,
genesis 118 OP_MUL,
genesis 119 OP_DIV,
genesis 120 OP_MOD,
genesis 121 OP_LSHIFT,
genesis 122 OP_RSHIFT,
genesis 123
genesis 124 OP_BOOLAND,
genesis 125 OP_BOOLOR,
genesis 126 OP_NUMEQUAL,
genesis 127 OP_NUMEQUALVERIFY,
genesis 128 OP_NUMNOTEQUAL,
genesis 129 OP_LESSTHAN,
genesis 130 OP_GREATERTHAN,
genesis 131 OP_LESSTHANOREQUAL,
genesis 132 OP_GREATERTHANOREQUAL,
genesis 133 OP_MIN,
genesis 134 OP_MAX,
genesis 135
genesis 136 OP_WITHIN,
genesis 137
genesis 138
genesis 139 OP_RIPEMD160,
genesis 140 OP_SHA1,
genesis 141 OP_SHA256,
genesis 142 OP_HASH160,
genesis 143 OP_HASH256,
genesis 144 OP_CODESEPARATOR,
genesis 145 OP_CHECKSIG,
genesis 146 OP_CHECKSIGVERIFY,
genesis 147 OP_CHECKMULTISIG,
genesis 148 OP_CHECKMULTISIGVERIFY,
genesis 149
genesis 150
genesis 151 OP_NOP1,
genesis 152 OP_NOP2,
genesis 153 OP_NOP3,
genesis 154 OP_NOP4,
genesis 155 OP_NOP5,
genesis 156 OP_NOP6,
genesis 157 OP_NOP7,
genesis 158 OP_NOP8,
genesis 159 OP_NOP9,
genesis 160 OP_NOP10,
genesis 161
genesis 162
genesis 163
genesis 164
genesis 165 OP_PUBKEYHASH = 0xfd,
genesis 166 OP_PUBKEY = 0xfe,
genesis 167
genesis 168 OP_INVALIDOPCODE = 0xff,
genesis 169 };
genesis 170
genesis 171
genesis 172
genesis 173
genesis 174
genesis 175
genesis 176
genesis 177
genesis 178 inline const char* GetOpName(opcodetype opcode)
genesis 179 {
genesis 180 switch (opcode)
genesis 181 {
genesis 182
genesis 183 case OP_0 : return "0";
genesis 184 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
genesis 185 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
genesis 186 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
genesis 187 case OP_1NEGATE : return "-1";
genesis 188 case OP_RESERVED : return "OP_RESERVED";
genesis 189 case OP_1 : return "1";
genesis 190 case OP_2 : return "2";
genesis 191 case OP_3 : return "3";
genesis 192 case OP_4 : return "4";
genesis 193 case OP_5 : return "5";
genesis 194 case OP_6 : return "6";
genesis 195 case OP_7 : return "7";
genesis 196 case OP_8 : return "8";
genesis 197 case OP_9 : return "9";
genesis 198 case OP_10 : return "10";
genesis 199 case OP_11 : return "11";
genesis 200 case OP_12 : return "12";
genesis 201 case OP_13 : return "13";
genesis 202 case OP_14 : return "14";
genesis 203 case OP_15 : return "15";
genesis 204 case OP_16 : return "16";
genesis 205
genesis 206
genesis 207 case OP_NOP : return "OP_NOP";
genesis 208 case OP_VER : return "OP_VER";
genesis 209 case OP_IF : return "OP_IF";
genesis 210 case OP_NOTIF : return "OP_NOTIF";
genesis 211 case OP_VERIF : return "OP_VERIF";
genesis 212 case OP_VERNOTIF : return "OP_VERNOTIF";
genesis 213 case OP_ELSE : return "OP_ELSE";
genesis 214 case OP_ENDIF : return "OP_ENDIF";
genesis 215 case OP_VERIFY : return "OP_VERIFY";
genesis 216 case OP_RETURN : return "OP_RETURN";
genesis 217
genesis 218
genesis 219 case OP_TOALTSTACK : return "OP_TOALTSTACK";
genesis 220 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
genesis 221 case OP_2DROP : return "OP_2DROP";
genesis 222 case OP_2DUP : return "OP_2DUP";
genesis 223 case OP_3DUP : return "OP_3DUP";
genesis 224 case OP_2OVER : return "OP_2OVER";
genesis 225 case OP_2ROT : return "OP_2ROT";
genesis 226 case OP_2SWAP : return "OP_2SWAP";
genesis 227 case OP_IFDUP : return "OP_IFDUP";
genesis 228 case OP_DEPTH : return "OP_DEPTH";
genesis 229 case OP_DROP : return "OP_DROP";
genesis 230 case OP_DUP : return "OP_DUP";
genesis 231 case OP_NIP : return "OP_NIP";
genesis 232 case OP_OVER : return "OP_OVER";
genesis 233 case OP_PICK : return "OP_PICK";
genesis 234 case OP_ROLL : return "OP_ROLL";
genesis 235 case OP_ROT : return "OP_ROT";
genesis 236 case OP_SWAP : return "OP_SWAP";
genesis 237 case OP_TUCK : return "OP_TUCK";
genesis 238
genesis 239
genesis 240 case OP_CAT : return "OP_CAT";
genesis 241 case OP_SUBSTR : return "OP_SUBSTR";
genesis 242 case OP_LEFT : return "OP_LEFT";
genesis 243 case OP_RIGHT : return "OP_RIGHT";
genesis 244 case OP_SIZE : return "OP_SIZE";
genesis 245
genesis 246
genesis 247 case OP_INVERT : return "OP_INVERT";
genesis 248 case OP_AND : return "OP_AND";
genesis 249 case OP_OR : return "OP_OR";
genesis 250 case OP_XOR : return "OP_XOR";
genesis 251 case OP_EQUAL : return "OP_EQUAL";
genesis 252 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
genesis 253 case OP_RESERVED1 : return "OP_RESERVED1";
genesis 254 case OP_RESERVED2 : return "OP_RESERVED2";
genesis 255
genesis 256
genesis 257 case OP_1ADD : return "OP_1ADD";
genesis 258 case OP_1SUB : return "OP_1SUB";
genesis 259 case OP_2MUL : return "OP_2MUL";
genesis 260 case OP_2DIV : return "OP_2DIV";
genesis 261 case OP_NEGATE : return "OP_NEGATE";
genesis 262 case OP_ABS : return "OP_ABS";
genesis 263 case OP_NOT : return "OP_NOT";
genesis 264 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
genesis 265 case OP_ADD : return "OP_ADD";
genesis 266 case OP_SUB : return "OP_SUB";
genesis 267 case OP_MUL : return "OP_MUL";
genesis 268 case OP_DIV : return "OP_DIV";
genesis 269 case OP_MOD : return "OP_MOD";
genesis 270 case OP_LSHIFT : return "OP_LSHIFT";
genesis 271 case OP_RSHIFT : return "OP_RSHIFT";
genesis 272 case OP_BOOLAND : return "OP_BOOLAND";
genesis 273 case OP_BOOLOR : return "OP_BOOLOR";
genesis 274 case OP_NUMEQUAL : return "OP_NUMEQUAL";
genesis 275 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
genesis 276 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
genesis 277 case OP_LESSTHAN : return "OP_LESSTHAN";
genesis 278 case OP_GREATERTHAN : return "OP_GREATERTHAN";
genesis 279 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
genesis 280 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
genesis 281 case OP_MIN : return "OP_MIN";
genesis 282 case OP_MAX : return "OP_MAX";
genesis 283 case OP_WITHIN : return "OP_WITHIN";
genesis 284
genesis 285
genesis 286 case OP_RIPEMD160 : return "OP_RIPEMD160";
genesis 287 case OP_SHA1 : return "OP_SHA1";
genesis 288 case OP_SHA256 : return "OP_SHA256";
genesis 289 case OP_HASH160 : return "OP_HASH160";
genesis 290 case OP_HASH256 : return "OP_HASH256";
genesis 291 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
genesis 292 case OP_CHECKSIG : return "OP_CHECKSIG";
genesis 293 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
genesis 294 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
genesis 295 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
genesis 296
genesis 297
genesis 298 case OP_NOP1 : return "OP_NOP1";
genesis 299 case OP_NOP2 : return "OP_NOP2";
genesis 300 case OP_NOP3 : return "OP_NOP3";
genesis 301 case OP_NOP4 : return "OP_NOP4";
genesis 302 case OP_NOP5 : return "OP_NOP5";
genesis 303 case OP_NOP6 : return "OP_NOP6";
genesis 304 case OP_NOP7 : return "OP_NOP7";
genesis 305 case OP_NOP8 : return "OP_NOP8";
genesis 306 case OP_NOP9 : return "OP_NOP9";
genesis 307 case OP_NOP10 : return "OP_NOP10";
genesis 308
genesis 309
genesis 310
genesis 311
genesis 312 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
genesis 313 case OP_PUBKEY : return "OP_PUBKEY";
genesis 314
genesis 315 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
genesis 316 default:
genesis 317 return "OP_UNKNOWN";
genesis 318 }
genesis 319 };
genesis 320
genesis 321
genesis 322
genesis 323
genesis 324 inline std::string ValueString(const std::vector<unsigned char>& vch)
genesis 325 {
genesis 326 if (vch.size() <= 4)
genesis 327 return strprintf("%d", CBigNum(vch).getint());
genesis 328 else
genesis 329 return HexStr(vch);
genesis 330 }
genesis 331
genesis 332 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
genesis 333 {
genesis 334 std::string str;
genesis 335 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
genesis 336 {
genesis 337 if (!str.empty())
genesis 338 str += " ";
genesis 339 str += ValueString(vch);
genesis 340 }
genesis 341 return str;
genesis 342 }
genesis 343
genesis 344
genesis 345
genesis 346
genesis 347
genesis 348
genesis 349
genesis 350
genesis 351
genesis 352 class CScript : public std::vector<unsigned char>
genesis 353 {
genesis 354 protected:
genesis 355 CScript& push_int64(int64 n)
genesis 356 {
genesis 357 if (n == -1 || (n >= 1 && n <= 16))
genesis 358 {
genesis 359 push_back(n + (OP_1 - 1));
genesis 360 }
genesis 361 else
genesis 362 {
genesis 363 CBigNum bn(n);
genesis 364 *this << bn.getvch();
genesis 365 }
genesis 366 return *this;
genesis 367 }
genesis 368
genesis 369 CScript& push_uint64(uint64 n)
genesis 370 {
genesis 371 if (n >= 1 && n <= 16)
genesis 372 {
genesis 373 push_back(n + (OP_1 - 1));
genesis 374 }
genesis 375 else
genesis 376 {
genesis 377 CBigNum bn(n);
genesis 378 *this << bn.getvch();
genesis 379 }
genesis 380 return *this;
genesis 381 }
genesis 382
genesis 383 public:
genesis 384 CScript() { }
genesis 385 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
genesis 386 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
genesis 387 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
genesis 388
genesis 389 CScript& operator+=(const CScript& b)
genesis 390 {
genesis 391 insert(end(), b.begin(), b.end());
genesis 392 return *this;
genesis 393 }
genesis 394
genesis 395 friend CScript operator+(const CScript& a, const CScript& b)
genesis 396 {
genesis 397 CScript ret = a;
genesis 398 ret += b;
genesis 399 return ret;
genesis 400 }
genesis 401
genesis 402
genesis 403 explicit CScript(char b) { operator<<(b); }
genesis 404 explicit CScript(short b) { operator<<(b); }
genesis 405 explicit CScript(int b) { operator<<(b); }
genesis 406 explicit CScript(long b) { operator<<(b); }
genesis 407 explicit CScript(int64 b) { operator<<(b); }
genesis 408 explicit CScript(unsigned char b) { operator<<(b); }
genesis 409 explicit CScript(unsigned int b) { operator<<(b); }
genesis 410 explicit CScript(unsigned short b) { operator<<(b); }
genesis 411 explicit CScript(unsigned long b) { operator<<(b); }
genesis 412 explicit CScript(uint64 b) { operator<<(b); }
genesis 413
genesis 414 explicit CScript(opcodetype b) { operator<<(b); }
genesis 415 explicit CScript(const uint256& b) { operator<<(b); }
genesis 416 explicit CScript(const CBigNum& b) { operator<<(b); }
genesis 417 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
genesis 418
genesis 419
genesis 420 CScript& operator<<(char b) { return push_int64(b); }
genesis 421 CScript& operator<<(short b) { return push_int64(b); }
genesis 422 CScript& operator<<(int b) { return push_int64(b); }
genesis 423 CScript& operator<<(long b) { return push_int64(b); }
genesis 424 CScript& operator<<(int64 b) { return push_int64(b); }
genesis 425 CScript& operator<<(unsigned char b) { return push_uint64(b); }
genesis 426 CScript& operator<<(unsigned int b) { return push_uint64(b); }
genesis 427 CScript& operator<<(unsigned short b) { return push_uint64(b); }
genesis 428 CScript& operator<<(unsigned long b) { return push_uint64(b); }
genesis 429 CScript& operator<<(uint64 b) { return push_uint64(b); }
genesis 430
genesis 431 CScript& operator<<(opcodetype opcode)
genesis 432 {
genesis 433 if (opcode < 0 || opcode > 0xff)
genesis 434 throw std::runtime_error("CScript::operator<<() : invalid opcode");
genesis 435 insert(end(), (unsigned char)opcode);
genesis 436 return *this;
genesis 437 }
genesis 438
genesis 439 CScript& operator<<(const uint160& b)
genesis 440 {
genesis 441 insert(end(), sizeof(b));
genesis 442 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
genesis 443 return *this;
genesis 444 }
genesis 445
genesis 446 CScript& operator<<(const uint256& b)
genesis 447 {
genesis 448 insert(end(), sizeof(b));
genesis 449 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
genesis 450 return *this;
genesis 451 }
genesis 452
genesis 453 CScript& operator<<(const CBigNum& b)
genesis 454 {
genesis 455 *this << b.getvch();
genesis 456 return *this;
genesis 457 }
genesis 458
genesis 459 CScript& operator<<(const std::vector<unsigned char>& b)
genesis 460 {
genesis 461 if (b.size() < OP_PUSHDATA1)
genesis 462 {
genesis 463 insert(end(), (unsigned char)b.size());
genesis 464 }
genesis 465 else if (b.size() <= 0xff)
genesis 466 {
genesis 467 insert(end(), OP_PUSHDATA1);
genesis 468 insert(end(), (unsigned char)b.size());
genesis 469 }
genesis 470 else if (b.size() <= 0xffff)
genesis 471 {
genesis 472 insert(end(), OP_PUSHDATA2);
genesis 473 unsigned short nSize = b.size();
genesis 474 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
genesis 475 }
genesis 476 else
genesis 477 {
genesis 478 insert(end(), OP_PUSHDATA4);
genesis 479 unsigned int nSize = b.size();
genesis 480 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
genesis 481 }
genesis 482 insert(end(), b.begin(), b.end());
genesis 483 return *this;
genesis 484 }
genesis 485
genesis 486 CScript& operator<<(const CScript& b)
genesis 487 {
genesis 488
genesis 489
genesis 490 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
genesis 491 return *this;
genesis 492 }
genesis 493
genesis 494
genesis 495 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
genesis 496 {
genesis 497
genesis 498 const_iterator pc2 = pc;
genesis 499 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
genesis 500 pc = begin() + (pc2 - begin());
genesis 501 return fRet;
genesis 502 }
genesis 503
genesis 504 bool GetOp(iterator& pc, opcodetype& opcodeRet)
genesis 505 {
genesis 506 const_iterator pc2 = pc;
genesis 507 bool fRet = GetOp2(pc2, opcodeRet, NULL);
genesis 508 pc = begin() + (pc2 - begin());
genesis 509 return fRet;
genesis 510 }
genesis 511
genesis 512 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
genesis 513 {
genesis 514 return GetOp2(pc, opcodeRet, &vchRet);
genesis 515 }
genesis 516
genesis 517 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
genesis 518 {
genesis 519 return GetOp2(pc, opcodeRet, NULL);
genesis 520 }
genesis 521
genesis 522 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
genesis 523 {
genesis 524 opcodeRet = OP_INVALIDOPCODE;
genesis 525 if (pvchRet)
genesis 526 pvchRet->clear();
genesis 527 if (pc >= end())
genesis 528 return false;
genesis 529
genesis 530
genesis 531 if (end() - pc < 1)
genesis 532 return false;
genesis 533 unsigned int opcode = *pc++;
genesis 534
genesis 535
genesis 536 if (opcode <= OP_PUSHDATA4)
genesis 537 {
genesis 538 unsigned int nSize;
genesis 539 if (opcode < OP_PUSHDATA1)
genesis 540 {
genesis 541 nSize = opcode;
genesis 542 }
genesis 543 else if (opcode == OP_PUSHDATA1)
genesis 544 {
genesis 545 if (end() - pc < 1)
genesis 546 return false;
genesis 547 nSize = *pc++;
genesis 548 }
genesis 549 else if (opcode == OP_PUSHDATA2)
genesis 550 {
genesis 551 if (end() - pc < 2)
genesis 552 return false;
genesis 553 nSize = 0;
genesis 554 memcpy(&nSize, &pc[0], 2);
genesis 555 pc += 2;
genesis 556 }
genesis 557 else if (opcode == OP_PUSHDATA4)
genesis 558 {
genesis 559 if (end() - pc < 4)
genesis 560 return false;
genesis 561 memcpy(&nSize, &pc[0], 4);
genesis 562 pc += 4;
genesis 563 }
genesis 564 if (end() - pc < nSize)
genesis 565 return false;
genesis 566 if (pvchRet)
genesis 567 pvchRet->assign(pc, pc + nSize);
genesis 568 pc += nSize;
genesis 569 }
genesis 570
genesis 571 opcodeRet = (opcodetype)opcode;
genesis 572 return true;
genesis 573 }
genesis 574
genesis 575
genesis 576 void FindAndDelete(const CScript& b)
genesis 577 {
genesis 578 if (b.empty())
genesis 579 return;
genesis 580 iterator pc = begin();
genesis 581 opcodetype opcode;
genesis 582 do
genesis 583 {
genesis 584 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
genesis 585 erase(pc, pc + b.size());
genesis 586 }
genesis 587 while (GetOp(pc, opcode));
genesis 588 }
genesis 589
genesis 590
genesis 591 int GetSigOpCount() const
genesis 592 {
genesis 593 int n = 0;
genesis 594 const_iterator pc = begin();
genesis 595 while (pc < end())
genesis 596 {
genesis 597 opcodetype opcode;
genesis 598 if (!GetOp(pc, opcode))
genesis 599 break;
genesis 600 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
genesis 601 n++;
genesis 602 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
genesis 603 n += 20;
genesis 604 }
genesis 605 return n;
genesis 606 }
genesis 607
genesis 608
genesis 609 bool IsPushOnly() const
genesis 610 {
genesis 611 if (size() > 200)
genesis 612 return false;
genesis 613 const_iterator pc = begin();
genesis 614 while (pc < end())
genesis 615 {
genesis 616 opcodetype opcode;
genesis 617 if (!GetOp(pc, opcode))
genesis 618 return false;
genesis 619 if (opcode > OP_16)
genesis 620 return false;
genesis 621 }
genesis 622 return true;
genesis 623 }
genesis 624
genesis 625
genesis 626 CBitcoinAddress GetBitcoinAddress() const
genesis 627 {
genesis 628 opcodetype opcode;
genesis 629 std::vector<unsigned char> vch;
genesis 630 CScript::const_iterator pc = begin();
genesis 631 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
genesis 632 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
genesis 633 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
genesis 634 uint160 hash160 = uint160(vch);
genesis 635 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
genesis 636 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
genesis 637 if (pc != end()) return 0;
genesis 638 return CBitcoinAddress(hash160);
genesis 639 }
genesis 640
genesis 641 void SetBitcoinAddress(const CBitcoinAddress& address)
genesis 642 {
genesis 643 this->clear();
genesis 644 *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
genesis 645 }
genesis 646
genesis 647 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
genesis 648 {
genesis 649 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
genesis 650 }
genesis 651
genesis 652
genesis 653 void PrintHex() const
genesis 654 {
genesis 655 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
genesis 656 }
genesis 657
genesis 658 std::string ToString() const
genesis 659 {
genesis 660 std::string str;
genesis 661 opcodetype opcode;
genesis 662 std::vector<unsigned char> vch;
genesis 663 const_iterator pc = begin();
genesis 664 while (pc < end())
genesis 665 {
genesis 666 if (!str.empty())
genesis 667 str += " ";
genesis 668 if (!GetOp(pc, opcode, vch))
genesis 669 {
genesis 670 str += "[error]";
genesis 671 return str;
genesis 672 }
genesis 673 if (0 <= opcode && opcode <= OP_PUSHDATA4)
genesis 674 str += ValueString(vch);
genesis 675 else
genesis 676 str += GetOpName(opcode);
genesis 677 }
genesis 678 return str;
genesis 679 }
genesis 680
genesis 681 void print() const
genesis 682 {
genesis 683 printf("%s\n", ToString().c_str());
genesis 684 }
genesis 685 };
genesis 686
genesis 687
genesis 688
genesis 689
genesis 690
genesis 691
genesis 692
genesis 693 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
genesis 694
genesis 695 bool IsStandard(const CScript& scriptPubKey);
genesis 696 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
genesis 697 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
genesis 698 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
genesis 699 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);
genesis 700
genesis 701 #endif