6#ifndef BITCOIN_SCRIPT_SCRIPT_H
7#define BITCOIN_SCRIPT_SCRIPT_H
46template <
typename T> std::vector<uint8_t>
ToByteVector(
const T &in) {
47 return std::vector<uint8_t>(in.begin(), in.end());
216 :
std::runtime_error(str) {}
221 :
std::runtime_error(str) {}
237 explicit CScriptNum(
const std::vector<uint8_t> &vch,
bool fRequireMinimal,
238 const size_t nMaxNumSize) {
239 if (vch.size() > nMaxNumSize) {
244 "non-minimally encoded script number");
250 const size_t nMaxNumSize);
364 static std::vector<uint8_t>
serialize(
const int64_t &value) {
369 std::vector<uint8_t> result;
370 const bool neg = value < 0;
371 uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1
372 :
static_cast<uint64_t
>(value);
375 result.push_back(absvalue & 0xff);
387 if (result.back() & 0x80) {
388 result.push_back(neg ? 0x80 : 0);
390 result.back() |= 0x80;
397 static int64_t
set_vch(
const std::vector<uint8_t> &vch) {
403 for (
size_t i = 0; i != vch.size(); ++i) {
404 result |= int64_t(vch[i]) << 8 * i;
409 if (vch.back() & 0x80) {
410 return -int64_t(result & ~(0x80ULL << (8 * (vch.size() - 1))));
429 std::vector<uint8_t> *pvchRet);
435 if (n == -1 || (n >= 1 && n <= 16)) {
449 CScript(std::vector<uint8_t>::const_iterator pbegin,
450 std::vector<uint8_t>::const_iterator pend)
452 CScript(
const uint8_t *pbegin,
const uint8_t *pend)
462 explicit CScript(
const std::vector<uint8_t> &b) =
delete;
470 if (opcode < 0 || opcode > 0xff) {
471 throw std::runtime_error(
"CScript::operator<<(): invalid opcode");
485 }
else if (b.size() <= 0xff) {
488 }
else if (b.size() <= 0xffff) {
492 insert(
end(), _data, _data +
sizeof(_data));
497 insert(
end(), _data, _data +
sizeof(_data));
504 std::vector<uint8_t> &vchRet)
const {
514 if (opcode ==
OP_0) {
519 return int(opcode) - int(
OP_1 - 1);
522 assert(n >= 0 && n <= 16);
Serialized script, used inside transaction inputs and outputs.
CScript(const_iterator pbegin, const_iterator pend)
bool IsPayToScriptHash() const
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
CScript & operator<<(const CScript &b)=delete
Delete non-existent operator to defend against future introduction.
CScript(const CScriptNum &b)
CScript(const uint8_t *pbegin, const uint8_t *pend)
CScript & push_int64(int64_t n)
SERIALIZE_METHODS(CScript, obj)
CScript(std::vector< uint8_t >::const_iterator pbegin, std::vector< uint8_t >::const_iterator pend)
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
bool IsWitnessProgram() const
CScript & operator<<(const std::vector< uint8_t > &b) LIFETIMEBOUND
bool HasValidOps() const
Check if the script contains valid OP_CODES.
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
CScript & operator<<(opcodetype opcode) LIFETIMEBOUND
CScript & operator<<(const CScriptNum &b) LIFETIMEBOUND
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
CScript(const std::vector< uint8_t > &b)=delete
CScript & operator<<(int64_t b) LIFETIMEBOUND
static opcodetype EncodeOP_N(int n)
CScriptNum & operator-=(const CScriptNum &rhs)
CScriptNum & operator-=(const int64_t &rhs)
CScriptNum operator-(const int64_t &rhs) const
bool operator<(const CScriptNum &rhs) const
CScriptNum operator+(const int64_t &rhs) const
CScriptNum & operator+=(const CScriptNum &rhs)
CScriptNum operator-(const CScriptNum &rhs) const
bool operator>(const CScriptNum &rhs) const
bool operator==(const CScriptNum &rhs) const
static std::vector< uint8_t > serialize(const int64_t &value)
CScriptNum(const std::vector< uint8_t > &vch, bool fRequireMinimal, const size_t nMaxNumSize)
CScriptNum & operator&=(const CScriptNum &rhs)
std::vector< uint8_t > getvch() const
bool operator<=(const CScriptNum &rhs) const
bool operator==(const int64_t &rhs) const
bool operator<(const int64_t &rhs) const
static bool IsMinimallyEncoded(const std::vector< uint8_t > &vch, const size_t nMaxNumSize)
CScriptNum operator%(const int64_t &rhs) const
CScriptNum operator&(const int64_t &rhs) const
CScriptNum operator&(const CScriptNum &rhs) const
CScriptNum & operator+=(const int64_t &rhs)
bool operator>=(const int64_t &rhs) const
bool operator<=(const int64_t &rhs) const
CScriptNum operator%(const CScriptNum &rhs) const
CScriptNum(const int64_t &n)
Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
static bool MinimallyEncode(std::vector< uint8_t > &data)
bool operator!=(const int64_t &rhs) const
bool operator>=(const CScriptNum &rhs) const
bool operator>(const int64_t &rhs) const
CScriptNum & operator&=(const int64_t &rhs)
static int64_t set_vch(const std::vector< uint8_t > &vch)
CScriptNum operator/(const CScriptNum &rhs) const
CScriptNum operator/(const int64_t &rhs) const
CScriptNum operator-() const
bool operator!=(const CScriptNum &rhs) const
CScriptNum & operator=(const int64_t &rhs)
CScriptNum operator+(const CScriptNum &rhs) const
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
iterator insert(iterator pos, const T &value)
void push_back(const T &value)
static void WriteLE16(uint8_t *ptr, uint16_t x)
static void WriteLE32(uint8_t *ptr, uint32_t x)
bool AddInt63Overflow(int64_t a, int64_t b, int64_t &result)
Computes a + b and stores it in result.
bool SubInt63Overflow(int64_t a, int64_t b, int64_t &result)
Computes a - b and stores it in result.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
static const unsigned int LOCKTIME_THRESHOLD
static const unsigned int MAX_OPCODE
std::string GetOpName(opcodetype opcode)
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
static const int MAX_SCRIPT_SIZE
opcodetype
Script opcodes.
@ FIRST_UNDEFINED_OP_VALUE
static const int MAX_STACK_SIZE
static const int MAX_OPS_PER_SCRIPT
bool CheckMinimalPush(const std::vector< uint8_t > &data, opcodetype opcode)
Check whether the given stack element data would be minimally pushed using the given opcode.
static const int MAX_PUBKEYS_PER_MULTISIG
constexpr size_t MAX_SCRIPTNUM_BYTE_SIZE
std::vector< uint8_t > ToByteVector(const T &in)
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< uint8_t > *pvchRet)
#define READWRITEAS(type, obj)
scriptnum_encoding_error(const std::string &str)
scriptnum_overflow_error(const std::string &str)