|
1 | 1 | #![doc(html_logo_url = "https://www.zfnd.org/images/zebra-icon.png")] |
2 | | -#![doc(html_root_url = "https://docs.rs/zcash_script/0.2.0")] |
| 2 | +#![doc(html_root_url = "https://docs.rs/zcash_script/0.3.0")] |
| 3 | +#![allow(unsafe_code)] |
3 | 4 |
|
4 | 5 | mod cxx; |
5 | 6 | pub use cxx::*; |
| 7 | + |
| 8 | +mod interpreter; |
| 9 | +pub use interpreter::{HashType, VerificationFlags}; |
| 10 | +mod zcash_script; |
| 11 | +pub use zcash_script::*; |
| 12 | + |
| 13 | +use std::os::raw::{c_int, c_uint, c_void}; |
| 14 | + |
| 15 | +pub enum Cxx {} |
| 16 | + |
| 17 | +impl From<zcash_script_error_t> for Error { |
| 18 | + #[allow(non_upper_case_globals)] |
| 19 | + fn from(err_code: zcash_script_error_t) -> Error { |
| 20 | + match err_code { |
| 21 | + zcash_script_error_t_zcash_script_ERR_OK => Error::Ok, |
| 22 | + zcash_script_error_t_zcash_script_ERR_VERIFY_SCRIPT => Error::VerifyScript, |
| 23 | + unknown => Error::Unknown(unknown), |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// The sighash callback to use with zcash_script. |
| 29 | +extern "C" fn sighash( |
| 30 | + sighash_out: *mut u8, |
| 31 | + sighash_out_len: c_uint, |
| 32 | + ctx: *const c_void, |
| 33 | + script_code: *const u8, |
| 34 | + script_code_len: c_uint, |
| 35 | + hash_type: c_int, |
| 36 | +) { |
| 37 | + // SAFETY: `ctx` is a valid SighashCallbackt because it is always passed to |
| 38 | + // `verify_callback` which simply forwards it to the callback. |
| 39 | + // `script_code` and `sighash_out` are valid buffers since they are always |
| 40 | + // specified when the callback is called. |
| 41 | + unsafe { |
| 42 | + let ctx = ctx as *const &SighashCallback; |
| 43 | + let script_code_vec = std::slice::from_raw_parts(script_code, script_code_len as usize); |
| 44 | + if let Some(sighash) = (*ctx)(script_code_vec, HashType::from_bits_retain(hash_type)) { |
| 45 | + // Sanity check; must always be true. |
| 46 | + assert_eq!(sighash_out_len, sighash.len() as c_uint); |
| 47 | + std::ptr::copy_nonoverlapping(sighash.as_ptr(), sighash_out, sighash.len()); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// This steals a bit of the wrapper code from zebra_script, to provide the API that they want. |
| 53 | +impl ZcashScript for Cxx { |
| 54 | + fn verify_callback( |
| 55 | + sighash_callback: &SighashCallback, |
| 56 | + lock_time: i64, |
| 57 | + is_final: bool, |
| 58 | + script_pub_key: &[u8], |
| 59 | + signature_script: &[u8], |
| 60 | + flags: VerificationFlags, |
| 61 | + ) -> Result<(), Error> { |
| 62 | + let mut err = 0; |
| 63 | + |
| 64 | + let flags = flags.bits(); |
| 65 | + |
| 66 | + let is_final = if is_final { 1 } else { 0 }; |
| 67 | + |
| 68 | + // SAFETY: The `script` fields are created from a valid Rust `slice`. |
| 69 | + let ret = unsafe { |
| 70 | + zcash_script_verify_callback( |
| 71 | + (&sighash_callback as *const &SighashCallback) as *const c_void, |
| 72 | + Some(sighash), |
| 73 | + lock_time, |
| 74 | + is_final, |
| 75 | + script_pub_key.as_ptr(), |
| 76 | + script_pub_key.len() as u32, |
| 77 | + signature_script.as_ptr(), |
| 78 | + signature_script.len() as u32, |
| 79 | + flags, |
| 80 | + &mut err, |
| 81 | + ) |
| 82 | + }; |
| 83 | + |
| 84 | + if ret == 1 { |
| 85 | + Ok(()) |
| 86 | + } else { |
| 87 | + Err(Error::from(err)) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// Returns the number of transparent signature operations in the |
| 92 | + /// transparent inputs and outputs of this transaction. |
| 93 | + fn legacy_sigop_count_script(script: &[u8]) -> u32 { |
| 94 | + unsafe { zcash_script_legacy_sigop_count_script(script.as_ptr(), script.len() as u32) } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + pub use super::*; |
| 101 | + use hex::FromHex; |
| 102 | + |
| 103 | + lazy_static::lazy_static! { |
| 104 | + pub static ref SCRIPT_PUBKEY: Vec<u8> = <Vec<u8>>::from_hex("a914c117756dcbe144a12a7c33a77cfa81aa5aeeb38187").unwrap(); |
| 105 | + pub static ref SCRIPT_SIG: Vec<u8> = <Vec<u8>>::from_hex("00483045022100d2ab3e6258fe244fa442cfb38f6cef9ac9a18c54e70b2f508e83fa87e20d040502200eead947521de943831d07a350e45af8e36c2166984a8636f0a8811ff03ed09401473044022013e15d865010c257eef133064ef69a780b4bc7ebe6eda367504e806614f940c3022062fdbc8c2d049f91db2042d6c9771de6f1ef0b3b1fea76c1ab5542e44ed29ed8014c69522103b2cc71d23eb30020a4893982a1e2d352da0d20ee657fa02901c432758909ed8f21029d1e9a9354c0d2aee9ffd0f0cea6c39bbf98c4066cf143115ba2279d0ba7dabe2103e32096b63fd57f3308149d238dcbb24d8d28aad95c0e4e74e3e5e6a11b61bcc453ae").expect("Block bytes are in valid hex representation"); |
| 106 | + } |
| 107 | + |
| 108 | + fn sighash(_script_code: &[u8], _hash_type: HashType) -> Option<[u8; 32]> { |
| 109 | + hex::decode("e8c7bdac77f6bb1f3aba2eaa1fada551a9c8b3b5ecd1ef86e6e58a5f1aab952c") |
| 110 | + .unwrap() |
| 111 | + .as_slice() |
| 112 | + .first_chunk::<32>() |
| 113 | + .map(|hash| *hash) |
| 114 | + } |
| 115 | + |
| 116 | + fn invalid_sighash(_script_code: &[u8], _hash_type: HashType) -> Option<[u8; 32]> { |
| 117 | + hex::decode("08c7bdac77f6bb1f3aba2eaa1fada551a9c8b3b5ecd1ef86e6e58a5f1aab952c") |
| 118 | + .unwrap() |
| 119 | + .as_slice() |
| 120 | + .first_chunk::<32>() |
| 121 | + .map(|hash| *hash) |
| 122 | + } |
| 123 | + |
| 124 | + fn missing_sighash(_script_code: &[u8], _hash_type: HashType) -> Option<[u8; 32]> { |
| 125 | + None |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn it_works() { |
| 130 | + let n_lock_time: i64 = 2410374; |
| 131 | + let is_final: bool = true; |
| 132 | + let script_pub_key = &SCRIPT_PUBKEY; |
| 133 | + let script_sig = &SCRIPT_SIG; |
| 134 | + let flags = VerificationFlags::P2SH | VerificationFlags::CHECKLOCKTIMEVERIFY; |
| 135 | + |
| 136 | + let ret = Cxx::verify_callback( |
| 137 | + &sighash, |
| 138 | + n_lock_time, |
| 139 | + is_final, |
| 140 | + script_pub_key, |
| 141 | + script_sig, |
| 142 | + flags, |
| 143 | + ); |
| 144 | + |
| 145 | + assert!(ret.is_ok()); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn it_fails_on_invalid_sighash() { |
| 150 | + let n_lock_time: i64 = 2410374; |
| 151 | + let is_final: bool = true; |
| 152 | + let script_pub_key = &SCRIPT_PUBKEY; |
| 153 | + let script_sig = &SCRIPT_SIG; |
| 154 | + let flags = VerificationFlags::P2SH | VerificationFlags::CHECKLOCKTIMEVERIFY; |
| 155 | + |
| 156 | + let ret = Cxx::verify_callback( |
| 157 | + &invalid_sighash, |
| 158 | + n_lock_time, |
| 159 | + is_final, |
| 160 | + script_pub_key, |
| 161 | + script_sig, |
| 162 | + flags, |
| 163 | + ); |
| 164 | + |
| 165 | + assert_eq!(ret, Err(Error::Ok)); |
| 166 | + } |
| 167 | + |
| 168 | + #[test] |
| 169 | + fn it_fails_on_missing_sighash() { |
| 170 | + let n_lock_time: i64 = 2410374; |
| 171 | + let is_final: bool = true; |
| 172 | + let script_pub_key = &SCRIPT_PUBKEY; |
| 173 | + let script_sig = &SCRIPT_SIG; |
| 174 | + let flags = VerificationFlags::P2SH | VerificationFlags::CHECKLOCKTIMEVERIFY; |
| 175 | + |
| 176 | + let ret = Cxx::verify_callback( |
| 177 | + &missing_sighash, |
| 178 | + n_lock_time, |
| 179 | + is_final, |
| 180 | + script_pub_key, |
| 181 | + script_sig, |
| 182 | + flags, |
| 183 | + ); |
| 184 | + |
| 185 | + assert_eq!(ret, Err(Error::Ok)); |
| 186 | + } |
| 187 | +} |
0 commit comments