Serialized script, used inside transaction inputs and outputs */
| 362 | |
| 363 | /** Serialized script, used inside transaction inputs and outputs */ |
| 364 | class CScript : public std::vector<unsigned char> |
| 365 | { |
| 366 | protected: |
| 367 | CScript& push_int64(int64_t n) |
| 368 | { |
| 369 | if (n == -1 || (n >= 1 && n <= 16)) |
| 370 | { |
| 371 | push_back(n + (OP_1 - 1)); |
| 372 | } |
| 373 | else if (n == 0) |
| 374 | { |
| 375 | push_back(OP_0); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | *this << CScriptNum::serialize(n); |
| 380 | } |
| 381 | return *this; |
| 382 | } |
| 383 | public: |
| 384 | CScript() { } |
| 385 | CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { } |
| 386 | CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { } |
| 387 | CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { } |
| 388 | |
| 389 | CScript& operator+=(const CScript& b) |
| 390 | { |
| 391 | insert(end(), b.begin(), b.end()); |
| 392 | return *this; |
| 393 | } |
| 394 | |
| 395 | friend CScript operator+(const CScript& a, const CScript& b) |
| 396 | { |
| 397 | CScript ret = a; |
| 398 | ret += b; |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | CScript(int64_t b) { operator<<(b); } |
| 403 | |
| 404 | explicit CScript(opcodetype b) { operator<<(b); } |
| 405 | explicit CScript(const CScriptNum& b) { operator<<(b); } |
| 406 | explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); } |
| 407 | |
| 408 | |
| 409 | CScript& operator<<(int64_t b) { return push_int64(b); } |
| 410 | |
| 411 | CScript& operator<<(opcodetype opcode) |
| 412 | { |
| 413 | if (opcode < 0 || opcode > 0xff) |
| 414 | throw std::runtime_error("CScript::operator<<(): invalid opcode"); |
| 415 | insert(end(), (unsigned char)opcode); |
| 416 | return *this; |
| 417 | } |
| 418 | |
| 419 | CScript& operator<<(const CScriptNum& b) |
| 420 | { |
| 421 | *this << b.getvch(); |