X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fln%2Fchan_utils.rs;h=2783d803d40af766f206842dc9ed8d459d607a87;hb=041d7aaa9da9b4e5cc0fb3276edf71301014fe41;hp=bf3953a09e40f9b20e2705afb91440c72fde0784;hpb=d735085cec8b119d8d702d833535d3140909d5d0;p=rust-lightning diff --git a/lightning-c-bindings/src/ln/chan_utils.rs b/lightning-c-bindings/src/ln/chan_utils.rs index bf3953a0..2783d803 100644 --- a/lightning-c-bindings/src/ln/chan_utils.rs +++ b/lightning-c-bindings/src/ln/chan_utils.rs @@ -19,9 +19,9 @@ pub extern "C" fn build_commitment_secret(commitment_seed: *const [u8; 32], mut /// Note that this is infallible iff we trust that at least one of the two input keys are randomly /// generated (ie our own). #[no_mangle] -pub extern "C" fn derive_private_key(per_commitment_point: crate::c_types::PublicKey, base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeySecpErrorZ { +pub extern "C" fn derive_private_key(mut per_commitment_point: crate::c_types::PublicKey, base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeyErrorZ { let mut ret = lightning::ln::chan_utils::derive_private_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *base_secret}[..]).unwrap()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() }; local_ret } @@ -32,9 +32,9 @@ pub extern "C" fn derive_private_key(per_commitment_point: crate::c_types::Publi /// Note that this is infallible iff we trust that at least one of the two input keys are randomly /// generated (ie our own). #[no_mangle] -pub extern "C" fn derive_public_key(per_commitment_point: crate::c_types::PublicKey, base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeySecpErrorZ { +pub extern "C" fn derive_public_key(mut per_commitment_point: crate::c_types::PublicKey, mut base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeyErrorZ { let mut ret = lightning::ln::chan_utils::derive_public_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &base_point.into_rust()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() }; local_ret } @@ -48,9 +48,9 @@ pub extern "C" fn derive_public_key(per_commitment_point: crate::c_types::Public /// Note that this is infallible iff we trust that at least one of the two input keys are randomly /// generated (ie our own). #[no_mangle] -pub extern "C" fn derive_private_revocation_key(per_commitment_secret: *const [u8; 32], countersignatory_revocation_base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeySecpErrorZ { +pub extern "C" fn derive_private_revocation_key(per_commitment_secret: *const [u8; 32], countersignatory_revocation_base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeyErrorZ { let mut ret = lightning::ln::chan_utils::derive_private_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *per_commitment_secret}[..]).unwrap(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *countersignatory_revocation_base_secret}[..]).unwrap()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() }; local_ret } @@ -66,9 +66,9 @@ pub extern "C" fn derive_private_revocation_key(per_commitment_secret: *const [u /// Note that this is infallible iff we trust that at least one of the two input keys are randomly /// generated (ie our own). #[no_mangle] -pub extern "C" fn derive_public_revocation_key(per_commitment_point: crate::c_types::PublicKey, countersignatory_revocation_base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeySecpErrorZ { +pub extern "C" fn derive_public_revocation_key(mut per_commitment_point: crate::c_types::PublicKey, mut countersignatory_revocation_base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeyErrorZ { let mut ret = lightning::ln::chan_utils::derive_public_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &countersignatory_revocation_base_point.into_rust()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() }; local_ret } @@ -85,12 +85,12 @@ type nativeTxCreationKeys = nativeTxCreationKeysImport; /// /// These keys are assumed to be good, either because the code derived them from /// channel basepoints via the new function, or they were obtained via -/// PreCalculatedTxCreationKeys.trust_key_derivation because we trusted the source of the +/// CommitmentTransaction.trust().keys() because we trusted the source of the /// pre-calculated keys. #[must_use] #[repr(C)] pub struct TxCreationKeys { - /// Nearly everyhwere, inner must be non-null, however in places where + /// 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 nativeTxCreationKeys, pub is_owned: bool, @@ -113,26 +113,13 @@ extern "C" fn TxCreationKeys_free_void(this_ptr: *mut c_void) { #[allow(unused)] /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl TxCreationKeys { - pub(crate) fn take_ptr(mut self) -> *mut nativeTxCreationKeys { + pub(crate) fn take_inner(mut self) -> *mut nativeTxCreationKeys { assert!(self.is_owned); let ret = self.inner; self.inner = std::ptr::null_mut(); ret } } -impl Clone for TxCreationKeys { - fn clone(&self) -> Self { - Self { - inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())), - is_owned: true, - } - } -} -#[allow(unused)] -/// Used only if an object of this type is returned as a trait impl by a method -pub(crate) extern "C" fn TxCreationKeys_clone_void(this_ptr: *const c_void) -> *mut c_void { - Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeTxCreationKeys)).clone() })) as *mut c_void -} /// The broadcaster's per-commitment public key which was used to derive the other keys. #[no_mangle] pub extern "C" fn TxCreationKeys_get_per_commitment_point(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey { @@ -203,85 +190,39 @@ pub extern "C" fn TxCreationKeys_new(mut per_commitment_point_arg: crate::c_type broadcaster_delayed_payment_key: broadcaster_delayed_payment_key_arg.into_rust(), })), is_owned: true } } -#[no_mangle] -pub extern "C" fn TxCreationKeys_write(obj: *const TxCreationKeys) -> crate::c_types::derived::CVec_u8Z { - crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) }) -} -#[no_mangle] -pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> TxCreationKeys { - if let Ok(res) = crate::c_types::deserialize_obj(ser) { - TxCreationKeys { inner: Box::into_raw(Box::new(res)), is_owned: true } - } else { - TxCreationKeys { inner: std::ptr::null_mut(), is_owned: true } - } -} - -use lightning::ln::chan_utils::PreCalculatedTxCreationKeys as nativePreCalculatedTxCreationKeysImport; -type nativePreCalculatedTxCreationKeys = nativePreCalculatedTxCreationKeysImport; - -/// The per-commitment point and a set of pre-calculated public keys used for transaction creation -/// in the signer. -/// The pre-calculated keys are an optimization, because ChannelKeys has enough -/// information to re-derive them. -#[must_use] -#[repr(C)] -pub struct PreCalculatedTxCreationKeys { - /// Nearly everyhwere, 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 nativePreCalculatedTxCreationKeys, - pub is_owned: bool, -} - -impl Drop for PreCalculatedTxCreationKeys { - fn drop(&mut self) { - if self.is_owned && !self.inner.is_null() { - let _ = unsafe { Box::from_raw(self.inner) }; +impl Clone for TxCreationKeys { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, } } } -#[no_mangle] -pub extern "C" fn PreCalculatedTxCreationKeys_free(this_ptr: PreCalculatedTxCreationKeys) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn PreCalculatedTxCreationKeys_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativePreCalculatedTxCreationKeys); } +pub(crate) extern "C" fn TxCreationKeys_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeTxCreationKeys)).clone() })) as *mut c_void } -#[allow(unused)] -/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy -impl PreCalculatedTxCreationKeys { - pub(crate) fn take_ptr(mut self) -> *mut nativePreCalculatedTxCreationKeys { - assert!(self.is_owned); - let ret = self.inner; - self.inner = std::ptr::null_mut(); - ret - } +#[no_mangle] +pub extern "C" fn TxCreationKeys_clone(orig: &TxCreationKeys) -> TxCreationKeys { + orig.clone() } -/// Create a new PreCalculatedTxCreationKeys from TxCreationKeys -#[must_use] #[no_mangle] -pub extern "C" fn PreCalculatedTxCreationKeys_new(mut keys: crate::ln::chan_utils::TxCreationKeys) -> PreCalculatedTxCreationKeys { - let mut ret = lightning::ln::chan_utils::PreCalculatedTxCreationKeys::new(*unsafe { Box::from_raw(keys.take_ptr()) }); - PreCalculatedTxCreationKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true } +pub extern "C" fn TxCreationKeys_write(obj: &TxCreationKeys) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) } - -/// The pre-calculated transaction creation public keys. -/// An external validating signer should not trust these keys. -#[must_use] #[no_mangle] -pub extern "C" fn PreCalculatedTxCreationKeys_trust_key_derivation(this_arg: &PreCalculatedTxCreationKeys) -> crate::ln::chan_utils::TxCreationKeys { - let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation(); - crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } +pub(crate) extern "C" fn TxCreationKeys_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeTxCreationKeys) }) } - -/// The transaction per-commitment point -#[must_use] #[no_mangle] -pub extern "C" fn PreCalculatedTxCreationKeys_per_commitment_point(this_arg: &PreCalculatedTxCreationKeys) -> crate::c_types::PublicKey { - let mut ret = unsafe { &*this_arg.inner }.per_commitment_point(); - crate::c_types::PublicKey::from_rust(&*ret) +pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_TxCreationKeysDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res } - use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport; type nativeChannelPublicKeys = nativeChannelPublicKeysImport; @@ -289,7 +230,7 @@ type nativeChannelPublicKeys = nativeChannelPublicKeysImport; #[must_use] #[repr(C)] pub struct ChannelPublicKeys { - /// Nearly everyhwere, inner must be non-null, however in places where + /// 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 nativeChannelPublicKeys, pub is_owned: bool, @@ -312,26 +253,13 @@ extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) { #[allow(unused)] /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl ChannelPublicKeys { - pub(crate) fn take_ptr(mut self) -> *mut nativeChannelPublicKeys { + pub(crate) fn take_inner(mut self) -> *mut nativeChannelPublicKeys { assert!(self.is_owned); let ret = self.inner; self.inner = std::ptr::null_mut(); ret } } -impl Clone for ChannelPublicKeys { - fn clone(&self) -> Self { - Self { - inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())), - is_owned: true, - } - } -} -#[allow(unused)] -/// Used only if an object of this type is returned as a trait impl by a method -pub(crate) extern "C" fn ChannelPublicKeys_clone_void(this_ptr: *const c_void) -> *mut c_void { - Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelPublicKeys)).clone() })) as *mut c_void -} /// The public key which is used to sign all commitment transactions, as it appears in the /// on-chain channel lock-in 2-of-2 multisig output. #[no_mangle] @@ -416,24 +344,55 @@ pub extern "C" fn ChannelPublicKeys_new(mut funding_pubkey_arg: crate::c_types:: htlc_basepoint: htlc_basepoint_arg.into_rust(), })), is_owned: true } } +impl Clone for ChannelPublicKeys { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ChannelPublicKeys_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelPublicKeys)).clone() })) as *mut c_void +} #[no_mangle] -pub extern "C" fn ChannelPublicKeys_write(obj: *const ChannelPublicKeys) -> crate::c_types::derived::CVec_u8Z { - crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) }) +pub extern "C" fn ChannelPublicKeys_clone(orig: &ChannelPublicKeys) -> ChannelPublicKeys { + orig.clone() } #[no_mangle] -pub extern "C" fn ChannelPublicKeys_read(ser: crate::c_types::u8slice) -> ChannelPublicKeys { - if let Ok(res) = crate::c_types::deserialize_obj(ser) { - ChannelPublicKeys { inner: Box::into_raw(Box::new(res)), is_owned: true } - } else { - ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true } - } +pub extern "C" fn ChannelPublicKeys_write(obj: &ChannelPublicKeys) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn ChannelPublicKeys_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelPublicKeys) }) } -/// Create a new TxCreationKeys from channel base points and the per-commitment point +#[no_mangle] +pub extern "C" fn ChannelPublicKeys_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelPublicKeysDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::ChannelPublicKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +/// Create per-state keys from channel base points and the per-commitment point. +/// Key set is asymmetric and can't be used as part of counter-signatory set of transactions. #[must_use] #[no_mangle] -pub extern "C" fn TxCreationKeys_derive_new(per_commitment_point: crate::c_types::PublicKey, broadcaster_delayed_payment_base: crate::c_types::PublicKey, broadcaster_htlc_base: crate::c_types::PublicKey, countersignatory_revocation_base: crate::c_types::PublicKey, countersignatory_htlc_base: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_TxCreationKeysSecpErrorZ { +pub extern "C" fn TxCreationKeys_derive_new(mut per_commitment_point: crate::c_types::PublicKey, mut broadcaster_delayed_payment_base: crate::c_types::PublicKey, mut broadcaster_htlc_base: crate::c_types::PublicKey, mut countersignatory_revocation_base: crate::c_types::PublicKey, mut countersignatory_htlc_base: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_TxCreationKeysErrorZ { let mut ret = lightning::ln::chan_utils::TxCreationKeys::derive_new(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &broadcaster_delayed_payment_base.into_rust(), &broadcaster_htlc_base.into_rust(), &countersignatory_revocation_base.into_rust(), &countersignatory_htlc_base.into_rust()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() }; + local_ret +} + +/// Generate per-state keys from channel static keys. +/// Key set is asymmetric and can't be used as part of counter-signatory set of transactions. +#[must_use] +#[no_mangle] +pub extern "C" fn TxCreationKeys_from_channel_static_keys(mut per_commitment_point: crate::c_types::PublicKey, broadcaster_keys: &crate::ln::chan_utils::ChannelPublicKeys, countersignatory_keys: &crate::ln::chan_utils::ChannelPublicKeys) -> crate::c_types::derived::CResult_TxCreationKeysErrorZ { + let mut ret = lightning::ln::chan_utils::TxCreationKeys::from_channel_static_keys(&per_commitment_point.into_rust(), unsafe { &*broadcaster_keys.inner }, unsafe { &*countersignatory_keys.inner }, &bitcoin::secp256k1::Secp256k1::new()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() }; local_ret } @@ -441,7 +400,7 @@ pub extern "C" fn TxCreationKeys_derive_new(per_commitment_point: crate::c_types /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain. /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions. #[no_mangle] -pub extern "C" fn get_revokeable_redeemscript(revocation_key: crate::c_types::PublicKey, mut contest_delay: u16, broadcaster_delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z { +pub extern "C" fn get_revokeable_redeemscript(mut revocation_key: crate::c_types::PublicKey, mut contest_delay: u16, mut broadcaster_delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z { let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), contest_delay, &broadcaster_delayed_payment_key.into_rust()); ret.into_bytes().into() } @@ -454,7 +413,7 @@ type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport; #[must_use] #[repr(C)] pub struct HTLCOutputInCommitment { - /// Nearly everyhwere, inner must be non-null, however in places where + /// 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 nativeHTLCOutputInCommitment, pub is_owned: bool, @@ -477,26 +436,13 @@ extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) { #[allow(unused)] /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl HTLCOutputInCommitment { - pub(crate) fn take_ptr(mut self) -> *mut nativeHTLCOutputInCommitment { + pub(crate) fn take_inner(mut self) -> *mut nativeHTLCOutputInCommitment { assert!(self.is_owned); let ret = self.inner; self.inner = std::ptr::null_mut(); ret } } -impl Clone for HTLCOutputInCommitment { - fn clone(&self) -> Self { - Self { - inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())), - is_owned: true, - } - } -} -#[allow(unused)] -/// Used only if an object of this type is returned as a trait impl by a method -pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void { - Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void -} /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction). /// Note that this is not the same as whether it is ountbound *from us*. To determine that you /// need to compare this value to whether the commitment transaction in question is that of @@ -549,17 +495,37 @@ pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputI pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) { unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data); } +impl Clone for HTLCOutputInCommitment { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void +} #[no_mangle] -pub extern "C" fn HTLCOutputInCommitment_write(obj: *const HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z { - crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) }) +pub extern "C" fn HTLCOutputInCommitment_clone(orig: &HTLCOutputInCommitment) -> HTLCOutputInCommitment { + orig.clone() } #[no_mangle] -pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> HTLCOutputInCommitment { - if let Ok(res) = crate::c_types::deserialize_obj(ser) { - HTLCOutputInCommitment { inner: Box::into_raw(Box::new(res)), is_owned: true } - } else { - HTLCOutputInCommitment { inner: std::ptr::null_mut(), is_owned: true } - } +pub extern "C" fn HTLCOutputInCommitment_write(obj: &HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn HTLCOutputInCommitment_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHTLCOutputInCommitment) }) +} +#[no_mangle] +pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HTLCOutputInCommitmentDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::HTLCOutputInCommitment { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res } /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc /// does not need to have its previous_output_index filled. @@ -572,30 +538,396 @@ pub extern "C" fn get_htlc_redeemscript(htlc: &crate::ln::chan_utils::HTLCOutput /// Gets the redeemscript for a funding output from the two funding public keys. /// Note that the order of funding public keys does not matter. #[no_mangle] -pub extern "C" fn make_funding_redeemscript(broadcaster: crate::c_types::PublicKey, countersignatory: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z { +pub extern "C" fn make_funding_redeemscript(mut broadcaster: crate::c_types::PublicKey, mut countersignatory: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z { let mut ret = lightning::ln::chan_utils::make_funding_redeemscript(&broadcaster.into_rust(), &countersignatory.into_rust()); ret.into_bytes().into() } /// panics if htlc.transaction_output_index.is_none()! #[no_mangle] -pub extern "C" fn build_htlc_transaction(prev_hash: *const [u8; 32], mut feerate_per_kw: u32, mut contest_delay: u16, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, broadcaster_delayed_payment_key: crate::c_types::PublicKey, revocation_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z { +pub extern "C" fn build_htlc_transaction(prev_hash: *const [u8; 32], mut feerate_per_kw: u32, mut contest_delay: u16, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, mut broadcaster_delayed_payment_key: crate::c_types::PublicKey, mut revocation_key: crate::c_types::PublicKey) -> crate::c_types::Transaction { let mut ret = lightning::ln::chan_utils::build_htlc_transaction(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*prev_hash }[..]).unwrap(), feerate_per_kw, contest_delay, unsafe { &*htlc.inner }, &broadcaster_delayed_payment_key.into_rust(), &revocation_key.into_rust()); let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret); - local_ret.into() + crate::c_types::Transaction::from_vec(local_ret) +} + + +use lightning::ln::chan_utils::ChannelTransactionParameters as nativeChannelTransactionParametersImport; +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. +/// +/// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters +/// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions. +#[must_use] +#[repr(C)] +pub struct ChannelTransactionParameters { + /// 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 nativeChannelTransactionParameters, + pub is_owned: bool, +} + +impl Drop for ChannelTransactionParameters { + fn drop(&mut self) { + if self.is_owned && !self.inner.is_null() { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_free(this_ptr: 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) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelTransactionParameters); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl ChannelTransactionParameters { + pub(crate) fn take_inner(mut self) -> *mut nativeChannelTransactionParameters { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// Holder public keys +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_get_holder_pubkeys(this_ptr: &ChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.holder_pubkeys; + crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } +} +/// Holder public keys +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_set_holder_pubkeys(this_ptr: &mut ChannelTransactionParameters, mut val: crate::ln::chan_utils::ChannelPublicKeys) { + unsafe { &mut *this_ptr.inner }.holder_pubkeys = *unsafe { Box::from_raw(val.take_inner()) }; +} +/// The contest delay selected by the holder, which applies to counterparty-broadcast transactions +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_get_holder_selected_contest_delay(this_ptr: &ChannelTransactionParameters) -> u16 { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.holder_selected_contest_delay; + (*inner_val) +} +/// The contest delay selected by the holder, which applies to counterparty-broadcast transactions +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_set_holder_selected_contest_delay(this_ptr: &mut ChannelTransactionParameters, mut val: u16) { + unsafe { &mut *this_ptr.inner }.holder_selected_contest_delay = val; +} +/// Whether the holder is the initiator of this channel. +/// This is an input to the commitment number obscure factor computation. +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_get_is_outbound_from_holder(this_ptr: &ChannelTransactionParameters) -> bool { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.is_outbound_from_holder; + (*inner_val) +} +/// Whether the holder is the initiator of this channel. +/// This is an input to the commitment number obscure factor computation. +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_set_is_outbound_from_holder(this_ptr: &mut ChannelTransactionParameters, mut val: bool) { + unsafe { &mut *this_ptr.inner }.is_outbound_from_holder = val; +} +/// The late-bound counterparty channel transaction parameters. +/// These parameters are populated at the point in the protocol where the counterparty provides them. +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_get_counterparty_parameters(this_ptr: &ChannelTransactionParameters) -> crate::ln::chan_utils::CounterpartyChannelTransactionParameters { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_parameters; + let mut local_inner_val = crate::ln::chan_utils::CounterpartyChannelTransactionParameters { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false }; + local_inner_val +} +/// The late-bound counterparty channel transaction parameters. +/// These parameters are populated at the point in the protocol where the counterparty provides them. +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_set_counterparty_parameters(this_ptr: &mut ChannelTransactionParameters, mut val: crate::ln::chan_utils::CounterpartyChannelTransactionParameters) { + let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; + unsafe { &mut *this_ptr.inner }.counterparty_parameters = local_val; +} +/// The late-bound funding outpoint +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_get_funding_outpoint(this_ptr: &ChannelTransactionParameters) -> crate::chain::transaction::OutPoint { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_outpoint; + let mut local_inner_val = crate::chain::transaction::OutPoint { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else { { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false }; + local_inner_val +} +/// The late-bound funding outpoint +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_set_funding_outpoint(this_ptr: &mut ChannelTransactionParameters, mut val: crate::chain::transaction::OutPoint) { + let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; + unsafe { &mut *this_ptr.inner }.funding_outpoint = local_val; +} +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_new(mut holder_pubkeys_arg: crate::ln::chan_utils::ChannelPublicKeys, mut holder_selected_contest_delay_arg: u16, mut is_outbound_from_holder_arg: bool, mut counterparty_parameters_arg: crate::ln::chan_utils::CounterpartyChannelTransactionParameters, mut funding_outpoint_arg: crate::chain::transaction::OutPoint) -> ChannelTransactionParameters { + let mut local_counterparty_parameters_arg = if counterparty_parameters_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(counterparty_parameters_arg.take_inner()) } }) }; + let mut local_funding_outpoint_arg = if funding_outpoint_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(funding_outpoint_arg.take_inner()) } }) }; + ChannelTransactionParameters { inner: Box::into_raw(Box::new(nativeChannelTransactionParameters { + holder_pubkeys: *unsafe { Box::from_raw(holder_pubkeys_arg.take_inner()) }, + holder_selected_contest_delay: holder_selected_contest_delay_arg, + is_outbound_from_holder: is_outbound_from_holder_arg, + counterparty_parameters: local_counterparty_parameters_arg, + funding_outpoint: local_funding_outpoint_arg, + })), is_owned: true } +} +impl Clone for ChannelTransactionParameters { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ChannelTransactionParameters_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelTransactionParameters)).clone() })) as *mut c_void +} +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_clone(orig: &ChannelTransactionParameters) -> ChannelTransactionParameters { + orig.clone() +} + +use lightning::ln::chan_utils::CounterpartyChannelTransactionParameters as nativeCounterpartyChannelTransactionParametersImport; +type nativeCounterpartyChannelTransactionParameters = nativeCounterpartyChannelTransactionParametersImport; + +/// Late-bound per-channel counterparty data used to build transactions. +#[must_use] +#[repr(C)] +pub struct CounterpartyChannelTransactionParameters { + /// 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 nativeCounterpartyChannelTransactionParameters, + pub is_owned: bool, +} + +impl Drop for CounterpartyChannelTransactionParameters { + fn drop(&mut self) { + if self.is_owned && !self.inner.is_null() { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_free(this_ptr: 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) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCounterpartyChannelTransactionParameters); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl CounterpartyChannelTransactionParameters { + pub(crate) fn take_inner(mut self) -> *mut nativeCounterpartyChannelTransactionParameters { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// Counter-party public keys +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_get_pubkeys(this_ptr: &CounterpartyChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.pubkeys; + crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } +} +/// Counter-party public keys +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_set_pubkeys(this_ptr: &mut CounterpartyChannelTransactionParameters, mut val: crate::ln::chan_utils::ChannelPublicKeys) { + unsafe { &mut *this_ptr.inner }.pubkeys = *unsafe { Box::from_raw(val.take_inner()) }; +} +/// The contest delay selected by the counterparty, which applies to holder-broadcast transactions +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_get_selected_contest_delay(this_ptr: &CounterpartyChannelTransactionParameters) -> u16 { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.selected_contest_delay; + (*inner_val) +} +/// The contest delay selected by the counterparty, which applies to holder-broadcast transactions +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_set_selected_contest_delay(this_ptr: &mut CounterpartyChannelTransactionParameters, mut val: u16) { + unsafe { &mut *this_ptr.inner }.selected_contest_delay = val; +} +#[must_use] +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_new(mut pubkeys_arg: crate::ln::chan_utils::ChannelPublicKeys, mut selected_contest_delay_arg: u16) -> CounterpartyChannelTransactionParameters { + CounterpartyChannelTransactionParameters { inner: Box::into_raw(Box::new(nativeCounterpartyChannelTransactionParameters { + pubkeys: *unsafe { Box::from_raw(pubkeys_arg.take_inner()) }, + selected_contest_delay: selected_contest_delay_arg, + })), is_owned: true } +} +impl Clone for CounterpartyChannelTransactionParameters { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeCounterpartyChannelTransactionParameters)).clone() })) as *mut c_void +} +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_clone(orig: &CounterpartyChannelTransactionParameters) -> CounterpartyChannelTransactionParameters { + orig.clone() +} +/// Whether the late bound parameters are populated. +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_is_populated(this_arg: &ChannelTransactionParameters) -> bool { + let mut ret = unsafe { &*this_arg.inner }.is_populated(); + ret +} + +/// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters, +/// given that the holder is the broadcaster. +/// +/// self.is_populated() must be true before calling this function. +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_as_holder_broadcastable(this_arg: &ChannelTransactionParameters) -> crate::ln::chan_utils::DirectedChannelTransactionParameters { + let mut ret = unsafe { &*this_arg.inner }.as_holder_broadcastable(); + crate::ln::chan_utils::DirectedChannelTransactionParameters { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +/// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters, +/// given that the counterparty is the broadcaster. +/// +/// self.is_populated() must be true before calling this function. +#[must_use] +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_as_counterparty_broadcastable(this_arg: &ChannelTransactionParameters) -> crate::ln::chan_utils::DirectedChannelTransactionParameters { + let mut ret = unsafe { &*this_arg.inner }.as_counterparty_broadcastable(); + crate::ln::chan_utils::DirectedChannelTransactionParameters { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_write(obj: &CounterpartyChannelTransactionParameters) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeCounterpartyChannelTransactionParameters) }) +} +#[no_mangle] +pub extern "C" fn CounterpartyChannelTransactionParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_CounterpartyChannelTransactionParametersDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::CounterpartyChannelTransactionParameters { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_write(obj: &ChannelTransactionParameters) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn ChannelTransactionParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelTransactionParameters) }) +} +#[no_mangle] +pub extern "C" fn ChannelTransactionParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelTransactionParametersDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::ChannelTransactionParameters { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} + +use lightning::ln::chan_utils::DirectedChannelTransactionParameters as nativeDirectedChannelTransactionParametersImport; +type nativeDirectedChannelTransactionParameters = nativeDirectedChannelTransactionParametersImport<'static>; + +/// Static channel fields used to build transactions given per-commitment fields, organized by +/// broadcaster/countersignatory. +/// +/// This is derived from the holder/counterparty-organized ChannelTransactionParameters via the +/// as_holder_broadcastable and as_counterparty_broadcastable functions. +#[must_use] +#[repr(C)] +pub struct DirectedChannelTransactionParameters { + /// 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 nativeDirectedChannelTransactionParameters, + pub is_owned: bool, +} + +impl Drop for DirectedChannelTransactionParameters { + fn drop(&mut self) { + if self.is_owned && !self.inner.is_null() { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +#[no_mangle] +pub extern "C" fn DirectedChannelTransactionParameters_free(this_ptr: 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) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDirectedChannelTransactionParameters); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl DirectedChannelTransactionParameters { + pub(crate) fn take_inner(mut self) -> *mut nativeDirectedChannelTransactionParameters { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// Get the channel pubkeys for the broadcaster +#[must_use] +#[no_mangle] +pub extern "C" fn DirectedChannelTransactionParameters_broadcaster_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys { + let mut ret = unsafe { &*this_arg.inner }.broadcaster_pubkeys(); + crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } +} + +/// Get the channel pubkeys for the countersignatory +#[must_use] +#[no_mangle] +pub extern "C" fn DirectedChannelTransactionParameters_countersignatory_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys { + let mut ret = unsafe { &*this_arg.inner }.countersignatory_pubkeys(); + crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } +} + +/// Get the contest delay applicable to the transactions. +/// Note that the contest delay was selected by the countersignatory. +#[must_use] +#[no_mangle] +pub extern "C" fn DirectedChannelTransactionParameters_contest_delay(this_arg: &DirectedChannelTransactionParameters) -> u16 { + let mut ret = unsafe { &*this_arg.inner }.contest_delay(); + ret +} + +/// Whether the channel is outbound from the broadcaster. +/// +/// The boolean representing the side that initiated the channel is +/// an input to the commitment number obscure factor computation. +#[must_use] +#[no_mangle] +pub extern "C" fn DirectedChannelTransactionParameters_is_outbound(this_arg: &DirectedChannelTransactionParameters) -> bool { + let mut ret = unsafe { &*this_arg.inner }.is_outbound(); + ret +} + +/// The funding outpoint +#[must_use] +#[no_mangle] +pub extern "C" fn DirectedChannelTransactionParameters_funding_outpoint(this_arg: &DirectedChannelTransactionParameters) -> crate::chain::transaction::OutPoint { + let mut ret = unsafe { &*this_arg.inner }.funding_outpoint(); + crate::c_types::bitcoin_to_C_outpoint(ret) } use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport; type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport; -/// We use this to track holder commitment transactions and put off signing them until we are ready -/// to broadcast. This class can be used inside a signer implementation to generate a signature -/// given the relevant secret key. +/// Information needed to build and sign a holder's commitment transaction. +/// +/// The transaction is only signed once we are ready to broadcast. #[must_use] #[repr(C)] pub struct HolderCommitmentTransaction { - /// Nearly everyhwere, inner must be non-null, however in places where + /// 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 nativeHolderCommitmentTransaction, pub is_owned: bool, @@ -618,17 +950,35 @@ extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) { #[allow(unused)] /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl HolderCommitmentTransaction { - pub(crate) fn take_ptr(mut self) -> *mut nativeHolderCommitmentTransaction { + pub(crate) fn take_inner(mut self) -> *mut nativeHolderCommitmentTransaction { assert!(self.is_owned); let ret = self.inner; self.inner = std::ptr::null_mut(); ret } } +/// Our counterparty's signature for the transaction +#[no_mangle] +pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig; + crate::c_types::Signature::from_rust(&(*inner_val)) +} +/// Our counterparty's signature for the transaction +#[no_mangle] +pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) { + unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust(); +} +/// All non-dust counterparty HTLC signatures, in the order they appear in the transaction +#[no_mangle] +pub extern "C" fn HolderCommitmentTransaction_set_counterparty_htlc_sigs(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_SignatureZ) { + let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item.into_rust() }); }; + unsafe { &mut *this_ptr.inner }.counterparty_htlc_sigs = local_val; +} impl Clone for HolderCommitmentTransaction { fn clone(&self) -> Self { Self { - inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())), + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, is_owned: true, } } @@ -638,124 +988,377 @@ impl Clone for HolderCommitmentTransaction { pub(crate) extern "C" fn HolderCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void { Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHolderCommitmentTransaction)).clone() })) as *mut c_void } -/// The commitment transaction itself, in unsigned form. #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_get_unsigned_tx(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.unsigned_tx; +pub extern "C" fn HolderCommitmentTransaction_clone(orig: &HolderCommitmentTransaction) -> HolderCommitmentTransaction { + orig.clone() +} +#[no_mangle] +pub extern "C" fn HolderCommitmentTransaction_write(obj: &HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn HolderCommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHolderCommitmentTransaction) }) +} +#[no_mangle] +pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HolderCommitmentTransactionDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::HolderCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +/// Create a new holder transaction with the given counterparty signatures. +/// The funding keys are used to figure out which signature should go first when building the transaction for broadcast. +#[must_use] +#[no_mangle] +pub extern "C" fn HolderCommitmentTransaction_new(mut commitment_tx: crate::ln::chan_utils::CommitmentTransaction, mut counterparty_sig: crate::c_types::Signature, mut counterparty_htlc_sigs: crate::c_types::derived::CVec_SignatureZ, mut holder_funding_key: crate::c_types::PublicKey, mut counterparty_funding_key: crate::c_types::PublicKey) -> HolderCommitmentTransaction { + let mut local_counterparty_htlc_sigs = Vec::new(); for mut item in counterparty_htlc_sigs.into_rust().drain(..) { local_counterparty_htlc_sigs.push( { item.into_rust() }); }; + let mut ret = lightning::ln::chan_utils::HolderCommitmentTransaction::new(*unsafe { Box::from_raw(commitment_tx.take_inner()) }, counterparty_sig.into_rust(), local_counterparty_htlc_sigs, &holder_funding_key.into_rust(), &counterparty_funding_key.into_rust()); + HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true } +} + + +use lightning::ln::chan_utils::BuiltCommitmentTransaction as nativeBuiltCommitmentTransactionImport; +type nativeBuiltCommitmentTransaction = nativeBuiltCommitmentTransactionImport; + +/// A pre-built Bitcoin commitment transaction and its txid. +#[must_use] +#[repr(C)] +pub struct BuiltCommitmentTransaction { + /// 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 nativeBuiltCommitmentTransaction, + pub is_owned: bool, +} + +impl Drop for BuiltCommitmentTransaction { + fn drop(&mut self) { + if self.is_owned && !self.inner.is_null() { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +#[no_mangle] +pub extern "C" fn BuiltCommitmentTransaction_free(this_ptr: 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) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBuiltCommitmentTransaction); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl BuiltCommitmentTransaction { + pub(crate) fn take_inner(mut self) -> *mut nativeBuiltCommitmentTransaction { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// The commitment transaction +#[no_mangle] +pub extern "C" fn BuiltCommitmentTransaction_get_transaction(this_ptr: &BuiltCommitmentTransaction) -> crate::c_types::Transaction { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.transaction; let mut local_inner_val = ::bitcoin::consensus::encode::serialize(inner_val); - local_inner_val.into() + crate::c_types::Transaction::from_vec(local_inner_val) } -/// The commitment transaction itself, in unsigned form. +/// The commitment transaction #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_set_unsigned_tx(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_u8Z) { - unsafe { &mut *this_ptr.inner }.unsigned_tx = ::bitcoin::consensus::encode::deserialize(&val.into_rust()[..]).unwrap(); +pub extern "C" fn BuiltCommitmentTransaction_set_transaction(this_ptr: &mut BuiltCommitmentTransaction, mut val: crate::c_types::Transaction) { + unsafe { &mut *this_ptr.inner }.transaction = val.into_bitcoin(); } -/// Our counterparty's signature for the transaction, above. +/// The txid for the commitment transaction. +/// +/// This is provided as a performance optimization, instead of calling transaction.txid() +/// multiple times. #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig; - crate::c_types::Signature::from_rust(&(*inner_val)) +pub extern "C" fn BuiltCommitmentTransaction_get_txid(this_ptr: &BuiltCommitmentTransaction) -> *const [u8; 32] { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.txid; + (*inner_val).as_inner() } -/// Our counterparty's signature for the transaction, above. +/// The txid for the commitment transaction. +/// +/// This is provided as a performance optimization, instead of calling transaction.txid() +/// multiple times. #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) { - unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust(); +pub extern "C" fn BuiltCommitmentTransaction_set_txid(this_ptr: &mut BuiltCommitmentTransaction, mut val: crate::c_types::ThirtyTwoBytes) { + unsafe { &mut *this_ptr.inner }.txid = ::bitcoin::hash_types::Txid::from_slice(&val.data[..]).unwrap(); } -/// The feerate paid per 1000-weight-unit in this commitment transaction. This value is -/// controlled by the channel initiator. +#[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_get_feerate_per_kw(this_ptr: &HolderCommitmentTransaction) -> u32 { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.feerate_per_kw; - (*inner_val) +pub extern "C" fn BuiltCommitmentTransaction_new(mut transaction_arg: crate::c_types::Transaction, mut txid_arg: crate::c_types::ThirtyTwoBytes) -> BuiltCommitmentTransaction { + BuiltCommitmentTransaction { inner: Box::into_raw(Box::new(nativeBuiltCommitmentTransaction { + transaction: transaction_arg.into_bitcoin(), + txid: ::bitcoin::hash_types::Txid::from_slice(&txid_arg.data[..]).unwrap(), + })), is_owned: true } +} +impl Clone for BuiltCommitmentTransaction { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn BuiltCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBuiltCommitmentTransaction)).clone() })) as *mut c_void +} +#[no_mangle] +pub extern "C" fn BuiltCommitmentTransaction_clone(orig: &BuiltCommitmentTransaction) -> BuiltCommitmentTransaction { + orig.clone() +} +#[no_mangle] +pub extern "C" fn BuiltCommitmentTransaction_write(obj: &BuiltCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) } -/// The feerate paid per 1000-weight-unit in this commitment transaction. This value is -/// controlled by the channel initiator. #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_set_feerate_per_kw(this_ptr: &mut HolderCommitmentTransaction, mut val: u32) { - unsafe { &mut *this_ptr.inner }.feerate_per_kw = val; +pub(crate) extern "C" fn BuiltCommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBuiltCommitmentTransaction) }) } -/// The HTLCs and counterparty htlc signatures which were included in this commitment transaction. +#[no_mangle] +pub extern "C" fn BuiltCommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BuiltCommitmentTransactionDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::BuiltCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +/// Get the SIGHASH_ALL sighash value of the transaction. /// -/// Note that this includes all HTLCs, including ones which were considered dust and not -/// actually included in the transaction as it appears on-chain, but who's value is burned as -/// fees and not included in the to_holder or to_counterparty outputs. +/// This can be used to verify a signature. +#[must_use] +#[no_mangle] +pub extern "C" fn BuiltCommitmentTransaction_get_sighash_all(this_arg: &BuiltCommitmentTransaction, mut funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::ThirtyTwoBytes { + let mut ret = unsafe { &*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 BuiltCommitmentTransaction_sign(this_arg: &BuiltCommitmentTransaction, 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 { &*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, &bitcoin::secp256k1::Secp256k1::new()); + crate::c_types::Signature::from_rust(&ret) +} + + +use lightning::ln::chan_utils::CommitmentTransaction as nativeCommitmentTransactionImport; +type nativeCommitmentTransaction = nativeCommitmentTransactionImport; + +/// This class tracks the per-transaction information needed to build a commitment transaction and to +/// 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. /// -/// The counterparty HTLC signatures in the second element will always be set for non-dust HTLCs, ie -/// those for which transaction_output_index.is_some(). +/// This class can be used inside a signer implementation to generate a signature given the relevant +/// secret key. +#[must_use] +#[repr(C)] +pub struct CommitmentTransaction { + /// 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 nativeCommitmentTransaction, + pub is_owned: bool, +} + +impl Drop for CommitmentTransaction { + fn drop(&mut self) { + if self.is_owned && !self.inner.is_null() { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +#[no_mangle] +pub extern "C" fn CommitmentTransaction_free(this_ptr: 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) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCommitmentTransaction); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl CommitmentTransaction { + pub(crate) fn take_inner(mut self) -> *mut nativeCommitmentTransaction { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +impl Clone for CommitmentTransaction { + fn clone(&self) -> Self { + Self { + inner: if self.inner.is_null() { std::ptr::null_mut() } else { + Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn CommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeCommitmentTransaction)).clone() })) as *mut c_void +} +#[no_mangle] +pub extern "C" fn CommitmentTransaction_clone(orig: &CommitmentTransaction) -> CommitmentTransaction { + orig.clone() +} +#[no_mangle] +pub extern "C" fn CommitmentTransaction_write(obj: &CommitmentTransaction) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) +} +#[no_mangle] +pub(crate) extern "C" fn CommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { + crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeCommitmentTransaction) }) +} +#[no_mangle] +pub extern "C" fn CommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_CommitmentTransactionDecodeErrorZ { + let res = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::CommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + local_res +} +/// The backwards-counting commitment number +#[must_use] +#[no_mangle] +pub extern "C" fn CommitmentTransaction_commitment_number(this_arg: &CommitmentTransaction) -> u64 { + let mut ret = unsafe { &*this_arg.inner }.commitment_number(); + ret +} + +/// The value to be sent to the broadcaster +#[must_use] +#[no_mangle] +pub extern "C" fn CommitmentTransaction_to_broadcaster_value_sat(this_arg: &CommitmentTransaction) -> u64 { + let mut ret = unsafe { &*this_arg.inner }.to_broadcaster_value_sat(); + ret +} + +/// The value to be sent to the counterparty +#[must_use] +#[no_mangle] +pub extern "C" fn CommitmentTransaction_to_countersignatory_value_sat(this_arg: &CommitmentTransaction) -> u64 { + let mut ret = unsafe { &*this_arg.inner }.to_countersignatory_value_sat(); + ret +} + +/// The feerate paid per 1000-weight-unit in this commitment transaction. +#[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_set_per_htlc(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_C2Tuple_HTLCOutputInCommitmentSignatureZZ) { - let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { let (mut orig_val_0_0, mut orig_val_0_1) = item.to_rust(); let mut local_orig_val_0_1 = if orig_val_0_1.is_null() { None } else { Some( { orig_val_0_1.into_rust() }) }; let mut local_val_0 = (*unsafe { Box::from_raw(orig_val_0_0.take_ptr()) }, local_orig_val_0_1); local_val_0 }); }; - unsafe { &mut *this_ptr.inner }.per_htlc = local_val; +pub extern "C" fn CommitmentTransaction_feerate_per_kw(this_arg: &CommitmentTransaction) -> u32 { + let mut ret = unsafe { &*this_arg.inner }.feerate_per_kw(); + ret } -/// Generate a new HolderCommitmentTransaction based on a raw commitment transaction, -/// counterparty signature and both parties keys. + +/// Trust our pre-built transaction and derived transaction creation public keys. +/// +/// Applies a wrapper which allows access to these fields. /// -/// The unsigned transaction outputs must be consistent with htlc_data. This function -/// only checks that the shape and amounts are consistent, but does not check the scriptPubkey. +/// 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. #[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_new_missing_holder_sig(mut unsigned_tx: crate::c_types::derived::CVec_u8Z, mut counterparty_sig: crate::c_types::Signature, holder_funding_key: crate::c_types::PublicKey, counterparty_funding_key: crate::c_types::PublicKey, mut keys: crate::ln::chan_utils::TxCreationKeys, mut feerate_per_kw: u32, mut htlc_data: crate::c_types::derived::CVec_C2Tuple_HTLCOutputInCommitmentSignatureZZ) -> crate::ln::chan_utils::HolderCommitmentTransaction { - let mut local_htlc_data = Vec::new(); for mut item in htlc_data.into_rust().drain(..) { local_htlc_data.push( { let (mut orig_htlc_data_0_0, mut orig_htlc_data_0_1) = item.to_rust(); let mut local_orig_htlc_data_0_1 = if orig_htlc_data_0_1.is_null() { None } else { Some( { orig_htlc_data_0_1.into_rust() }) }; let mut local_htlc_data_0 = (*unsafe { Box::from_raw(orig_htlc_data_0_0.take_ptr()) }, local_orig_htlc_data_0_1); local_htlc_data_0 }); }; - let mut ret = lightning::ln::chan_utils::HolderCommitmentTransaction::new_missing_holder_sig(::bitcoin::consensus::encode::deserialize(&unsigned_tx.into_rust()[..]).unwrap(), counterparty_sig.into_rust(), &holder_funding_key.into_rust(), &counterparty_funding_key.into_rust(), *unsafe { Box::from_raw(keys.take_ptr()) }, feerate_per_kw, local_htlc_data); - crate::ln::chan_utils::HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true } +pub extern "C" fn CommitmentTransaction_trust(this_arg: &CommitmentTransaction) -> crate::ln::chan_utils::TrustedCommitmentTransaction { + let mut ret = unsafe { &*this_arg.inner }.trust(); + crate::ln::chan_utils::TrustedCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true } } -/// The pre-calculated transaction creation public keys. -/// An external validating signer should not trust these keys. +/// Verify our pre-built transaction and derived transaction creation public keys. +/// +/// Applies a wrapper which allows access to these fields. +/// +/// An external validating signer must call this method before signing +/// or using the built transaction. #[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_trust_key_derivation(this_arg: &HolderCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys { - let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation(); - crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } +pub extern "C" fn CommitmentTransaction_verify(this_arg: &CommitmentTransaction, channel_parameters: &crate::ln::chan_utils::DirectedChannelTransactionParameters, broadcaster_keys: &crate::ln::chan_utils::ChannelPublicKeys, countersignatory_keys: &crate::ln::chan_utils::ChannelPublicKeys) -> crate::c_types::derived::CResult_TrustedCommitmentTransactionNoneZ { + let mut ret = unsafe { &*this_arg.inner }.verify(unsafe { &*channel_parameters.inner }, unsafe { &*broadcaster_keys.inner }, unsafe { &*countersignatory_keys.inner }, &bitcoin::secp256k1::Secp256k1::new()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TrustedCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() }; + local_ret } -/// Get the txid of the holder commitment transaction contained in this -/// HolderCommitmentTransaction + +use lightning::ln::chan_utils::TrustedCommitmentTransaction as nativeTrustedCommitmentTransactionImport; +type nativeTrustedCommitmentTransaction = nativeTrustedCommitmentTransactionImport<'static>; + +/// A wrapper on CommitmentTransaction indicating that the derived fields (the built bitcoin +/// transaction and the transaction creation keys) are trusted. +/// +/// See trust() and verify() functions on CommitmentTransaction. +/// +/// This structure implements Deref. +#[must_use] +#[repr(C)] +pub struct TrustedCommitmentTransaction { + /// 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 nativeTrustedCommitmentTransaction, + pub is_owned: bool, +} + +impl Drop for TrustedCommitmentTransaction { + fn drop(&mut self) { + if self.is_owned && !self.inner.is_null() { + let _ = unsafe { Box::from_raw(self.inner) }; + } + } +} +#[no_mangle] +pub extern "C" fn TrustedCommitmentTransaction_free(this_ptr: 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) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTrustedCommitmentTransaction); } +} +#[allow(unused)] +/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy +impl TrustedCommitmentTransaction { + pub(crate) fn take_inner(mut self) -> *mut nativeTrustedCommitmentTransaction { + assert!(self.is_owned); + let ret = self.inner; + self.inner = std::ptr::null_mut(); + ret + } +} +/// The transaction ID of the built Bitcoin transaction #[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_txid(this_arg: &HolderCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes { +pub extern "C" fn TrustedCommitmentTransaction_txid(this_arg: &TrustedCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes { let mut ret = unsafe { &*this_arg.inner }.txid(); crate::c_types::ThirtyTwoBytes { data: ret.into_inner() } } -/// Gets holder signature for the contained commitment transaction given holder funding private key. -/// -/// Funding key is your key included in the 2-2 funding_outpoint lock. Should be provided -/// by your ChannelKeys. -/// Funding redeemscript is script locking funding_outpoint. This is the mutlsig script -/// between your own funding key and your counterparty's. Currently, this is provided in -/// ChannelKeys::sign_holder_commitment() calls directly. -/// Channel value is amount locked in funding_outpoint. +/// The pre-built Bitcoin commitment transaction #[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_get_holder_sig(this_arg: &HolderCommitmentTransaction, funding_key: *const [u8; 32], funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::Signature { - let mut ret = unsafe { &*this_arg.inner }.get_holder_sig(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *funding_key}[..]).unwrap(), &::bitcoin::blockdata::script::Script::from(Vec::from(funding_redeemscript.to_slice())), channel_value_satoshis, &bitcoin::secp256k1::Secp256k1::new()); - crate::c_types::Signature::from_rust(&ret) +pub extern "C" fn TrustedCommitmentTransaction_built_transaction(this_arg: &TrustedCommitmentTransaction) -> crate::ln::chan_utils::BuiltCommitmentTransaction { + let mut ret = unsafe { &*this_arg.inner }.built_transaction(); + crate::ln::chan_utils::BuiltCommitmentTransaction { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } +} + +/// The pre-calculated transaction creation public keys. +#[must_use] +#[no_mangle] +pub extern "C" fn TrustedCommitmentTransaction_keys(this_arg: &TrustedCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys { + let mut ret = unsafe { &*this_arg.inner }.keys(); + crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false } } /// Get a signature for each HTLC which was included in the commitment transaction (ie for /// which HTLCOutputInCommitment::transaction_output_index.is_some()). /// -/// The returned Vec has one entry for each HTLC, and in the same order. For HTLCs which were -/// considered dust and not included, a None entry exists, for all others a signature is -/// included. +/// The returned Vec has one entry for each HTLC, and in the same order. #[must_use] #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_get_htlc_sigs(this_arg: &HolderCommitmentTransaction, htlc_base_key: *const [u8; 32], mut counterparty_selected_contest_delay: u16) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ { - let mut ret = unsafe { &*this_arg.inner }.get_htlc_sigs(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *htlc_base_key}[..]).unwrap(), counterparty_selected_contest_delay, &bitcoin::secp256k1::Secp256k1::new()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for item in o.drain(..) { local_ret_0.push( { let mut local_ret_0_0 = if item.is_none() { crate::c_types::Signature::null() } else { { crate::c_types::Signature::from_rust(&(item.unwrap())) } }; local_ret_0_0 }); }; local_ret_0.into() }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) }; +pub extern "C" fn TrustedCommitmentTransaction_get_htlc_sigs(this_arg: &TrustedCommitmentTransaction, htlc_base_key: *const [u8; 32], channel_parameters: &crate::ln::chan_utils::DirectedChannelTransactionParameters) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ { + let mut ret = unsafe { &*this_arg.inner }.get_htlc_sigs(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *htlc_base_key}[..]).unwrap(), unsafe { &*channel_parameters.inner }, &bitcoin::secp256k1::Secp256k1::new()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for item in o.drain(..) { local_ret_0.push( { crate::c_types::Signature::from_rust(&item) }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() }; local_ret } +/// Get the transaction number obscure factor #[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_write(obj: *const HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z { - crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) }) -} -#[no_mangle] -pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> HolderCommitmentTransaction { - if let Ok(res) = crate::c_types::deserialize_obj(ser) { - HolderCommitmentTransaction { inner: Box::into_raw(Box::new(res)), is_owned: true } - } else { - HolderCommitmentTransaction { inner: std::ptr::null_mut(), is_owned: true } - } +pub extern "C" fn get_commitment_transaction_number_obscure_factor(mut broadcaster_payment_basepoint: crate::c_types::PublicKey, mut countersignatory_payment_basepoint: crate::c_types::PublicKey, mut outbound_from_broadcaster: bool) -> u64 { + let mut ret = lightning::ln::chan_utils::get_commitment_transaction_number_obscure_factor(&broadcaster_payment_basepoint.into_rust(), &countersignatory_payment_basepoint.into_rust(), outbound_from_broadcaster); + ret } +