+#[repr(C)]
+/// A serialized witness.
+pub struct Witness {
+ /// The serialized transaction data.
+ ///
+ /// This is non-const for your convenience, an object passed to Rust is never written to.
+ pub data: *mut u8,
+ /// The length of the serialized transaction
+ pub datalen: usize,
+ /// Whether the data pointed to by `data` should be freed or not.
+ pub data_is_owned: bool,
+}
+impl Witness {
+ fn from_vec(vec: Vec<u8>) -> Self {
+ let datalen = vec.len();
+ let data = Box::into_raw(vec.into_boxed_slice());
+ Self {
+ data: unsafe { (*data).as_mut_ptr() },
+ datalen,
+ data_is_owned: true,
+ }
+ }
+ pub(crate) fn into_bitcoin(&self) -> BitcoinWitness {
+ ::bitcoin::consensus::encode::deserialize(unsafe { core::slice::from_raw_parts(self.data, self.datalen) }).unwrap()
+ }
+ pub(crate) fn from_bitcoin(btc: &BitcoinWitness) -> Self {
+ let vec = ::bitcoin::consensus::encode::serialize(btc);
+ Self::from_vec(vec)
+ }
+}
+
+impl Drop for Witness {
+ fn drop(&mut self) {
+ if self.data_is_owned && self.datalen != 0 {
+ let _ = derived::CVec_u8Z { data: self.data as *mut u8, datalen: self.datalen };
+ }
+ }
+}
+impl Clone for Witness {
+ fn clone(&self) -> Self {
+ let sl = unsafe { core::slice::from_raw_parts(self.data, self.datalen) };
+ let mut v = Vec::new();
+ v.extend_from_slice(&sl);
+ Self::from_vec(v)
+ }
+}
+
+#[no_mangle]
+/// Creates a new Witness which has the same data as `orig` but with a new buffer.
+pub extern "C" fn Witness_clone(orig: &Witness) -> Witness { orig.clone() }
+
+#[no_mangle]
+/// Frees the data pointed to by data
+pub extern "C" fn Witness_free(_res: Witness) { }
+
+pub(crate) fn bitcoin_to_C_outpoint(outpoint: &::bitcoin::blockdata::transaction::OutPoint) -> crate::lightning::chain::transaction::OutPoint {
+ crate::lightning::chain::transaction::OutPoint_new(ThirtyTwoBytes { data: *outpoint.txid.as_ref() }, outpoint.vout.try_into().unwrap())
+}
+pub(crate) fn C_to_bitcoin_outpoint(outpoint: crate::lightning::chain::transaction::OutPoint) -> ::bitcoin::blockdata::transaction::OutPoint {
+ unsafe {
+ ::bitcoin::blockdata::transaction::OutPoint {
+ txid: (*outpoint.inner).txid, vout: (*outpoint.inner).index as u32
+ }
+ }
+}
+
+#[repr(C)]
+#[derive(Clone)]
+/// An input to a transaction.
+///
+/// This contains the witness, the scriptSig and the previous outpoint and represents a single
+/// input to a transaction
+pub struct TxIn {
+ /// The witness which includes any signatures required to spend a segwit output.
+ pub witness: Witness,
+ /// The script_sig which includes signatures requires to spend a pre-segwit output (or a
+ /// P2SH-wrapped segwit output).
+ pub script_sig: derived::CVec_u8Z,
+ /// The sequence number of the transaction input
+ pub sequence: u32,
+ /// The txid of the transaction being spent.
+ pub previous_txid: ThirtyTwoBytes,
+ /// The output index of the transaction being spent.
+ pub previous_vout: u32,
+}
+
+impl TxIn {
+ pub(crate) fn from_rust(txin: &::bitcoin::blockdata::transaction::TxIn) -> Self {
+ TxIn {
+ witness: Witness::from_bitcoin(&txin.witness),
+ script_sig: derived::CVec_u8Z::from(txin.script_sig.clone().into_bytes()),
+ sequence: txin.sequence.0,
+ previous_txid: ThirtyTwoBytes { data: *txin.previous_output.txid.as_ref() },
+ previous_vout: txin.previous_output.vout,
+ }
+ }
+}
+#[no_mangle]
+/// Convenience function for constructing a new TxIn
+pub extern "C" fn TxIn_new(witness: Witness, script_sig: derived::CVec_u8Z, sequence: u32, previous_txid: ThirtyTwoBytes, previous_vout: u32) -> TxIn {
+ TxIn { witness, script_sig, sequence, previous_txid, previous_vout }
+}
+#[no_mangle]
+/// Gets the `witness` in the given `TxIn`.
+pub extern "C" fn TxIn_get_witness(txin: &TxIn) -> Witness {
+ txin.witness.clone()
+}
+#[no_mangle]
+/// Gets the `script_sig` in the given `TxIn`.
+pub extern "C" fn TxIn_get_script_sig(txin: &TxIn) -> u8slice {
+ u8slice::from_vec(&txin.script_sig)
+}
+#[no_mangle]
+/// Gets the `sequence` in the given `TxIn`.
+pub extern "C" fn TxIn_get_sequence(txin: &TxIn) -> u32 {
+ txin.sequence
+}
+#[no_mangle]
+/// Gets the previous outpoint txid in the given `TxIn`.
+pub extern "C" fn TxIn_get_previous_txid(txin: &TxIn) -> ThirtyTwoBytes {
+ txin.previous_txid