1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Utilities for bumping transactions originating from [`Event`]s.
11 //! [`Event`]: crate::events::Event
13 use alloc::str::FromStr;
14 use alloc::string::String;
15 use core::ffi::c_void;
16 use core::convert::Infallible;
17 use bitcoin::hashes::Hash;
18 use crate::c_types::*;
19 #[cfg(feature="no-std")]
20 use alloc::{vec::Vec, boxed::Box};
23 use lightning::events::bump_transaction::AnchorDescriptor as nativeAnchorDescriptorImport;
24 pub(crate) type nativeAnchorDescriptor = nativeAnchorDescriptorImport;
26 /// A descriptor used to sign for a commitment transaction's anchor output.
29 pub struct AnchorDescriptor {
30 /// A pointer to the opaque Rust object.
32 /// Nearly everywhere, inner must be non-null, however in places where
33 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
34 pub inner: *mut nativeAnchorDescriptor,
35 /// Indicates that this is the only struct which contains the same pointer.
37 /// Rust functions which take ownership of an object provided via an argument require
38 /// this to be true and invalidate the object pointed to by inner.
42 impl Drop for AnchorDescriptor {
44 if self.is_owned && !<*mut nativeAnchorDescriptor>::is_null(self.inner) {
45 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
49 /// Frees any resources used by the AnchorDescriptor, if is_owned is set and inner is non-NULL.
51 pub extern "C" fn AnchorDescriptor_free(this_obj: AnchorDescriptor) { }
53 /// Used only if an object of this type is returned as a trait impl by a method
54 pub(crate) extern "C" fn AnchorDescriptor_free_void(this_ptr: *mut c_void) {
55 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeAnchorDescriptor) };
58 impl AnchorDescriptor {
59 pub(crate) fn get_native_ref(&self) -> &'static nativeAnchorDescriptor {
60 unsafe { &*ObjOps::untweak_ptr(self.inner) }
62 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeAnchorDescriptor {
63 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
65 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
66 pub(crate) fn take_inner(mut self) -> *mut nativeAnchorDescriptor {
67 assert!(self.is_owned);
68 let ret = ObjOps::untweak_ptr(self.inner);
69 self.inner = core::ptr::null_mut();
73 /// The parameters required to derive the signer for the anchor input.
75 pub extern "C" fn AnchorDescriptor_get_channel_derivation_parameters(this_ptr: &AnchorDescriptor) -> crate::lightning::sign::ChannelDerivationParameters {
76 let mut inner_val = &mut this_ptr.get_native_mut_ref().channel_derivation_parameters;
77 crate::lightning::sign::ChannelDerivationParameters { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::sign::ChannelDerivationParameters<>) as *mut _) }, is_owned: false }
79 /// The parameters required to derive the signer for the anchor input.
81 pub extern "C" fn AnchorDescriptor_set_channel_derivation_parameters(this_ptr: &mut AnchorDescriptor, mut val: crate::lightning::sign::ChannelDerivationParameters) {
82 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.channel_derivation_parameters = *unsafe { Box::from_raw(val.take_inner()) };
84 /// The transaction input's outpoint corresponding to the commitment transaction's anchor
87 pub extern "C" fn AnchorDescriptor_get_outpoint(this_ptr: &AnchorDescriptor) -> crate::lightning::chain::transaction::OutPoint {
88 let mut inner_val = &mut this_ptr.get_native_mut_ref().outpoint;
89 crate::c_types::bitcoin_to_C_outpoint(inner_val)
91 /// The transaction input's outpoint corresponding to the commitment transaction's anchor
94 pub extern "C" fn AnchorDescriptor_set_outpoint(this_ptr: &mut AnchorDescriptor, mut val: crate::lightning::chain::transaction::OutPoint) {
95 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.outpoint = crate::c_types::C_to_bitcoin_outpoint(val);
97 /// Constructs a new AnchorDescriptor given each field
100 pub extern "C" fn AnchorDescriptor_new(mut channel_derivation_parameters_arg: crate::lightning::sign::ChannelDerivationParameters, mut outpoint_arg: crate::lightning::chain::transaction::OutPoint) -> AnchorDescriptor {
101 AnchorDescriptor { inner: ObjOps::heap_alloc(nativeAnchorDescriptor {
102 channel_derivation_parameters: *unsafe { Box::from_raw(channel_derivation_parameters_arg.take_inner()) },
103 outpoint: crate::c_types::C_to_bitcoin_outpoint(outpoint_arg),
106 impl Clone for AnchorDescriptor {
107 fn clone(&self) -> Self {
109 inner: if <*mut nativeAnchorDescriptor>::is_null(self.inner) { core::ptr::null_mut() } else {
110 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
116 /// Used only if an object of this type is returned as a trait impl by a method
117 pub(crate) extern "C" fn AnchorDescriptor_clone_void(this_ptr: *const c_void) -> *mut c_void {
118 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeAnchorDescriptor)).clone() })) as *mut c_void
121 /// Creates a copy of the AnchorDescriptor
122 pub extern "C" fn AnchorDescriptor_clone(orig: &AnchorDescriptor) -> AnchorDescriptor {
125 /// Checks if two AnchorDescriptors contain equal inner contents.
126 /// This ignores pointers and is_owned flags and looks at the values in fields.
127 /// Two objects with NULL inner values will be considered "equal" here.
129 pub extern "C" fn AnchorDescriptor_eq(a: &AnchorDescriptor, b: &AnchorDescriptor) -> bool {
130 if a.inner == b.inner { return true; }
131 if a.inner.is_null() || b.inner.is_null() { return false; }
132 if a.get_native_ref() == b.get_native_ref() { true } else { false }
134 /// Returns the UTXO to be spent by the anchor input, which can be obtained via
135 /// [`Self::unsigned_tx_input`].
138 pub extern "C" fn AnchorDescriptor_previous_utxo(this_arg: &crate::lightning::events::bump_transaction::AnchorDescriptor) -> crate::c_types::TxOut {
139 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.previous_utxo();
140 crate::c_types::TxOut::from_rust(&ret)
143 /// Returns the unsigned transaction input spending the anchor output in the commitment
147 pub extern "C" fn AnchorDescriptor_unsigned_tx_input(this_arg: &crate::lightning::events::bump_transaction::AnchorDescriptor) -> crate::c_types::TxIn {
148 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.unsigned_tx_input();
149 crate::c_types::TxIn::from_rust(&ret)
152 /// Returns the witness script of the anchor output in the commitment transaction.
155 pub extern "C" fn AnchorDescriptor_witness_script(this_arg: &crate::lightning::events::bump_transaction::AnchorDescriptor) -> crate::c_types::derived::CVec_u8Z {
156 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.witness_script();
157 ret.into_bytes().into()
160 /// Returns the fully signed witness required to spend the anchor output in the commitment
164 pub extern "C" fn AnchorDescriptor_tx_input_witness(this_arg: &crate::lightning::events::bump_transaction::AnchorDescriptor, mut signature: crate::c_types::ECDSASignature) -> crate::c_types::Witness {
165 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.tx_input_witness(&signature.into_rust());
166 crate::c_types::Witness::from_bitcoin(&ret)
169 /// Derives the channel signer required to sign the anchor input.
172 pub extern "C" fn AnchorDescriptor_derive_channel_signer(this_arg: &crate::lightning::events::bump_transaction::AnchorDescriptor, signer_provider: &crate::lightning::sign::SignerProvider) -> crate::lightning::sign::WriteableEcdsaChannelSigner {
173 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.derive_channel_signer(signer_provider);
177 /// Represents the different types of transactions, originating from LDK, to be bumped.
181 pub enum BumpTransactionEvent {
182 /// Indicates that a channel featuring anchor outputs is to be closed by broadcasting the local
183 /// commitment transaction. Since commitment transactions have a static feerate pre-agreed upon,
184 /// they may need additional fees to be attached through a child transaction using the popular
185 /// [Child-Pays-For-Parent](https://bitcoinops.org/en/topics/cpfp) fee bumping technique. This
186 /// child transaction must include the anchor input described within `anchor_descriptor` along
187 /// with additional inputs to meet the target feerate. Failure to meet the target feerate
188 /// decreases the confirmation odds of the transaction package (which includes the commitment
189 /// and child anchor transactions), possibly resulting in a loss of funds. Once the transaction
190 /// is constructed, it must be fully signed for and broadcast by the consumer of the event
191 /// along with the `commitment_tx` enclosed. Note that the `commitment_tx` must always be
192 /// broadcast first, as the child anchor transaction depends on it.
194 /// The consumer should be able to sign for any of the additional inputs included within the
195 /// child anchor transaction. To sign its anchor input, an [`EcdsaChannelSigner`] should be
196 /// re-derived through [`AnchorDescriptor::derive_channel_signer`]. The anchor input signature
197 /// can be computed with [`EcdsaChannelSigner::sign_holder_anchor_input`], which can then be
198 /// provided to [`build_anchor_input_witness`] along with the `funding_pubkey` to obtain the
199 /// full witness required to spend.
201 /// It is possible to receive more than one instance of this event if a valid child anchor
202 /// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
203 /// be taken by the consumer of the event to ensure any future iterations of the child anchor
204 /// transaction adhere to the [Replace-By-Fee
205 /// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
206 /// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
207 /// these events is not user-controlled, users may ignore/drop the event if they are no longer
208 /// able to commit external confirmed funds to the child anchor transaction.
210 /// The set of `pending_htlcs` on the commitment transaction to be broadcast can be inspected to
211 /// determine whether a significant portion of the channel's funds are allocated to HTLCs,
212 /// enabling users to make their own decisions regarding the importance of the commitment
213 /// transaction's confirmation. Note that this is not required, but simply exists as an option
214 /// for users to override LDK's behavior. On commitments with no HTLCs (indicated by those with
215 /// an empty `pending_htlcs`), confirmation of the commitment transaction can be considered to
218 /// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
219 /// [`EcdsaChannelSigner::sign_holder_anchor_input`]: crate::sign::EcdsaChannelSigner::sign_holder_anchor_input
220 /// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
222 /// The unique identifier for the claim of the anchor output in the commitment transaction.
224 /// The identifier must map to the set of external UTXOs assigned to the claim, such that
225 /// they can be reused when a new claim with the same identifier needs to be made, resulting
226 /// in a fee-bumping attempt.
227 claim_id: crate::c_types::ThirtyTwoBytes,
228 /// The target feerate that the transaction package, which consists of the commitment
229 /// transaction and the to-be-crafted child anchor transaction, must meet.
230 package_target_feerate_sat_per_1000_weight: u32,
231 /// The channel's commitment transaction to bump the fee of. This transaction should be
232 /// broadcast along with the anchor transaction constructed as a result of consuming this
234 commitment_tx: crate::c_types::Transaction,
235 /// The absolute fee in satoshis of the commitment transaction. This can be used along the
236 /// with weight of the commitment transaction to determine its feerate.
237 commitment_tx_fee_satoshis: u64,
238 /// The descriptor to sign the anchor input of the anchor transaction constructed as a
239 /// result of consuming this event.
240 anchor_descriptor: crate::lightning::events::bump_transaction::AnchorDescriptor,
241 /// The set of pending HTLCs on the commitment transaction that need to be resolved once the
242 /// commitment transaction confirms.
243 pending_htlcs: crate::c_types::derived::CVec_HTLCOutputInCommitmentZ,
245 /// Indicates that a channel featuring anchor outputs has unilaterally closed on-chain by a
246 /// holder commitment transaction and its HTLC(s) need to be resolved on-chain. With the
247 /// zero-HTLC-transaction-fee variant of anchor outputs, the pre-signed HTLC
248 /// transactions have a zero fee, thus requiring additional inputs and/or outputs to be attached
249 /// for a timely confirmation within the chain. These additional inputs and/or outputs must be
250 /// appended to the resulting HTLC transaction to meet the target feerate. Failure to meet the
251 /// target feerate decreases the confirmation odds of the transaction, possibly resulting in a
252 /// loss of funds. Once the transaction meets the target feerate, it must be signed for and
253 /// broadcast by the consumer of the event.
255 /// The consumer should be able to sign for any of the non-HTLC inputs added to the resulting
256 /// HTLC transaction. To sign HTLC inputs, an [`EcdsaChannelSigner`] should be re-derived
257 /// through [`HTLCDescriptor::derive_channel_signer`]. Each HTLC input's signature can be
258 /// computed with [`EcdsaChannelSigner::sign_holder_htlc_transaction`], which can then be
259 /// provided to [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required
262 /// It is possible to receive more than one instance of this event if a valid HTLC transaction
263 /// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
264 /// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
265 /// the [Replace-By-Fee
266 /// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
267 /// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
268 /// these events is not user-controlled, users may ignore/drop the event if either they are no
269 /// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
270 /// to the HTLC transaction is greater in value than the HTLCs being claimed.
272 /// [`EcdsaChannelSigner`]: crate::sign::EcdsaChannelSigner
273 /// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::EcdsaChannelSigner::sign_holder_htlc_transaction
275 /// The unique identifier for the claim of the HTLCs in the confirmed commitment
278 /// The identifier must map to the set of external UTXOs assigned to the claim, such that
279 /// they can be reused when a new claim with the same identifier needs to be made, resulting
280 /// in a fee-bumping attempt.
281 claim_id: crate::c_types::ThirtyTwoBytes,
282 /// The target feerate that the resulting HTLC transaction must meet.
283 target_feerate_sat_per_1000_weight: u32,
284 /// The set of pending HTLCs on the confirmed commitment that need to be claimed, preferably
285 /// by the same transaction.
286 htlc_descriptors: crate::c_types::derived::CVec_HTLCDescriptorZ,
287 /// The locktime required for the resulting HTLC transaction.
291 use lightning::events::bump_transaction::BumpTransactionEvent as BumpTransactionEventImport;
292 pub(crate) type nativeBumpTransactionEvent = BumpTransactionEventImport;
294 impl BumpTransactionEvent {
296 pub(crate) fn to_native(&self) -> nativeBumpTransactionEvent {
298 BumpTransactionEvent::ChannelClose {ref claim_id, ref package_target_feerate_sat_per_1000_weight, ref commitment_tx, ref commitment_tx_fee_satoshis, ref anchor_descriptor, ref pending_htlcs, } => {
299 let mut claim_id_nonref = Clone::clone(claim_id);
300 let mut package_target_feerate_sat_per_1000_weight_nonref = Clone::clone(package_target_feerate_sat_per_1000_weight);
301 let mut commitment_tx_nonref = Clone::clone(commitment_tx);
302 let mut commitment_tx_fee_satoshis_nonref = Clone::clone(commitment_tx_fee_satoshis);
303 let mut anchor_descriptor_nonref = Clone::clone(anchor_descriptor);
304 let mut pending_htlcs_nonref = Clone::clone(pending_htlcs);
305 let mut local_pending_htlcs_nonref = Vec::new(); for mut item in pending_htlcs_nonref.into_rust().drain(..) { local_pending_htlcs_nonref.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
306 nativeBumpTransactionEvent::ChannelClose {
307 claim_id: ::lightning::chain::ClaimId(claim_id_nonref.data),
308 package_target_feerate_sat_per_1000_weight: package_target_feerate_sat_per_1000_weight_nonref,
309 commitment_tx: commitment_tx_nonref.into_bitcoin(),
310 commitment_tx_fee_satoshis: commitment_tx_fee_satoshis_nonref,
311 anchor_descriptor: *unsafe { Box::from_raw(anchor_descriptor_nonref.take_inner()) },
312 pending_htlcs: local_pending_htlcs_nonref,
315 BumpTransactionEvent::HTLCResolution {ref claim_id, ref target_feerate_sat_per_1000_weight, ref htlc_descriptors, ref tx_lock_time, } => {
316 let mut claim_id_nonref = Clone::clone(claim_id);
317 let mut target_feerate_sat_per_1000_weight_nonref = Clone::clone(target_feerate_sat_per_1000_weight);
318 let mut htlc_descriptors_nonref = Clone::clone(htlc_descriptors);
319 let mut local_htlc_descriptors_nonref = Vec::new(); for mut item in htlc_descriptors_nonref.into_rust().drain(..) { local_htlc_descriptors_nonref.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
320 let mut tx_lock_time_nonref = Clone::clone(tx_lock_time);
321 nativeBumpTransactionEvent::HTLCResolution {
322 claim_id: ::lightning::chain::ClaimId(claim_id_nonref.data),
323 target_feerate_sat_per_1000_weight: target_feerate_sat_per_1000_weight_nonref,
324 htlc_descriptors: local_htlc_descriptors_nonref,
325 tx_lock_time: ::bitcoin::PackedLockTime(tx_lock_time_nonref),
331 pub(crate) fn into_native(self) -> nativeBumpTransactionEvent {
333 BumpTransactionEvent::ChannelClose {mut claim_id, mut package_target_feerate_sat_per_1000_weight, mut commitment_tx, mut commitment_tx_fee_satoshis, mut anchor_descriptor, mut pending_htlcs, } => {
334 let mut local_pending_htlcs = Vec::new(); for mut item in pending_htlcs.into_rust().drain(..) { local_pending_htlcs.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
335 nativeBumpTransactionEvent::ChannelClose {
336 claim_id: ::lightning::chain::ClaimId(claim_id.data),
337 package_target_feerate_sat_per_1000_weight: package_target_feerate_sat_per_1000_weight,
338 commitment_tx: commitment_tx.into_bitcoin(),
339 commitment_tx_fee_satoshis: commitment_tx_fee_satoshis,
340 anchor_descriptor: *unsafe { Box::from_raw(anchor_descriptor.take_inner()) },
341 pending_htlcs: local_pending_htlcs,
344 BumpTransactionEvent::HTLCResolution {mut claim_id, mut target_feerate_sat_per_1000_weight, mut htlc_descriptors, mut tx_lock_time, } => {
345 let mut local_htlc_descriptors = Vec::new(); for mut item in htlc_descriptors.into_rust().drain(..) { local_htlc_descriptors.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
346 nativeBumpTransactionEvent::HTLCResolution {
347 claim_id: ::lightning::chain::ClaimId(claim_id.data),
348 target_feerate_sat_per_1000_weight: target_feerate_sat_per_1000_weight,
349 htlc_descriptors: local_htlc_descriptors,
350 tx_lock_time: ::bitcoin::PackedLockTime(tx_lock_time),
356 pub(crate) fn from_native(native: &nativeBumpTransactionEvent) -> Self {
358 nativeBumpTransactionEvent::ChannelClose {ref claim_id, ref package_target_feerate_sat_per_1000_weight, ref commitment_tx, ref commitment_tx_fee_satoshis, ref anchor_descriptor, ref pending_htlcs, } => {
359 let mut claim_id_nonref = Clone::clone(claim_id);
360 let mut package_target_feerate_sat_per_1000_weight_nonref = Clone::clone(package_target_feerate_sat_per_1000_weight);
361 let mut commitment_tx_nonref = Clone::clone(commitment_tx);
362 let mut commitment_tx_fee_satoshis_nonref = Clone::clone(commitment_tx_fee_satoshis);
363 let mut anchor_descriptor_nonref = Clone::clone(anchor_descriptor);
364 let mut pending_htlcs_nonref = Clone::clone(pending_htlcs);
365 let mut local_pending_htlcs_nonref = Vec::new(); for mut item in pending_htlcs_nonref.drain(..) { local_pending_htlcs_nonref.push( { crate::lightning::ln::chan_utils::HTLCOutputInCommitment { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
366 BumpTransactionEvent::ChannelClose {
367 claim_id: crate::c_types::ThirtyTwoBytes { data: claim_id_nonref.0 },
368 package_target_feerate_sat_per_1000_weight: package_target_feerate_sat_per_1000_weight_nonref,
369 commitment_tx: crate::c_types::Transaction::from_bitcoin(&commitment_tx_nonref),
370 commitment_tx_fee_satoshis: commitment_tx_fee_satoshis_nonref,
371 anchor_descriptor: crate::lightning::events::bump_transaction::AnchorDescriptor { inner: ObjOps::heap_alloc(anchor_descriptor_nonref), is_owned: true },
372 pending_htlcs: local_pending_htlcs_nonref.into(),
375 nativeBumpTransactionEvent::HTLCResolution {ref claim_id, ref target_feerate_sat_per_1000_weight, ref htlc_descriptors, ref tx_lock_time, } => {
376 let mut claim_id_nonref = Clone::clone(claim_id);
377 let mut target_feerate_sat_per_1000_weight_nonref = Clone::clone(target_feerate_sat_per_1000_weight);
378 let mut htlc_descriptors_nonref = Clone::clone(htlc_descriptors);
379 let mut local_htlc_descriptors_nonref = Vec::new(); for mut item in htlc_descriptors_nonref.drain(..) { local_htlc_descriptors_nonref.push( { crate::lightning::sign::HTLCDescriptor { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
380 let mut tx_lock_time_nonref = Clone::clone(tx_lock_time);
381 BumpTransactionEvent::HTLCResolution {
382 claim_id: crate::c_types::ThirtyTwoBytes { data: claim_id_nonref.0 },
383 target_feerate_sat_per_1000_weight: target_feerate_sat_per_1000_weight_nonref,
384 htlc_descriptors: local_htlc_descriptors_nonref.into(),
385 tx_lock_time: tx_lock_time_nonref.0,
391 pub(crate) fn native_into(native: nativeBumpTransactionEvent) -> Self {
393 nativeBumpTransactionEvent::ChannelClose {mut claim_id, mut package_target_feerate_sat_per_1000_weight, mut commitment_tx, mut commitment_tx_fee_satoshis, mut anchor_descriptor, mut pending_htlcs, } => {
394 let mut local_pending_htlcs = Vec::new(); for mut item in pending_htlcs.drain(..) { local_pending_htlcs.push( { crate::lightning::ln::chan_utils::HTLCOutputInCommitment { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
395 BumpTransactionEvent::ChannelClose {
396 claim_id: crate::c_types::ThirtyTwoBytes { data: claim_id.0 },
397 package_target_feerate_sat_per_1000_weight: package_target_feerate_sat_per_1000_weight,
398 commitment_tx: crate::c_types::Transaction::from_bitcoin(&commitment_tx),
399 commitment_tx_fee_satoshis: commitment_tx_fee_satoshis,
400 anchor_descriptor: crate::lightning::events::bump_transaction::AnchorDescriptor { inner: ObjOps::heap_alloc(anchor_descriptor), is_owned: true },
401 pending_htlcs: local_pending_htlcs.into(),
404 nativeBumpTransactionEvent::HTLCResolution {mut claim_id, mut target_feerate_sat_per_1000_weight, mut htlc_descriptors, mut tx_lock_time, } => {
405 let mut local_htlc_descriptors = Vec::new(); for mut item in htlc_descriptors.drain(..) { local_htlc_descriptors.push( { crate::lightning::sign::HTLCDescriptor { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
406 BumpTransactionEvent::HTLCResolution {
407 claim_id: crate::c_types::ThirtyTwoBytes { data: claim_id.0 },
408 target_feerate_sat_per_1000_weight: target_feerate_sat_per_1000_weight,
409 htlc_descriptors: local_htlc_descriptors.into(),
410 tx_lock_time: tx_lock_time.0,
416 /// Frees any resources used by the BumpTransactionEvent
418 pub extern "C" fn BumpTransactionEvent_free(this_ptr: BumpTransactionEvent) { }
419 /// Creates a copy of the BumpTransactionEvent
421 pub extern "C" fn BumpTransactionEvent_clone(orig: &BumpTransactionEvent) -> BumpTransactionEvent {
425 /// Used only if an object of this type is returned as a trait impl by a method
426 pub(crate) extern "C" fn BumpTransactionEvent_clone_void(this_ptr: *const c_void) -> *mut c_void {
427 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const BumpTransactionEvent)).clone() })) as *mut c_void
430 /// Used only if an object of this type is returned as a trait impl by a method
431 pub(crate) extern "C" fn BumpTransactionEvent_free_void(this_ptr: *mut c_void) {
432 let _ = unsafe { Box::from_raw(this_ptr as *mut BumpTransactionEvent) };
435 /// Utility method to constructs a new ChannelClose-variant BumpTransactionEvent
436 pub extern "C" fn BumpTransactionEvent_channel_close(claim_id: crate::c_types::ThirtyTwoBytes, package_target_feerate_sat_per_1000_weight: u32, commitment_tx: crate::c_types::Transaction, commitment_tx_fee_satoshis: u64, anchor_descriptor: crate::lightning::events::bump_transaction::AnchorDescriptor, pending_htlcs: crate::c_types::derived::CVec_HTLCOutputInCommitmentZ) -> BumpTransactionEvent {
437 BumpTransactionEvent::ChannelClose {
439 package_target_feerate_sat_per_1000_weight,
441 commitment_tx_fee_satoshis,
447 /// Utility method to constructs a new HTLCResolution-variant BumpTransactionEvent
448 pub extern "C" fn BumpTransactionEvent_htlcresolution(claim_id: crate::c_types::ThirtyTwoBytes, target_feerate_sat_per_1000_weight: u32, htlc_descriptors: crate::c_types::derived::CVec_HTLCDescriptorZ, tx_lock_time: u32) -> BumpTransactionEvent {
449 BumpTransactionEvent::HTLCResolution {
451 target_feerate_sat_per_1000_weight,
456 /// Checks if two BumpTransactionEvents contain equal inner contents.
457 /// This ignores pointers and is_owned flags and looks at the values in fields.
459 pub extern "C" fn BumpTransactionEvent_eq(a: &BumpTransactionEvent, b: &BumpTransactionEvent) -> bool {
460 if &a.to_native() == &b.to_native() { true } else { false }
463 use lightning::events::bump_transaction::Input as nativeInputImport;
464 pub(crate) type nativeInput = nativeInputImport;
466 /// An input that must be included in a transaction when performing coin selection through
467 /// [`CoinSelectionSource::select_confirmed_utxos`]. It is guaranteed to be a SegWit input, so it
468 /// must have an empty [`TxIn::script_sig`] when spent.
472 /// A pointer to the opaque Rust object.
474 /// Nearly everywhere, inner must be non-null, however in places where
475 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
476 pub inner: *mut nativeInput,
477 /// Indicates that this is the only struct which contains the same pointer.
479 /// Rust functions which take ownership of an object provided via an argument require
480 /// this to be true and invalidate the object pointed to by inner.
484 impl Drop for Input {
486 if self.is_owned && !<*mut nativeInput>::is_null(self.inner) {
487 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
491 /// Frees any resources used by the Input, if is_owned is set and inner is non-NULL.
493 pub extern "C" fn Input_free(this_obj: Input) { }
495 /// Used only if an object of this type is returned as a trait impl by a method
496 pub(crate) extern "C" fn Input_free_void(this_ptr: *mut c_void) {
497 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInput) };
501 pub(crate) fn get_native_ref(&self) -> &'static nativeInput {
502 unsafe { &*ObjOps::untweak_ptr(self.inner) }
504 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInput {
505 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
507 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
508 pub(crate) fn take_inner(mut self) -> *mut nativeInput {
509 assert!(self.is_owned);
510 let ret = ObjOps::untweak_ptr(self.inner);
511 self.inner = core::ptr::null_mut();
515 /// The unique identifier of the input.
517 pub extern "C" fn Input_get_outpoint(this_ptr: &Input) -> crate::lightning::chain::transaction::OutPoint {
518 let mut inner_val = &mut this_ptr.get_native_mut_ref().outpoint;
519 crate::c_types::bitcoin_to_C_outpoint(inner_val)
521 /// The unique identifier of the input.
523 pub extern "C" fn Input_set_outpoint(this_ptr: &mut Input, mut val: crate::lightning::chain::transaction::OutPoint) {
524 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.outpoint = crate::c_types::C_to_bitcoin_outpoint(val);
526 /// The UTXO being spent by the input.
528 pub extern "C" fn Input_get_previous_utxo(this_ptr: &Input) -> crate::c_types::TxOut {
529 let mut inner_val = &mut this_ptr.get_native_mut_ref().previous_utxo;
530 crate::c_types::TxOut::from_rust(inner_val)
532 /// The UTXO being spent by the input.
534 pub extern "C" fn Input_set_previous_utxo(this_ptr: &mut Input, mut val: crate::c_types::TxOut) {
535 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.previous_utxo = val.into_rust();
537 /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and
538 /// [`TxIn::witness`], each with their lengths included, required to satisfy the output's
541 pub extern "C" fn Input_get_satisfaction_weight(this_ptr: &Input) -> u64 {
542 let mut inner_val = &mut this_ptr.get_native_mut_ref().satisfaction_weight;
545 /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and
546 /// [`TxIn::witness`], each with their lengths included, required to satisfy the output's
549 pub extern "C" fn Input_set_satisfaction_weight(this_ptr: &mut Input, mut val: u64) {
550 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.satisfaction_weight = val;
552 /// Constructs a new Input given each field
555 pub extern "C" fn Input_new(mut outpoint_arg: crate::lightning::chain::transaction::OutPoint, mut previous_utxo_arg: crate::c_types::TxOut, mut satisfaction_weight_arg: u64) -> Input {
556 Input { inner: ObjOps::heap_alloc(nativeInput {
557 outpoint: crate::c_types::C_to_bitcoin_outpoint(outpoint_arg),
558 previous_utxo: previous_utxo_arg.into_rust(),
559 satisfaction_weight: satisfaction_weight_arg,
562 impl Clone for Input {
563 fn clone(&self) -> Self {
565 inner: if <*mut nativeInput>::is_null(self.inner) { core::ptr::null_mut() } else {
566 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
572 /// Used only if an object of this type is returned as a trait impl by a method
573 pub(crate) extern "C" fn Input_clone_void(this_ptr: *const c_void) -> *mut c_void {
574 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeInput)).clone() })) as *mut c_void
577 /// Creates a copy of the Input
578 pub extern "C" fn Input_clone(orig: &Input) -> Input {
581 /// Generates a non-cryptographic 64-bit hash of the Input.
583 pub extern "C" fn Input_hash(o: &Input) -> u64 {
584 if o.inner.is_null() { return 0; }
585 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
587 let mut hasher = core::hash::SipHasher::new();
588 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
589 core::hash::Hasher::finish(&hasher)
591 /// Checks if two Inputs contain equal inner contents.
592 /// This ignores pointers and is_owned flags and looks at the values in fields.
593 /// Two objects with NULL inner values will be considered "equal" here.
595 pub extern "C" fn Input_eq(a: &Input, b: &Input) -> bool {
596 if a.inner == b.inner { return true; }
597 if a.inner.is_null() || b.inner.is_null() { return false; }
598 if a.get_native_ref() == b.get_native_ref() { true } else { false }
601 use lightning::events::bump_transaction::Utxo as nativeUtxoImport;
602 pub(crate) type nativeUtxo = nativeUtxoImport;
604 /// An unspent transaction output that is available to spend resulting from a successful
605 /// [`CoinSelection`] attempt.
609 /// A pointer to the opaque Rust object.
611 /// Nearly everywhere, inner must be non-null, however in places where
612 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
613 pub inner: *mut nativeUtxo,
614 /// Indicates that this is the only struct which contains the same pointer.
616 /// Rust functions which take ownership of an object provided via an argument require
617 /// this to be true and invalidate the object pointed to by inner.
623 if self.is_owned && !<*mut nativeUtxo>::is_null(self.inner) {
624 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
628 /// Frees any resources used by the Utxo, if is_owned is set and inner is non-NULL.
630 pub extern "C" fn Utxo_free(this_obj: Utxo) { }
632 /// Used only if an object of this type is returned as a trait impl by a method
633 pub(crate) extern "C" fn Utxo_free_void(this_ptr: *mut c_void) {
634 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeUtxo) };
638 pub(crate) fn get_native_ref(&self) -> &'static nativeUtxo {
639 unsafe { &*ObjOps::untweak_ptr(self.inner) }
641 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeUtxo {
642 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
644 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
645 pub(crate) fn take_inner(mut self) -> *mut nativeUtxo {
646 assert!(self.is_owned);
647 let ret = ObjOps::untweak_ptr(self.inner);
648 self.inner = core::ptr::null_mut();
652 /// The unique identifier of the output.
654 pub extern "C" fn Utxo_get_outpoint(this_ptr: &Utxo) -> crate::lightning::chain::transaction::OutPoint {
655 let mut inner_val = &mut this_ptr.get_native_mut_ref().outpoint;
656 crate::c_types::bitcoin_to_C_outpoint(inner_val)
658 /// The unique identifier of the output.
660 pub extern "C" fn Utxo_set_outpoint(this_ptr: &mut Utxo, mut val: crate::lightning::chain::transaction::OutPoint) {
661 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.outpoint = crate::c_types::C_to_bitcoin_outpoint(val);
663 /// The output to spend.
665 pub extern "C" fn Utxo_get_output(this_ptr: &Utxo) -> crate::c_types::TxOut {
666 let mut inner_val = &mut this_ptr.get_native_mut_ref().output;
667 crate::c_types::TxOut::from_rust(inner_val)
669 /// The output to spend.
671 pub extern "C" fn Utxo_set_output(this_ptr: &mut Utxo, mut val: crate::c_types::TxOut) {
672 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.output = val.into_rust();
674 /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and [`TxIn::witness`], each
675 /// with their lengths included, required to satisfy the output's script. The weight consumed by
676 /// the input's `script_sig` must account for [`WITNESS_SCALE_FACTOR`].
678 pub extern "C" fn Utxo_get_satisfaction_weight(this_ptr: &Utxo) -> u64 {
679 let mut inner_val = &mut this_ptr.get_native_mut_ref().satisfaction_weight;
682 /// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and [`TxIn::witness`], each
683 /// with their lengths included, required to satisfy the output's script. The weight consumed by
684 /// the input's `script_sig` must account for [`WITNESS_SCALE_FACTOR`].
686 pub extern "C" fn Utxo_set_satisfaction_weight(this_ptr: &mut Utxo, mut val: u64) {
687 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.satisfaction_weight = val;
689 /// Constructs a new Utxo given each field
692 pub extern "C" fn Utxo_new(mut outpoint_arg: crate::lightning::chain::transaction::OutPoint, mut output_arg: crate::c_types::TxOut, mut satisfaction_weight_arg: u64) -> Utxo {
693 Utxo { inner: ObjOps::heap_alloc(nativeUtxo {
694 outpoint: crate::c_types::C_to_bitcoin_outpoint(outpoint_arg),
695 output: output_arg.into_rust(),
696 satisfaction_weight: satisfaction_weight_arg,
699 impl Clone for Utxo {
700 fn clone(&self) -> Self {
702 inner: if <*mut nativeUtxo>::is_null(self.inner) { core::ptr::null_mut() } else {
703 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
709 /// Used only if an object of this type is returned as a trait impl by a method
710 pub(crate) extern "C" fn Utxo_clone_void(this_ptr: *const c_void) -> *mut c_void {
711 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeUtxo)).clone() })) as *mut c_void
714 /// Creates a copy of the Utxo
715 pub extern "C" fn Utxo_clone(orig: &Utxo) -> Utxo {
718 /// Generates a non-cryptographic 64-bit hash of the Utxo.
720 pub extern "C" fn Utxo_hash(o: &Utxo) -> u64 {
721 if o.inner.is_null() { return 0; }
722 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
724 let mut hasher = core::hash::SipHasher::new();
725 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
726 core::hash::Hasher::finish(&hasher)
728 /// Checks if two Utxos contain equal inner contents.
729 /// This ignores pointers and is_owned flags and looks at the values in fields.
730 /// Two objects with NULL inner values will be considered "equal" here.
732 pub extern "C" fn Utxo_eq(a: &Utxo, b: &Utxo) -> bool {
733 if a.inner == b.inner { return true; }
734 if a.inner.is_null() || b.inner.is_null() { return false; }
735 if a.get_native_ref() == b.get_native_ref() { true } else { false }
737 /// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output.
740 pub extern "C" fn Utxo_new_p2pkh(mut outpoint: crate::lightning::chain::transaction::OutPoint, mut value: u64, pubkey_hash: *const [u8; 20]) -> crate::lightning::events::bump_transaction::Utxo {
741 let mut ret = lightning::events::bump_transaction::Utxo::new_p2pkh(crate::c_types::C_to_bitcoin_outpoint(outpoint), value, &bitcoin::hash_types::PubkeyHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *pubkey_hash }.clone())));
742 crate::lightning::events::bump_transaction::Utxo { inner: ObjOps::heap_alloc(ret), is_owned: true }
746 use lightning::events::bump_transaction::CoinSelection as nativeCoinSelectionImport;
747 pub(crate) type nativeCoinSelection = nativeCoinSelectionImport;
749 /// The result of a successful coin selection attempt for a transaction requiring additional UTXOs
750 /// to cover its fees.
753 pub struct CoinSelection {
754 /// A pointer to the opaque Rust object.
756 /// Nearly everywhere, inner must be non-null, however in places where
757 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
758 pub inner: *mut nativeCoinSelection,
759 /// Indicates that this is the only struct which contains the same pointer.
761 /// Rust functions which take ownership of an object provided via an argument require
762 /// this to be true and invalidate the object pointed to by inner.
766 impl Drop for CoinSelection {
768 if self.is_owned && !<*mut nativeCoinSelection>::is_null(self.inner) {
769 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
773 /// Frees any resources used by the CoinSelection, if is_owned is set and inner is non-NULL.
775 pub extern "C" fn CoinSelection_free(this_obj: CoinSelection) { }
777 /// Used only if an object of this type is returned as a trait impl by a method
778 pub(crate) extern "C" fn CoinSelection_free_void(this_ptr: *mut c_void) {
779 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeCoinSelection) };
783 pub(crate) fn get_native_ref(&self) -> &'static nativeCoinSelection {
784 unsafe { &*ObjOps::untweak_ptr(self.inner) }
786 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeCoinSelection {
787 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
789 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
790 pub(crate) fn take_inner(mut self) -> *mut nativeCoinSelection {
791 assert!(self.is_owned);
792 let ret = ObjOps::untweak_ptr(self.inner);
793 self.inner = core::ptr::null_mut();
797 /// The set of UTXOs (with at least 1 confirmation) to spend and use within a transaction
798 /// requiring additional fees.
800 pub extern "C" fn CoinSelection_get_confirmed_utxos(this_ptr: &CoinSelection) -> crate::c_types::derived::CVec_UtxoZ {
801 let mut inner_val = &mut this_ptr.get_native_mut_ref().confirmed_utxos;
802 let mut local_inner_val = Vec::new(); for item in inner_val.iter() { local_inner_val.push( { crate::lightning::events::bump_transaction::Utxo { inner: unsafe { ObjOps::nonnull_ptr_to_inner((item as *const lightning::events::bump_transaction::Utxo<>) as *mut _) }, is_owned: false } }); };
803 local_inner_val.into()
805 /// The set of UTXOs (with at least 1 confirmation) to spend and use within a transaction
806 /// requiring additional fees.
808 pub extern "C" fn CoinSelection_set_confirmed_utxos(this_ptr: &mut CoinSelection, mut val: crate::c_types::derived::CVec_UtxoZ) {
809 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
810 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.confirmed_utxos = local_val;
812 /// An additional output tracking whether any change remained after coin selection. This output
813 /// should always have a value above dust for its given `script_pubkey`. It should not be
814 /// spent until the transaction it belongs to confirms to ensure mempool descendant limits are
815 /// not met. This implies no other party should be able to spend it except us.
817 pub extern "C" fn CoinSelection_get_change_output(this_ptr: &CoinSelection) -> crate::c_types::derived::COption_TxOutZ {
818 let mut inner_val = &mut this_ptr.get_native_mut_ref().change_output;
819 let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_TxOutZ::None } else { crate::c_types::derived::COption_TxOutZ::Some(/* WARNING: CLONING CONVERSION HERE! &Option<Enum> is otherwise un-expressable. */ { crate::c_types::TxOut::from_rust(&(*inner_val.as_ref().unwrap()).clone()) }) };
822 /// An additional output tracking whether any change remained after coin selection. This output
823 /// should always have a value above dust for its given `script_pubkey`. It should not be
824 /// spent until the transaction it belongs to confirms to ensure mempool descendant limits are
825 /// not met. This implies no other party should be able to spend it except us.
827 pub extern "C" fn CoinSelection_set_change_output(this_ptr: &mut CoinSelection, mut val: crate::c_types::derived::COption_TxOutZ) {
828 let mut local_val = { /*val*/ let val_opt = val; if val_opt.is_none() { None } else { Some({ { { val_opt.take() }.into_rust() }})} };
829 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.change_output = local_val;
831 /// Constructs a new CoinSelection given each field
834 pub extern "C" fn CoinSelection_new(mut confirmed_utxos_arg: crate::c_types::derived::CVec_UtxoZ, mut change_output_arg: crate::c_types::derived::COption_TxOutZ) -> CoinSelection {
835 let mut local_confirmed_utxos_arg = Vec::new(); for mut item in confirmed_utxos_arg.into_rust().drain(..) { local_confirmed_utxos_arg.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
836 let mut local_change_output_arg = { /*change_output_arg*/ let change_output_arg_opt = change_output_arg; if change_output_arg_opt.is_none() { None } else { Some({ { { change_output_arg_opt.take() }.into_rust() }})} };
837 CoinSelection { inner: ObjOps::heap_alloc(nativeCoinSelection {
838 confirmed_utxos: local_confirmed_utxos_arg,
839 change_output: local_change_output_arg,
842 impl Clone for CoinSelection {
843 fn clone(&self) -> Self {
845 inner: if <*mut nativeCoinSelection>::is_null(self.inner) { core::ptr::null_mut() } else {
846 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
852 /// Used only if an object of this type is returned as a trait impl by a method
853 pub(crate) extern "C" fn CoinSelection_clone_void(this_ptr: *const c_void) -> *mut c_void {
854 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeCoinSelection)).clone() })) as *mut c_void
857 /// Creates a copy of the CoinSelection
858 pub extern "C" fn CoinSelection_clone(orig: &CoinSelection) -> CoinSelection {
861 /// An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
862 /// sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
863 /// which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
864 /// which can provide a default implementation of this trait when used with [`Wallet`].
866 pub struct CoinSelectionSource {
867 /// An opaque pointer which is passed to your function implementations as an argument.
868 /// This has no meaning in the LDK, and can be NULL or any other value.
869 pub this_arg: *mut c_void,
870 /// Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
871 /// available to spend. Implementations are free to pick their coin selection algorithm of
872 /// choice, as long as the following requirements are met:
874 /// 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
875 /// throughout coin selection, but must not be returned as part of the result.
876 /// 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
877 /// throughout coin selection. In some cases, like when funding an anchor transaction, this
878 /// set is empty. Implementations should ensure they handle this correctly on their end,
879 /// e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
880 /// provided, in which case a zero-value empty OP_RETURN output can be used instead.
881 /// 3. Enough inputs must be selected/contributed for the resulting transaction (including the
882 /// inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
884 /// Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
885 /// the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
886 /// providing the full input weight. Failing to do so may lead to underestimating fee bumps and
887 /// delaying block inclusion.
889 /// The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
890 /// can be re-used within new fee-bumped iterations of the original claiming transaction,
891 /// ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
892 /// transaction associated with it, and all of the available UTXOs have already been assigned to
893 /// other claims, implementations must be willing to double spend their UTXOs. The choice of
894 /// which UTXOs to double spend is left to the implementation, but it must strive to keep the
895 /// set of other claims being double spent to a minimum.
896 pub select_confirmed_utxos: extern "C" fn (this_arg: *const c_void, claim_id: crate::c_types::ThirtyTwoBytes, must_spend: crate::c_types::derived::CVec_InputZ, must_pay_to: crate::c_types::derived::CVec_TxOutZ, target_feerate_sat_per_1000_weight: u32) -> crate::c_types::derived::CResult_CoinSelectionNoneZ,
897 /// Signs and provides the full witness for all inputs within the transaction known to the
898 /// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
899 pub sign_tx: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_TransactionNoneZ,
900 /// Frees any resources associated with this object given its this_arg pointer.
901 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
902 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
904 unsafe impl Send for CoinSelectionSource {}
905 unsafe impl Sync for CoinSelectionSource {}
907 pub(crate) fn CoinSelectionSource_clone_fields(orig: &CoinSelectionSource) -> CoinSelectionSource {
908 CoinSelectionSource {
909 this_arg: orig.this_arg,
910 select_confirmed_utxos: Clone::clone(&orig.select_confirmed_utxos),
911 sign_tx: Clone::clone(&orig.sign_tx),
912 free: Clone::clone(&orig.free),
916 use lightning::events::bump_transaction::CoinSelectionSource as rustCoinSelectionSource;
917 impl rustCoinSelectionSource for CoinSelectionSource {
918 fn select_confirmed_utxos(&self, mut claim_id: lightning::chain::ClaimId, mut must_spend: Vec<lightning::events::bump_transaction::Input>, mut must_pay_to: &[bitcoin::TxOut], mut target_feerate_sat_per_1000_weight: u32) -> Result<lightning::events::bump_transaction::CoinSelection, ()> {
919 let mut local_must_spend = Vec::new(); for mut item in must_spend.drain(..) { local_must_spend.push( { crate::lightning::events::bump_transaction::Input { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
920 let mut local_must_pay_to_clone = Vec::new(); local_must_pay_to_clone.extend_from_slice(must_pay_to); let mut must_pay_to = local_must_pay_to_clone; let mut local_must_pay_to = Vec::new(); for mut item in must_pay_to.drain(..) { local_must_pay_to.push( { crate::c_types::TxOut::from_rust(&item) }); };
921 let mut ret = (self.select_confirmed_utxos)(self.this_arg, crate::c_types::ThirtyTwoBytes { data: claim_id.0 }, local_must_spend.into(), local_must_pay_to.into(), target_feerate_sat_per_1000_weight);
922 let mut local_ret = match ret.result_ok { true => Ok( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).take_inner()) } }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
925 fn sign_tx(&self, mut tx: bitcoin::Transaction) -> Result<bitcoin::Transaction, ()> {
926 let mut ret = (self.sign_tx)(self.this_arg, crate::c_types::Transaction::from_bitcoin(&tx));
927 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_bitcoin() }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
932 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
933 // directly as a Deref trait in higher-level structs:
934 impl core::ops::Deref for CoinSelectionSource {
936 fn deref(&self) -> &Self {
940 impl core::ops::DerefMut for CoinSelectionSource {
941 fn deref_mut(&mut self) -> &mut Self {
945 /// Calls the free function if one is set
947 pub extern "C" fn CoinSelectionSource_free(this_ptr: CoinSelectionSource) { }
948 impl Drop for CoinSelectionSource {
950 if let Some(f) = self.free {
955 /// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
956 /// provide a default implementation to [`CoinSelectionSource`].
958 pub struct WalletSource {
959 /// An opaque pointer which is passed to your function implementations as an argument.
960 /// This has no meaning in the LDK, and can be NULL or any other value.
961 pub this_arg: *mut c_void,
962 /// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
963 pub list_confirmed_utxos: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CResult_CVec_UtxoZNoneZ,
964 /// Returns a script to use for change above dust resulting from a successful coin selection
966 pub get_change_script: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CResult_CVec_u8ZNoneZ,
967 /// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
968 /// the transaction known to the wallet (i.e., any provided via
969 /// [`WalletSource::list_confirmed_utxos`]).
970 pub sign_tx: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_TransactionNoneZ,
971 /// Frees any resources associated with this object given its this_arg pointer.
972 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
973 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
975 unsafe impl Send for WalletSource {}
976 unsafe impl Sync for WalletSource {}
978 pub(crate) fn WalletSource_clone_fields(orig: &WalletSource) -> WalletSource {
980 this_arg: orig.this_arg,
981 list_confirmed_utxos: Clone::clone(&orig.list_confirmed_utxos),
982 get_change_script: Clone::clone(&orig.get_change_script),
983 sign_tx: Clone::clone(&orig.sign_tx),
984 free: Clone::clone(&orig.free),
988 use lightning::events::bump_transaction::WalletSource as rustWalletSource;
989 impl rustWalletSource for WalletSource {
990 fn list_confirmed_utxos(&self) -> Result<Vec<lightning::events::bump_transaction::Utxo>, ()> {
991 let mut ret = (self.list_confirmed_utxos)(self.this_arg);
992 let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = Vec::new(); for mut item in (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust().drain(..) { local_ret_0.push( { *unsafe { Box::from_raw(item.take_inner()) } }); }; local_ret_0 }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
995 fn get_change_script(&self) -> Result<bitcoin::Script, ()> {
996 let mut ret = (self.get_change_script)(self.this_arg);
997 let mut local_ret = match ret.result_ok { true => Ok( { ::bitcoin::blockdata::script::Script::from((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust()) }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
1000 fn sign_tx(&self, mut tx: bitcoin::Transaction) -> Result<bitcoin::Transaction, ()> {
1001 let mut ret = (self.sign_tx)(self.this_arg, crate::c_types::Transaction::from_bitcoin(&tx));
1002 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_bitcoin() }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
1007 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
1008 // directly as a Deref trait in higher-level structs:
1009 impl core::ops::Deref for WalletSource {
1011 fn deref(&self) -> &Self {
1015 impl core::ops::DerefMut for WalletSource {
1016 fn deref_mut(&mut self) -> &mut Self {
1020 /// Calls the free function if one is set
1022 pub extern "C" fn WalletSource_free(this_ptr: WalletSource) { }
1023 impl Drop for WalletSource {
1024 fn drop(&mut self) {
1025 if let Some(f) = self.free {
1031 use lightning::events::bump_transaction::Wallet as nativeWalletImport;
1032 pub(crate) type nativeWallet = nativeWalletImport<crate::lightning::events::bump_transaction::WalletSource, crate::lightning::util::logger::Logger>;
1034 /// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
1035 /// avoid conflicting double spends. If not enough UTXOs are available to do so, conflicting double
1036 /// spends may happen.
1040 /// A pointer to the opaque Rust object.
1042 /// Nearly everywhere, inner must be non-null, however in places where
1043 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1044 pub inner: *mut nativeWallet,
1045 /// Indicates that this is the only struct which contains the same pointer.
1047 /// Rust functions which take ownership of an object provided via an argument require
1048 /// this to be true and invalidate the object pointed to by inner.
1052 impl Drop for Wallet {
1053 fn drop(&mut self) {
1054 if self.is_owned && !<*mut nativeWallet>::is_null(self.inner) {
1055 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1059 /// Frees any resources used by the Wallet, if is_owned is set and inner is non-NULL.
1061 pub extern "C" fn Wallet_free(this_obj: Wallet) { }
1063 /// Used only if an object of this type is returned as a trait impl by a method
1064 pub(crate) extern "C" fn Wallet_free_void(this_ptr: *mut c_void) {
1065 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeWallet) };
1069 pub(crate) fn get_native_ref(&self) -> &'static nativeWallet {
1070 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1072 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeWallet {
1073 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1075 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1076 pub(crate) fn take_inner(mut self) -> *mut nativeWallet {
1077 assert!(self.is_owned);
1078 let ret = ObjOps::untweak_ptr(self.inner);
1079 self.inner = core::ptr::null_mut();
1083 /// Returns a new instance backed by the given [`WalletSource`] that serves as an implementation
1084 /// of [`CoinSelectionSource`].
1087 pub extern "C" fn Wallet_new(mut source: crate::lightning::events::bump_transaction::WalletSource, mut logger: crate::lightning::util::logger::Logger) -> crate::lightning::events::bump_transaction::Wallet {
1088 let mut ret = lightning::events::bump_transaction::Wallet::new(source, logger);
1089 crate::lightning::events::bump_transaction::Wallet { inner: ObjOps::heap_alloc(ret), is_owned: true }
1092 impl From<nativeWallet> for crate::lightning::events::bump_transaction::CoinSelectionSource {
1093 fn from(obj: nativeWallet) -> Self {
1094 let rust_obj = crate::lightning::events::bump_transaction::Wallet { inner: ObjOps::heap_alloc(obj), is_owned: true };
1095 let mut ret = Wallet_as_CoinSelectionSource(&rust_obj);
1096 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn
1097 core::mem::forget(rust_obj);
1098 ret.free = Some(Wallet_free_void);
1102 /// Constructs a new CoinSelectionSource which calls the relevant methods on this_arg.
1103 /// This copies the `inner` pointer in this_arg and thus the returned CoinSelectionSource must be freed before this_arg is
1105 pub extern "C" fn Wallet_as_CoinSelectionSource(this_arg: &Wallet) -> crate::lightning::events::bump_transaction::CoinSelectionSource {
1106 crate::lightning::events::bump_transaction::CoinSelectionSource {
1107 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
1109 select_confirmed_utxos: Wallet_CoinSelectionSource_select_confirmed_utxos,
1110 sign_tx: Wallet_CoinSelectionSource_sign_tx,
1115 extern "C" fn Wallet_CoinSelectionSource_select_confirmed_utxos(this_arg: *const c_void, mut claim_id: crate::c_types::ThirtyTwoBytes, mut must_spend: crate::c_types::derived::CVec_InputZ, mut must_pay_to: crate::c_types::derived::CVec_TxOutZ, mut target_feerate_sat_per_1000_weight: u32) -> crate::c_types::derived::CResult_CoinSelectionNoneZ {
1116 let mut local_must_spend = Vec::new(); for mut item in must_spend.into_rust().drain(..) { local_must_spend.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
1117 let mut local_must_pay_to = Vec::new(); for mut item in must_pay_to.into_rust().drain(..) { local_must_pay_to.push( { item.into_rust() }); };
1118 let mut ret = <nativeWallet as lightning::events::bump_transaction::CoinSelectionSource<>>::select_confirmed_utxos(unsafe { &mut *(this_arg as *mut nativeWallet) }, ::lightning::chain::ClaimId(claim_id.data), local_must_spend, &local_must_pay_to[..], target_feerate_sat_per_1000_weight);
1119 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::events::bump_transaction::CoinSelection { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
1123 extern "C" fn Wallet_CoinSelectionSource_sign_tx(this_arg: *const c_void, mut tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_TransactionNoneZ {
1124 let mut ret = <nativeWallet as lightning::events::bump_transaction::CoinSelectionSource<>>::sign_tx(unsafe { &mut *(this_arg as *mut nativeWallet) }, tx.into_bitcoin());
1125 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Transaction::from_bitcoin(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
1130 use lightning::events::bump_transaction::BumpTransactionEventHandler as nativeBumpTransactionEventHandlerImport;
1131 pub(crate) type nativeBumpTransactionEventHandler = nativeBumpTransactionEventHandlerImport<crate::lightning::chain::chaininterface::BroadcasterInterface, crate::lightning::events::bump_transaction::CoinSelectionSource, crate::lightning::sign::SignerProvider, crate::lightning::util::logger::Logger>;
1133 /// A handler for [`Event::BumpTransaction`] events that sources confirmed UTXOs from a
1134 /// [`CoinSelectionSource`] to fee bump transactions via Child-Pays-For-Parent (CPFP) or
1135 /// Replace-By-Fee (RBF).
1137 /// [`Event::BumpTransaction`]: crate::events::Event::BumpTransaction
1140 pub struct BumpTransactionEventHandler {
1141 /// A pointer to the opaque Rust object.
1143 /// Nearly everywhere, inner must be non-null, however in places where
1144 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1145 pub inner: *mut nativeBumpTransactionEventHandler,
1146 /// Indicates that this is the only struct which contains the same pointer.
1148 /// Rust functions which take ownership of an object provided via an argument require
1149 /// this to be true and invalidate the object pointed to by inner.
1153 impl Drop for BumpTransactionEventHandler {
1154 fn drop(&mut self) {
1155 if self.is_owned && !<*mut nativeBumpTransactionEventHandler>::is_null(self.inner) {
1156 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1160 /// Frees any resources used by the BumpTransactionEventHandler, if is_owned is set and inner is non-NULL.
1162 pub extern "C" fn BumpTransactionEventHandler_free(this_obj: BumpTransactionEventHandler) { }
1164 /// Used only if an object of this type is returned as a trait impl by a method
1165 pub(crate) extern "C" fn BumpTransactionEventHandler_free_void(this_ptr: *mut c_void) {
1166 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBumpTransactionEventHandler) };
1169 impl BumpTransactionEventHandler {
1170 pub(crate) fn get_native_ref(&self) -> &'static nativeBumpTransactionEventHandler {
1171 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1173 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBumpTransactionEventHandler {
1174 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1176 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1177 pub(crate) fn take_inner(mut self) -> *mut nativeBumpTransactionEventHandler {
1178 assert!(self.is_owned);
1179 let ret = ObjOps::untweak_ptr(self.inner);
1180 self.inner = core::ptr::null_mut();
1184 /// Returns a new instance capable of handling [`Event::BumpTransaction`] events.
1186 /// [`Event::BumpTransaction`]: crate::events::Event::BumpTransaction
1189 pub extern "C" fn BumpTransactionEventHandler_new(mut broadcaster: crate::lightning::chain::chaininterface::BroadcasterInterface, mut utxo_source: crate::lightning::events::bump_transaction::CoinSelectionSource, mut signer_provider: crate::lightning::sign::SignerProvider, mut logger: crate::lightning::util::logger::Logger) -> crate::lightning::events::bump_transaction::BumpTransactionEventHandler {
1190 let mut ret = lightning::events::bump_transaction::BumpTransactionEventHandler::new(broadcaster, utxo_source, signer_provider, logger);
1191 crate::lightning::events::bump_transaction::BumpTransactionEventHandler { inner: ObjOps::heap_alloc(ret), is_owned: true }
1194 /// Handles all variants of [`BumpTransactionEvent`].
1196 pub extern "C" fn BumpTransactionEventHandler_handle_event(this_arg: &crate::lightning::events::bump_transaction::BumpTransactionEventHandler, event: &crate::lightning::events::bump_transaction::BumpTransactionEvent) {
1197 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.handle_event(&event.to_native())