Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_tests.cpp
Show All 29 Lines | |||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <univalue.h> | #include <univalue.h> | ||||
// Uncomment if you want to output updated JSON tests. | // Uncomment if you want to output updated JSON tests. | ||||
// #define UPDATE_JSON_TESTS | // #define UPDATE_JSON_TESTS | ||||
static const unsigned int flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC; | static const uint32_t flags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC; | ||||
struct ScriptErrorDesc { | struct ScriptErrorDesc { | ||||
ScriptError_t err; | ScriptError_t err; | ||||
const char *name; | const char *name; | ||||
}; | }; | ||||
static ScriptErrorDesc script_errors[] = { | static ScriptErrorDesc script_errors[] = { | ||||
{SCRIPT_ERR_OK, "OK"}, | {SCRIPT_ERR_OK, "OK"}, | ||||
▲ Show 20 Lines • Show All 206 Lines • ▼ Show 20 Lines | private: | ||||
} | } | ||||
void DoPush(const std::vector<uint8_t> &data) { | void DoPush(const std::vector<uint8_t> &data) { | ||||
DoPush(); | DoPush(); | ||||
push = data; | push = data; | ||||
havePush = true; | havePush = true; | ||||
} | } | ||||
std::vector<uint8_t> DoSign(const CKey &key, const uint256 &hash, | |||||
unsigned int lenR = 32, | |||||
unsigned int lenS = 32) const { | |||||
std::vector<uint8_t> vchSig, r, s; | |||||
uint32_t iter = 0; | |||||
do { | |||||
key.Sign(hash, vchSig, iter++); | |||||
if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) { | |||||
NegateSignatureS(vchSig); | |||||
} | |||||
r = std::vector<uint8_t>(vchSig.begin() + 4, | |||||
vchSig.begin() + 4 + vchSig[3]); | |||||
s = std::vector<uint8_t>(vchSig.begin() + 6 + vchSig[3], | |||||
vchSig.begin() + 6 + vchSig[3] + | |||||
vchSig[5 + vchSig[3]]); | |||||
} while (lenR != r.size() || lenS != s.size()); | |||||
return vchSig; | |||||
} | |||||
public: | public: | ||||
TestBuilder(const CScript &script_, const std::string &comment_, int flags_, | TestBuilder(const CScript &script_, const std::string &comment_, int flags_, | ||||
bool P2SH = false, Amount nValue_ = Amount(0)) | bool P2SH = false, Amount nValue_ = Amount(0)) | ||||
: script(script_), havePush(false), comment(comment_), flags(flags_), | : script(script_), havePush(false), comment(comment_), flags(flags_), | ||||
scriptError(SCRIPT_ERR_OK), nValue(nValue_) { | scriptError(SCRIPT_ERR_OK), nValue(nValue_) { | ||||
CScript scriptPubKey = script; | CScript scriptPubKey = script; | ||||
if (P2SH) { | if (P2SH) { | ||||
redeemscript = scriptPubKey; | redeemscript = scriptPubKey; | ||||
Show All 35 Lines | public: | ||||
TestBuilder &PushSig(const CKey &key, | TestBuilder &PushSig(const CKey &key, | ||||
SigHashType sigHashType = SigHashType(), | SigHashType sigHashType = SigHashType(), | ||||
unsigned int lenR = 32, unsigned int lenS = 32, | unsigned int lenR = 32, unsigned int lenS = 32, | ||||
Amount amount = Amount(0), | Amount amount = Amount(0), | ||||
uint32_t flags = SCRIPT_ENABLE_SIGHASH_FORKID) { | uint32_t flags = SCRIPT_ENABLE_SIGHASH_FORKID) { | ||||
uint256 hash = SignatureHash(script, CTransaction(spendTx), 0, | uint256 hash = SignatureHash(script, CTransaction(spendTx), 0, | ||||
sigHashType, amount, nullptr, flags); | sigHashType, amount, nullptr, flags); | ||||
std::vector<uint8_t> vchSig, r, s; | std::vector<uint8_t> vchSig = DoSign(key, hash, lenR, lenS); | ||||
uint32_t iter = 0; | |||||
do { | |||||
key.Sign(hash, vchSig, iter++); | |||||
if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) { | |||||
NegateSignatureS(vchSig); | |||||
} | |||||
r = std::vector<uint8_t>(vchSig.begin() + 4, | |||||
vchSig.begin() + 4 + vchSig[3]); | |||||
s = std::vector<uint8_t>(vchSig.begin() + 6 + vchSig[3], | |||||
vchSig.begin() + 6 + vchSig[3] + | |||||
vchSig[5 + vchSig[3]]); | |||||
} while (lenR != r.size() || lenS != s.size()); | |||||
vchSig.push_back(static_cast<uint8_t>(sigHashType.getRawSigHashType())); | vchSig.push_back(static_cast<uint8_t>(sigHashType.getRawSigHashType())); | ||||
DoPush(vchSig); | DoPush(vchSig); | ||||
return *this; | return *this; | ||||
} | } | ||||
TestBuilder &Push(const CPubKey &pubkey) { | TestBuilder &Push(const CPubKey &pubkey) { | ||||
DoPush(std::vector<uint8_t>(pubkey.begin(), pubkey.end())); | DoPush(std::vector<uint8_t>(pubkey.begin(), pubkey.end())); | ||||
return *this; | return *this; | ||||
▲ Show 20 Lines • Show All 1,537 Lines • Show Last 20 Lines |