6#ifndef BITCOIN_SCRIPT_SCRIPT_H
7#define BITCOIN_SCRIPT_SCRIPT_H
70 return std::vector<unsigned char>(in.begin(), in.end());
246 explicit CScriptNum(
const std::vector<unsigned char>& vch,
bool fRequireMinimal,
249 if (vch.size() > nMaxNumSize) {
252 if (fRequireMinimal && vch.size() > 0) {
259 if ((vch.back() & 0x7f) == 0) {
265 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
306 assert(rhs == 0 || (rhs > 0 &&
m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
307 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
314 assert(rhs == 0 || (rhs > 0 &&
m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
315 (rhs < 0 &&
m_value <= std::numeric_limits<int64_t>::max() + rhs));
328 if (
m_value > std::numeric_limits<int>::max())
329 return std::numeric_limits<int>::max();
330 else if (
m_value < std::numeric_limits<int>::min())
331 return std::numeric_limits<int>::min();
337 std::vector<unsigned char>
getvch()
const
342 static std::vector<unsigned char>
serialize(
const int64_t& value)
345 return std::vector<unsigned char>();
347 std::vector<unsigned char> result;
348 const bool neg = value < 0;
349 uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 :
static_cast<uint64_t
>(value);
353 result.push_back(absvalue & 0xff);
367 if (result.back() & 0x80)
368 result.push_back(neg ? 0x80 : 0);
370 result.back() |= 0x80;
376 static int64_t
set_vch(
const std::vector<unsigned char>& vch)
382 for (
size_t i = 0; i != vch.size(); ++i)
383 result |=
static_cast<int64_t
>(vch[i]) << 8*i;
387 if (vch.back() & 0x80)
388 return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
402bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end,
opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet);
412 }
else if (
size <= 0xff) {
415 }
else if (
size <= 0xffff) {
436 if (n == -1 || (n >= 1 && n <= 16))
453 template <std::input_iterator InputIterator>
463 explicit CScript(
const std::vector<unsigned char>& b) =
delete;
472 if (opcode < 0 || opcode > 0xff)
473 throw std::runtime_error(
"CScript::operator<<(): invalid opcode");
494 return *
this << std::as_bytes(b);
497 bool GetOp(const_iterator& pc,
opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
const
513 return (
int)opcode - (int)(
OP_1 - 1);
517 assert(n >= 0 && n <= 16);
544 static bool IsPayToAnchor(
int version,
const std::vector<unsigned char>& program);
548 bool IsWitnessProgram(
int& version, std::vector<unsigned char>& program)
const;
581 std::vector<std::vector<unsigned char> >
stack;
610template<
typename... Ts>
616 ([&
ret, &cnt] (Ts&& input) {
617 if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<Ts>>,
CScript>) {
620 ret = std::forward<Ts>(input);
622 ret.insert(
ret.end(), input.begin(), input.end());
629 } (std::forward<Ts>(inputs)), ...);
std::string ToString(const T &t)
Locale-independent version of std::to_string.
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 IsPayToTaproot() const
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)
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
static constexpr size_t nDefaultMaxNumSize
auto operator<=>(const CScriptNum &rhs) const
bool operator==(const CScriptNum &rhs) const
CScriptNum & operator&=(const CScriptNum &rhs)
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
auto 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)
CScriptNum(const int64_t &n)
Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
static int64_t set_vch(const std::vector< unsigned char > &vch)
CScriptNum & operator&=(const int64_t &rhs)
CScriptNum operator-() 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 uint8_t &value)
void push_back(const uint8_t &value)
scriptnum_error(const std::string &str)
void WriteLE32(B *ptr, uint32_t x)
void WriteLE16(B *ptr, uint16_t x)
#define T(expected, seed, data)
std::string GetOpName(opcodetype opcode)
bool CheckMinimalPush(const std::vector< unsigned char > &data, opcodetype opcode)
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet)
bool IsOpSuccess(const opcodetype &opcode)
Test for OP_SUCCESSx opcodes as defined by BIP342.
constexpr unsigned int MAX_OPCODE
constexpr int64_t VALIDATION_WEIGHT_PER_SIGOP_PASSED
constexpr int MAX_SCRIPT_SIZE
opcodetype
Script opcodes.
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)
constexpr unsigned int MAX_SCRIPT_ELEMENT_SIZE
constexpr int MAX_PUBKEYS_PER_MULTISIG
constexpr int MAX_STACK_SIZE
constexpr int64_t VALIDATION_WEIGHT_OFFSET
constexpr unsigned int LOCKTIME_THRESHOLD
CScript BuildScript(Ts &&... inputs)
Build a script by concatenating other scripts, or any argument accepted by CScript::operator<<.
constexpr int MAX_OPS_PER_SCRIPT
constexpr uint32_t LOCKTIME_MAX
std::vector< unsigned char > ToByteVector(const T &in)
prevector< 36, uint8_t > CScriptBase
We use a prevector for the script to reduce the considerable memory overhead of vectors in cases wher...
constexpr unsigned int ANNEX_TAG
Out & AsBase(In &x)
Convert any argument to a reference to X, maintaining constness.
bool operator==(const CScriptWitness &) const =default
std::vector< std::vector< unsigned char > > stack