6#ifndef BITCOIN_SCRIPT_SCRIPT_H
7#define BITCOIN_SCRIPT_SCRIPT_H
69 return std::vector<unsigned char>(in.begin(), in.end());
245 explicit CScriptNum(
const std::vector<unsigned char>& vch,
bool fRequireMinimal,
248 if (vch.size() > nMaxNumSize) {
251 if (fRequireMinimal && vch.size() > 0) {
258 if ((vch.back() & 0x7f) == 0) {
264 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
313 assert(rhs == 0 || (rhs > 0 &&
m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
314 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
321 assert(rhs == 0 || (rhs > 0 &&
m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
322 (rhs < 0 &&
m_value <= std::numeric_limits<int64_t>::max() + rhs));
335 if (
m_value > std::numeric_limits<int>::max())
336 return std::numeric_limits<int>::max();
337 else if (
m_value < std::numeric_limits<int>::min())
338 return std::numeric_limits<int>::min();
344 std::vector<unsigned char>
getvch()
const
349 static std::vector<unsigned char>
serialize(
const int64_t& value)
352 return std::vector<unsigned char>();
354 std::vector<unsigned char> result;
355 const bool neg = value < 0;
356 uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 :
static_cast<uint64_t
>(value);
360 result.push_back(absvalue & 0xff);
374 if (result.back() & 0x80)
375 result.push_back(neg ? 0x80 : 0);
377 result.back() |= 0x80;
383 static int64_t
set_vch(
const std::vector<unsigned char>& vch)
389 for (
size_t i = 0; i != vch.size(); ++i)
390 result |=
static_cast<int64_t
>(vch[i]) << 8*i;
394 if (vch.back() & 0x80)
395 return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
421 }
else if (
size <= 0xff) {
424 }
else if (
size <= 0xffff) {
445 if (n == -1 || (n >= 1 && n <= 16))
462 template <std::input_iterator InputIterator>
472 explicit CScript(
const std::vector<unsigned char>& b) =
delete;
481 if (opcode < 0 || opcode > 0xff)
482 throw std::runtime_error(
"CScript::operator<<(): invalid opcode");
503 return *
this << std::as_bytes(b);
522 return (
int)opcode - (int)(
OP_1 - 1);
526 assert(n >= 0 && n <= 16);
553 static bool IsPayToAnchor(
int version,
const std::vector<unsigned char>& program);
557 bool IsWitnessProgram(
int& version, std::vector<unsigned char>& program)
const;
588 std::vector<std::vector<unsigned char> >
stack;
615template<
typename... Ts>
621 ([&
ret, &cnt] (Ts&& input) {
622 if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>,
CScript>) {
625 ret = std::forward<Ts>(input);
627 ret.insert(
ret.end(), input.begin(), input.end());
634 } (std::forward<Ts>(inputs)), ...);
Serialized script, used inside transaction inputs and outputs.
bool IsPayToScriptHash() const
CScript(InputIterator first, InputIterator last)
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)
void AppendData(std::span< const value_type > data)
CScript & push_int64(int64_t n)
SERIALIZE_METHODS(CScript, obj)
CScript & operator<<(std::span< const value_type > b) LIFETIMEBOUND
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
bool IsPayToWitnessScriptHash() const
void AppendDataSize(const uint32_t size)
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
bool HasValidOps() const
Check if the script contains valid OP_CODES.
CScript(const std::vector< unsigned char > &b)=delete
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
CScript & operator<<(opcodetype opcode) LIFETIMEBOUND
CScript & operator<<(const CScriptNum &b) LIFETIMEBOUND
CScript & operator<<(std::span< const std::byte > b) LIFETIMEBOUND
bool IsPayToAnchor() const
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
CScript & operator<<(int64_t b) LIFETIMEBOUND
static opcodetype EncodeOP_N(int n)
A reference to a CScript: the Hash160 of its serialization.
CScriptID(const uint160 &in)
static std::vector< unsigned char > serialize(const int64_t &value)
CScriptNum & operator-=(const CScriptNum &rhs)
CScriptNum & operator-=(const int64_t &rhs)
CScriptNum operator-(const int64_t &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
CScriptNum & operator&=(const CScriptNum &rhs)
bool operator<=(const CScriptNum &rhs) const
bool operator==(const int64_t &rhs) const
bool operator<(const int64_t &rhs) const
CScriptNum(const std::vector< unsigned char > &vch, bool fRequireMinimal, const size_t nMaxNumSize=nDefaultMaxNumSize)
std::vector< unsigned char > getvch() const
static const size_t nDefaultMaxNumSize
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(const int64_t &n)
Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
bool operator!=(const int64_t &rhs) const
bool operator>=(const CScriptNum &rhs) const
bool operator>(const int64_t &rhs) const
static int64_t set_vch(const std::vector< unsigned char > &vch)
CScriptNum & operator&=(const int64_t &rhs)
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)
scriptnum_error(const std::string &str)
static void WriteLE16(unsigned char *ptr, uint16_t x)
static void WriteLE32(unsigned char *ptr, uint32_t x)
static constexpr int64_t VALIDATION_WEIGHT_PER_SIGOP_PASSED
static const unsigned int LOCKTIME_THRESHOLD
static const unsigned int MAX_OPCODE
std::string GetOpName(opcodetype opcode)
prevector< 28, unsigned char > CScriptBase
We use a prevector for the script to reduce the considerable memory overhead of vectors in cases wher...
bool CheckMinimalPush(const std::vector< unsigned char > &data, opcodetype opcode)
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
static const int MAX_SCRIPT_SIZE
opcodetype
Script opcodes.
static constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A
The limit of keys in OP_CHECKSIGADD-based scripts.
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet)
static const int MAX_STACK_SIZE
bool IsOpSuccess(const opcodetype &opcode)
Test for OP_SUCCESSx opcodes as defined by BIP342.
static const int MAX_OPS_PER_SCRIPT
static constexpr int64_t VALIDATION_WEIGHT_OFFSET
CScript BuildScript(Ts &&... inputs)
Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<.
static const int MAX_PUBKEYS_PER_MULTISIG
std::vector< unsigned char > ToByteVector(const T &in)
static const uint32_t LOCKTIME_MAX
static constexpr unsigned int ANNEX_TAG
std::string ToString() const
std::vector< std::vector< unsigned char > > stack