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