8 #ifndef PIVX_SCRIPT_SCRIPT_H
9 #define PIVX_SCRIPT_SCRIPT_H
25 typedef std::vector<unsigned char>
valtype;
27 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
30 static const int MAX_PUBKEYS_PER_MULTISIG = 20;
33 static const int MAX_SCRIPT_SIZE = 10000;
36 static const int MAX_STACK_SIZE = 1000;
40 static const unsigned int LOCKTIME_THRESHOLD = 500000000;
45 return std::vector<unsigned char>(in.begin(), in.end());
226 explicit CScriptNum(
const std::vector<unsigned char>& vch,
bool fRequireMinimal,
229 if (vch.size() > nMaxNumSize) {
232 if (fRequireMinimal && vch.size() > 0) {
239 if ((vch.back() & 0x7f) == 0) {
245 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
277 assert(
m_value != std::numeric_limits<int64_t>::min());
289 assert(rhs == 0 || (rhs > 0 &&
m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
290 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
297 assert(rhs == 0 || (rhs > 0 &&
m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
298 (rhs < 0 &&
m_value <= std::numeric_limits<int64_t>::max() + rhs));
305 if (
m_value > std::numeric_limits<int>::max())
306 return std::numeric_limits<int>::max();
307 else if (
m_value < std::numeric_limits<int>::min())
308 return std::numeric_limits<int>::min();
312 std::vector<unsigned char>
getvch()
const
317 static std::vector<unsigned char>
serialize(
const int64_t& value)
320 return std::vector<unsigned char>();
322 std::vector<unsigned char> result;
323 const bool neg = value < 0;
324 uint64_t absvalue = neg ? ~static_cast<uint64_t>(value) + 1 :
static_cast<uint64_t
>(value);
328 result.push_back(absvalue & 0xff);
342 if (result.back() & 0x80)
343 result.push_back(neg ? 0x80 : 0);
345 result.back() |= 0x80;
351 static int64_t
set_vch(
const std::vector<unsigned char>& vch)
357 for (
size_t i = 0; i != vch.size(); ++i)
358 result |=
static_cast<int64_t
>(vch[i]) << 8*i;
362 if (vch.back() & 0x80)
363 return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
385 if (n == -1 || (n >= 1 && n <= 16))
402 CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) :
CScriptBase(pbegin, pend) { }
432 if (opcode < 0 || opcode > 0xff)
433 throw std::runtime_error(
"CScript::operator<<() : invalid opcode");
450 else if (b.size() <= 0xff)
455 else if (b.size() <= 0xffff)
459 WriteLE16(
data, b.size());
466 WriteLE32(
data, b.size());
477 assert(!
"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
483 std::vector<unsigned char> vchKey = key.
Raw();
484 return (*
this) << vchKey;
492 bool fRet =
GetOp2(pc2, opcodeRet, &vchRet);
500 bool fRet =
GetOp2(pc2, opcodeRet,
nullptr);
507 return GetOp2(pc, opcodeRet, &vchRet);
512 return GetOp2(pc, opcodeRet,
nullptr);
526 unsigned int opcode = *pc++;
531 unsigned int nSize = 0;
546 nSize = ReadLE16(&pc[0]);
553 nSize = ReadLE32(&pc[0]);
556 if (
end() - pc < 0 || (
unsigned int)(
end() - pc) < nSize)
559 pvchRet->assign(pc, pc + nSize);
572 assert(opcode >=
OP_1 && opcode <=
OP_16);
573 return (
int)opcode - (int)(
OP_1 - 1);
577 assert(n >= 0 && n <= 16);
594 while (
static_cast<size_t>(
end() - pc) >= b.
size() && std::equal(b.
begin(), b.
end(), pc))
601 while (
GetOp(pc, opcode));
An encapsulated public key.
std::vector< unsigned char > Raw() const
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 CScriptNum &b)
CScript(const CScriptNum &b)
bool IsPayToColdStakingLOF() const
friend CScript operator+(const CScript &a, const CScript &b)
bool StartsWithOpcode(const opcodetype opcode) const
SERIALIZE_METHODS(CScript, obj)
CScript & operator<<(const std::vector< unsigned char > &b)
size_t DynamicMemoryUsage() const
CScript(std::vector< unsigned char >::const_iterator pbegin, std::vector< unsigned char >::const_iterator pend)
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
bool IsZerocoinSpend() const
int Find(opcodetype op) const
CScript & operator<<(const CPubKey &key)
bool GetOp(iterator &pc, opcodetype &opcodeRet)
bool IsZerocoinPublicSpend() const
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
CScript & operator<<(const CScript &b)
CScript(const unsigned char *pbegin, const unsigned char *pend)
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
int FindAndDelete(const CScript &b)
bool IsZerocoinMint() const
CScript & operator<<(int64_t b)
CScript(const std::vector< unsigned char > &b)
bool IsPayToPublicKeyHash() const
bool IsPayToColdStaking() const
CScript & push_int64(int64_t n)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
bool IsPayToExchangeAddress() const
static opcodetype EncodeOP_N(int n)
CScript & operator<<(opcodetype opcode)
CScript & operator+=(const CScript &b)
CScriptNum operator-(const int64_t &rhs) const
CScriptNum operator+(const int64_t &rhs) const
static std::vector< unsigned char > serialize(const int64_t &value)
CScriptNum operator-(const CScriptNum &rhs) const
CScriptNum & operator+=(const CScriptNum &rhs)
bool operator==(const CScriptNum &rhs) const
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)
CScriptNum & operator+=(const int64_t &rhs)
static const size_t nDefaultMaxNumSize
bool operator>=(const int64_t &rhs) const
CScriptNum & operator-=(const CScriptNum &rhs)
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.
CScriptNum & operator=(const int64_t &rhs)
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)
std::vector< unsigned char > getvch() 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...
void reserve(size_type new_capacity)
iterator insert(iterator pos, const T &value)
void push_back(const T &value)
scriptnum_error(const std::string &str)
#define T(expected, seed, data)
prevector< 28, unsigned char > CScriptBase
We use a prevector for the script to reduce the considerable memory overhead of vectors in cases wher...
std::vector< unsigned char > ToByteVector(const T &in)
const char * GetOpName(opcodetype opcode)
opcodetype
Script opcodes.
@ OP_CHECKCOLDSTAKEVERIFY
@ OP_CHECKCOLDSTAKEVERIFY_LOF
std::vector< unsigned char > valtype
#define READWRITEAS(type, obj)