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))) 58 static inline void popstack(std::vector<valtype>& stack)
61 throw std::runtime_error(
"popstack(): stack empty");
66 if (vchPubKey.size() < 33) {
70 if (vchPubKey[0] == 0x04) {
71 if (vchPubKey.size() != 65) {
75 }
else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
76 if (vchPubKey.size() != 33) {
87 bool static IsCompressedPubKey(
const valtype &vchPubKey) {
88 if (vchPubKey.size() != 33) {
92 if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) {
109 bool static IsValidSignatureEncoding(
const std::vector<unsigned char> &sig,
bool haveHashType =
true) {
123 if (sig.size() < 9)
return false;
124 if (sig.size() > 73)
return false;
127 if (sig[0] != 0x30)
return false;
130 if (sig[1] != sig.size() - (haveHashType ? 3 : 2))
return false;
133 unsigned int lenR = sig[3];
136 if (5 + lenR >= sig.size())
return false;
139 unsigned int lenS = sig[5 + lenR];
143 if ((
size_t)(lenR + lenS + (haveHashType ? 7 : 6)) != sig.size())
return false;
146 if (sig[2] != 0x02)
return false;
149 if (lenR == 0)
return false;
152 if (sig[4] & 0x80)
return false;
156 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80))
return false;
159 if (sig[lenR + 4] != 0x02)
return false;
162 if (lenS == 0)
return false;
165 if (sig[lenR + 6] & 0x80)
return false;
169 if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80))
return false;
175 if (!IsValidSignatureEncoding(vchSig, haveHashType)) {
178 std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - (haveHashType ? 1 : 0));
185 if (!IsValidSignatureEncoding(vchSig, haveHashType)) {
191 bool static IsDefinedHashtypeSignature(
const valtype &vchSig) {
192 if (vchSig.size() == 0) {
205 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) {
236 return opcode ==
OP_1 + (
data[0] - 1);
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) {
259 static const valtype vchFalse(0);
261 static const valtype vchTrue(1, 1);
268 std::vector<bool> vfExec;
269 std::vector<valtype> altstack;
271 if (script.
size() > MAX_SCRIPT_SIZE)
280 bool fExec = !
count(vfExec.begin(), vfExec.end(),
false);
285 if (!script.
GetOp(pc, opcode, vchPushValue))
287 if (vchPushValue.size() > MAX_SCRIPT_ELEMENT_SIZE)
291 if (opcode >
OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT)
312 if (fRequireMinimal && !CheckMinimalPush(vchPushValue, opcode)) {
315 stack.push_back(vchPushValue);
342 stack.push_back(bn.
getvch());
365 if (stack.size() < 1)
407 if (stack.size() < 1)
449 if (stack.size() < 1)
455 if (vch.size() == 1 && vch[0] != 1)
463 vfExec.push_back(fValue);
471 vfExec.back() = !vfExec.back();
487 if (stack.size() < 1)
509 if (stack.size() < 1)
518 if (altstack.size() < 1)
528 if (stack.size() < 2)
538 if (stack.size() < 2)
542 stack.push_back(vch1);
543 stack.push_back(vch2);
550 if (stack.size() < 3)
555 stack.push_back(vch1);
556 stack.push_back(vch2);
557 stack.push_back(vch3);
564 if (stack.size() < 4)
568 stack.push_back(vch1);
569 stack.push_back(vch2);
576 if (stack.size() < 6)
580 stack.erase(stack.end()-6, stack.end()-4);
581 stack.push_back(vch1);
582 stack.push_back(vch2);
589 if (stack.size() < 4)
599 if (stack.size() < 1)
603 stack.push_back(vch);
611 stack.push_back(bn.getvch());
618 if (stack.size() < 1)
627 if (stack.size() < 1)
630 stack.push_back(vch);
637 if (stack.size() < 2)
639 stack.erase(stack.end() - 2);
646 if (stack.size() < 2)
649 stack.push_back(vch);
658 if (stack.size() < 2)
662 if (n < 0 || n >= (
int)stack.size())
666 stack.erase(stack.end()-n-1);
667 stack.push_back(vch);
676 if (stack.size() < 3)
686 if (stack.size() < 2)
695 if (stack.size() < 2)
698 stack.insert(stack.end()-2, vch);
706 if (stack.size() < 1)
709 stack.push_back(bn.
getvch());
722 if (stack.size() < 2)
726 bool fEqual = (vch1 == vch2);
734 stack.push_back(fEqual ? vchTrue : vchFalse);
757 if (stack.size() < 1)
762 case OP_1ADD: bn += bnOne;
break;
763 case OP_1SUB: bn -= bnOne;
break;
765 case OP_ABS:
if (bn < bnZero) bn = -bn;
break;
766 case OP_NOT: bn = (bn == bnZero);
break;
768 default:
assert(!
"invalid opcode");
break;
771 stack.push_back(bn.
getvch());
790 if (stack.size() < 2)
805 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero);
break;
806 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero);
break;
814 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2);
break;
815 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2);
break;
816 default:
assert(!
"invalid opcode");
break;
820 stack.push_back(bn.
getvch());
835 if (stack.size() < 3)
840 bool fValue = (bn2 <= bn1 && bn1 < bn3);
844 stack.push_back(fValue ? vchTrue : vchFalse);
859 if (stack.size() < 1)
866 CSHA1().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
868 CSHA256().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
870 CHash160().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
872 CHash256().
Write(vch.data(), vch.size()).Finalize(vchHash.data());
874 stack.push_back(vchHash);
889 if (stack.size() < 2)
896 CScript scriptCode(pbegincodehash, pend);
903 if (!
CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
907 bool fSuccess = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
914 stack.push_back(fSuccess ? vchTrue : vchFalse);
931 if ((
int)stack.size() < i)
935 if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG)
937 nOpCount += nKeysCount;
938 if (nOpCount > MAX_OPS_PER_SCRIPT)
943 int ikey2 = nKeysCount + 2;
945 if ((
int)stack.size() < i)
949 if (nSigsCount < 0 || nSigsCount > nKeysCount)
953 if ((
int)stack.size() < i)
957 CScript scriptCode(pbegincodehash, pend);
960 for (
int k = 0; k < nSigsCount; k++)
968 bool fSuccess =
true;
969 while (fSuccess && nSigsCount > 0)
977 if (!
CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) {
983 bool fOk = checker.
CheckSig(vchSig, vchPubKey, scriptCode, sigversion);
995 if (nSigsCount > nKeysCount)
1015 if (stack.size() < 1)
1021 stack.push_back(fSuccess ? vchTrue : vchFalse);
1042 valtype scriptRest(pc - 1, pend);
1043 stack.push_back(scriptRest);
1053 if (stack.size() + altstack.size() > MAX_STACK_SIZE)
1062 if (!vfExec.empty())
1065 return set_success(serror);
1074 class CTransactionSignatureSerializer {
1078 const unsigned int nIn;
1079 const bool fAnyoneCanPay;
1080 const bool fHashSingle;
1081 const bool fHashNone;
1084 CTransactionSignatureSerializer(
const CTransaction &txToIn,
const CScript &scriptCodeIn,
unsigned int nInIn,
int nHashTypeIn) :
1085 txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),
1091 template<
typename S>
1092 void SerializeScriptCode(
S &s)
const {
1096 unsigned int nCodeSeparators = 0;
1097 while (scriptCode.
GetOp(it, opcode)) {
1103 while (scriptCode.
GetOp(it, opcode)) {
1105 s.write((
char*)&itBegin[0], it-itBegin-1);
1109 if (itBegin != scriptCode.
end())
1110 s.write((
char*)&itBegin[0], it-itBegin);
1114 template<
typename S>
1115 void SerializeInput(
S &s,
unsigned int nInput)
const {
1126 SerializeScriptCode(s);
1128 if (nInput != nIn && (fHashSingle || fHashNone))
1136 template<
typename S>
1137 void SerializeOutput(
S &s,
unsigned int nOutput)
const {
1138 if (fHashSingle && nOutput != nIn)
1146 template<
typename S>
1151 unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.
vin.size();
1153 for (
unsigned int nInput = 0; nInput < nInputs; nInput++)
1154 SerializeInput(s, nInput);
1156 unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.
vout.size());
1158 for (
unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)
1159 SerializeOutput(s, nOutput);
1167 for (
const auto& txin : txTo.
vin) {
1175 for (
const auto& txin : txTo.
vin) {
1176 ss << txin.nSequence;
1183 for (
const auto& txout : txTo.
vout) {
1193 hashPrevouts = GetPrevoutHash(txTo);
1194 hashSequence = GetSequenceHash(txTo);
1195 hashOutputs = GetOutputsHash(txTo);
1206 hashPrevouts = cache ? cache->
hashPrevouts : GetPrevoutHash(txTo);
1210 hashSequence = cache ? cache->
hashSequence : GetSequenceHash(txTo);
1215 hashOutputs = cache ? cache->
hashOutputs : GetOutputsHash(txTo);
1218 ss << txTo.
vout[nIn];
1231 ss << txTo.
vin[nIn].prevout;
1234 ss << txTo.
vin[nIn].nSequence;
1245 static const uint256 one(
uint256S(
"0000000000000000000000000000000000000000000000000000000000000001"));
1246 if (nIn >= txTo.
vin.size()) {
1253 if (nIn >= txTo.
vout.size()) {
1260 CTransactionSignatureSerializer txTmp(txTo, scriptCode, nIn, nHashType);
1264 ss << txTmp << nHashType;
1270 return pubkey.
Verify(sighash, vchSig);
1280 std::vector<unsigned char> vchSig(vchSigIn);
1283 int nHashType = vchSig.back();
1286 uint256 sighash =
SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata);
1304 (txTo->nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) ||
1305 (txTo->nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD)
1311 if (nLockTime > (int64_t)txTo->nLockTime)
1334 const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence;
1338 if (static_cast<uint32_t>(txTo->nVersion) < 2)
1351 const int64_t txToSequenceMasked = txToSequence & nLockTimeMask;
1352 const CScriptNum nSequenceMasked = nSequence & nLockTimeMask;
1370 if (nSequenceMasked > txToSequenceMasked)
1378 std::vector<std::vector<unsigned char> > stack;
1381 if (witversion == 0) {
1382 if (program.size() == 32) {
1384 if (witness.
stack.size() == 0) {
1387 scriptPubKey =
CScript(witness.
stack.back().begin(), witness.
stack.back().end());
1388 stack = std::vector<std::vector<unsigned char> >(witness.
stack.begin(), witness.
stack.end() - 1);
1390 CSHA256().
Write(&scriptPubKey[0], scriptPubKey.
size()).Finalize(hashScriptPubKey.begin());
1391 if (memcmp(hashScriptPubKey.begin(), &program[0], 32)) {
1394 }
else if (program.size() == 20) {
1396 if (witness.
stack.size() != 2) {
1400 stack = witness.
stack;
1408 return set_success(serror);
1412 for (
unsigned int i = 0; i < stack.size(); i++) {
1413 if (stack.at(i).size() > MAX_SCRIPT_ELEMENT_SIZE)
1422 if (stack.size() != 1)
1432 if (witness ==
nullptr) {
1433 witness = &emptyWitness;
1435 bool hadWitness =
false;
1443 std::vector<std::vector<unsigned char> > stack, stackCopy;
1459 std::vector<unsigned char> witnessprogram;
1463 if (scriptSig.
size() != 0) {
1467 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror)) {
1484 swap(stack, stackCopy);
1491 const valtype& pubKeySerialized = stack.back();
1492 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1504 if (flags & SCRIPT_VERIFY_WITNESS) {
1505 if (pubKey2.IsWitnessProgram(witnessversion, witnessprogram)) {
1507 if (scriptSig !=
CScript() << std::vector<unsigned char>(pubKey2.begin(), pubKey2.end())) {
1512 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror)) {
1528 assert((flags & SCRIPT_VERIFY_P2SH) != 0);
1529 assert((flags & SCRIPT_VERIFY_WITNESS) != 0);
1530 if (stack.size() != 1) {
1535 if (flags & SCRIPT_VERIFY_WITNESS) {
1539 assert((flags & SCRIPT_VERIFY_P2SH) != 0);
1540 if (!hadWitness && !witness->
IsNull()) {
1545 return set_success(serror);
1548 size_t static WitnessSigOps(
int witversion,
const std::vector<unsigned char>& witprogram,
const CScriptWitness& witness,
int flags)
1550 if (witversion == 0) {
1551 if (witprogram.size() == 20)
1554 if (witprogram.size() == 32 && witness.
stack.size() > 0) {
1574 std::vector<unsigned char> witnessprogram;
1576 return WitnessSigOps(witnessversion, witnessprogram, witness ? *witness : witnessEmpty, flags);
1581 std::vector<unsigned char>
data;
1582 while (pc < scriptSig.
end()) {
1584 scriptSig.
GetOp(pc, opcode, data);
1586 CScript subscript(data.begin(), data.end());
1587 if (subscript.IsWitnessProgram(witnessversion, witnessprogram)) {
1588 return WitnessSigOps(witnessversion, witnessprogram, witness ? *witness : witnessEmpty, flags);
CSHA256 & Write(const unsigned char *data, size_t len)
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
CSHA1 & Write(const unsigned char *data, size_t len)
enum ScriptError_t ScriptError
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
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)
bool IsPayToScriptHash() const
static const uint32_t SEQUENCE_FINAL
Only serialized through CTransaction.
static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG
bool IsDERSignature(const valtype &vchSig, ScriptError *serror, bool haveHashType)
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).
assert(len-trim+(2 *lenIndices)<=WIDTH)
A hasher class for Fabcoin'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
if(a.IndicesBefore(b, len, lenIndices))
const std::vector< CTxIn > vin
void Serialize(Stream &s, char a)
int64_t CAmount
Amount in lius (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)
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Fabcoin always counted CHECKMULTISIGs as 20 sigops.
PrecomputedTransactionData(const CTransaction &tx)
uint256 uint256S(const char *str)
An encapsulated public key.
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
const std::vector< CTxOut > vout
#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
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
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)
virtual bool CheckSequence(const CScriptNum &nSequence) const
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
CRIPEMD160 & Write(const unsigned char *data, size_t len)
bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey)
static const uint32_t SEQUENCE_LOCKTIME_MASK
uint8_t const size_t const size
Serialized script, used inside transaction inputs and outputs.
std::vector< unsigned char > getvch() const
int FindAndDelete(const CScript &b)
std::vector< unsigned char > valtype
bool VerifySignature(const Coin &coin, const uint256 txFromHash, const CTransaction &txTo, unsigned int nIn, unsigned int flags)
A writer stream (for serialization) that computes a 256-bit hash.
std::vector< unsigned char > valtype
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 Fabcoin's 160-bit hash (SHA-256 + RIPEMD-160).
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
A hasher class for SHA-256.
bool CastToBool(const valtype &vch)
virtual bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
bool IsLowDERSignature(const valtype &vchSig, ScriptError *serror, bool haveHashType)
A hasher class for RIPEMD-160.