Skip to main content

bsv_script/interpreter/
flags.rs

1//! Script verification flags (bitmask).
2
3use std::ops::{BitAnd, BitOr, BitOrAssign};
4
5/// Script verification flags controlling interpreter behavior.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub struct ScriptFlags(pub u32);
8
9impl ScriptFlags {
10    /// No flags set; accept all transactions.
11    pub const NONE: ScriptFlags = ScriptFlags(0);
12    /// Evaluate P2SH (BIP16) subscripts.
13    pub const BIP16: ScriptFlags = ScriptFlags(1 << 0);
14    /// Enforce strict multisig dummy element (must be OP_0).
15    pub const STRICT_MULTI_SIG: ScriptFlags = ScriptFlags(1 << 1);
16    /// Discourage use of upgradable NOP opcodes (NOP1-NOP10).
17    pub const DISCOURAGE_UPGRADABLE_NOPS: ScriptFlags = ScriptFlags(1 << 2);
18    /// Enforce OP_CHECKLOCKTIMEVERIFY (BIP65).
19    pub const VERIFY_CHECKLOCKTIMEVERIFY: ScriptFlags = ScriptFlags(1 << 3);
20    /// Enforce OP_CHECKSEQUENCEVERIFY (BIP112).
21    pub const VERIFY_CHECKSEQUENCEVERIFY: ScriptFlags = ScriptFlags(1 << 4);
22    /// Require exactly one element on the stack after execution.
23    pub const VERIFY_CLEAN_STACK: ScriptFlags = ScriptFlags(1 << 5);
24    /// Require strict DER encoding for signatures.
25    pub const VERIFY_DER_SIGNATURES: ScriptFlags = ScriptFlags(1 << 6);
26    /// Require the S value in signatures to be in the lower half of the curve order.
27    pub const VERIFY_LOW_S: ScriptFlags = ScriptFlags(1 << 7);
28    /// Require minimal encoding for data pushes.
29    pub const VERIFY_MINIMAL_DATA: ScriptFlags = ScriptFlags(1 << 8);
30    /// Require failed CHECK(MULTI)SIG operations to have empty signatures.
31    pub const VERIFY_NULL_FAIL: ScriptFlags = ScriptFlags(1 << 9);
32    /// Require the unlocking script to contain only push opcodes.
33    pub const VERIFY_SIG_PUSH_ONLY: ScriptFlags = ScriptFlags(1 << 10);
34    /// Enable SIGHASH_FORKID replay protection (BSV-specific).
35    pub const ENABLE_SIGHASH_FORKID: ScriptFlags = ScriptFlags(1 << 11);
36    /// Require strict signature and public key encoding.
37    pub const VERIFY_STRICT_ENCODING: ScriptFlags = ScriptFlags(1 << 12);
38    /// Use BIP143-style sighash algorithm for signature verification.
39    pub const VERIFY_BIP143_SIGHASH: ScriptFlags = ScriptFlags(1 << 13);
40    /// Indicates the UTXO being spent was created after the genesis upgrade.
41    pub const UTXO_AFTER_GENESIS: ScriptFlags = ScriptFlags(1 << 14);
42    /// Require OP_IF/OP_NOTIF arguments to be exactly empty or 0x01.
43    pub const VERIFY_MINIMAL_IF: ScriptFlags = ScriptFlags(1 << 15);
44
45    /// Return true if the given flag is set in this flags value.
46    pub fn has_flag(self, flag: ScriptFlags) -> bool {
47        self.0 & flag.0 == flag.0
48    }
49
50    /// Return true if any of the given flags are set in this flags value.
51    pub fn has_any(self, flags: &[ScriptFlags]) -> bool {
52        flags.iter().any(|f| self.has_flag(*f))
53    }
54
55    /// Set the given flag bits in this flags value.
56    pub fn add_flag(&mut self, flag: ScriptFlags) {
57        self.0 |= flag.0;
58    }
59}
60
61impl BitOr for ScriptFlags {
62    type Output = Self;
63    fn bitor(self, rhs: Self) -> Self {
64        ScriptFlags(self.0 | rhs.0)
65    }
66}
67
68impl BitOrAssign for ScriptFlags {
69    fn bitor_assign(&mut self, rhs: Self) {
70        self.0 |= rhs.0;
71    }
72}
73
74impl BitAnd for ScriptFlags {
75    type Output = Self;
76    fn bitand(self, rhs: Self) -> Self {
77        ScriptFlags(self.0 & rhs.0)
78    }
79}