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