6 #ifndef BITCOIN_SCRIPT_SCRIPT_H 7 #define BITCOIN_SCRIPT_SCRIPT_H 41 template <
typename T> std::vector<uint8_t>
ToByteVector(
const T &in) {
42 return std::vector<uint8_t>(in.begin(), in.end());
212 :
std::runtime_error(str) {}
226 static const size_t MAXIMUM_ELEMENT_SIZE = 4;
230 explicit CScriptNum(
const std::vector<uint8_t> &vch,
bool fRequireMinimal,
231 const size_t nMaxNumSize = MAXIMUM_ELEMENT_SIZE) {
232 if (vch.size() > nMaxNumSize) {
235 if (fRequireMinimal && !IsMinimallyEncoded(vch, nMaxNumSize)) {
238 m_value = set_vch(vch);
241 static bool IsMinimallyEncoded(
242 const std::vector<uint8_t> &vch,
245 static bool MinimallyEncode(std::vector<uint8_t> &data);
247 inline bool operator==(
const int64_t &rhs)
const {
return m_value == rhs; }
248 inline bool operator!=(
const int64_t &rhs)
const {
return m_value != rhs; }
249 inline bool operator<=(
const int64_t &rhs)
const {
return m_value <= rhs; }
250 inline bool operator<(
const int64_t &rhs)
const {
return m_value < rhs; }
251 inline bool operator>=(
const int64_t &rhs)
const {
return m_value >= rhs; }
252 inline bool operator>(
const int64_t &rhs)
const {
return m_value > rhs; }
258 return operator!=(rhs.
m_value);
261 return operator<=(rhs.
m_value);
267 return operator>=(rhs.
m_value);
301 return operator+=(rhs.
m_value);
304 return operator-=(rhs.
m_value);
315 return operator&=(rhs.
m_value);
319 assert(m_value != std::numeric_limits<int64_t>::min());
331 (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
332 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
340 (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
341 (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
352 if (m_value > std::numeric_limits<int>::max()) {
353 return std::numeric_limits<int>::max();
354 }
else if (m_value < std::numeric_limits<int>::min()) {
355 return std::numeric_limits<int>::min();
360 std::vector<uint8_t>
getvch()
const {
return serialize(m_value); }
362 static std::vector<uint8_t>
serialize(
const int64_t &value) {
367 std::vector<uint8_t> result;
368 const bool neg = value < 0;
369 uint64_t absvalue = neg ? -value : value;
372 result.push_back(absvalue & 0xff);
384 if (result.back() & 0x80) {
385 result.push_back(neg ? 0x80 : 0);
387 result.back() |= 0x80;
394 static int64_t
set_vch(
const std::vector<uint8_t> &vch) {
400 for (
size_t i = 0; i != vch.size(); ++i) {
401 result |= int64_t(vch[i]) << 8 * i;
406 if (vch.back() & 0x80) {
407 return -int64_t(result & ~(0x80ULL << (8 * (vch.size() - 1))));
426 std::vector<uint8_t> *pvchRet);
432 if (n == -1 || (n >= 1 && n <= 16)) {
433 push_back(n + (
OP_1 - 1));
446 CScript(std::vector<uint8_t>::const_iterator pbegin,
447 std::vector<uint8_t>::const_iterator pend)
449 CScript(
const uint8_t *pbegin,
const uint8_t *pend)
454 template <
typename Stream,
typename Operation>
460 reserve(size() + b.
size());
477 explicit CScript(
const std::vector<uint8_t> &b) =
delete;
482 if (opcode < 0 || opcode > 0xff) {
483 throw std::runtime_error(
"CScript::operator<<(): invalid opcode");
485 insert(end(), uint8_t(opcode));
494 CScript &operator<<(const std::vector<uint8_t> &b) {
496 insert(end(), uint8_t(b.size()));
497 }
else if (b.size() <= 0xff) {
499 insert(end(), uint8_t(b.size()));
500 }
else if (b.size() <= 0xffff) {
504 insert(end(), _data, _data +
sizeof(_data));
509 insert(end(), _data, _data +
sizeof(_data));
511 insert(end(), b.begin(), b.end());
519 assert(!
"Warning: Pushing a CScript onto a CScript with << is probably " 520 "not intended, use + to concatenate!");
525 std::vector<uint8_t> &vchRet)
const {
535 if (opcode ==
OP_0) {
539 assert(opcode >=
OP_1 && opcode <=
OP_16);
540 return int(opcode) - int(
OP_1 - 1);
543 assert(n >= 0 && n <= 16);
551 bool IsPayToScriptHash()
const;
552 bool IsCommitment(
const std::vector<uint8_t> &data)
const;
553 bool IsWitnessProgram(
int &version, std::vector<uint8_t> &program)
const;
554 bool IsWitnessProgram()
const;
561 bool IsPushOnly()
const;
564 bool HasValidOps()
const;
572 return (size() > 0 && *begin() ==
OP_RETURN) ||
583 #endif // BITCOIN_SCRIPT_SCRIPT_H
static const int MAX_PUBKEYS_PER_MULTISIG
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
static int64_t set_vch(const std::vector< uint8_t > &vch)
static void WriteLE32(uint8_t *ptr, uint32_t x)
static const size_t MAXIMUM_ELEMENT_SIZE
Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
CScript(const uint8_t *pbegin, const uint8_t *pend)
static const unsigned int MAX_OPCODE
bool operator==(const CNetAddr &a, const CNetAddr &b)
bool operator<(const int64_t &rhs) const
CScriptNum(const int64_t &n)
CScriptNum & operator-=(const int64_t &rhs)
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 void WriteLE16(uint8_t *ptr, uint16_t x)
CScript & push_int64(int64_t n)
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
CScriptNum operator%(const CScriptNum &rhs) const
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
friend CScript operator+(const CScript &a, const CScript &b)
bool operator<=(const int64_t &rhs) const
#define READWRITEAS(type, obj)
CScriptNum & operator=(const int64_t &rhs)
std::ostream & operator<<(std::ostream &os, const PeerMessagingState &state)
static const int MAX_OPS_PER_SCRIPT
const char * GetOpName(opcodetype opcode)
CScriptNum & operator+=(const CScriptNum &rhs)
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
bool operator<(const CScriptNum &rhs) const
CScript & operator<<(const CScriptNum &b)
CScript & operator<<(opcodetype opcode)
CScriptNum operator/(const int64_t &rhs) const
prevector< 28, uint8_t > CScriptBase
We use a prevector for the script to reduce the considerable memory overhead of vectors in cases wher...
opcodetype
Script opcodes.
CScript(const CScriptNum &b)
bool operator==(const CScriptNum &rhs) const
CScript & operator<<(const CScript &b)
CScriptNum operator+(const int64_t &rhs) const
bilingual_str operator+(bilingual_str lhs, const bilingual_str &rhs)
bool operator!=(const int64_t &rhs) const
CScriptNum operator-() const
bool operator>=(const CScriptNum &rhs) const
CScriptNum operator+(const CScriptNum &rhs) const
scriptnum_error(const std::string &str)
CScript(std::vector< uint8_t >::const_iterator pbegin, std::vector< uint8_t >::const_iterator pend)
bool operator>(const CScriptNum &rhs) const
std::vector< uint8_t > ToByteVector(const T &in)
bool operator>(const int64_t &rhs) const
static const int MAX_SCRIPT_SIZE
static const int MAX_STACK_SIZE
static std::vector< uint8_t > serialize(const int64_t &value)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
CScriptNum operator%(const int64_t &rhs) const
CScriptNum operator/(const CScriptNum &rhs) const
bool operator<(const CNetAddr &a, const CNetAddr &b)
CScriptNum & operator-=(const CScriptNum &rhs)
CScriptNum operator-(const CScriptNum &rhs) const
std::vector< uint8_t > getvch() const
CScriptNum operator-(const int64_t &rhs) const
CScript(const_iterator pbegin, const_iterator pend)
static opcodetype EncodeOP_N(int n)
Serialized script, used inside transaction inputs and outputs.
CScript & operator+=(const CScript &b)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
CScript & operator<<(int64_t b)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
CScriptNum & operator+=(const int64_t &rhs)
static const unsigned int LOCKTIME_THRESHOLD
void SerializationOp(Stream &s, Operation ser_action)
CScriptNum(const std::vector< uint8_t > &vch, bool fRequireMinimal, const size_t nMaxNumSize=MAXIMUM_ELEMENT_SIZE)
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< uint8_t > *pvchRet)
bool operator==(const int64_t &rhs) const
bool operator>=(const int64_t &rhs) const
bool operator!=(const CScriptNum &rhs) const
bool operator<=(const CScriptNum &rhs) const