X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Fchan_utils.rs;h=5cf8b63069a6870372c370ff85ef4e49b7b9b922;hp=fe989bdc782ba77d39c51ec9d8661df55c44bc92;hb=a82e075188fc15a103234832686915c196bfe240;hpb=1ea51d14faf91619495778e82bca2e6fad0ae1ae diff --git a/lightning-c-bindings/src/lightning/ln/chan_utils.rs b/lightning-c-bindings/src/lightning/ln/chan_utils.rs index fe989bd..5cf8b63 100644 --- a/lightning-c-bindings/src/lightning/ln/chan_utils.rs +++ b/lightning-c-bindings/src/lightning/ln/chan_utils.rs @@ -9,6 +9,7 @@ //! Various utilities for building scripts and deriving keys related to channels. These are //! largely of interest for those implementing chain::keysinterface::Sign message signing by hand. +use std::str::FromStr; use std::ffi::c_void; use bitcoin::hashes::Hash; use crate::c_types::*; @@ -138,7 +139,7 @@ impl TxCreationKeys { #[no_mangle] pub extern "C" fn TxCreationKeys_get_per_commitment_point(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.per_commitment_point; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// The broadcaster's per-commitment public key which was used to derive the other keys. #[no_mangle] @@ -151,7 +152,7 @@ pub extern "C" fn TxCreationKeys_set_per_commitment_point(this_ptr: &mut TxCreat #[no_mangle] pub extern "C" fn TxCreationKeys_get_revocation_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_key; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// The revocation key which is used to allow the broadcaster of the commitment /// transaction to provide their counterparty the ability to punish them if they broadcast @@ -164,7 +165,7 @@ pub extern "C" fn TxCreationKeys_set_revocation_key(this_ptr: &mut TxCreationKey #[no_mangle] pub extern "C" fn TxCreationKeys_get_broadcaster_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// Broadcaster's HTLC Key #[no_mangle] @@ -175,7 +176,7 @@ pub extern "C" fn TxCreationKeys_set_broadcaster_htlc_key(this_ptr: &mut TxCreat #[no_mangle] pub extern "C" fn TxCreationKeys_get_countersignatory_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// Countersignatory's HTLC Key #[no_mangle] @@ -186,7 +187,7 @@ pub extern "C" fn TxCreationKeys_set_countersignatory_htlc_key(this_ptr: &mut Tx #[no_mangle] pub extern "C" fn TxCreationKeys_get_broadcaster_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay) #[no_mangle] @@ -290,7 +291,7 @@ impl ChannelPublicKeys { #[no_mangle] pub extern "C" fn ChannelPublicKeys_get_funding_pubkey(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_pubkey; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// 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. @@ -305,7 +306,7 @@ pub extern "C" fn ChannelPublicKeys_set_funding_pubkey(this_ptr: &mut ChannelPub #[no_mangle] pub extern "C" fn ChannelPublicKeys_get_revocation_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_basepoint; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// The base point which is used (with derive_public_revocation_key) to derive per-commitment /// revocation keys. This is combined with the per-commitment-secret generated by the @@ -321,7 +322,7 @@ pub extern "C" fn ChannelPublicKeys_set_revocation_basepoint(this_ptr: &mut Chan #[no_mangle] pub extern "C" fn ChannelPublicKeys_get_payment_point(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_point; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately /// spendable primary channel balance on the broadcaster's commitment transaction. This key is @@ -336,7 +337,7 @@ pub extern "C" fn ChannelPublicKeys_set_payment_point(this_ptr: &mut ChannelPubl #[no_mangle] pub extern "C" fn ChannelPublicKeys_get_delayed_payment_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// The base point which is used (with derive_public_key) to derive a per-commitment payment /// public key which receives non-HTLC-encumbered funds which are only available for spending @@ -350,7 +351,7 @@ pub extern "C" fn ChannelPublicKeys_set_delayed_payment_basepoint(this_ptr: &mut #[no_mangle] pub extern "C" fn ChannelPublicKeys_get_htlc_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_basepoint; - crate::c_types::PublicKey::from_rust(&(*inner_val)) + crate::c_types::PublicKey::from_rust(&inner_val) } /// The base point which is used (with derive_public_key) to derive a per-commitment public key /// which is used to encumber HTLC-in-flight outputs. @@ -490,7 +491,7 @@ impl HTLCOutputInCommitment { #[no_mangle] pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered; - (*inner_val) + *inner_val } /// 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 @@ -505,7 +506,7 @@ pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputIn #[no_mangle] pub extern "C" fn HTLCOutputInCommitment_get_amount_msat(this_ptr: &HTLCOutputInCommitment) -> u64 { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.amount_msat; - (*inner_val) + *inner_val } /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is /// this divided by 1000. @@ -517,7 +518,7 @@ pub extern "C" fn HTLCOutputInCommitment_set_amount_msat(this_ptr: &mut HTLCOutp #[no_mangle] pub extern "C" fn HTLCOutputInCommitment_get_cltv_expiry(this_ptr: &HTLCOutputInCommitment) -> u32 { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry; - (*inner_val) + *inner_val } /// The CLTV lock-time at which this HTLC expires. #[no_mangle] @@ -528,12 +529,12 @@ pub extern "C" fn HTLCOutputInCommitment_set_cltv_expiry(this_ptr: &mut HTLCOutp #[no_mangle] pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputInCommitment) -> *const [u8; 32] { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_hash; - &(*inner_val).0 + &inner_val.0 } /// The hash of the preimage which unlocks this HTLC. #[no_mangle] 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); + unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::PaymentHash(val.data); } /// The position within the commitment transactions' outputs. This may be None if the value is /// below the dust limit (in which case no output appears in the commitment transaction and the @@ -561,7 +562,7 @@ pub extern "C" fn HTLCOutputInCommitment_new(mut offered_arg: bool, mut amount_m offered: offered_arg, amount_msat: amount_msat_arg, cltv_expiry: cltv_expiry_arg, - payment_hash: ::lightning::ln::channelmanager::PaymentHash(payment_hash_arg.data), + payment_hash: ::lightning::ln::PaymentHash(payment_hash_arg.data), transaction_output_index: local_transaction_output_index_arg, })), is_owned: true } } @@ -616,12 +617,17 @@ pub extern "C" fn make_funding_redeemscript(mut broadcaster: crate::c_types::Pub ret.into_bytes().into() } -/// panics if htlc.transaction_output_index.is_none()! +/// Builds an unsigned HTLC-Success or HTLC-Timeout transaction from the given channel and HTLC +/// parameters. This is used by [`TrustedCommitmentTransaction::get_htlc_sigs`] to fetch the +/// transaction which needs signing, and can be used to construct an HTLC transaction which is +/// broadcastable given a counterparty HTLC signature. +/// +/// Panics if htlc.transaction_output_index.is_none() (as such HTLCs do not appear in the +/// commitment transaction). #[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::lightning::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); - crate::c_types::Transaction::from_vec(local_ret) +pub extern "C" fn build_htlc_transaction(commitment_txid: *const [u8; 32], mut feerate_per_kw: u32, mut contest_delay: u16, htlc: &crate::lightning::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 { &*commitment_txid }[..]).unwrap(), feerate_per_kw, contest_delay, unsafe { &*htlc.inner }, &broadcaster_delayed_payment_key.into_rust(), &revocation_key.into_rust()); + crate::c_types::Transaction::from_bitcoin(&ret) } @@ -677,7 +683,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 unsafe { &mut *this_ptr.inner }.holder_pubkeys; - crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false } } /// Holder public keys #[no_mangle] @@ -688,7 +694,7 @@ pub extern "C" fn ChannelTransactionParameters_set_holder_pubkeys(this_ptr: &mut #[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) + *inner_val } /// The contest delay selected by the holder, which applies to counterparty-broadcast transactions #[no_mangle] @@ -700,7 +706,7 @@ pub extern "C" fn ChannelTransactionParameters_set_holder_selected_contest_delay #[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) + *inner_val } /// Whether the holder is the initiator of this channel. /// This is an input to the commitment number obscure factor computation. @@ -710,6 +716,8 @@ pub extern "C" fn ChannelTransactionParameters_set_is_outbound_from_holder(this_ } /// The late-bound counterparty channel transaction parameters. /// These parameters are populated at the point in the protocol where the counterparty provides them. +/// +/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] pub extern "C" fn ChannelTransactionParameters_get_counterparty_parameters(this_ptr: &ChannelTransactionParameters) -> crate::lightning::ln::chan_utils::CounterpartyChannelTransactionParameters { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_parameters; @@ -718,12 +726,16 @@ pub extern "C" fn ChannelTransactionParameters_get_counterparty_parameters(this_ } /// The late-bound counterparty channel transaction parameters. /// These parameters are populated at the point in the protocol where the counterparty provides them. +/// +/// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] pub extern "C" fn ChannelTransactionParameters_set_counterparty_parameters(this_ptr: &mut ChannelTransactionParameters, mut val: crate::lightning::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 +/// +/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] pub extern "C" fn ChannelTransactionParameters_get_funding_outpoint(this_ptr: &ChannelTransactionParameters) -> crate::lightning::chain::transaction::OutPoint { let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_outpoint; @@ -731,6 +743,8 @@ pub extern "C" fn ChannelTransactionParameters_get_funding_outpoint(this_ptr: &C local_inner_val } /// The late-bound funding outpoint +/// +/// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] pub extern "C" fn ChannelTransactionParameters_set_funding_outpoint(this_ptr: &mut ChannelTransactionParameters, mut val: crate::lightning::chain::transaction::OutPoint) { let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) }; @@ -818,7 +832,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 unsafe { &mut *this_ptr.inner }.pubkeys; - crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } + crate::lightning::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false } } /// Counter-party public keys #[no_mangle] @@ -829,7 +843,7 @@ pub extern "C" fn CounterpartyChannelTransactionParameters_set_pubkeys(this_ptr: #[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) + *inner_val } /// The contest delay selected by the counterparty, which applies to holder-broadcast transactions #[no_mangle] @@ -1070,7 +1084,7 @@ impl HolderCommitmentTransaction { #[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)) + crate::c_types::Signature::from_rust(&inner_val) } /// Our counterparty's signature for the transaction #[no_mangle] @@ -1177,8 +1191,7 @@ impl BuiltCommitmentTransaction { #[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); - crate::c_types::Transaction::from_vec(local_inner_val) + crate::c_types::Transaction::from_bitcoin(inner_val) } /// The commitment transaction #[no_mangle] @@ -1192,7 +1205,7 @@ pub extern "C" fn BuiltCommitmentTransaction_set_transaction(this_ptr: &mut Buil #[no_mangle] 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() + inner_val.as_inner() } /// The txid for the commitment transaction. /// @@ -1405,7 +1418,7 @@ pub extern "C" fn CommitmentTransaction_trust(this_arg: &CommitmentTransaction) #[no_mangle] pub extern "C" fn CommitmentTransaction_verify(this_arg: &CommitmentTransaction, channel_parameters: &crate::lightning::ln::chan_utils::DirectedChannelTransactionParameters, broadcaster_keys: &crate::lightning::ln::chan_utils::ChannelPublicKeys, countersignatory_keys: &crate::lightning::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 }, secp256k1::SECP256K1); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::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() }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::chan_utils::TrustedCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() }; local_ret } @@ -1491,11 +1504,16 @@ pub extern "C" fn TrustedCommitmentTransaction_keys(this_arg: &TrustedCommitment #[no_mangle] pub extern "C" fn TrustedCommitmentTransaction_get_htlc_sigs(this_arg: &TrustedCommitmentTransaction, htlc_base_key: *const [u8; 32], channel_parameters: &crate::lightning::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 }, secp256k1::SECP256K1); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut 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() }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut 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( { () /*e*/ }).into() }; local_ret } -/// Get the transaction number obscure factor +/// Commitment transaction numbers which appear in the transactions themselves are XOR'd with a +/// shared secret first. This prevents on-chain observers from discovering how many commitment +/// transactions occurred in a channel before it was closed. +/// +/// This function gets the shared secret from relevant channel public keys and can be used to +/// \"decrypt\" the commitment transaction number given a commitment transaction on-chain. #[no_mangle] 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);