X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Fchan_utils.rs;h=8228a8a5492832b3ee31289b25ea35850881fbc4;hb=5271372bfe3ddc870c806f7b7ee13c4a7660e7e5;hp=0fa8612da83a6e50745dd742e67c2fa529fc6bda;hpb=ed6eb46f7b4e0d4c869ad08dffc600b4e93e2129;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/ln/chan_utils.rs b/lightning-c-bindings/src/lightning/ln/chan_utils.rs index 0fa8612..8228a8a 100644 --- a/lightning-c-bindings/src/lightning/ln/chan_utils.rs +++ b/lightning-c-bindings/src/lightning/ln/chan_utils.rs @@ -11,6 +11,7 @@ use std::str::FromStr; use std::ffi::c_void; +use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; @@ -21,6 +22,13 @@ pub extern "C" fn build_commitment_secret(commitment_seed: *const [u8; 32], mut crate::c_types::ThirtyTwoBytes { data: ret } } +/// Build a closing transaction +#[no_mangle] +pub extern "C" fn build_closing_transaction(mut to_holder_value_sat: u64, mut to_counterparty_value_sat: u64, mut to_holder_script: crate::c_types::derived::CVec_u8Z, mut to_counterparty_script: crate::c_types::derived::CVec_u8Z, mut funding_outpoint: crate::lightning::chain::transaction::OutPoint) -> crate::c_types::Transaction { + let mut ret = lightning::ln::chan_utils::build_closing_transaction(to_holder_value_sat, to_counterparty_value_sat, ::bitcoin::blockdata::script::Script::from(to_holder_script.into_rust()), ::bitcoin::blockdata::script::Script::from(to_counterparty_script.into_rust()), crate::c_types::C_to_bitcoin_outpoint(funding_outpoint)); + crate::c_types::Transaction::from_bitcoin(&ret) +} + /// Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key) /// from the base secret and the per_commitment_point. /// @@ -82,7 +90,7 @@ pub extern "C" fn derive_public_revocation_key(mut per_commitment_point: crate:: use lightning::ln::chan_utils::TxCreationKeys as nativeTxCreationKeysImport; -type nativeTxCreationKeys = nativeTxCreationKeysImport; +pub(crate) type nativeTxCreationKeys = nativeTxCreationKeysImport; /// The set of public keys which are used in the creation of one commitment transaction. /// These are derived from the channel base keys and per-commitment data. @@ -122,7 +130,7 @@ impl Drop for TxCreationKeys { pub extern "C" fn TxCreationKeys_free(this_obj: TxCreationKeys) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn TxCreationKeys_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn TxCreationKeys_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTxCreationKeys); } } #[allow(unused)] @@ -249,7 +257,7 @@ pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> crate::c_ } use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport; -type nativeChannelPublicKeys = nativeChannelPublicKeysImport; +pub(crate) type nativeChannelPublicKeys = nativeChannelPublicKeysImport; /// One counterparty's public keys which do not change over the life of a channel. #[must_use] @@ -279,7 +287,7 @@ impl Drop for ChannelPublicKeys { pub extern "C" fn ChannelPublicKeys_free(this_obj: ChannelPublicKeys) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelPublicKeys); } } #[allow(unused)] @@ -453,7 +461,7 @@ pub extern "C" fn get_revokeable_redeemscript(mut revocation_key: crate::c_types use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport; -type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport; +pub(crate) type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport; /// Information about an HTLC as it appears in a commitment transaction #[must_use] @@ -483,7 +491,7 @@ impl Drop for HTLCOutputInCommitment { pub extern "C" fn HTLCOutputInCommitment_free(this_obj: HTLCOutputInCommitment) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCOutputInCommitment); } } #[allow(unused)] @@ -560,7 +568,7 @@ pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOut #[no_mangle] pub extern "C" fn HTLCOutputInCommitment_get_transaction_output_index(this_ptr: &HTLCOutputInCommitment) -> crate::c_types::derived::COption_u32Z { let mut inner_val = &mut this_ptr.get_native_mut_ref().transaction_output_index; - let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u32Z::None } else { { crate::c_types::derived::COption_u32Z::Some(inner_val.unwrap()) } }; + let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u32Z::None } else { crate::c_types::derived::COption_u32Z::Some( { inner_val.unwrap() }) }; local_inner_val } /// The position within the commitment transactions' outputs. This may be None if the value is @@ -650,7 +658,7 @@ pub extern "C" fn build_htlc_transaction(commitment_txid: *const [u8; 32], mut f use lightning::ln::chan_utils::ChannelTransactionParameters as nativeChannelTransactionParametersImport; -type nativeChannelTransactionParameters = nativeChannelTransactionParametersImport; +pub(crate) type nativeChannelTransactionParameters = nativeChannelTransactionParametersImport; /// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction). /// The fields are organized by holder/counterparty. @@ -684,7 +692,7 @@ impl Drop for ChannelTransactionParameters { pub extern "C" fn ChannelTransactionParameters_free(this_obj: ChannelTransactionParameters) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn ChannelTransactionParameters_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn ChannelTransactionParameters_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelTransactionParameters); } } #[allow(unused)] @@ -707,7 +715,7 @@ impl ChannelTransactionParameters { #[no_mangle] pub extern "C" fn ChannelTransactionParameters_get_holder_pubkeys(this_ptr: &ChannelTransactionParameters) -> crate::lightning::ln::chan_utils::ChannelPublicKeys { let mut inner_val = &mut this_ptr.get_native_mut_ref().holder_pubkeys; - crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::ln::chan_utils::ChannelPublicKeys<>) as *mut _) }, is_owned: false } } /// Holder public keys #[no_mangle] @@ -745,7 +753,7 @@ pub extern "C" fn ChannelTransactionParameters_set_is_outbound_from_holder(this_ #[no_mangle] pub extern "C" fn ChannelTransactionParameters_get_counterparty_parameters(this_ptr: &ChannelTransactionParameters) -> crate::lightning::ln::chan_utils::CounterpartyChannelTransactionParameters { let mut inner_val = &mut this_ptr.get_native_mut_ref().counterparty_parameters; - let mut local_inner_val = crate::lightning::ln::chan_utils::CounterpartyChannelTransactionParameters { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const _) as *mut _ }, is_owned: false }; + let mut local_inner_val = crate::lightning::ln::chan_utils::CounterpartyChannelTransactionParameters { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const lightning::ln::chan_utils::CounterpartyChannelTransactionParameters<>) as *mut _ }, is_owned: false }; local_inner_val } /// The late-bound counterparty channel transaction parameters. @@ -763,7 +771,7 @@ pub extern "C" fn ChannelTransactionParameters_set_counterparty_parameters(this_ #[no_mangle] pub extern "C" fn ChannelTransactionParameters_get_funding_outpoint(this_ptr: &ChannelTransactionParameters) -> crate::lightning::chain::transaction::OutPoint { let mut inner_val = &mut this_ptr.get_native_mut_ref().funding_outpoint; - let mut local_inner_val = crate::lightning::chain::transaction::OutPoint { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const _) as *mut _ }, is_owned: false }; + let mut local_inner_val = crate::lightning::chain::transaction::OutPoint { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const lightning::chain::transaction::OutPoint<>) as *mut _ }, is_owned: false }; local_inner_val } /// The late-bound funding outpoint @@ -809,7 +817,7 @@ pub extern "C" fn ChannelTransactionParameters_clone(orig: &ChannelTransactionPa } use lightning::ln::chan_utils::CounterpartyChannelTransactionParameters as nativeCounterpartyChannelTransactionParametersImport; -type nativeCounterpartyChannelTransactionParameters = nativeCounterpartyChannelTransactionParametersImport; +pub(crate) type nativeCounterpartyChannelTransactionParameters = nativeCounterpartyChannelTransactionParametersImport; /// Late-bound per-channel counterparty data used to build transactions. #[must_use] @@ -839,7 +847,7 @@ impl Drop for CounterpartyChannelTransactionParameters { pub extern "C" fn CounterpartyChannelTransactionParameters_free(this_obj: CounterpartyChannelTransactionParameters) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn CounterpartyChannelTransactionParameters_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCounterpartyChannelTransactionParameters); } } #[allow(unused)] @@ -862,7 +870,7 @@ impl CounterpartyChannelTransactionParameters { #[no_mangle] pub extern "C" fn CounterpartyChannelTransactionParameters_get_pubkeys(this_ptr: &CounterpartyChannelTransactionParameters) -> crate::lightning::ln::chan_utils::ChannelPublicKeys { let mut inner_val = &mut this_ptr.get_native_mut_ref().pubkeys; - crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::ln::chan_utils::ChannelPublicKeys<>) as *mut _) }, is_owned: false } } /// Counter-party public keys #[no_mangle] @@ -972,7 +980,7 @@ pub extern "C" fn ChannelTransactionParameters_read(ser: crate::c_types::u8slice } use lightning::ln::chan_utils::DirectedChannelTransactionParameters as nativeDirectedChannelTransactionParametersImport; -type nativeDirectedChannelTransactionParameters = nativeDirectedChannelTransactionParametersImport<'static>; +pub(crate) type nativeDirectedChannelTransactionParameters = nativeDirectedChannelTransactionParametersImport<'static>; /// Static channel fields used to build transactions given per-commitment fields, organized by /// broadcaster/countersignatory. @@ -1006,7 +1014,7 @@ impl Drop for DirectedChannelTransactionParameters { pub extern "C" fn DirectedChannelTransactionParameters_free(this_obj: DirectedChannelTransactionParameters) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn DirectedChannelTransactionParameters_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn DirectedChannelTransactionParameters_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDirectedChannelTransactionParameters); } } #[allow(unused)] @@ -1030,7 +1038,7 @@ impl DirectedChannelTransactionParameters { #[no_mangle] pub extern "C" fn DirectedChannelTransactionParameters_broadcaster_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::lightning::ln::chan_utils::ChannelPublicKeys { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.broadcaster_pubkeys(); - crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::chan_utils::ChannelPublicKeys<>) as *mut _) }, is_owned: false } } /// Get the channel pubkeys for the countersignatory @@ -1038,7 +1046,7 @@ pub extern "C" fn DirectedChannelTransactionParameters_broadcaster_pubkeys(this_ #[no_mangle] pub extern "C" fn DirectedChannelTransactionParameters_countersignatory_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::lightning::ln::chan_utils::ChannelPublicKeys { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.countersignatory_pubkeys(); - crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::chan_utils::ChannelPublicKeys<>) as *mut _) }, is_owned: false } } /// Get the contest delay applicable to the transactions. @@ -1071,7 +1079,7 @@ pub extern "C" fn DirectedChannelTransactionParameters_funding_outpoint(this_arg use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport; -type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport; +pub(crate) type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport; /// Information needed to build and sign a holder's commitment transaction. /// @@ -1103,7 +1111,7 @@ impl Drop for HolderCommitmentTransaction { pub extern "C" fn HolderCommitmentTransaction_free(this_obj: HolderCommitmentTransaction) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHolderCommitmentTransaction); } } #[allow(unused)] @@ -1186,7 +1194,7 @@ pub extern "C" fn HolderCommitmentTransaction_new(mut commitment_tx: crate::ligh use lightning::ln::chan_utils::BuiltCommitmentTransaction as nativeBuiltCommitmentTransactionImport; -type nativeBuiltCommitmentTransaction = nativeBuiltCommitmentTransactionImport; +pub(crate) type nativeBuiltCommitmentTransaction = nativeBuiltCommitmentTransactionImport; /// A pre-built Bitcoin commitment transaction and its txid. #[must_use] @@ -1216,7 +1224,7 @@ impl Drop for BuiltCommitmentTransaction { pub extern "C" fn BuiltCommitmentTransaction_free(this_obj: BuiltCommitmentTransaction) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn BuiltCommitmentTransaction_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn BuiltCommitmentTransaction_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBuiltCommitmentTransaction); } } #[allow(unused)] @@ -1327,10 +1335,215 @@ pub extern "C" fn BuiltCommitmentTransaction_sign(this_arg: &BuiltCommitmentTran } +use lightning::ln::chan_utils::ClosingTransaction as nativeClosingTransactionImport; +pub(crate) type nativeClosingTransaction = nativeClosingTransactionImport; + +/// This class tracks the per-transaction information needed to build a closing transaction and will +/// actually build it and sign. +/// +/// This class can be used inside a signer implementation to generate a signature given the relevant +/// secret key. +#[must_use] +#[repr(C)] +pub struct ClosingTransaction { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeClosingTransaction, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for ClosingTransaction { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeClosingTransaction>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; + } + } +} +/// Frees any resources used by the ClosingTransaction, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn ClosingTransaction_free(this_obj: ClosingTransaction) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ClosingTransaction_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeClosingTransaction); } +} +#[allow(unused)] +impl ClosingTransaction { + pub(crate) fn get_native_ref(&self) -> &'static nativeClosingTransaction { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeClosingTransaction { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy + pub(crate) fn take_inner(mut self) -> *mut nativeClosingTransaction { + assert!(self.is_owned); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = std::ptr::null_mut(); + ret + } +} +/// Construct an object of the class +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_new(mut to_holder_value_sat: u64, mut to_counterparty_value_sat: u64, mut to_holder_script: crate::c_types::derived::CVec_u8Z, mut to_counterparty_script: crate::c_types::derived::CVec_u8Z, mut funding_outpoint: crate::lightning::chain::transaction::OutPoint) -> ClosingTransaction { + let mut ret = lightning::ln::chan_utils::ClosingTransaction::new(to_holder_value_sat, to_counterparty_value_sat, ::bitcoin::blockdata::script::Script::from(to_holder_script.into_rust()), ::bitcoin::blockdata::script::Script::from(to_counterparty_script.into_rust()), crate::c_types::C_to_bitcoin_outpoint(funding_outpoint)); + ClosingTransaction { inner: ObjOps::heap_alloc(ret), is_owned: true } +} + +/// Trust our pre-built transaction. +/// +/// Applies a wrapper which allows access to the transaction. +/// +/// This should only be used if you fully trust the builder of this object. It should not +/// be used by an external signer - instead use the verify function. +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_trust(this_arg: &ClosingTransaction) -> crate::lightning::ln::chan_utils::TrustedClosingTransaction { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.trust(); + crate::lightning::ln::chan_utils::TrustedClosingTransaction { inner: ObjOps::heap_alloc(ret), is_owned: true } +} + +/// Verify our pre-built transaction. +/// +/// Applies a wrapper which allows access to the transaction. +/// +/// An external validating signer must call this method before signing +/// or using the built transaction. +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_verify(this_arg: &ClosingTransaction, mut funding_outpoint: crate::lightning::chain::transaction::OutPoint) -> crate::c_types::derived::CResult_TrustedClosingTransactionNoneZ { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.verify(crate::c_types::C_to_bitcoin_outpoint(funding_outpoint)); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::chan_utils::TrustedClosingTransaction { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() }; + local_ret +} + +/// The value to be sent to the holder, or zero if the output will be omitted +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_to_holder_value_sat(this_arg: &ClosingTransaction) -> u64 { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_holder_value_sat(); + ret +} + +/// The value to be sent to the counterparty, or zero if the output will be omitted +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_to_counterparty_value_sat(this_arg: &ClosingTransaction) -> u64 { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_counterparty_value_sat(); + ret +} + +/// The destination of the holder's output +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_to_holder_script(this_arg: &ClosingTransaction) -> crate::c_types::u8slice { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_holder_script(); + crate::c_types::u8slice::from_slice(&ret[..]) +} + +/// The destination of the counterparty's output +#[must_use] +#[no_mangle] +pub extern "C" fn ClosingTransaction_to_counterparty_script(this_arg: &ClosingTransaction) -> crate::c_types::u8slice { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_counterparty_script(); + crate::c_types::u8slice::from_slice(&ret[..]) +} + + +use lightning::ln::chan_utils::TrustedClosingTransaction as nativeTrustedClosingTransactionImport; +pub(crate) type nativeTrustedClosingTransaction = nativeTrustedClosingTransactionImport<'static>; + +/// A wrapper on ClosingTransaction indicating that the built bitcoin +/// transaction is trusted. +/// +/// See trust() and verify() functions on CommitmentTransaction. +/// +/// This structure implements Deref. +#[must_use] +#[repr(C)] +pub struct TrustedClosingTransaction { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeTrustedClosingTransaction, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for TrustedClosingTransaction { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeTrustedClosingTransaction>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; + } + } +} +/// Frees any resources used by the TrustedClosingTransaction, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn TrustedClosingTransaction_free(this_obj: TrustedClosingTransaction) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn TrustedClosingTransaction_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTrustedClosingTransaction); } +} +#[allow(unused)] +impl TrustedClosingTransaction { + pub(crate) fn get_native_ref(&self) -> &'static nativeTrustedClosingTransaction { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeTrustedClosingTransaction { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy + pub(crate) fn take_inner(mut self) -> *mut nativeTrustedClosingTransaction { + assert!(self.is_owned); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = std::ptr::null_mut(); + ret + } +} +/// The pre-built Bitcoin commitment transaction +#[must_use] +#[no_mangle] +pub extern "C" fn TrustedClosingTransaction_built_transaction(this_arg: &TrustedClosingTransaction) -> crate::c_types::Transaction { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.built_transaction(); + crate::c_types::Transaction::from_bitcoin(ret) +} + +/// Get the SIGHASH_ALL sighash value of the transaction. +/// +/// This can be used to verify a signature. +#[must_use] +#[no_mangle] +pub extern "C" fn TrustedClosingTransaction_get_sighash_all(this_arg: &TrustedClosingTransaction, mut funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::ThirtyTwoBytes { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_sighash_all(&::bitcoin::blockdata::script::Script::from(Vec::from(funding_redeemscript.to_slice())), channel_value_satoshis); + crate::c_types::ThirtyTwoBytes { data: ret.as_ref().clone() } +} + +/// Sign a transaction, either because we are counter-signing the counterparty's transaction or +/// because we are about to broadcast a holder transaction. +#[must_use] +#[no_mangle] +pub extern "C" fn TrustedClosingTransaction_sign(this_arg: &TrustedClosingTransaction, funding_key: *const [u8; 32], mut funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::Signature { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.sign(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *funding_key}[..]).unwrap(), &::bitcoin::blockdata::script::Script::from(Vec::from(funding_redeemscript.to_slice())), channel_value_satoshis, secp256k1::SECP256K1); + crate::c_types::Signature::from_rust(&ret) +} + + use lightning::ln::chan_utils::CommitmentTransaction as nativeCommitmentTransactionImport; -type nativeCommitmentTransaction = nativeCommitmentTransactionImport; +pub(crate) type nativeCommitmentTransaction = nativeCommitmentTransactionImport; -/// This class tracks the per-transaction information needed to build a commitment transaction and to +/// This class tracks the per-transaction information needed to build a commitment transaction and will /// actually build it and sign. It is used for holder transactions that we sign only when needed /// and for transactions we sign for the counterparty. /// @@ -1363,7 +1576,7 @@ impl Drop for CommitmentTransaction { pub extern "C" fn CommitmentTransaction_free(this_obj: CommitmentTransaction) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn CommitmentTransaction_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn CommitmentTransaction_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCommitmentTransaction); } } #[allow(unused)] @@ -1454,7 +1667,7 @@ pub extern "C" fn CommitmentTransaction_feerate_per_kw(this_arg: &CommitmentTran /// Applies a wrapper which allows access to these fields. /// /// This should only be used if you fully trust the builder of this object. It should not -///\tbe used by an external signer - instead use the verify function. +/// be used by an external signer - instead use the verify function. #[must_use] #[no_mangle] pub extern "C" fn CommitmentTransaction_trust(this_arg: &CommitmentTransaction) -> crate::lightning::ln::chan_utils::TrustedCommitmentTransaction { @@ -1478,7 +1691,7 @@ pub extern "C" fn CommitmentTransaction_verify(this_arg: &CommitmentTransaction, use lightning::ln::chan_utils::TrustedCommitmentTransaction as nativeTrustedCommitmentTransactionImport; -type nativeTrustedCommitmentTransaction = nativeTrustedCommitmentTransactionImport<'static>; +pub(crate) type nativeTrustedCommitmentTransaction = nativeTrustedCommitmentTransactionImport<'static>; /// A wrapper on CommitmentTransaction indicating that the derived fields (the built bitcoin /// transaction and the transaction creation keys) are trusted. @@ -1513,7 +1726,7 @@ impl Drop for TrustedCommitmentTransaction { pub extern "C" fn TrustedCommitmentTransaction_free(this_obj: TrustedCommitmentTransaction) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn TrustedCommitmentTransaction_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn TrustedCommitmentTransaction_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTrustedCommitmentTransaction); } } #[allow(unused)] @@ -1545,7 +1758,7 @@ pub extern "C" fn TrustedCommitmentTransaction_txid(this_arg: &TrustedCommitment #[no_mangle] pub extern "C" fn TrustedCommitmentTransaction_built_transaction(this_arg: &TrustedCommitmentTransaction) -> crate::lightning::ln::chan_utils::BuiltCommitmentTransaction { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.built_transaction(); - crate::lightning::ln::chan_utils::BuiltCommitmentTransaction { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::BuiltCommitmentTransaction { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::chan_utils::BuiltCommitmentTransaction<>) as *mut _) }, is_owned: false } } /// The pre-calculated transaction creation public keys. @@ -1553,7 +1766,7 @@ pub extern "C" fn TrustedCommitmentTransaction_built_transaction(this_arg: &Trus #[no_mangle] pub extern "C" fn TrustedCommitmentTransaction_keys(this_arg: &TrustedCommitmentTransaction) -> crate::lightning::ln::chan_utils::TxCreationKeys { let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.keys(); - crate::lightning::ln::chan_utils::TxCreationKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::TxCreationKeys { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::chan_utils::TxCreationKeys<>) as *mut _) }, is_owned: false } } /// Get a signature for each HTLC which was included in the commitment transaction (ie for