|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright (C) 2017-2021 The btclib developers |
| 4 | +# |
| 5 | +# This file is part of btclib. It is subject to the license terms in the |
| 6 | +# LICENSE file found in the top-level directory of this distribution. |
| 7 | +# |
| 8 | +# No part of btclib including this file, may be copied, modified, propagated, |
| 9 | +# or distributed except according to the terms contained in the LICENSE file. |
| 10 | +"""Bitcoin Script engine.""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from typing import cast |
| 15 | + |
| 16 | +from btclib.alias import Command, ScriptList |
| 17 | +from btclib.exceptions import BTClibValueError |
| 18 | +from btclib.hashes import sha256 |
| 19 | +from btclib.script.engine import tapscript |
| 20 | +from btclib.script.engine.script import verify_script as verify_script_legacy |
| 21 | +from btclib.script.engine.script_op_codes import _to_num |
| 22 | +from btclib.script.script import parse, serialize |
| 23 | +from btclib.script.script_pub_key import is_segwit, type_and_payload |
| 24 | +from btclib.script.taproot import check_output_pubkey |
| 25 | +from btclib.script.witness import Witness |
| 26 | +from btclib.tx.tx import Tx |
| 27 | +from btclib.tx.tx_out import TxOut |
| 28 | + |
| 29 | + |
| 30 | +def taproot_unwrap_script( |
| 31 | + script: bytes, stack: list[bytes] |
| 32 | +) -> tuple[bytes, list[bytes], int]: |
| 33 | + pub_key = type_and_payload(script)[1] |
| 34 | + script_bytes = stack[-2] |
| 35 | + control = stack[-1] |
| 36 | + |
| 37 | + if not check_output_pubkey(pub_key, script_bytes, control): |
| 38 | + raise BTClibValueError() |
| 39 | + |
| 40 | + leaf_version = stack[-1][0] & 0xFE |
| 41 | + |
| 42 | + return script_bytes, stack[:-2], leaf_version |
| 43 | + |
| 44 | + |
| 45 | +def taproot_get_annex(witness: Witness) -> bytes: |
| 46 | + annex = b"" |
| 47 | + if len(witness.stack) >= 2 and witness.stack[-1][0] == 0x50: |
| 48 | + annex = witness.stack[-1] |
| 49 | + witness.stack = witness.stack[:-1] |
| 50 | + return annex |
| 51 | + |
| 52 | + |
| 53 | +def validate_redeem_script(redeem_script: ScriptList) -> None: |
| 54 | + for c in redeem_script: |
| 55 | + if isinstance(c, str): |
| 56 | + if c == "OP_1NEGATE": |
| 57 | + continue |
| 58 | + if c[:2] == "OP" and not c[3:].isdigit(): |
| 59 | + raise BTClibValueError() |
| 60 | + |
| 61 | + |
| 62 | +ALL_FLAGS = [ |
| 63 | + "P2SH", |
| 64 | + # Bip 62, never finalized |
| 65 | + # "SIGPUSHONLY", |
| 66 | + # "LOW_S", |
| 67 | + # "STRICTENC", |
| 68 | + # "CONST_SCRIPTCODE", |
| 69 | + # "CLEANSTACK", |
| 70 | + # "MINIMALDATA", |
| 71 | + "DERSIG", |
| 72 | + # only standard, not consensus |
| 73 | + # "NULLFAIL", |
| 74 | + # "MINMALIF", |
| 75 | + # "DISCOURAGE_UPGRADABLE_NOPS", |
| 76 | + # "DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM", |
| 77 | + "CHECKLOCKTIMEVERIFY", |
| 78 | + "CHECKSEQUENCEVERIFY", |
| 79 | + "WITNESS", |
| 80 | + "NULLDUMMY", |
| 81 | + # only standard, not strictly consensus |
| 82 | + # "WITNESS_PUBKEYTYPE", |
| 83 | + "TAPROOT", |
| 84 | +] |
| 85 | + |
| 86 | + |
| 87 | +def verify_input(prevouts: list[TxOut], tx: Tx, i: int, flags: list[str]) -> None: |
| 88 | + script_sig = tx.vin[i].script_sig |
| 89 | + parsed_script_sig = parse(script_sig, accept_unknown=True) |
| 90 | + if "SIGPUSHONLY" in flags: |
| 91 | + validate_redeem_script(parsed_script_sig) |
| 92 | + if "CONST_SCRIPTCODE" in flags: |
| 93 | + for x in parsed_script_sig: |
| 94 | + op_checks = [ |
| 95 | + "OP_CHECKSIG", |
| 96 | + "OP_CHECKSIGVERIFY", |
| 97 | + "OP_CHECKMULTISIG", |
| 98 | + "OP_CHECKSIGVERIFY", |
| 99 | + ] |
| 100 | + if x in op_checks: |
| 101 | + raise BTClibValueError() |
| 102 | + stack: list[bytes] = [] |
| 103 | + verify_script_legacy( |
| 104 | + script_sig, stack, prevouts[i].value, tx, i, flags, False, False |
| 105 | + ) |
| 106 | + p2sh_script = stack[-1] if stack else b"\x00" |
| 107 | + |
| 108 | + script = prevouts[i].script_pub_key.script |
| 109 | + verify_script_legacy(script, stack, prevouts[i].value, tx, i, flags, False, True) |
| 110 | + |
| 111 | + script_type, payload = type_and_payload(script) |
| 112 | + |
| 113 | + p2sh = False |
| 114 | + if script_type == "p2sh" and "P2SH" in flags: |
| 115 | + p2sh = True |
| 116 | + validate_redeem_script(parsed_script_sig) # similar to SIGPUSHONLY |
| 117 | + script = p2sh_script |
| 118 | + verify_script_legacy( |
| 119 | + script, stack, prevouts[i].value, tx, i, flags, False, True |
| 120 | + ) |
| 121 | + script_type, payload = type_and_payload(script) |
| 122 | + |
| 123 | + segwit_version = _to_num(stack[-1], []) if is_segwit(script) else -1 |
| 124 | + supported_segwit_version = -1 |
| 125 | + if "WITNESS" in flags: |
| 126 | + supported_segwit_version = 0 |
| 127 | + if "TAPROOT" in flags: |
| 128 | + supported_segwit_version = 1 |
| 129 | + if segwit_version + 1 and tx.vin[i].script_sig and not p2sh: |
| 130 | + raise BTClibValueError() |
| 131 | + if not (segwit_version + 1) and tx.vin[i].script_witness: |
| 132 | + raise BTClibValueError() # witness without witness script |
| 133 | + if segwit_version > supported_segwit_version: |
| 134 | + if segwit_version + 1 and "DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM" in flags: |
| 135 | + raise BTClibValueError() |
| 136 | + return |
| 137 | + |
| 138 | + if segwit_version == 1: |
| 139 | + if script_type == "p2tr": |
| 140 | + if p2sh: |
| 141 | + return # remains unencumbered |
| 142 | + witness = tx.vin[i].script_witness |
| 143 | + budget = 50 + len(witness.serialize()) |
| 144 | + annex = taproot_get_annex(witness) |
| 145 | + stack = witness.stack |
| 146 | + if len(stack) == 0: |
| 147 | + raise BTClibValueError() |
| 148 | + if len(stack) == 1: |
| 149 | + tapscript.verify_key_path(script, stack, prevouts, tx, i, annex) |
| 150 | + stack = [] |
| 151 | + else: |
| 152 | + script_bytes, stack, leaf_version = taproot_unwrap_script(script, stack) |
| 153 | + if leaf_version == 0xC0: |
| 154 | + tapscript.verify_script_path_vc0( |
| 155 | + script_bytes, stack, prevouts, tx, i, annex, budget, flags |
| 156 | + ) |
| 157 | + else: |
| 158 | + return # unknown program, passes validation |
| 159 | + |
| 160 | + if segwit_version == 0: |
| 161 | + if script_type == "p2wpkh": |
| 162 | + stack = tx.vin[i].script_witness.stack |
| 163 | + # serialization of ["OP_DUP", "OP_HASH160", payload, "OP_EQUALVERIFY", "OP_CHECKSIG"] |
| 164 | + script = b"v\xa9\x14" + payload + b"\x88\xac" |
| 165 | + elif script_type == "p2wsh": |
| 166 | + stack = tx.vin[i].script_witness.stack |
| 167 | + if any(len(x) > 520 for x in stack[:-1]): |
| 168 | + raise BTClibValueError() |
| 169 | + script = stack[-1] |
| 170 | + if payload != sha256(script): |
| 171 | + raise BTClibValueError() |
| 172 | + stack = stack[:-1] |
| 173 | + else: |
| 174 | + raise BTClibValueError() |
| 175 | + |
| 176 | + if "OP_CODESEPARATOR" in parse(script): |
| 177 | + return |
| 178 | + |
| 179 | + verify_script_legacy(script, stack, prevouts[i].value, tx, i, flags, True, True) |
| 180 | + |
| 181 | + if stack and ("CLEANSTACK" in flags or segwit_version == 0): |
| 182 | + raise BTClibValueError() |
| 183 | + |
| 184 | + |
| 185 | +def verify_transaction( |
| 186 | + prevouts: list[TxOut], tx: Tx, flags: list | None = None |
| 187 | +) -> None: |
| 188 | + if flags is None: |
| 189 | + flags = ALL_FLAGS[:] |
| 190 | + if len(prevouts) != len(tx.vin): |
| 191 | + raise BTClibValueError() |
| 192 | + for i in range(len(prevouts)): |
| 193 | + verify_input(prevouts, tx, i, flags) |
0 commit comments