10 #include "hdacscript.h" 13 std::vector<unsigned char> ToByteVector(
const T& in)
15 return std::vector<unsigned char>(in.begin(), in.end());
61 OP_FROMALTSTACK = 0x6c,
93 OP_EQUALVERIFY = 0x88,
118 OP_NUMEQUALVERIFY = 0x9d,
119 OP_NUMNOTEQUAL = 0x9e,
121 OP_GREATERTHAN = 0xa0,
122 OP_LESSTHANOREQUAL = 0xa1,
123 OP_GREATERTHANOREQUAL = 0xa2,
135 OP_CODESEPARATOR = 0xab,
137 OP_CHECKSIGVERIFY = 0xad,
138 OP_CHECKMULTISIG = 0xae,
139 OP_CHECKMULTISIGVERIFY = 0xaf,
158 OP_SMALLINTEGER = 0xfa,
160 OP_PUBKEYHASH = 0xfd,
163 OP_INVALIDOPCODE = 0xff,
169 explicit scriptnum_error(
const std::string& str) : std::runtime_error(str) {}
189 explicit CScriptNum(
const std::vector<unsigned char>& vch,
bool fRequireMinimal)
191 if (vch.size() > nMaxNumSize) {
194 if (fRequireMinimal && vch.size() > 0) {
201 if ((vch.back() & 0x7f) == 0) {
207 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
212 m_value = set_vch(vch);
215 inline bool operator==(
const int64_t& rhs)
const {
return m_value == rhs; }
216 inline bool operator!=(
const int64_t& rhs)
const {
return m_value != rhs; }
217 inline bool operator<=(
const int64_t& rhs)
const {
return m_value <= rhs; }
218 inline bool operator< (
const int64_t& rhs)
const {
return m_value < rhs; }
219 inline bool operator>=(
const int64_t& rhs)
const {
return m_value >= rhs; }
220 inline bool operator> (
const int64_t& rhs)
const {
return m_value > rhs; }
222 inline bool operator==(
const CScriptNum& rhs)
const {
return operator==(rhs.m_value); }
223 inline bool operator!=(
const CScriptNum& rhs)
const {
return operator!=(rhs.m_value); }
224 inline bool operator<=(
const CScriptNum& rhs)
const {
return operator<=(rhs.m_value); }
225 inline bool operator< (
const CScriptNum& rhs)
const {
return operator< (rhs.m_value); }
226 inline bool operator>=(
const CScriptNum& rhs)
const {
return operator>=(rhs.m_value); }
227 inline bool operator> (
const CScriptNum& rhs)
const {
return operator> (rhs.m_value); }
239 assert(m_value != std::numeric_limits<int64_t>::min());
243 inline CScriptNum& operator=(
const int64_t& rhs)
249 inline CScriptNum& operator+=(
const int64_t& rhs)
251 assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
252 (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
257 inline CScriptNum& operator-=(
const int64_t& rhs)
259 assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
260 (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
267 if (m_value > std::numeric_limits<int>::max())
268 return std::numeric_limits<int>::max();
269 else if (m_value < std::numeric_limits<int>::min())
270 return std::numeric_limits<int>::min();
274 std::vector<unsigned char> getvch()
const 276 return serialize(m_value);
279 static std::vector<unsigned char> serialize(
const int64_t& value)
282 return std::vector<unsigned char>();
284 std::vector<unsigned char> result;
285 const bool neg = value < 0;
286 uint64_t absvalue = neg ? -value : value;
290 result.push_back(absvalue & 0xff);
304 if (result.back() & 0x80)
305 result.push_back(neg ? 0x80 : 0);
307 result.back() |= 0x80;
312 static const size_t nMaxNumSize = 4;
315 static int64_t set_vch(
const std::vector<unsigned char>& vch)
321 for (
size_t i = 0; i != vch.size(); ++i)
322 result |= static_cast<int64_t>(vch[i]) << 8*i;
326 if (vch.back() & 0x80)
327 return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
336 class CScript :
public std::vector<unsigned char>
341 if (n == -1 || (n >= 1 && n <= 16))
343 push_back((
unsigned char)(n + (OP_1 - 1)));
351 *
this << CScriptNum::serialize(n);
357 CScript(
const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
358 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
359 CScript(
const unsigned char* pbegin,
const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
363 insert(end(), b.begin(), b.end());
374 CScript(int64_t b) { operator<<(b); }
376 explicit CScript(opcodetype b) { operator<<(b); }
378 explicit CScript(
const std::vector<unsigned char>& b) { operator<<(b); }
381 CScript& operator<<(int64_t b) {
return push_int64(b); }
383 CScript& operator<<(opcodetype opcode)
385 if (opcode < 0 || opcode > 0xff)
386 throw std::runtime_error(
"CScript::operator<<() : invalid opcode");
387 insert(end(), (
unsigned char)opcode);
397 CScript& operator<<(const std::vector<unsigned char>& b)
399 if (b.size() < OP_PUSHDATA1)
401 insert(end(), (
unsigned char)b.size());
403 else if (b.size() <= 0xff)
405 insert(end(), OP_PUSHDATA1);
406 insert(end(), (
unsigned char)b.size());
408 else if (b.size() <= 0xffff)
410 insert(end(), OP_PUSHDATA2);
411 unsigned short nSize = (
unsigned short)b.size();
412 insert(end(), (
unsigned char*)&nSize, (
unsigned char*)&nSize +
sizeof(nSize));
416 insert(end(), OP_PUSHDATA4);
417 unsigned int nSize = b.size();
418 insert(end(), (
unsigned char*)&nSize, (
unsigned char*)&nSize +
sizeof(nSize));
420 insert(end(), b.begin(), b.end());
428 assert(!
"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
433 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
436 const_iterator pc2 = pc;
437 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
438 pc = begin() + (pc2 - begin());
442 bool GetOp(iterator& pc, opcodetype& opcodeRet)
444 const_iterator pc2 = pc;
445 bool fRet = GetOp2(pc2, opcodeRet, NULL);
446 pc = begin() + (pc2 - begin());
450 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
const 452 return GetOp2(pc, opcodeRet, &vchRet);
455 bool GetOp(const_iterator& pc, opcodetype& opcodeRet)
const 457 return GetOp2(pc, opcodeRet, NULL);
460 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
const 462 opcodeRet = OP_INVALIDOPCODE;
471 unsigned int opcode = *pc++;
474 if (opcode <= OP_PUSHDATA4)
476 unsigned int nSize = 0;
477 if (opcode < OP_PUSHDATA1)
481 else if (opcode == OP_PUSHDATA1)
487 else if (opcode == OP_PUSHDATA2)
492 memcpy(&nSize, &pc[0], 2);
495 else if (opcode == OP_PUSHDATA4)
499 memcpy(&nSize, &pc[0], 4);
502 if (end() - pc < 0 || (
unsigned int)(end() - pc) < nSize)
505 pvchRet->assign(pc, pc + nSize);
509 opcodeRet = (opcodetype)opcode;
518 assert(opcode >= OP_1 && opcode <= OP_16);
519 return (
int)opcode - (int)(OP_1 - 1);
521 static opcodetype EncodeOP_N(
int n)
523 assert(n >= 0 && n <= 16);
526 return (opcodetype)(OP_1+n-1);
529 int FindAndDelete(
const CScript& b)
534 iterator pc = begin();
538 while (end() - pc >= (
long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
540 pc = erase(pc, pc + b.size());
544 while (GetOp(pc, opcode));
547 int Find(opcodetype op)
const 551 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
564 unsigned int GetSigOpCount(
bool fAccurate)
const;
570 unsigned int GetSigOpCount(
const CScript& scriptSig)
const;
572 bool IsPayToScriptHash()
const;
575 bool IsPushOnly()
const;
577 bool HasSmallIntegerInTheBeginning()
const;
586 int op_drop_offset[2];
588 int op_return_offset,op_return_size;
589 return (mc_ParseOpDropOpReturnScript((
unsigned char*)&begin()[0],(
int)size(),op_drop_offset,op_drop_size,2,&op_return_offset,&op_return_size)) != NULL;
592 std::string ToString()
const;
599 std::vector<unsigned char>().swap(*
this);
603 const char* GetOpName(opcodetype opcode);
static int DecodeOP_N(opcodetype opcode)
Definition: script.h:514
CScriptNum(const int64_t &n)
Definition: script.h:184
bool IsUnspendable() const
Definition: script.h:584