33//! High level APIs for creating C FFI compatible environment.
44//!
55
6+ use bitcoin:: taproot:: TAPROOT_ANNEX_PREFIX ;
67use hashes:: Hash ;
78use std:: os:: raw:: c_uchar;
89
@@ -33,7 +34,6 @@ struct RawOutputData {
3334/// passed to the C FFI.
3435#[ derive( Debug ) ]
3536struct RawInputData {
36- #[ allow( dead_code) ] // see FIXME below
3737 pub annex : Option < Vec < c_uchar > > ,
3838 // pegin
3939 pub genesis_hash : Option < [ c_uchar ; 32 ] > ,
@@ -73,10 +73,10 @@ fn new_raw_input<'raw>(
7373 inp : & ' raw elements:: TxIn ,
7474 in_utxo : & ' raw ElementsUtxo ,
7575 inp_data : & ' raw RawInputData ,
76+ annex : * const c_elements:: CRawBuffer ,
7677) -> c_elements:: CRawInput < ' raw > {
7778 c_elements:: CRawInput {
78- // FIXME actually pass the annex in; see https://github.com/BlockstreamResearch/simplicity/issues/311 for some difficulty here.
79- annex : core:: ptr:: null ( ) ,
79+ annex,
8080 prev_txid : inp. previous_output . txid . as_ref ( ) ,
8181 pegin : inp_data. genesis_hash . as_ref ( ) ,
8282 issuance : if inp. has_issuance ( ) {
@@ -114,7 +114,7 @@ fn new_tx_data(tx: &elements::Transaction, in_utxos: &[ElementsUtxo]) -> RawTran
114114 } ;
115115 for ( inp, in_utxo) in tx. input . iter ( ) . zip ( in_utxos. iter ( ) ) {
116116 let inp_data = RawInputData {
117- annex : None , // Actually store annex
117+ annex : get_annex ( & inp . witness ) . map ( |s| s . to_vec ( ) ) ,
118118 genesis_hash : inp
119119 . pegin_data ( )
120120 . map ( |x| x. genesis_hash . to_raw_hash ( ) . to_byte_array ( ) ) ,
@@ -148,15 +148,29 @@ pub(super) fn new_tx(
148148) -> * mut c_elements:: CTransaction {
149149 let mut raw_inputs = Vec :: new ( ) ;
150150 let mut raw_outputs = Vec :: new ( ) ;
151+
152+ // SAFETY: this allocation *must* live until after the `simplicity_mallocTransaction`
153+ // at the bottom of this function. We convert the vector to a boxed slice to ensure
154+ // it cannot be resized, which would potentially trigger a reallocation.
155+ let mut raw_annexes = Vec :: from_iter ( ( 0 ..tx. input . len ( ) ) . map ( |_| None ) ) . into_boxed_slice ( ) ;
156+
151157 let txid = tx. txid ( ) ;
152158 let tx_data = new_tx_data ( tx, in_utxos) ;
153- for ( ( inp, in_utxo) , inp_data) in tx
159+ for ( ( ( n , inp) , in_utxo) , inp_data) in tx
154160 . input
155161 . iter ( )
162+ . enumerate ( )
156163 . zip ( in_utxos. iter ( ) )
157164 . zip ( tx_data. inputs . iter ( ) )
158165 {
159- let res = new_raw_input ( inp, in_utxo, inp_data) ;
166+ raw_annexes[ n] = inp_data
167+ . annex
168+ . as_ref ( )
169+ . map ( |annex| c_elements:: CRawBuffer :: new ( annex) ) ;
170+ let annex_ptr = raw_annexes[ n]
171+ . as_ref ( )
172+ . map_or ( core:: ptr:: null ( ) , |b| b as * const _ ) ;
173+ let res = new_raw_input ( inp, in_utxo, inp_data, annex_ptr) ;
160174 raw_inputs. push ( res) ;
161175 }
162176 for ( out, out_data) in tx. output . iter ( ) . zip ( tx_data. outputs . iter ( ) ) {
@@ -172,10 +186,16 @@ pub(super) fn new_tx(
172186 version : tx. version ,
173187 locktime : tx. lock_time . to_consensus_u32 ( ) ,
174188 } ;
175- unsafe {
189+ let ret = unsafe {
176190 // SAFETY: this is a FFI call and we constructed its argument correctly.
177191 c_elements:: simplicity_mallocTransaction ( & c_raw_tx)
178- }
192+ } ;
193+
194+ // Explicitly drop raw_annexes so Rust doesn't try any funny business dropping it early.
195+ // Drop raw_inputs first since it contains pointers into raw_annexes
196+ drop ( raw_inputs) ;
197+ drop ( raw_annexes) ;
198+ ret
179199}
180200
181201pub ( super ) fn new_tap_env (
@@ -256,3 +276,13 @@ fn serialize_surjection_proof(surjection_proof: &Option<Box<SurjectionProof>>) -
256276 . map ( |x| x. serialize ( ) )
257277 . unwrap_or_default ( )
258278}
279+
280+ /// If the last item in the witness stack is an annex, return the data following the 0x50 byte.
281+ fn get_annex ( in_witness : & elements:: TxInWitness ) -> Option < & [ u8 ] > {
282+ let last_item = in_witness. script_witness . last ( ) ?;
283+ if * last_item. first ( ) ? == TAPROOT_ANNEX_PREFIX {
284+ Some ( & last_item[ 1 ..] )
285+ } else {
286+ None
287+ }
288+ }
0 commit comments