| 288 | }; |
| 289 | |
| 290 | class TestBuilder |
| 291 | { |
| 292 | private: |
| 293 | //! Actually executed script |
| 294 | CScript script; |
| 295 | //! The P2SH redeemscript |
| 296 | CScript redeemscript; |
| 297 | //! The Witness embedded script |
| 298 | CScript witscript; |
| 299 | CScriptWitness scriptWitness; |
| 300 | CTransactionRef creditTx; |
| 301 | CMutableTransaction spendTx; |
| 302 | bool havePush; |
| 303 | std::vector<unsigned char> push; |
| 304 | std::string comment; |
| 305 | uint32_t flags; |
| 306 | int scriptError; |
| 307 | CAmount nValue; |
| 308 | |
| 309 | void DoPush() |
| 310 | { |
| 311 | if (havePush) { |
| 312 | spendTx.vin[0].scriptSig << push; |
| 313 | havePush = false; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void DoPush(const std::vector<unsigned char>& data) |
| 318 | { |
| 319 | DoPush(); |
| 320 | push = data; |
| 321 | havePush = true; |
| 322 | } |
| 323 | |
| 324 | public: |
| 325 | TestBuilder(const CScript& script_, const std::string& comment_, uint32_t flags_, bool P2SH = false, WitnessMode wm = WitnessMode::NONE, int witnessversion = 0, CAmount nValue_ = 0) : script(script_), havePush(false), comment(comment_), flags(flags_), scriptError(SCRIPT_ERR_OK), nValue(nValue_) |
| 326 | { |
| 327 | CScript scriptPubKey = script; |
| 328 | if (wm == WitnessMode::PKH) { |
| 329 | uint160 hash; |
| 330 | CHash160().Write(Span{script}.subspan(1)).Finalize(hash); |
| 331 | script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 332 | scriptPubKey = CScript() << witnessversion << ToByteVector(hash); |
| 333 | } else if (wm == WitnessMode::SH) { |
| 334 | witscript = scriptPubKey; |
| 335 | uint256 hash; |
| 336 | CSHA256().Write(witscript.data(), witscript.size()).Finalize(hash.begin()); |
| 337 | scriptPubKey = CScript() << witnessversion << ToByteVector(hash); |
| 338 | } |
| 339 | if (P2SH) { |
| 340 | redeemscript = scriptPubKey; |
| 341 | scriptPubKey = CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemscript)) << OP_EQUAL; |
| 342 | } |
| 343 | creditTx = MakeTransactionRef(BuildCreditingTransaction(scriptPubKey, nValue)); |
| 344 | spendTx = BuildSpendingTransaction(CScript(), CScriptWitness(), *creditTx); |
| 345 | } |
| 346 | |
| 347 | TestBuilder& ScriptError(ScriptError_t err) |