16 typedef std::vector<unsigned char>
valtype;
39 for (
unsigned int i = 0; i < vch.size(); i++)
44 if (i == vch.size() - 1 && vch[i] == 0x80)
56 #define stacktop(i) (stack.at(stack.size()+(i))) 57 #define altstacktop(i) (altstack.at(altstack.size()+(i))) 59 static inline void popstack(std::vector<valtype> &stack)
62 throw std::runtime_error(
"popstack(): stack empty");
66 bool static IsCompressedOrUncompressedPubKey(
const valtype &vchPubKey)
68 if (vchPubKey.size() < 33)
73 if (vchPubKey[0] == 0x04)
75 if (vchPubKey.size() != 65)
81 else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03)
83 if (vchPubKey.size() != 33)
97 bool static IsCompressedPubKey(
const valtype &vchPubKey)
99 if (vchPubKey.size() != 33)
104 if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03)
122 bool static IsValidSignatureEncoding(
const std::vector<unsigned char> &sig)
137 if (sig.size() < 9)
return false;
138 if (sig.size() > 73)
return false;
141 if (sig[0] != 0x30)
return false;
144 if (sig[1] != sig.size() - 3)
return false;
147 unsigned int lenR = sig[3];
150 if (5 + lenR >= sig.size())
return false;
153 unsigned int lenS = sig[5 + lenR];
157 if ((
size_t) (lenR + lenS + 7) != sig.size())
return false;
160 if (sig[2] != 0x02)
return false;
163 if (lenR == 0)
return false;
166 if (sig[4] & 0x80)
return false;
170 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80))
return false;
173 if (sig[lenR + 4] != 0x02)
return false;
176 if (lenS == 0)
return false;
179 if (sig[lenR + 6] & 0x80)
return false;
183 if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80))
return false;
190 if (!IsValidSignatureEncoding(vchSig))
194 std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - 1);
202 bool static IsDefinedHashtypeSignature(
const valtype &vchSig)
204 if (vchSig.size() == 0)
219 if (vchSig.size() == 0)
255 if (data.size() == 0)
258 return opcode ==
OP_0;
260 else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16)
263 return opcode ==
OP_1 + (data[0] - 1);
265 else if (data.size() == 1 && data[0] == 0x81)
270 else if (data.size() <= 75)
273 return opcode == data.size();
275 else if (data.size() <= 255)
280 else if (data.size() <= 65535)
294 static const valtype vchFalse(0);
296 static const valtype vchTrue(1, 1);
303 std::vector<bool> vfExec;
304 std::vector<valtype> altstack;
306 if (script.
size() > MAX_SCRIPT_SIZE)
315 bool fExec = !count(vfExec.begin(), vfExec.end(),
false);
320 if (!script.
GetOp(pc, opcode, vchPushValue))
322 if (vchPushValue.size() > MAX_SCRIPT_ELEMENT_SIZE)
326 if (opcode >
OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT)
348 if (fRequireMinimal && !CheckMinimalPush(vchPushValue, opcode))
352 stack.push_back(vchPushValue);
381 stack.push_back(bn.
getvch());
404 if (stack.size() < 1)
447 if (stack.size() < 1)
493 if (stack.size() < 1)
500 if (vch.size() == 1 && vch[0] != 1)
508 vfExec.push_back(fValue);
515 vfExec.back() = !vfExec.back();
530 if (stack.size() < 1)
550 if (stack.size() < 1)
558 if (altstack.size() < 1)
567 if (stack.size() < 2)
576 if (stack.size() < 2)
580 stack.push_back(vch1);
581 stack.push_back(vch2);
587 if (stack.size() < 3)
592 stack.push_back(vch1);
593 stack.push_back(vch2);
594 stack.push_back(vch3);
601 if (stack.size() < 4)
605 stack.push_back(vch1);
606 stack.push_back(vch2);
613 if (stack.size() < 6)
617 stack.erase(stack.end() - 6, stack.end() - 4);
618 stack.push_back(vch1);
619 stack.push_back(vch2);
626 if (stack.size() < 4)
636 if (stack.size() < 1)
640 stack.push_back(vch);
648 stack.push_back(bn.getvch());
655 if (stack.size() < 1)
664 if (stack.size() < 1)
667 stack.push_back(vch);
674 if (stack.size() < 2)
676 stack.erase(stack.end() - 2);
683 if (stack.size() < 2)
686 stack.push_back(vch);
695 if (stack.size() < 2)
699 if (n < 0 || n >= (
int) stack.size())
703 stack.erase(stack.end() - n - 1);
704 stack.push_back(vch);
713 if (stack.size() < 3)
723 if (stack.size() < 2)
732 if (stack.size() < 2)
735 stack.insert(stack.end() - 2, vch);
743 if (stack.size() < 1)
746 stack.push_back(bn.
getvch());
759 if (stack.size() < 2)
763 bool fEqual = (vch1 == vch2);
771 stack.push_back(fEqual ? vchTrue : vchFalse);
794 if (stack.size() < 1)
809 if (bn < bnZero) bn = -bn;
818 assert(!
"invalid opcode");
822 stack.push_back(bn.
getvch());
841 if (stack.size() < 2)
857 bn = (bn1 != bnZero && bn2 != bnZero);
860 bn = (bn1 != bnZero || bn2 != bnZero);
884 bn = (bn1 < bn2 ? bn1 : bn2);
887 bn = (bn1 > bn2 ? bn1 : bn2);
890 assert(!
"invalid opcode");
895 stack.push_back(bn.
getvch());
910 if (stack.size() < 3)
915 bool fValue = (bn2 <= bn1 && bn1 < bn3);
919 stack.push_back(fValue ? vchTrue : vchFalse);
934 if (stack.size() < 1)
942 CSHA1().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
944 CSHA256().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
946 CHash160().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
948 CHash256().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
950 stack.push_back(vchHash);
965 if (stack.size() < 2)
972 CScript scriptCode(pbegincodehash, pend);
981 !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror))
986 bool fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
993 stack.push_back(fSuccess ? vchTrue : vchFalse);
1010 if ((
int) stack.size() < i)
1014 if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)
1016 nOpCount += nKeysCount;
1017 if (nOpCount > MAX_OPS_PER_SCRIPT)
1022 int ikey2 = nKeysCount + 2;
1024 if ((
int) stack.size() < i)
1028 if (nSigsCount < 0 || nSigsCount > nKeysCount)
1032 if ((
int) stack.size() < i)
1036 CScript scriptCode(pbegincodehash, pend);
1039 for (
int k = 0; k < nSigsCount; k++)
1048 bool fSuccess =
true;
1049 while (fSuccess && nSigsCount > 0)
1058 !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror))
1065 bool fOk = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
1078 if (nSigsCount > nKeysCount)
1099 if (stack.size() < 1)
1105 stack.push_back(fSuccess ? vchTrue : vchFalse);
1128 if (stack.size() + altstack.size() > MAX_STACK_SIZE)
1137 if (!vfExec.empty())
1140 return set_success(serror);
1150 class CTransactionSignatureSerializer
1155 const unsigned int nIn;
1156 const bool fAnyoneCanPay;
1157 const bool fHashSingle;
1158 const bool fHashNone;
1161 CTransactionSignatureSerializer(
const CTransaction &txToIn,
const CScript &scriptCodeIn,
unsigned int nInIn,
int nHashTypeIn)
1163 txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1170 template<
typename S>
1171 void SerializeScriptCode(
S &s)
const 1176 unsigned int nCodeSeparators = 0;
1177 while (scriptCode.
GetOp(it, opcode))
1184 while (scriptCode.
GetOp(it, opcode))
1188 s.write((
char *) &itBegin[0], it - itBegin - 1);
1192 if (itBegin != scriptCode.
end())
1193 s.write((
char *) &itBegin[0], it - itBegin);
1197 template<
typename S>
1198 void SerializeInput(
S &s,
unsigned int nInput)
const 1210 SerializeScriptCode(s);
1212 if (nInput != nIn && (fHashSingle || fHashNone))
1220 template<
typename S>
1221 void SerializeOutput(
S &s,
unsigned int nOutput)
const 1223 if (fHashSingle && nOutput != nIn)
1231 template<
typename S>
1237 unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.
vin.size();
1239 for (
unsigned int nInput = 0; nInput < nInputs; nInput++) SerializeInput(s, nInput);
1241 unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn + 1 : txTo.
vout.size());
1243 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) SerializeOutput(s, nOutput);
1252 for (
const auto &txin : txTo.
vin)
1262 for (
const auto &txin : txTo.
vin)
1264 ss << txin.nSequence;
1272 for (
const auto &txout : txTo.
vout)
1286 hashPrevouts = GetPrevoutHash(txTo);
1287 hashSequence = GetSequenceHash(txTo);
1288 hashOutputs = GetOutputsHash(txTo);
1295 assert(nIn < txTo.
vin.size());
1302 const bool cacheready = cache && cache->
ready;
1306 hashPrevouts = cacheready ? cache->
hashPrevouts : GetPrevoutHash(txTo);
1311 hashSequence = cacheready ? cache->
hashSequence : GetSequenceHash(txTo);
1317 hashOutputs = cacheready ? cache->
hashOutputs : GetOutputsHash(txTo);
1322 ss << txTo.
vout[nIn];
1335 ss << txTo.
vin[nIn].prevout;
1338 ss << txTo.
vin[nIn].nSequence;
1349 static const uint256 one(
uint256S(
"0000000000000000000000000000000000000000000000000000000000000001"));
1354 if (nIn >= txTo.
vout.size())
1362 CTransactionSignatureSerializer txTmp(txTo, scriptCode, nIn, nHashType);
1366 ss << txTmp << nHashType;
1372 return pubkey.
Verify(sighash, vchSig);
1382 std::vector<unsigned char> vchSig(vchSigIn);
1385 int nHashType = vchSig.back();
1388 uint256 sighash =
SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata);
1390 if (!VerifySignature(vchSig, pubkey, sighash))
1405 if (!((txTo->nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) || (txTo->nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD)))
1410 if (nLockTime > (int64_t) txTo->nLockTime)
1433 const int64_t txToSequence = (int64_t) txTo->vin[nIn].nSequence;
1437 if (static_cast<uint32_t>(txTo->nVersion) < 2)
1450 const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
1451 const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
1470 if (nSequenceMasked > txToSequenceMasked)
1478 std::vector<std::vector<unsigned char> > stack;
1481 if (witversion == 0)
1483 if (program.size() == 32)
1486 if (witness.
stack.size() == 0)
1490 scriptPubKey =
CScript(witness.
stack.back().begin(), witness.
stack.back().end());
1491 stack = std::vector<std::vector<unsigned char> >(witness.
stack.begin(), witness.
stack.end() - 1);
1493 CSHA256().
Write(&scriptPubKey[0], scriptPubKey.
size()).Finalize(hashScriptPubKey.begin());
1494 if (memcmp(hashScriptPubKey.begin(), program.data(), 32))
1499 else if (program.size() == 20)
1502 if (witness.
stack.size() != 2)
1507 stack = witness.
stack;
1521 return set_success(serror);
1525 for (
unsigned int i = 0; i < stack.size(); i++)
1527 if (stack.at(i).size() > MAX_SCRIPT_ELEMENT_SIZE)
1537 if (stack.size() != 1)
1548 if (witness ==
nullptr)
1550 witness = &emptyWitness;
1552 bool hadWitness =
false;
1561 std::vector<std::vector<unsigned char> > stack, stackCopy;
1583 std::vector<unsigned char> witnessprogram;
1589 if (scriptSig.
size() != 0)
1594 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror))
1612 swap(stack, stackCopy);
1617 assert(!stack.empty());
1619 const valtype &pubKeySerialized = stack.back();
1620 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1632 if (flags & SCRIPT_VERIFY_WITNESS)
1634 if (pubKey2.IsWitnessProgram(witnessversion, witnessprogram))
1637 if (scriptSig !=
CScript() << std::vector<unsigned char>(pubKey2.begin(), pubKey2.end()))
1643 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror))
1661 assert((flags & SCRIPT_VERIFY_P2SH) != 0);
1662 assert((flags & SCRIPT_VERIFY_WITNESS) != 0);
1663 if (stack.size() != 1)
1669 if (flags & SCRIPT_VERIFY_WITNESS)
1674 assert((flags & SCRIPT_VERIFY_P2SH) != 0);
1675 if (!hadWitness && !witness->
IsNull())
1681 return set_success(serror);
1684 size_t static WitnessSigOps(
int witversion,
const std::vector<unsigned char> &witprogram,
const CScriptWitness &witness,
int flags)
1686 if (witversion == 0)
1688 if (witprogram.size() == 20)
1691 if (witprogram.size() == 32 && witness.
stack.size() > 0)
1713 std::vector<unsigned char> witnessprogram;
1716 return WitnessSigOps(witnessversion, witnessprogram, witness ? *witness : witnessEmpty, flags);
1722 std::vector<unsigned char> data;
1723 while (pc < scriptSig.
end())
1726 scriptSig.
GetOp(pc, opcode, data);
1728 CScript subscript(data.begin(), data.end());
1729 if (subscript.IsWitnessProgram(witnessversion, witnessprogram))
1731 return WitnessSigOps(witnessversion, witnessprogram, witness ? *witness : witnessEmpty, flags);
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Raven always counted CHECKMULTISIGs as 20 sigops.
CSHA256 & Write(const unsigned char *data, size_t len)
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
CSHA1 & Write(const unsigned char *data, size_t len)
enum ScriptError_t ScriptError
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
bool IsPayToScriptHash() const
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
size_t CountWitnessSigOps(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags)
CHash256 & Write(const unsigned char *data, size_t len)
static const uint32_t SEQUENCE_FINAL
Only serialized through CTransaction.
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
bool CheckSequence(const CScriptNum &nSequence) const override
std::vector< std::vector< unsigned char > > stack
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
A hasher class for Raven's 256-bit hash (double SHA-256).
bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
const std::vector< CTxIn > vin
void Serialize(Stream &s, char a)
int64_t CAmount
Amount in corbies (Can be negative)
uint256 SignatureHash(const CScript &scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
opcodetype
Script opcodes.
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
PrecomputedTransactionData(const CTransaction &tx)
uint256 uint256S(const char *str)
An encapsulated public key.
const std::vector< CTxOut > vout
#define S(x0, x1, x2, x3, cb, r)
std::vector< unsigned char > getvch() const
#define stacktop(i)
Script is a stack machine (like Forth) that evaluates a predicate returning a bool indicating valid o...
CHash160 & Write(const unsigned char *data, size_t len)
bool CheckLockTime(const CScriptNum &nLockTime) const override
An output of a transaction.
static const uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
CRIPEMD160 & Write(const unsigned char *data, size_t len)
static const uint32_t SEQUENCE_LOCKTIME_MASK
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Serialized script, used inside transaction inputs and outputs.
int FindAndDelete(const CScript &b)
virtual bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
std::vector< unsigned char > valtype
virtual bool CheckSequence(const CScriptNum &nSequence) const
A writer stream (for serialization) that computes a 256-bit hash.
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
The basic transaction that is broadcasted on the network and contained in blocks. ...
A hasher class for Raven's 160-bit hash (SHA-256 + RIPEMD-160).
A hasher class for SHA-256.
bool CastToBool(const valtype &vch)
A hasher class for RIPEMD-160.