Serialized script, used inside transaction inputs and outputs */
| 430 | |
| 431 | /** Serialized script, used inside transaction inputs and outputs */ |
| 432 | class CScript : public CScriptBase { |
| 433 | protected: |
| 434 | CScript &push_int64(int64_t n) { |
| 435 | if (n == -1 || (n >= 1 && n <= 16)) { |
| 436 | push_back(n + (OP_1 - 1)); |
| 437 | } else if (n == 0) { |
| 438 | push_back(OP_0); |
| 439 | } else { |
| 440 | *this << CScriptNum::serialize(n); |
| 441 | } |
| 442 | return *this; |
| 443 | } |
| 444 | |
| 445 | public: |
| 446 | CScript() {} |
| 447 | CScript(const_iterator pbegin, const_iterator pend) |
| 448 | : CScriptBase(pbegin, pend) {} |
| 449 | CScript(std::vector<uint8_t>::const_iterator pbegin, |
| 450 | std::vector<uint8_t>::const_iterator pend) |
| 451 | : CScriptBase(pbegin, pend) {} |
| 452 | CScript(const uint8_t *pbegin, const uint8_t *pend) |
| 453 | : CScriptBase(pbegin, pend) {} |
| 454 | |
| 455 | SERIALIZE_METHODS(CScript, obj) { READWRITE(AsBase<CScriptBase>(obj)); } |
| 456 | |
| 457 | explicit CScript(int64_t b) { operator<<(b); } |
| 458 | explicit CScript(opcodetype b) { operator<<(b); } |
| 459 | explicit CScript(const CScriptNum &b) { operator<<(b); } |
| 460 | // delete non-existent constructor to defend against future introduction |
| 461 | // e.g. via prevector |
| 462 | explicit CScript(const std::vector<uint8_t> &b) = delete; |
| 463 | |
| 464 | /** Delete non-existent operator to defend against future introduction */ |
| 465 | CScript &operator<<(const CScript &b) = delete; |
| 466 | |
| 467 | CScript &operator<<(int64_t b) LIFETIMEBOUND { return push_int64(b); } |
| 468 | |
| 469 | CScript &operator<<(opcodetype opcode) LIFETIMEBOUND { |
| 470 | if (opcode < 0 || opcode > 0xff) { |
| 471 | throw std::runtime_error("CScript::operator<<(): invalid opcode"); |
| 472 | } |
| 473 | insert(end(), uint8_t(opcode)); |
| 474 | return *this; |
| 475 | } |
| 476 | |
| 477 | CScript &operator<<(const CScriptNum &b) LIFETIMEBOUND { |
| 478 | *this << b.getvch(); |
| 479 | return *this; |
| 480 | } |
| 481 | |
| 482 | CScript &operator<<(const std::vector<uint8_t> &b) LIFETIMEBOUND { |
| 483 | if (b.size() < OP_PUSHDATA1) { |
| 484 | insert(end(), uint8_t(b.size())); |
| 485 | } else if (b.size() <= 0xff) { |
| 486 | insert(end(), OP_PUSHDATA1); |
| 487 | insert(end(), uint8_t(b.size())); |
| 488 | } else if (b.size() <= 0xffff) { |
| 489 | insert(end(), OP_PUSHDATA2); |