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