raw
genesis                 1 // Copyright (c) 2009-2010 Satoshi Nakamoto
genesis 2 // Copyright (c) 2011 The Bitcoin developers
genesis 3 // Distributed under the MIT/X11 software license, see the accompanying
genesis 4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
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 // push value
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 // control
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 // stack ops
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 // splice ops
genesis 90 OP_CAT,
genesis 91 OP_SUBSTR,
genesis 92 OP_LEFT,
genesis 93 OP_RIGHT,
genesis 94 OP_SIZE,
genesis 95
genesis 96 // bit logic
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 // numeric
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 // crypto
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 // expansion
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 // template matching params
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 // push value
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 // control
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 // stack ops
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 // splice ops
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 // bit logic
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 // numeric
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 // crypto
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 // expanson
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 // template matching params
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 #ifndef _MSC_VER
genesis 388 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
genesis 389 #endif
genesis 390
genesis 391 CScript& operator+=(const CScript& b)
genesis 392 {
genesis 393 insert(end(), b.begin(), b.end());
genesis 394 return *this;
genesis 395 }
genesis 396
genesis 397 friend CScript operator+(const CScript& a, const CScript& b)
genesis 398 {
genesis 399 CScript ret = a;
genesis 400 ret += b;
genesis 401 return ret;
genesis 402 }
genesis 403
genesis 404
genesis 405 explicit CScript(char b) { operator<<(b); }
genesis 406 explicit CScript(short b) { operator<<(b); }
genesis 407 explicit CScript(int b) { operator<<(b); }
genesis 408 explicit CScript(long b) { operator<<(b); }
genesis 409 explicit CScript(int64 b) { operator<<(b); }
genesis 410 explicit CScript(unsigned char b) { operator<<(b); }
genesis 411 explicit CScript(unsigned int b) { operator<<(b); }
genesis 412 explicit CScript(unsigned short b) { operator<<(b); }
genesis 413 explicit CScript(unsigned long b) { operator<<(b); }
genesis 414 explicit CScript(uint64 b) { operator<<(b); }
genesis 415
genesis 416 explicit CScript(opcodetype b) { operator<<(b); }
genesis 417 explicit CScript(const uint256& b) { operator<<(b); }
genesis 418 explicit CScript(const CBigNum& b) { operator<<(b); }
genesis 419 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
genesis 420
genesis 421
genesis 422 CScript& operator<<(char b) { return push_int64(b); }
genesis 423 CScript& operator<<(short b) { return push_int64(b); }
genesis 424 CScript& operator<<(int b) { return push_int64(b); }
genesis 425 CScript& operator<<(long b) { return push_int64(b); }
genesis 426 CScript& operator<<(int64 b) { return push_int64(b); }
genesis 427 CScript& operator<<(unsigned char b) { return push_uint64(b); }
genesis 428 CScript& operator<<(unsigned int b) { return push_uint64(b); }
genesis 429 CScript& operator<<(unsigned short b) { return push_uint64(b); }
genesis 430 CScript& operator<<(unsigned long b) { return push_uint64(b); }
genesis 431 CScript& operator<<(uint64 b) { return push_uint64(b); }
genesis 432
genesis 433 CScript& operator<<(opcodetype opcode)
genesis 434 {
genesis 435 if (opcode < 0 || opcode > 0xff)
genesis 436 throw std::runtime_error("CScript::operator<<() : invalid opcode");
genesis 437 insert(end(), (unsigned char)opcode);
genesis 438 return *this;
genesis 439 }
genesis 440
genesis 441 CScript& operator<<(const uint160& b)
genesis 442 {
genesis 443 insert(end(), sizeof(b));
genesis 444 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
genesis 445 return *this;
genesis 446 }
genesis 447
genesis 448 CScript& operator<<(const uint256& b)
genesis 449 {
genesis 450 insert(end(), sizeof(b));
genesis 451 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
genesis 452 return *this;
genesis 453 }
genesis 454
genesis 455 CScript& operator<<(const CBigNum& b)
genesis 456 {
genesis 457 *this << b.getvch();
genesis 458 return *this;
genesis 459 }
genesis 460
genesis 461 CScript& operator<<(const std::vector<unsigned char>& b)
genesis 462 {
genesis 463 if (b.size() < OP_PUSHDATA1)
genesis 464 {
genesis 465 insert(end(), (unsigned char)b.size());
genesis 466 }
genesis 467 else if (b.size() <= 0xff)
genesis 468 {
genesis 469 insert(end(), OP_PUSHDATA1);
genesis 470 insert(end(), (unsigned char)b.size());
genesis 471 }
genesis 472 else if (b.size() <= 0xffff)
genesis 473 {
genesis 474 insert(end(), OP_PUSHDATA2);
genesis 475 unsigned short nSize = b.size();
genesis 476 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
genesis 477 }
genesis 478 else
genesis 479 {
genesis 480 insert(end(), OP_PUSHDATA4);
genesis 481 unsigned int nSize = b.size();
genesis 482 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
genesis 483 }
genesis 484 insert(end(), b.begin(), b.end());
genesis 485 return *this;
genesis 486 }
genesis 487
genesis 488 CScript& operator<<(const CScript& b)
genesis 489 {
genesis 490 // I'm not sure if this should push the script or concatenate scripts.
genesis 491 // If there's ever a use for pushing a script onto a script, delete this member fn
genesis 492 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
genesis 493 return *this;
genesis 494 }
genesis 495
genesis 496
genesis 497 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
genesis 498 {
genesis 499 // Wrapper so it can be called with either iterator or const_iterator
genesis 500 const_iterator pc2 = pc;
genesis 501 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
genesis 502 pc = begin() + (pc2 - begin());
genesis 503 return fRet;
genesis 504 }
genesis 505
genesis 506 bool GetOp(iterator& pc, opcodetype& opcodeRet)
genesis 507 {
genesis 508 const_iterator pc2 = pc;
genesis 509 bool fRet = GetOp2(pc2, opcodeRet, NULL);
genesis 510 pc = begin() + (pc2 - begin());
genesis 511 return fRet;
genesis 512 }
genesis 513
genesis 514 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
genesis 515 {
genesis 516 return GetOp2(pc, opcodeRet, &vchRet);
genesis 517 }
genesis 518
genesis 519 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
genesis 520 {
genesis 521 return GetOp2(pc, opcodeRet, NULL);
genesis 522 }
genesis 523
genesis 524 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
genesis 525 {
genesis 526 opcodeRet = OP_INVALIDOPCODE;
genesis 527 if (pvchRet)
genesis 528 pvchRet->clear();
genesis 529 if (pc >= end())
genesis 530 return false;
genesis 531
genesis 532 // Read instruction
genesis 533 if (end() - pc < 1)
genesis 534 return false;
genesis 535 unsigned int opcode = *pc++;
genesis 536
genesis 537 // Immediate operand
genesis 538 if (opcode <= OP_PUSHDATA4)
genesis 539 {
genesis 540 unsigned int nSize;
genesis 541 if (opcode < OP_PUSHDATA1)
genesis 542 {
genesis 543 nSize = opcode;
genesis 544 }
genesis 545 else if (opcode == OP_PUSHDATA1)
genesis 546 {
genesis 547 if (end() - pc < 1)
genesis 548 return false;
genesis 549 nSize = *pc++;
genesis 550 }
genesis 551 else if (opcode == OP_PUSHDATA2)
genesis 552 {
genesis 553 if (end() - pc < 2)
genesis 554 return false;
genesis 555 nSize = 0;
genesis 556 memcpy(&nSize, &pc[0], 2);
genesis 557 pc += 2;
genesis 558 }
genesis 559 else if (opcode == OP_PUSHDATA4)
genesis 560 {
genesis 561 if (end() - pc < 4)
genesis 562 return false;
genesis 563 memcpy(&nSize, &pc[0], 4);
genesis 564 pc += 4;
genesis 565 }
genesis 566 if (end() - pc < nSize)
genesis 567 return false;
genesis 568 if (pvchRet)
genesis 569 pvchRet->assign(pc, pc + nSize);
genesis 570 pc += nSize;
genesis 571 }
genesis 572
genesis 573 opcodeRet = (opcodetype)opcode;
genesis 574 return true;
genesis 575 }
genesis 576
genesis 577
genesis 578 void FindAndDelete(const CScript& b)
genesis 579 {
genesis 580 if (b.empty())
genesis 581 return;
genesis 582 iterator pc = begin();
genesis 583 opcodetype opcode;
genesis 584 do
genesis 585 {
genesis 586 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
genesis 587 erase(pc, pc + b.size());
genesis 588 }
genesis 589 while (GetOp(pc, opcode));
genesis 590 }
genesis 591
genesis 592
genesis 593 int GetSigOpCount() const
genesis 594 {
genesis 595 int n = 0;
genesis 596 const_iterator pc = begin();
genesis 597 while (pc < end())
genesis 598 {
genesis 599 opcodetype opcode;
genesis 600 if (!GetOp(pc, opcode))
genesis 601 break;
genesis 602 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
genesis 603 n++;
genesis 604 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
genesis 605 n += 20;
genesis 606 }
genesis 607 return n;
genesis 608 }
genesis 609
genesis 610
genesis 611 bool IsPushOnly() const
genesis 612 {
genesis 613 if (size() > 200)
genesis 614 return false;
genesis 615 const_iterator pc = begin();
genesis 616 while (pc < end())
genesis 617 {
genesis 618 opcodetype opcode;
genesis 619 if (!GetOp(pc, opcode))
genesis 620 return false;
genesis 621 if (opcode > OP_16)
genesis 622 return false;
genesis 623 }
genesis 624 return true;
genesis 625 }
genesis 626
genesis 627
genesis 628 CBitcoinAddress GetBitcoinAddress() const
genesis 629 {
genesis 630 opcodetype opcode;
genesis 631 std::vector<unsigned char> vch;
genesis 632 CScript::const_iterator pc = begin();
genesis 633 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
genesis 634 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
genesis 635 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
genesis 636 uint160 hash160 = uint160(vch);
genesis 637 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
genesis 638 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
genesis 639 if (pc != end()) return 0;
genesis 640 return CBitcoinAddress(hash160);
genesis 641 }
genesis 642
genesis 643 void SetBitcoinAddress(const CBitcoinAddress& address)
genesis 644 {
genesis 645 this->clear();
genesis 646 *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
genesis 647 }
genesis 648
genesis 649 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
genesis 650 {
genesis 651 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
genesis 652 }
genesis 653
genesis 654
genesis 655 void PrintHex() const
genesis 656 {
genesis 657 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
genesis 658 }
genesis 659
genesis 660 std::string ToString() const
genesis 661 {
genesis 662 std::string str;
genesis 663 opcodetype opcode;
genesis 664 std::vector<unsigned char> vch;
genesis 665 const_iterator pc = begin();
genesis 666 while (pc < end())
genesis 667 {
genesis 668 if (!str.empty())
genesis 669 str += " ";
genesis 670 if (!GetOp(pc, opcode, vch))
genesis 671 {
genesis 672 str += "[error]";
genesis 673 return str;
genesis 674 }
genesis 675 if (0 <= opcode && opcode <= OP_PUSHDATA4)
genesis 676 str += ValueString(vch);
genesis 677 else
genesis 678 str += GetOpName(opcode);
genesis 679 }
genesis 680 return str;
genesis 681 }
genesis 682
genesis 683 void print() const
genesis 684 {
genesis 685 printf("%s\n", ToString().c_str());
genesis 686 }
genesis 687 };
genesis 688
genesis 689
genesis 690
genesis 691
genesis 692
genesis 693
genesis 694
genesis 695 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
genesis 696
genesis 697 bool IsStandard(const CScript& scriptPubKey);
genesis 698 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
genesis 699 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
genesis 700 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
genesis 701 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);
genesis 702
genesis 703 #endif