1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![doc(html_logo_url = "https://www.zfnd.org/images/zebra-icon.png")]
#![doc(html_root_url = "https://docs.rs/zcash_script/0.1.5")]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

mod blake2b;

#[cfg(test)]
mod tests {
    pub use super::zcash_script_error_t;
    use hex::FromHex;

    lazy_static::lazy_static! {
        pub static ref SCRIPT_PUBKEY: Vec<u8> = <Vec<u8>>::from_hex("76a914f47cac1e6fec195c055994e8064ffccce0044dd788ac").unwrap();
        pub static ref SCRIPT_TX: Vec<u8> = <Vec<u8>>::from_hex("0400008085202f8901fcaf44919d4a17f6181a02a7ebe0420be6f7dad1ef86755b81d5a9567456653c010000006a473044022035224ed7276e61affd53315eca059c92876bc2df61d84277cafd7af61d4dbf4002203ed72ea497a9f6b38eb29df08e830d99e32377edb8a574b8a289024f0241d7c40121031f54b095eae066d96b2557c1f99e40e967978a5fd117465dbec0986ca74201a6feffffff020050d6dc0100000017a9141b8a9bda4b62cd0d0582b55455d0778c86f8628f870d03c812030000001976a914e4ff5512ffafe9287992a1cd177ca6e408e0300388ac62070d0095070d000000000000000000000000").expect("Block bytes are in valid hex representation");
    }

    pub fn verify_script(
        script_pub_key: &[u8],
        amount: i64,
        tx_to: &[u8],
        nIn: u32,
        flags: u32,
        consensus_branch_id: u32,
    ) -> Result<(), zcash_script_error_t> {
        let script_ptr = script_pub_key.as_ptr();
        let script_len = script_pub_key.len();
        let tx_to_ptr = tx_to.as_ptr();
        let tx_to_len = tx_to.len();
        let mut err = 0;

        let ret = unsafe {
            super::zcash_script_verify(
                script_ptr,
                script_len as u32,
                amount,
                tx_to_ptr,
                tx_to_len as u32,
                nIn,
                flags,
                consensus_branch_id,
                &mut err,
            )
        };

        if ret == 1 {
            Ok(())
        } else {
            Err(err)
        }
    }

    pub fn verify_script_precompute(
        script_pub_key: &[u8],
        amount: i64,
        tx_to: &[u8],
        nIn: u32,
        flags: u32,
        consensus_branch_id: u32,
    ) -> Result<(), zcash_script_error_t> {
        let script_ptr = script_pub_key.as_ptr();
        let script_len = script_pub_key.len();
        let tx_to_ptr = tx_to.as_ptr();
        let tx_to_len = tx_to.len();
        let mut err = 0;

        let precomputed =
            unsafe { super::zcash_script_new_precomputed_tx(tx_to_ptr, tx_to_len as _, &mut err) };

        let ret = unsafe {
            super::zcash_script_verify_precomputed(
                precomputed,
                nIn,
                script_ptr,
                script_len as u32,
                amount,
                flags,
                consensus_branch_id,
                &mut err,
            )
        };

        unsafe { super::zcash_script_free_precomputed_tx(precomputed) };

        if ret == 1 {
            Ok(())
        } else {
            Err(err)
        }
    }

    #[test]
    fn it_works() {
        let coin = i64::pow(10, 8);
        let script_pub_key = &*SCRIPT_PUBKEY;
        let amount = 212 * coin;
        let tx_to = &*SCRIPT_TX;
        let nIn = 0;
        let flags = 1;
        let branch_id = 0x2bb40e60;

        verify_script(script_pub_key, amount, tx_to, nIn, flags, branch_id).unwrap();
    }

    #[test]
    fn it_works_precomputed() {
        let coin = i64::pow(10, 8);
        let script_pub_key = &*SCRIPT_PUBKEY;
        let amount = 212 * coin;
        let tx_to = &*SCRIPT_TX;
        let nIn = 0;
        let flags = 1;
        let branch_id = 0x2bb40e60;

        verify_script_precompute(script_pub_key, amount, tx_to, nIn, flags, branch_id).unwrap();
    }

    #[test]
    fn it_doesnt_work() {
        let coin = i64::pow(10, 8);
        let script_pub_key = &*SCRIPT_PUBKEY;
        let amount = 212 * coin;
        let tx_to = &*SCRIPT_TX;
        let nIn = 0;
        let flags = 1;
        let branch_id = 0x2bb40e61;

        verify_script(script_pub_key, amount, tx_to, nIn, flags, branch_id).unwrap_err();
    }

    #[test]
    fn it_doesnt_work_precomputed() {
        let coin = i64::pow(10, 8);
        let script_pub_key = &*SCRIPT_PUBKEY;
        let amount = 212 * coin;
        let tx_to = &*SCRIPT_TX;
        let nIn = 0;
        let flags = 1;
        let branch_id = 0x2bb40e61;

        verify_script_precompute(script_pub_key, amount, tx_to, nIn, flags, branch_id).unwrap_err();
    }
}