1919
2020extern crate libc;
2121
22- use libc:: { c_int, c_uchar, c_uint} ;
2322use core:: fmt;
2423
24+ use libc:: { c_int, c_uchar, c_uint} ;
25+
2526/// Errors returned by `libbitcoinconsensus` (see github.com/bitcoin/bitcoin/doc/shared-libraries.md).
2627#[ allow( non_camel_case_types) ]
2728#[ derive( PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Clone , Copy ) ]
@@ -38,7 +39,7 @@ pub enum Error {
3839 /// Input amount is required if WITNESS is used.
3940 ERR_AMOUNT_REQUIRED ,
4041 /// Script verification `flags` are invalid (i.e. not part of the libconsensus interface).
41- ERR_INVALID_FLAGS
42+ ERR_INVALID_FLAGS ,
4243}
4344
4445impl fmt:: Display for Error {
@@ -63,33 +64,33 @@ impl std::error::Error for Error {
6364 use self :: Error :: * ;
6465
6566 match * self {
66- ERR_SCRIPT
67- | ERR_TX_INDEX
68- | ERR_TX_SIZE_MISMATCH
69- | ERR_TX_DESERIALIZE
70- | ERR_AMOUNT_REQUIRED
71- | ERR_INVALID_FLAGS => None ,
67+ ERR_SCRIPT | ERR_TX_INDEX | ERR_TX_SIZE_MISMATCH | ERR_TX_DESERIALIZE
68+ | ERR_AMOUNT_REQUIRED | ERR_INVALID_FLAGS => None ,
7269 }
7370 }
7471}
7572
7673/// Do not enable any verification.
77- pub const VERIFY_NONE : c_uint = 0 ;
74+ pub const VERIFY_NONE : c_uint = 0 ;
7875/// Evaluate P2SH (BIP16) subscripts.
79- pub const VERIFY_P2SH : c_uint = 1 << 0 ;
76+ pub const VERIFY_P2SH : c_uint = 1 << 0 ;
8077/// Enforce strict DER (BIP66) compliance.
81- pub const VERIFY_DERSIG : c_uint = 1 << 2 ;
78+ pub const VERIFY_DERSIG : c_uint = 1 << 2 ;
8279/// Enforce NULLDUMMY (BIP147).
83- pub const VERIFY_NULLDUMMY : c_uint = 1 << 4 ;
80+ pub const VERIFY_NULLDUMMY : c_uint = 1 << 4 ;
8481/// Enable CHECKLOCKTIMEVERIFY (BIP65).
85- pub const VERIFY_CHECKLOCKTIMEVERIFY : c_uint = 1 << 9 ;
82+ pub const VERIFY_CHECKLOCKTIMEVERIFY : c_uint = 1 << 9 ;
8683/// Enable CHECKSEQUENCEVERIFY (BIP112).
87- pub const VERIFY_CHECKSEQUENCEVERIFY : c_uint = 1 << 10 ;
84+ pub const VERIFY_CHECKSEQUENCEVERIFY : c_uint = 1 << 10 ;
8885/// Enable WITNESS (BIP141).
89- pub const VERIFY_WITNESS : c_uint = 1 << 11 ;
86+ pub const VERIFY_WITNESS : c_uint = 1 << 11 ;
9087
91- pub const VERIFY_ALL : c_uint = VERIFY_P2SH | VERIFY_DERSIG | VERIFY_NULLDUMMY |
92- VERIFY_CHECKLOCKTIMEVERIFY | VERIFY_CHECKSEQUENCEVERIFY | VERIFY_WITNESS ;
88+ pub const VERIFY_ALL : c_uint = VERIFY_P2SH
89+ | VERIFY_DERSIG
90+ | VERIFY_NULLDUMMY
91+ | VERIFY_CHECKLOCKTIMEVERIFY
92+ | VERIFY_CHECKSEQUENCEVERIFY
93+ | VERIFY_WITNESS ;
9394
9495extern "C" {
9596 /// Returns `libbitcoinconsensus` version.
@@ -98,14 +99,15 @@ extern "C" {
9899 /// Verifies that the transaction input correctly spends the previous
99100 /// output, considering any additional constraints specified by flags.
100101 pub fn bitcoinconsensus_verify_script_with_amount (
101- script_pubkey : * const c_uchar ,
102+ script_pubkey : * const c_uchar ,
102103 script_pubkeylen : c_uint ,
103104 amount : u64 ,
104105 tx_to : * const c_uchar ,
105106 tx_tolen : c_uint ,
106107 n_in : c_uint ,
107108 flags : c_uint ,
108- err : * mut Error ) -> c_int ;
109+ err : * mut Error ,
110+ ) -> c_int ;
109111}
110112
111113/// Computes flags for soft fork activation heights on the Bitcoin network.
@@ -132,9 +134,7 @@ pub fn height_to_flags(height: u32) -> u32 {
132134}
133135
134136/// Returns `libbitcoinconsensus` version.
135- pub fn version ( ) -> u32 {
136- unsafe { bitcoinconsensus_version ( ) as u32 }
137- }
137+ pub fn version ( ) -> u32 { unsafe { bitcoinconsensus_version ( ) as u32 } }
138138
139139/// Verifies a single spend (input) of a Bitcoin transaction.
140140///
@@ -174,12 +174,23 @@ pub fn version () -> u32 {
174174/// should return `Ok(())`.
175175///
176176/// **Note** since the spent amount will only be checked for Segwit transactions and the above example is not segwit, `verify` will succeed with any amount.
177- pub fn verify ( spent_output : & [ u8 ] , amount : u64 , spending_transaction : & [ u8 ] , input_index : usize ) -> Result < ( ) , Error > {
178- verify_with_flags ( spent_output, amount, spending_transaction, input_index, VERIFY_ALL )
177+ pub fn verify (
178+ spent_output : & [ u8 ] ,
179+ amount : u64 ,
180+ spending_transaction : & [ u8 ] ,
181+ input_index : usize ,
182+ ) -> Result < ( ) , Error > {
183+ verify_with_flags ( spent_output, amount, spending_transaction, input_index, VERIFY_ALL )
179184}
180185
181186/// Same as verify but with flags that turn past soft fork features on or off.
182- pub fn verify_with_flags ( spent_output_script : & [ u8 ] , amount : u64 , spending_transaction : & [ u8 ] , input_index : usize , flags : u32 ) -> Result < ( ) , Error > {
187+ pub fn verify_with_flags (
188+ spent_output_script : & [ u8 ] ,
189+ amount : u64 ,
190+ spending_transaction : & [ u8 ] ,
191+ input_index : usize ,
192+ flags : u32 ,
193+ ) -> Result < ( ) , Error > {
183194 unsafe {
184195 let mut error = Error :: ERR_SCRIPT ;
185196
@@ -191,7 +202,7 @@ pub fn verify_with_flags (spent_output_script: &[u8], amount: u64, spending_tran
191202 spending_transaction. len ( ) as c_uint ,
192203 input_index as c_uint ,
193204 flags as c_uint ,
194- & mut error
205+ & mut error,
195206 ) ;
196207 if ret != 1 {
197208 Err ( error)
@@ -205,8 +216,8 @@ pub fn verify_with_flags (spent_output_script: &[u8], amount: u64, spending_tran
205216mod tests {
206217 extern crate rustc_serialize as serialize;
207218
208- use super :: * ;
209219 use self :: serialize:: hex:: FromHex ;
220+ use super :: * ;
210221
211222 #[ test]
212223 fn bitcoinconsensus_test ( ) {
@@ -250,15 +261,17 @@ mod tests {
250261 "010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000" ,
251262 18393430 , 0
252263 ) . is_err( ) ) ;
253-
254264 }
255265
256- fn verify_test ( spent : & str , spending : & str , amount : u64 , input : usize ) -> Result < ( ) , Error > {
257- verify ( spent. from_hex ( ) . unwrap ( ) . as_slice ( ) , amount, spending. from_hex ( ) . unwrap ( ) . as_slice ( ) , input)
266+ fn verify_test ( spent : & str , spending : & str , amount : u64 , input : usize ) -> Result < ( ) , Error > {
267+ verify (
268+ spent. from_hex ( ) . unwrap ( ) . as_slice ( ) ,
269+ amount,
270+ spending. from_hex ( ) . unwrap ( ) . as_slice ( ) ,
271+ input,
272+ )
258273 }
259274
260275 #[ test]
261- fn invalid_flags_test ( ) {
262- verify_with_flags ( & [ ] , 0 , & [ ] , 0 , VERIFY_ALL + 1 ) . unwrap_err ( ) ;
263- }
276+ fn invalid_flags_test ( ) { verify_with_flags ( & [ ] , 0 , & [ ] , 0 , VERIFY_ALL + 1 ) . unwrap_err ( ) ; }
264277}
0 commit comments