|
| 1 | +/// This maps to `zcash_script_error_t`, but most of those cases aren’t used any more. This only |
| 2 | +/// replicates the still-used cases, and then an `Unknown` bucket for anything else that might |
| 3 | +/// happen. |
| 4 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 5 | +#[repr(u32)] |
| 6 | +pub enum Error { |
| 7 | + Ok = 0, |
| 8 | + VerifyScript = 7, |
| 9 | + Unknown(u32), |
| 10 | +} |
| 11 | + |
| 12 | +bitflags::bitflags! { |
| 13 | + #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 14 | + pub struct VerificationFlags: u32 { |
| 15 | + const None = 0; |
| 16 | + |
| 17 | + /// Evaluate P2SH subscripts (softfork safe, BIP16). |
| 18 | + const P2SH = 1 << 0; |
| 19 | + |
| 20 | + /// Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure. |
| 21 | + /// Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure. |
| 22 | + /// (softfork safe, but not used or intended as a consensus rule). |
| 23 | + const StrictEnc = 1 << 1; |
| 24 | + |
| 25 | + /// Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure |
| 26 | + /// (softfork safe, BIP62 rule 5). |
| 27 | + const LowS = 1 << 3; |
| 28 | + |
| 29 | + /// verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7). |
| 30 | + const NullDummy = 1 << 4; |
| 31 | + |
| 32 | + /// Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2). |
| 33 | + const SigPushOnly = 1 << 5; |
| 34 | + |
| 35 | + /// Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct |
| 36 | + /// pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating |
| 37 | + /// any other push causes the script to fail (BIP62 rule 3). |
| 38 | + /// In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4). |
| 39 | + /// (softfork safe) |
| 40 | + const MinimalData = 1 << 6; |
| 41 | + |
| 42 | + /// Discourage use of NOPs reserved for upgrades (NOP1-10) |
| 43 | + /// |
| 44 | + /// Provided so that nodes can avoid accepting or mining transactions |
| 45 | + /// containing executed NOP's whose meaning may change after a soft-fork, |
| 46 | + /// thus rendering the script invalid; with this flag set executing |
| 47 | + /// discouraged NOPs fails the script. This verification flag will never be |
| 48 | + /// a mandatory flag applied to scripts in a block. NOPs that are not |
| 49 | + /// executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected. |
| 50 | + const DiscourageUpgradableNOPs = 1 << 7; |
| 51 | + |
| 52 | + /// Require that only a single stack element remains after evaluation. This changes the success criterion from |
| 53 | + /// "At least one stack element must remain, and when interpreted as a boolean, it must be true" to |
| 54 | + /// "Exactly one stack element must remain, and when interpreted as a boolean, it must be true". |
| 55 | + /// (softfork safe, BIP62 rule 6) |
| 56 | + /// Note: CLEANSTACK should never be used without P2SH. |
| 57 | + const CleanStack = 1 << 8; |
| 58 | + |
| 59 | + /// Verify CHECKLOCKTIMEVERIFY |
| 60 | + /// |
| 61 | + /// See BIP65 for details. |
| 62 | + const CHECKLOCKTIMEVERIFY = 1 << 9; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +bitflags::bitflags! { |
| 67 | + /// The different SigHash types, as defined in <https://zips.z.cash/zip-0143> |
| 68 | + /// |
| 69 | + /// TODO: There are three implementations of this (with three distinct primitive types): |
| 70 | + /// - u8 constants in librustzcash, |
| 71 | + /// - i32 (well, c_int) bitflags from the C++ constants here, and |
| 72 | + /// - u32 bitflags in zebra-chain. |
| 73 | + /// |
| 74 | + /// Ideally we could unify on bitflags in librustzcash. |
| 75 | + #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 76 | + pub struct HashType: i32 { |
| 77 | + /// Sign all the outputs |
| 78 | + const All = 1; |
| 79 | + /// Sign none of the outputs - anyone can spend |
| 80 | + const None = 2; |
| 81 | + /// Sign one of the outputs - anyone can spend the rest |
| 82 | + const Single = Self::All.bits() | Self::None.bits(); |
| 83 | + /// Anyone can add inputs to this transaction |
| 84 | + const AnyoneCanPay = 0x80; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// A function which is called to obtain the sighash. |
| 89 | +/// - script_code: the scriptCode being validated. Note that this not always |
| 90 | +/// matches script_sig, i.e. for P2SH. |
| 91 | +/// - hash_type: the hash type being used. |
| 92 | +/// |
| 93 | +/// The underlying C++ callback doesn’t give much opportunity for rich failure reporting, but |
| 94 | +/// returning `None` indicates _some_ failure to produce the desired hash. |
| 95 | +/// |
| 96 | +/// TODO: Can we get the “32” from somewhere rather than hardcoding it? |
| 97 | +pub type SighashCallback = dyn Fn(&[u8], HashType) -> Option<[u8; 32]>; |
| 98 | + |
| 99 | +/// The external API of zcash_script. This is defined to make it possible to compare the C++ and |
| 100 | +/// Rust implementations. |
| 101 | +pub trait Script { |
| 102 | + /// Returns `Ok(())` if the a transparent input correctly spends the matching output |
| 103 | + /// under the additional constraints specified by `flags`. This function |
| 104 | + /// receives only the required information to validate the spend and not |
| 105 | + /// the transaction itself. In particular, the sighash for the spend |
| 106 | + /// is obtained using a callback function. |
| 107 | + /// |
| 108 | + /// - sighash_callback: a callback function which is called to obtain the sighash. |
| 109 | + /// - n_lock_time: the lock time of the transaction being validated. |
| 110 | + /// - is_final: a boolean indicating whether the input being validated is final |
| 111 | + /// (i.e. its sequence number is 0xFFFFFFFF). |
| 112 | + /// - script_pub_key: the scriptPubKey of the output being spent. |
| 113 | + /// - script_sig: the scriptSig of the input being validated. |
| 114 | + /// - flags: the script verification flags to use. |
| 115 | + /// - err: if not NULL, err will contain an error/success code for the operation. |
| 116 | + /// |
| 117 | + /// Note that script verification failure is indicated by `Err(Error::Ok)`. |
| 118 | + fn verify_callback( |
| 119 | + sighash_callback: &SighashCallback, |
| 120 | + n_lock_time: i64, |
| 121 | + is_final: bool, |
| 122 | + script_pub_key: &[u8], |
| 123 | + script_sig: &[u8], |
| 124 | + flags: VerificationFlags, |
| 125 | + ) -> Result<(), Error>; |
| 126 | + |
| 127 | + /// Returns the number of transparent signature operations in the input or |
| 128 | + /// output script pointed to by script. |
| 129 | + fn legacy_sigop_count_script(script: &[u8]) -> u32; |
| 130 | +} |
0 commit comments