40 for (
unsigned int i = 0; i < vch.size(); i++)
45 if (i == vch.size()-1 && vch[i] == 0x80)
57 #define stacktop(i) (stack.at(stack.size()+(i))) 58 #define altstacktop(i) (altstack.at(altstack.size()+(i))) 59 static inline void popstack(vector<valtype>& stack)
62 throw runtime_error(
"popstack(): stack empty");
67 if (vchPubKey.size() < 33) {
71 if (vchPubKey[0] == 0x04) {
72 if (vchPubKey.size() != 65) {
76 }
else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
77 if (vchPubKey.size() != 33) {
112 if (sig.size() < 9)
return false;
113 if (sig.size() > 73)
return false;
116 if (sig[0] != 0x30)
return false;
119 if (sig[1] != sig.size() - 3)
return false;
122 unsigned int lenR = sig[3];
125 if (5 + lenR >= sig.size())
return false;
128 unsigned int lenS = sig[5 + lenR];
132 if ((
size_t)(lenR + lenS + 7) != sig.size())
return false;
135 if (sig[2] != 0x02)
return false;
138 if (lenR == 0)
return false;
141 if (sig[4] & 0x80)
return false;
145 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80))
return false;
148 if (sig[lenR + 4] != 0x02)
return false;
151 if (lenS == 0)
return false;
154 if (sig[lenR + 6] & 0x80)
return false;
158 if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80))
return false;
167 std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - 1);
175 if (vchSig.size() == 0) {
188 if (vchSig.size() == 0) {
210 if (
data.size() == 0) {
212 return opcode ==
OP_0;
213 }
else if (
data.size() == 1 &&
data[0] >= 1 &&
data[0] <= 16) {
215 return opcode ==
OP_1 + (
data[0] - 1);
216 }
else if (
data.size() == 1 &&
data[0] == 0x81) {
219 }
else if (
data.size() <= 75) {
221 return opcode ==
data.size();
222 }
else if (
data.size() <= 255) {
225 }
else if (
data.size() <= 65535) {
238 static const valtype vchFalse(0);
239 static const valtype vchZero(0);
240 static const valtype vchTrue(1, 1);
248 vector<valtype> altstack;
250 if (script.
size() > 10000)
259 bool fExec = !
count(vfExec.begin(), vfExec.end(),
false);
264 if (!script.
GetOp(pc, opcode, vchPushValue))
294 stack.push_back(vchPushValue);
321 stack.push_back(bn.
getvch());
344 if (stack.size() < 1)
386 if (stack.size() < 1)
428 if (stack.size() < 1)
436 vfExec.push_back(fValue);
444 vfExec.back() = !vfExec.back();
460 if (stack.size() < 1)
482 if (stack.size() < 1)
491 if (altstack.size() < 1)
501 if (stack.size() < 2)
511 if (stack.size() < 2)
515 stack.push_back(vch1);
516 stack.push_back(vch2);
523 if (stack.size() < 3)
528 stack.push_back(vch1);
529 stack.push_back(vch2);
530 stack.push_back(vch3);
537 if (stack.size() < 4)
541 stack.push_back(vch1);
542 stack.push_back(vch2);
549 if (stack.size() < 6)
553 stack.erase(stack.end()-6, stack.end()-4);
554 stack.push_back(vch1);
555 stack.push_back(vch2);
562 if (stack.size() < 4)
572 if (stack.size() < 1)
576 stack.push_back(vch);
584 stack.push_back(bn.getvch());
591 if (stack.size() < 1)
600 if (stack.size() < 1)
603 stack.push_back(vch);
610 if (stack.size() < 2)
612 stack.erase(stack.end() - 2);
619 if (stack.size() < 2)
622 stack.push_back(vch);
631 if (stack.size() < 2)
635 if (n < 0 || n >= (
int)stack.size())
639 stack.erase(stack.end()-
n-1);
640 stack.push_back(vch);
649 if (stack.size() < 3)
659 if (stack.size() < 2)
668 if (stack.size() < 2)
671 stack.insert(stack.end()-2, vch);
679 if (stack.size() < 1)
682 stack.push_back(bn.
getvch());
695 if (stack.size() < 2)
699 bool fEqual = (vch1 == vch2);
707 stack.push_back(fEqual ? vchTrue : vchFalse);
730 if (stack.size() < 1)
735 case OP_1ADD: bn += bnOne;
break;
736 case OP_1SUB: bn -= bnOne;
break;
738 case OP_ABS:
if (bn < bnZero) bn = -bn;
break;
739 case OP_NOT: bn = (bn == bnZero);
break;
741 default: assert(!
"invalid opcode");
break;
744 stack.push_back(bn.
getvch());
763 if (stack.size() < 2)
778 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero);
break;
779 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero);
break;
787 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2);
break;
788 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2);
break;
789 default: assert(!
"invalid opcode");
break;
793 stack.push_back(bn.
getvch());
808 if (stack.size() < 3)
813 bool fValue = (bn2 <= bn1 && bn1 < bn3);
817 stack.push_back(fValue ? vchTrue : vchFalse);
832 if (stack.size() < 1)
847 stack.push_back(vchHash);
862 if (stack.size() < 2)
869 CScript scriptCode(pbegincodehash, pend);
878 bool fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode);
882 stack.push_back(fSuccess ? vchTrue : vchFalse);
899 if ((
int)stack.size() < i)
905 nOpCount += nKeysCount;
910 if ((
int)stack.size() < i)
914 if (nSigsCount < 0 || nSigsCount > nKeysCount)
918 if ((
int)stack.size() < i)
922 CScript scriptCode(pbegincodehash, pend);
925 for (
int k = 0; k < nSigsCount; k++)
931 bool fSuccess =
true;
932 while (fSuccess && nSigsCount > 0)
946 bool fOk = checker.
CheckSig(vchSig, vchPubKey, scriptCode);
958 if (nSigsCount > nKeysCount)
972 if (stack.size() < 1)
978 stack.push_back(fSuccess ? vchTrue : vchFalse);
995 if (stack.size() + altstack.size() > 1000)
1004 if (!vfExec.empty())
1007 return set_success(serror);
1016 class CTransactionSignatureSerializer {
1020 const unsigned int nIn;
1021 const bool fAnyoneCanPay;
1022 const bool fHashSingle;
1023 const bool fHashNone;
1026 CTransactionSignatureSerializer(
const CTransaction &txToIn,
const CScript &scriptCodeIn,
unsigned int nInIn,
int nHashTypeIn) :
1027 txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1033 template<
typename S>
1034 void SerializeScriptCode(
S &s,
int nType,
int nVersion)
const {
1038 unsigned int nCodeSeparators = 0;
1039 while (scriptCode.
GetOp(it, opcode)) {
1045 while (scriptCode.
GetOp(it, opcode)) {
1047 s.write((
char*)&itBegin[0], it-itBegin-1);
1051 if (itBegin != scriptCode.
end())
1052 s.write((
char*)&itBegin[0], it-itBegin);
1056 template<
typename S>
1057 void SerializeInput(
S &s,
unsigned int nInput,
int nType,
int nVersion)
const {
1068 SerializeScriptCode(s, nType, nVersion);
1070 if (nInput != nIn && (fHashSingle || fHashNone))
1078 template<
typename S>
1079 void SerializeOutput(
S &s,
unsigned int nOutput,
int nType,
int nVersion)
const {
1080 if (fHashSingle && nOutput != nIn)
1088 template<
typename S>
1089 void Serialize(
S &s,
int nType,
int nVersion)
const {
1093 unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.
vin.size();
1095 for (
unsigned int nInput = 0; nInput < nInputs; nInput++)
1096 SerializeInput(s, nInput, nType, nVersion);
1098 unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.
vout.size());
1100 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)
1101 SerializeOutput(s, nOutput, nType, nVersion);
1111 static const uint256 one(
uint256S(
"0000000000000000000000000000000000000000000000000000000000000001"));
1112 if (nIn >= txTo.
vin.size()) {
1119 if (nIn >= txTo.
vout.size()) {
1126 CTransactionSignatureSerializer txTmp(txTo, scriptCode, nIn, nHashType);
1130 ss << txTmp << nHashType;
1136 return pubkey.
Verify(sighash, vchSig);
1146 vector<unsigned char> vchSig(vchSigIn);
1149 int nHashType = vchSig.back();
1154 if (!VerifySignature(vchSig, pubkey, sighash))
1177 if (nLockTime > (int64_t)txTo->
nLockTime)
1200 const int64_t txToSequence = (int64_t)txTo->
vin[nIn].nSequence;
1204 if (static_cast<uint32_t>(txTo->
nVersion) < 2)
1217 const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
1218 const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
1236 if (nSequenceMasked > txToSequenceMasked)
1250 vector<vector<unsigned char> > stack, stackCopy;
1272 swap(stack, stackCopy);
1277 assert(!stack.empty());
1279 const valtype& pubKeySerialized = stack.back();
1280 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1299 if (stack.size() != 1) {
1304 return set_success(serror);
static const int MAX_PUBKEYS_PER_MULTISIG
virtual bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode) const
enum ScriptError_t ScriptError
void Serialize(Stream &s, char a, int, int=0)
static const uint32_t SEQUENCE_FINAL
bool CheckLockTime(const CScriptNum &nLockTime) const
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
std::vector< unsigned char > getvch() const
int FindAndDelete(const CScript &b)
CRIPEMD160 & Write(const unsigned char *data, size_t len)
static bool CheckMinimalPush(const valtype &data, opcodetype opcode)
static const int MAX_OPS_PER_SCRIPT
static const uint32_t SEQUENCE_LOCKTIME_MASK
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode) const
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
static void popstack(vector< valtype > &stack)
prevector< 28, unsigned char > CScriptBase
bool EvalScript(vector< vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
uint256 uint256S(const char *str)
#define S(x0, x1, x2, x3, cb, r)
bool CheckSignatureEncoding(const vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
bool CheckSequence(const CScriptNum &nSequence) const
static bool IsValidSignatureEncoding(const std::vector< unsigned char > &sig)
bool IsPayToScriptHash() const
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
V::value_type * begin_ptr(V &v)
uint256 SignatureHash(const CScript &scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
virtual bool CheckSequence(const CScriptNum &nSequence) const
const std::vector< CTxIn > vin
CSHA256 & Write(const unsigned char *data, size_t len)
CHash160 & Write(const unsigned char *data, size_t len)
static bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey)
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
vector< unsigned char > valtype
CSHA1 & Write(const unsigned char *data, size_t len)
bool IsPushOnly(const_iterator pc) const
const std::vector< CTxOut > vout
static bool CheckPubKeyEncoding(const valtype &vchSig, unsigned int flags, ScriptError *serror)
static bool IsLowDERSignature(const valtype &vchSig, ScriptError *serror)
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
static const unsigned int LOCKTIME_THRESHOLD
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
void WriteCompactSize(Stream &os, uint64_t nSize)
bool CastToBool(const valtype &vch)
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
static bool IsDefinedHashtypeSignature(const valtype &vchSig)
CHash256 & Write(const unsigned char *data, size_t len)