15 typedef std::vector<unsigned char>
valtype;
37 for (
unsigned int i = 0; i < vch.size(); i++)
42 if (i == vch.size()-1 && vch[i] == 0x80)
54 #define stacktop(i) (stack.at(stack.size()+(i))) 55 #define altstacktop(i) (altstack.at(altstack.size()+(i))) 56 static inline void popstack(std::vector<valtype>& stack)
59 throw std::runtime_error(
"popstack(): stack empty");
63 bool static IsCompressedOrUncompressedPubKey(
const valtype &vchPubKey) {
68 if (vchPubKey[0] == 0x04) {
73 }
else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
85 bool static IsCompressedPubKey(
const valtype &vchPubKey) {
90 if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) {
107 bool static IsValidSignatureEncoding(
const std::vector<unsigned char> &sig) {
121 if (sig.size() < 9)
return false;
122 if (sig.size() > 73)
return false;
125 if (sig[0] != 0x30)
return false;
128 if (sig[1] != sig.size() - 3)
return false;
131 unsigned int lenR = sig[3];
134 if (5 + lenR >= sig.size())
return false;
137 unsigned int lenS = sig[5 + lenR];
141 if ((
size_t)(lenR + lenS + 7) != sig.size())
return false;
144 if (sig[2] != 0x02)
return false;
147 if (lenR == 0)
return false;
150 if (sig[4] & 0x80)
return false;
154 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80))
return false;
157 if (sig[lenR + 4] != 0x02)
return false;
160 if (lenS == 0)
return false;
163 if (sig[lenR + 6] & 0x80)
return false;
167 if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80))
return false;
173 if (!IsValidSignatureEncoding(vchSig)) {
179 std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - 1);
189 bool static IsDefinedHashtypeSignature(
const valtype &vchSig) {
190 if (vchSig.size() == 0) {
203 if (vchSig.size() == 0) {
231 if (data.size() == 0) {
233 return opcode ==
OP_0;
234 }
else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
237 }
else if (data.size() == 1 && data[0] == 0x81) {
240 }
else if (data.size() <= 75) {
242 return opcode == data.size();
243 }
else if (data.size() <= 255) {
246 }
else if (data.size() <= 65535) {
264 while (static_cast<size_t>(
end - pc) >= b.
size() && std::equal(b.
begin(), b.
end(), pc))
271 while (script.
GetOp(pc, opcode));
275 script = std::move(result);
287 static const valtype vchFalse(0);
289 static const valtype vchTrue(1, 1);
296 std::vector<bool> vfExec;
297 std::vector<valtype> altstack;
299 if (script.
size() > MAX_SCRIPT_SIZE)
308 bool fExec = !count(vfExec.begin(), vfExec.end(),
false);
313 if (!script.
GetOp(pc, opcode, vchPushValue))
315 if (vchPushValue.size() > MAX_SCRIPT_ELEMENT_SIZE)
319 if (opcode >
OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT)
344 if (fRequireMinimal && !CheckMinimalPush(vchPushValue, opcode)) {
347 stack.push_back(vchPushValue);
374 stack.push_back(bn.
getvch());
394 if (stack.size() < 1)
433 if (stack.size() < 1)
475 if (stack.size() < 1)
481 if (vch.size() == 1 && vch[0] != 1)
489 vfExec.push_back(fValue);
497 vfExec.back() = !vfExec.back();
513 if (stack.size() < 1)
535 if (stack.size() < 1)
544 if (altstack.size() < 1)
554 if (stack.size() < 2)
564 if (stack.size() < 2)
568 stack.push_back(vch1);
569 stack.push_back(vch2);
576 if (stack.size() < 3)
581 stack.push_back(vch1);
582 stack.push_back(vch2);
583 stack.push_back(vch3);
590 if (stack.size() < 4)
594 stack.push_back(vch1);
595 stack.push_back(vch2);
602 if (stack.size() < 6)
606 stack.erase(stack.end()-6, stack.end()-4);
607 stack.push_back(vch1);
608 stack.push_back(vch2);
615 if (stack.size() < 4)
625 if (stack.size() < 1)
629 stack.push_back(vch);
637 stack.push_back(bn.getvch());
644 if (stack.size() < 1)
653 if (stack.size() < 1)
656 stack.push_back(vch);
663 if (stack.size() < 2)
665 stack.erase(stack.end() - 2);
672 if (stack.size() < 2)
675 stack.push_back(vch);
684 if (stack.size() < 2)
688 if (n < 0 || n >= (
int)stack.size())
692 stack.erase(stack.end()-n-1);
693 stack.push_back(vch);
702 if (stack.size() < 3)
712 if (stack.size() < 2)
721 if (stack.size() < 2)
724 stack.insert(stack.end()-2, vch);
732 if (stack.size() < 1)
735 stack.push_back(bn.
getvch());
748 if (stack.size() < 2)
752 bool fEqual = (vch1 == vch2);
760 stack.push_back(fEqual ? vchTrue : vchFalse);
783 if (stack.size() < 1)
788 case OP_1ADD: bn += bnOne;
break;
789 case OP_1SUB: bn -= bnOne;
break;
791 case OP_ABS:
if (bn < bnZero) bn = -bn;
break;
792 case OP_NOT: bn = (bn == bnZero);
break;
794 default: assert(!
"invalid opcode");
break;
797 stack.push_back(bn.
getvch());
816 if (stack.size() < 2)
831 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero);
break;
832 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero);
break;
840 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2);
break;
841 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2);
break;
842 default: assert(!
"invalid opcode");
break;
846 stack.push_back(bn.
getvch());
861 if (stack.size() < 3)
866 bool fValue = (bn2 <= bn1 && bn1 < bn3);
870 stack.push_back(fValue ? vchTrue : vchFalse);
885 if (stack.size() < 1)
892 CSHA3().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
894 CSHA256().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
896 CHash360().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
898 CHash3().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
900 stack.push_back(vchHash);
918 if (stack.size() < 2)
925 CScript scriptCode(pbegincodehash, pend);
938 bool fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
945 stack.push_back(fSuccess ? vchTrue : vchFalse);
962 if ((
int)stack.size() < i)
966 if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)
968 nOpCount += nKeysCount;
969 if (nOpCount > MAX_OPS_PER_SCRIPT)
974 int ikey2 = nKeysCount + 2;
976 if ((
int)stack.size() < i)
980 if (nSigsCount < 0 || nSigsCount > nKeysCount)
984 if ((
int)stack.size() < i)
988 CScript scriptCode(pbegincodehash, pend);
991 for (
int k = 0; k < nSigsCount; k++)
1001 bool fSuccess =
true;
1002 while (fSuccess && nSigsCount > 0)
1016 bool fOk = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
1028 if (nSigsCount > nKeysCount)
1048 if (stack.size() < 1)
1054 stack.push_back(fSuccess ? vchTrue : vchFalse);
1071 if (stack.size() + altstack.size() > MAX_STACK_SIZE)
1080 if (!vfExec.empty())
1083 return set_success(serror);
1093 class CTransactionSignatureSerializer
1098 const unsigned int nIn;
1099 const bool fAnyoneCanPay;
1100 const bool fHashSingle;
1101 const bool fHashNone;
1104 CTransactionSignatureSerializer(
const T& txToIn,
const CScript& scriptCodeIn,
unsigned int nInIn,
int nHashTypeIn) :
1105 txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1111 template<
typename S>
1112 void SerializeScriptCode(S &s)
const {
1116 unsigned int nCodeSeparators = 0;
1117 while (scriptCode.
GetOp(it, opcode)) {
1123 while (scriptCode.
GetOp(it, opcode)) {
1125 s.write((
char*)&itBegin[0], it-itBegin-1);
1129 if (itBegin != scriptCode.
end())
1130 s.write((
char*)&itBegin[0], it-itBegin);
1134 template<
typename S>
1135 void SerializeInput(S &s,
unsigned int nInput)
const {
1146 SerializeScriptCode(s);
1148 if (nInput != nIn && (fHashSingle || fHashNone))
1156 template<
typename S>
1157 void SerializeOutput(S &s,
unsigned int nOutput)
const {
1158 if (fHashSingle && nOutput != nIn)
1166 template<
typename S>
1171 unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size();
1173 for (
unsigned int nInput = 0; nInput < nInputs; nInput++)
1174 SerializeInput(s, nInput);
1176 unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size());
1178 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)
1179 SerializeOutput(s, nOutput);
1186 uint256 GetPrevoutHash(
const T& txTo)
1189 for (
const auto& txin : txTo.vin) {
1192 return ss.GetHash();
1196 uint256 GetSequenceHash(
const T& txTo)
1199 for (
const auto& txin : txTo.vin) {
1200 ss << txin.nSequence;
1202 return ss.GetHash();
1206 uint256 GetOutputsHash(
const T& txTo)
1209 for (
const auto& txout : txTo.vout) {
1212 return ss.GetHash();
1221 if (txTo.HasWitness()) {
1236 assert(nIn < txTo.vin.size());
1242 const bool cacheready = cache && cache->
ready;
1245 hashPrevouts = cacheready ? cache->
hashPrevouts : GetPrevoutHash(txTo);
1249 hashSequence = cacheready ? cache->
hashSequence : GetSequenceHash(txTo);
1254 hashOutputs = cacheready ? cache->
hashOutputs : GetOutputsHash(txTo);
1255 }
else if ((nHashType & 0x1f) ==
SIGHASH_SINGLE && nIn < txTo.vout.size()) {
1257 ss << txTo.vout[nIn];
1270 ss << txTo.vin[nIn].prevout;
1273 ss << txTo.vin[nIn].nSequence;
1277 ss << txTo.nLockTime;
1284 static const uint256 one(
uint256S(
"0000000000000000000000000000000000000000000000000000000000000001"));
1288 if (nIn >= txTo.vout.size()) {
1295 CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn, nHashType);
1299 ss << txTmp << nHashType;
1306 return pubkey.
Verify(sighash, vchSig);
1317 std::vector<unsigned char> vchSig(vchSigIn);
1320 int nHashType = vchSig.back();
1323 uint256 sighash =
SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata);
1325 if (!VerifySignature(vchSig, pubkey, sighash))
1342 (txTo->nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) ||
1343 (txTo->nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD)
1349 if (nLockTime > (int64_t)txTo->nLockTime)
1373 const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence;
1377 if (static_cast<uint32_t>(txTo->nVersion) < 2)
1390 const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
1391 const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
1409 if (nSequenceMasked > txToSequenceMasked)
1421 std::vector<std::vector<unsigned char> > stack;
1424 if (witversion == 0) {
1425 if (program.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
1427 if (witness.
stack.size() == 0) {
1430 scriptPubKey =
CScript(witness.
stack.back().begin(), witness.
stack.back().end());
1431 stack = std::vector<std::vector<unsigned char> >(witness.
stack.begin(), witness.
stack.end() - 1);
1433 CSHA3().
Write(&scriptPubKey[0], scriptPubKey.
size()).Finalize(hashScriptPubKey.begin());
1434 if (memcmp(hashScriptPubKey.begin(), program.data(), 32)) {
1437 }
else if (program.size() == WITNESS_V0_KEYHASH_SIZE) {
1439 if (witness.
stack.size() != 2) {
1443 stack = witness.
stack;
1451 return set_success(serror);
1455 for (
unsigned int i = 0; i < stack.
size(); i++) {
1456 if (stack.at(i).
size() > MAX_SCRIPT_ELEMENT_SIZE)
1465 if (stack.
size() != 1)
1475 if (witness ==
nullptr) {
1476 witness = &emptyWitness;
1478 bool hadWitness =
false;
1486 std::vector<std::vector<unsigned char> > stack, stackCopy;
1502 std::vector<unsigned char> witnessprogram;
1506 if (scriptSig.
size() != 0) {
1510 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram,
flags, checker, serror)) {
1527 swap(stack, stackCopy);
1532 assert(!stack.empty());
1534 const valtype& pubKeySerialized = stack.back();
1535 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1548 if (pubKey2.IsWitnessProgram(witnessversion, witnessprogram)) {
1550 if (scriptSig !=
CScript() << std::vector<unsigned char>(pubKey2.begin(), pubKey2.end())) {
1555 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram,
flags, checker, serror)) {
1573 if (stack.size() != 1) {
1583 if (!hadWitness && !witness->
IsNull()) {
1588 return set_success(serror);
1591 size_t static WitnessSigOps(
int witversion,
const std::vector<unsigned char>& witprogram,
const CScriptWitness& witness)
1593 if (witversion == 0) {
1594 if (witprogram.size() == WITNESS_V0_KEYHASH_SIZE)
1597 if (witprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE && witness.
stack.size() > 0) {
1617 std::vector<unsigned char> witnessprogram;
1619 return WitnessSigOps(witnessversion, witnessprogram, witness ? *witness : witnessEmpty);
1624 std::vector<unsigned char> data;
1625 while (pc < scriptSig.
end()) {
1627 scriptSig.
GetOp(pc, opcode, data);
1629 CScript subscript(data.begin(), data.end());
1630 if (subscript.IsWitnessProgram(witnessversion, witnessprogram)) {
1631 return WitnessSigOps(witnessversion, witnessprogram, witness ? *witness : witnessEmpty);
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
CSHA256 & Write(const unsigned char *data, size_t len)
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
enum ScriptError_t ScriptError
iterator insert(iterator pos, const T &value)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) 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)
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
UniValue ret(UniValue::VARR)
static const uint32_t SEQUENCE_FINAL
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
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
void Serialize(Stream &s, char a)
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
int64_t CAmount
Amount in satoshis (Can be negative)
PrecomputedTransactionData(const T &tx)
opcodetype
Script opcodes.
if(!params[0].isNull()) nMinDepth
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).
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
uint256 uint256S(const char *str)
An encapsulated public key.
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...
bool CheckLockTime(const CScriptNum &nLockTime) const override
bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) 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)
A hasher class for SHA3-256.
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).
CHash360 & Write(const unsigned char *data, size_t len)
Serialized script, used inside transaction inputs and outputs.
CHash3 & Write(const unsigned char *data, size_t len)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
CSHA3 & Write(const unsigned char *data, size_t len)
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 hasher class for BSHA3's 256-bit hash (double SHA-3).
A mutable version of CTransaction.
A writer stream (for serialization) that computes a 256-bit SHA-3-256 hash.
The basic transaction that is broadcasted on the network and contained in blocks. ...
bool CheckSequence(const CScriptNum &nSequence) const override
A hasher class for Bitcoin's 160-bit hash (SHA3-256 + RIPEMD-160).
A hasher class for SHA-256.
int FindAndDelete(CScript &script, const CScript &b)
bool CastToBool(const valtype &vch)
A hasher class for RIPEMD-160.