17 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)))
58 static inline void popstack(std::vector<valtype>& stack)
61 throw std::runtime_error(
"popstack() : stack empty");
65 bool static IsCompressedOrUncompressedPubKey(
const valtype &vchPubKey) {
70 if (vchPubKey[0] == 0x04) {
75 }
else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
97 bool static IsValidSignatureEncoding(
const std::vector<unsigned char> &sig) {
111 if (sig.size() < 9)
return false;
112 if (sig.size() > 73)
return false;
115 if (sig[0] != 0x30)
return false;
118 if (sig[1] != sig.size() - 3)
return false;
121 unsigned int lenR = sig[3];
124 if (5 + lenR >= sig.size())
return false;
127 unsigned int lenS = sig[5 + lenR];
131 if ((
size_t)(lenR + lenS + 7) != sig.size())
return false;
134 if (sig[2] != 0x02)
return false;
137 if (lenR == 0)
return false;
140 if (sig[4] & 0x80)
return false;
144 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80))
return false;
147 if (sig[lenR + 4] != 0x02)
return false;
150 if (lenS == 0)
return false;
153 if (sig[lenR + 6] & 0x80)
return false;
157 if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80))
return false;
163 if (!IsValidSignatureEncoding(vchSig)) {
169 std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - 1);
179 bool static IsDefinedHashtypeSignature(
const valtype &vchSig) {
180 if (vchSig.size() == 0) {
193 if (vchSig.size() == 0) {
215 if (data.size() == 0) {
217 return opcode ==
OP_0;
218 }
else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
220 return opcode ==
OP_1 + (data[0] - 1);
221 }
else if (data.size() == 1 && data[0] == 0x81) {
224 }
else if (data.size() <= 75) {
226 return opcode == data.size();
227 }
else if (data.size() <= 255) {
230 }
else if (data.size() <= 65535) {
241 static const valtype vchFalse(0);
242 static const valtype vchTrue(1, 1);
249 std::vector<bool> vfExec;
250 std::vector<valtype> altstack;
252 if (script.
size() > MAX_SCRIPT_SIZE)
261 bool fExec = !count(vfExec.begin(), vfExec.end(),
false);
266 if (!script.
GetOp(pc, opcode, vchPushValue))
268 if (vchPushValue.size() > MAX_SCRIPT_ELEMENT_SIZE)
272 if (opcode >
OP_16 && ++nOpCount > 201)
293 if (fRequireMinimal && !CheckMinimalPush(vchPushValue, opcode)) {
296 stack.push_back(vchPushValue);
323 stack.push_back(bn.
getvch());
355 if (stack.size() < 1)
405 if (stack.size() < 1)
413 vfExec.push_back(fValue);
421 vfExec.back() = !vfExec.back();
437 if (stack.size() < 1)
459 if (stack.size() < 1)
468 if (altstack.size() < 1)
478 if (stack.size() < 2)
488 if (stack.size() < 2)
492 stack.push_back(vch1);
493 stack.push_back(vch2);
500 if (stack.size() < 3)
505 stack.push_back(vch1);
506 stack.push_back(vch2);
507 stack.push_back(vch3);
514 if (stack.size() < 4)
518 stack.push_back(vch1);
519 stack.push_back(vch2);
526 if (stack.size() < 6)
530 stack.erase(stack.end()-6, stack.end()-4);
531 stack.push_back(vch1);
532 stack.push_back(vch2);
539 if (stack.size() < 4)
549 if (stack.size() < 1)
553 stack.push_back(vch);
561 stack.push_back(bn.
getvch());
568 if (stack.size() < 1)
577 if (stack.size() < 1)
580 stack.push_back(vch);
587 if (stack.size() < 2)
589 stack.erase(stack.end() - 2);
596 if (stack.size() < 2)
599 stack.push_back(vch);
608 if (stack.size() < 2)
612 if (n < 0 || n >= (
int)stack.size())
616 stack.erase(stack.end()-n-1);
617 stack.push_back(vch);
626 if (stack.size() < 3)
636 if (stack.size() < 2)
645 if (stack.size() < 2)
648 stack.insert(stack.end()-2, vch);
656 if (stack.size() < 1)
659 stack.push_back(bn.
getvch());
672 if (stack.size() < 2)
676 bool fEqual = (vch1 == vch2);
684 stack.push_back(fEqual ? vchTrue : vchFalse);
707 if (stack.size() < 1)
712 case OP_1ADD: bn += bnOne;
break;
713 case OP_1SUB: bn -= bnOne;
break;
715 case OP_ABS:
if (bn < bnZero) bn = -bn;
break;
716 case OP_NOT: bn = (bn == bnZero);
break;
718 default: assert(!
"invalid opcode");
break;
721 stack.push_back(bn.
getvch());
740 if (stack.size() < 2)
755 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero);
break;
756 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero);
break;
764 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2);
break;
765 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2);
break;
766 default: assert(!
"invalid opcode");
break;
770 stack.push_back(bn.
getvch());
785 if (stack.size() < 3)
790 bool fValue = (bn2 <= bn1 && bn1 < bn3);
794 stack.push_back(fValue ? vchTrue : vchFalse);
809 if (stack.size() < 1)
824 stack.push_back(vchHash);
839 if (stack.size() < 2)
846 CScript scriptCode(pbegincodehash, pend);
855 bool fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
859 stack.push_back(fSuccess ? vchTrue : vchFalse);
876 if ((
int)stack.size() < i)
880 if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)
882 nOpCount += nKeysCount;
887 if ((
int)stack.size() < i)
891 if (nSigsCount < 0 || nSigsCount > nKeysCount)
895 if ((
int)stack.size() < i)
899 CScript scriptCode(pbegincodehash, pend);
902 for (
int k = 0; k < nSigsCount; k++)
908 bool fSuccess =
true;
909 while (fSuccess && nSigsCount > 0)
923 bool fOk = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
935 if (nSigsCount > nKeysCount)
949 if (stack.size() < 1)
955 stack.push_back(fSuccess ? vchTrue : vchFalse);
991 if (stack.size() + altstack.size() > MAX_STACK_SIZE)
1000 if (!vfExec.empty())
1003 return set_success(serror);
1012 class CTransactionSignatureSerializer {
1016 const unsigned int nIn;
1017 const bool fAnyoneCanPay;
1018 const bool fHashSingle;
1019 const bool fHashNone;
1022 CTransactionSignatureSerializer(
const CTransaction &txToIn,
const CScript &scriptCodeIn,
unsigned int nInIn,
int nHashTypeIn) :
1023 txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1029 template<
typename S>
1030 void SerializeScriptCode(
S &s)
const {
1034 unsigned int nCodeSeparators = 0;
1035 while (scriptCode.
GetOp(it, opcode)) {
1041 while (scriptCode.
GetOp(it, opcode)) {
1043 s.write((
char*)&itBegin[0], it-itBegin-1);
1047 if (itBegin != scriptCode.
end())
1048 s.write((
char*)&itBegin[0], it-itBegin);
1052 template<
typename S>
1053 void SerializeInput(
S &s,
unsigned int nInput)
const {
1065 SerializeScriptCode(s);
1067 if (nInput != nIn && (fHashSingle || fHashNone))
1075 template<
typename S>
1076 void SerializeOutput(
S &s,
unsigned int nOutput)
const {
1077 if (fHashSingle && nOutput != nIn)
1085 template<
typename S>
1092 unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.
vin.size();
1094 for (
unsigned int nInput = 0; nInput < nInputs; nInput++)
1095 SerializeInput(s, nInput);
1097 unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.
vout.size());
1099 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)
1100 SerializeOutput(s, nOutput);
1106 const unsigned char PIVX_PREVOUTS_HASH_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
1107 {
'P',
'I',
'V',
'X',
'P',
'r',
'e',
'v',
'o',
'u',
't',
'H',
'a',
's',
'h'};
1108 const unsigned char PIVX_SEQUENCE_HASH_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
1109 {
'P',
'I',
'V',
'X',
'S',
'e',
'q',
'u',
'e',
'n',
'c',
'H',
'a',
's',
'h'};
1110 const unsigned char PIVX_OUTPUTS_HASH_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
1111 {
'P',
'I',
'V',
'X',
'O',
'u',
't',
'p',
'u',
't',
's',
'H',
'a',
's',
'h'};
1112 const unsigned char PIVX_SHIELDED_SPENDS_HASH_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
1113 {
'P',
'I',
'V',
'X',
'S',
'S',
'p',
'e',
'n',
'd',
's',
'H',
'a',
's',
'h'};
1114 const unsigned char PIVX_SHIELDED_OUTPUTS_HASH_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
1115 {
'P',
'I',
'V',
'X',
'S',
'O',
'u',
't',
'p',
'u',
't',
'H',
'a',
's',
'h'};
1121 for (
unsigned int n = 0; n < txTo.
vin.size(); n++) {
1122 ss << txTo.
vin[n].prevout;
1124 return ss.GetHash();
1129 for (
unsigned int n = 0; n < txTo.
vin.size(); n++) {
1130 ss << txTo.
vin[n].nSequence;
1132 return ss.GetHash();
1137 for (
unsigned int n = 0; n < txTo.
vout.size(); n++) {
1140 return ss.GetHash();
1147 for (
const auto& n : sapData->vShieldedSpend) {
1154 return ss.GetHash();
1161 for (
const auto& n : sapData->vShieldedOutput) {
1164 return ss.GetHash();
1188 throw std::runtime_error(
"SignatureHash in Sapling tx with wrong sigversion " + std::to_string(sigversion));
1198 bool hasSapData =
false;
1201 hashPrevouts = cache ? cache->
hashPrevouts : GetPrevoutHash(txTo);
1205 hashSequence = cache ? cache->
hashSequence : GetSequenceHash(txTo);
1209 hashOutputs = cache ? cache->
hashOutputs : GetOutputsHash(txTo);
1212 ss << txTo.
vout[nIn];
1217 if (!txTo.
sapData->vShieldedSpend.empty()) {
1218 hashShieldedSpends = cache ? cache->
hashShieldedSpends : GetShieldedSpendsHash(txTo);
1222 if (!txTo.
sapData->vShieldedOutput.empty()) {
1229 uint32_t leConsensusBranchId =
htole32(0);
1230 unsigned char personalization[16] = {};
1231 memcpy(personalization,
"PIVXSigHash", 12);
1232 memcpy(personalization+12, &leConsensusBranchId, 4);
1247 ss << hashShieldedSpends;
1249 ss << hashShieldedOutputs;
1251 ss << txTo.
sapData->valueBalance;
1258 ss << txTo.
vin[nIn].prevout;
1261 ss << txTo.
vin[nIn].nSequence;
1279 if (nIn >= txTo.
vout.size()) {
1286 CTransactionSignatureSerializer txTmp(txTo, scriptCode, nIn, nHashType);
1290 ss << txTmp << nHashType;
1296 return pubkey.
Verify(sighash, vchSig);
1306 std::vector<unsigned char> vchSig(vchSigIn);
1309 int nHashType = vchSig.back();
1330 (
txTo->
nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) ||
1331 (
txTo->
nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD)
1359 if ((
int)stack.size() != 3) {
1366 !CheckPubKeyEncoding(vchPubKey,
flags, serror)) {
1372 if ((
int)vchPubKeyHash.size() != 20) {
1386 const unsigned int outs =
txTo->
vout.size();
1393 for (
unsigned int i = 1; i < outs; i++) {
1394 if (
txTo->
vout[i].scriptPubKey != prevoutScript) {
1396 if (!fAllowLastOutputFree || i != outs-1 || outs < 3) {
1418 std::vector<std::vector<unsigned char> > stack, stackCopy;
1419 if (!
EvalScript(stack, scriptSig,
flags, checker, sigversion, serror))
1424 if (!
EvalScript(stack, scriptPubKey,
flags, checker, sigversion, serror))
1440 swap(stack, stackCopy);
1445 assert(!stack.empty());
1447 const valtype& pubKeySerialized = stack.back();
1448 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1467 if (stack.size() != 1) {
1472 return set_success(serror);
int64_t CAmount
Amount in PIV (Can be negative)
virtual bool CheckColdStake(bool fAllowLastOutputFree, const CScript &prevoutScript, std::vector< valtype > &stack, unsigned int flags, ScriptError *error) const
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
virtual bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
A writer stream (for serialization) that computes a 256-bit BLAKE2b hash.
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
void Finalize(unsigned char hash[OUTPUT_SIZE])
CHash160 & Write(const unsigned char *data, size_t len)
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
CHash256 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
A writer stream (for serialization) that computes a 256-bit hash.
An encapsulated public key.
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
A hasher class for RIPEMD-160.
CRIPEMD160 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA1 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
Serialized script, used inside transaction inputs and outputs.
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
bool IsPayToScriptHash() const
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
int FindAndDelete(const CScript &b)
bool IsPayToExchangeAddress() const
std::vector< unsigned char > getvch() const
The basic transaction that is broadcasted on the network and contained in blocks.
Optional< std::vector< uint8_t > > extraPayload
Optional< SaplingTxData > sapData
bool isSaplingVersion() const
std::vector< CTxOut > vout
An output of a transaction.
const PrecomputedTransactionData * precomTxData
bool CheckColdStake(bool fAllowLastOutputFree, const CScript &prevoutScript, std::vector< valtype > &stack, unsigned int flags, ScriptError *serror) const override
bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
bool CheckLockTime(const CScriptNum &nLockTime) const override
const CTransaction * txTo
uint32_t htole32(uint32_t host_32bits)
void * memcpy(void *a, const void *b, size_t c)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
bool CastToBool(const valtype &vch)
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
uint256 SignatureHash(const CScript &scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
std::vector< unsigned char > valtype
#define stacktop(i)
Script is a stack machine (like Forth) that evaluates a predicate returning a bool indicating valid o...
@ SCRIPT_VERIFY_NULLDUMMY
@ SCRIPT_VERIFY_SIGPUSHONLY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_VERIFY_STRICTENC
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_MINIMALDATA
@ SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
@ SCRIPT_VERIFY_EXCHANGEADDR
const unsigned int NOT_AN_INPUT
Special case nIn for signing Sapling txs.
#define S(x0, x1, x2, x3, cb, r)
opcodetype
Script opcodes.
@ OP_CHECKCOLDSTAKEVERIFY
@ OP_CHECKCOLDSTAKEVERIFY_LOF
enum ScriptError_t ScriptError
@ SCRIPT_ERR_SIG_PUSHONLY
@ SCRIPT_ERR_NUMEQUALVERIFY
@ SCRIPT_ERR_DISABLED_OPCODE
@ SCRIPT_ERR_INVALID_ALTSTACK_OPERATION
@ SCRIPT_ERR_EXCHANGEADDRVERIFY
@ SCRIPT_ERR_UNKNOWN_ERROR
@ SCRIPT_ERR_SIG_HASHTYPE
@ SCRIPT_ERR_CHECKCOLDSTAKEVERIFY
@ SCRIPT_ERR_CHECKSIGVERIFY
@ SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS
@ SCRIPT_ERR_INVALID_STACK_OPERATION
@ SCRIPT_ERR_NEGATIVE_LOCKTIME
@ SCRIPT_ERR_SIG_NULLDUMMY
@ SCRIPT_ERR_CHECKMULTISIGVERIFY
@ SCRIPT_ERR_UNSATISFIED_LOCKTIME
@ SCRIPT_ERR_PUBKEY_COUNT
@ SCRIPT_ERR_UNBALANCED_CONDITIONAL
void Serialize(Stream &s, char a)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
uint256 hashShieldedSpends
uint256 hashShieldedOutputs
PrecomputedTransactionData(const CTransaction &tx)
const uint256 UINT256_ONE