| 218 | }; |
| 219 | |
| 220 | class TestBuilder |
| 221 | { |
| 222 | private: |
| 223 | //! Actually executed script |
| 224 | CScript script; |
| 225 | //! The P2SH redeemscript |
| 226 | CScript redeemscript; |
| 227 | //! The Witness embedded script |
| 228 | CScript witscript; |
| 229 | CScriptWitness scriptWitness; |
| 230 | CTransactionRef creditTx; |
| 231 | CMutableTransaction spendTx; |
| 232 | bool havePush{false}; |
| 233 | std::vector<unsigned char> push; |
| 234 | std::string comment; |
| 235 | script_verify_flags flags; |
| 236 | int scriptError{SCRIPT_ERR_OK}; |
| 237 | CAmount nValue; |
| 238 | |
| 239 | void DoPush() |
| 240 | { |
| 241 | if (havePush) { |
| 242 | spendTx.vin[0].scriptSig << push; |
| 243 | havePush = false; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void DoPush(const std::vector<unsigned char>& data) |
| 248 | { |
| 249 | DoPush(); |
| 250 | push = data; |
| 251 | havePush = true; |
| 252 | } |
| 253 | |
| 254 | public: |
| 255 | TestBuilder(const CScript& script_, const std::string& comment_, script_verify_flags flags_, bool P2SH = false, WitnessMode wm = WitnessMode::NONE, int witnessversion = 0, CAmount nValue_ = 0) : script(script_), comment(comment_), flags(flags_), nValue(nValue_) |
| 256 | { |
| 257 | CScript scriptPubKey = script; |
| 258 | if (wm == WitnessMode::PKH) { |
| 259 | uint160 hash; |
| 260 | CHash160().Write(std::span{script}.subspan(1)).Finalize(hash); |
| 261 | script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 262 | scriptPubKey = CScript() << witnessversion << ToByteVector(hash); |
| 263 | } else if (wm == WitnessMode::SH) { |
| 264 | witscript = scriptPubKey; |
| 265 | uint256 hash; |
| 266 | CSHA256().Write(witscript.data(), witscript.size()).Finalize(hash.begin()); |
| 267 | scriptPubKey = CScript() << witnessversion << ToByteVector(hash); |
| 268 | } |
| 269 | if (P2SH) { |
| 270 | redeemscript = scriptPubKey; |
| 271 | scriptPubKey = CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemscript)) << OP_EQUAL; |
| 272 | } |
| 273 | creditTx = MakeTransactionRef(BuildCreditingTransaction(scriptPubKey, nValue)); |
| 274 | spendTx = BuildSpendingTransaction(CScript(), CScriptWitness(), *creditTx); |
| 275 | } |
| 276 | |
| 277 | TestBuilder& ScriptError(ScriptError_t err) |