Merge pull request #18 from TheBlueMatt/2021-04-invoice-incl
[ldk-c-bindings] / lightning-c-bindings / src / lightning / chain / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
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.
8
9 //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
10
11 use std::str::FromStr;
12 use std::ffi::c_void;
13 use bitcoin::hashes::Hash;
14 use crate::c_types::*;
15
16 pub mod chaininterface;
17 pub mod chainmonitor;
18 pub mod channelmonitor;
19 pub mod transaction;
20 pub mod keysinterface;
21 /// An error when accessing the chain via [`Access`].
22 #[must_use]
23 #[derive(Clone)]
24 #[repr(C)]
25 pub enum AccessError {
26         /// The requested chain is unknown.
27         UnknownChain,
28         /// The requested transaction doesn't exist or hasn't confirmed.
29         UnknownTx,
30 }
31 use lightning::chain::AccessError as nativeAccessError;
32 impl AccessError {
33         #[allow(unused)]
34         pub(crate) fn to_native(&self) -> nativeAccessError {
35                 match self {
36                         AccessError::UnknownChain => nativeAccessError::UnknownChain,
37                         AccessError::UnknownTx => nativeAccessError::UnknownTx,
38                 }
39         }
40         #[allow(unused)]
41         pub(crate) fn into_native(self) -> nativeAccessError {
42                 match self {
43                         AccessError::UnknownChain => nativeAccessError::UnknownChain,
44                         AccessError::UnknownTx => nativeAccessError::UnknownTx,
45                 }
46         }
47         #[allow(unused)]
48         pub(crate) fn from_native(native: &nativeAccessError) -> Self {
49                 match native {
50                         nativeAccessError::UnknownChain => AccessError::UnknownChain,
51                         nativeAccessError::UnknownTx => AccessError::UnknownTx,
52                 }
53         }
54         #[allow(unused)]
55         pub(crate) fn native_into(native: nativeAccessError) -> Self {
56                 match native {
57                         nativeAccessError::UnknownChain => AccessError::UnknownChain,
58                         nativeAccessError::UnknownTx => AccessError::UnknownTx,
59                 }
60         }
61 }
62 /// Creates a copy of the AccessError
63 #[no_mangle]
64 pub extern "C" fn AccessError_clone(orig: &AccessError) -> AccessError {
65         orig.clone()
66 }
67 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
68 /// UTXOs.
69 #[repr(C)]
70 pub struct Access {
71         /// An opaque pointer which is passed to your function implementations as an argument.
72         /// This has no meaning in the LDK, and can be NULL or any other value.
73         pub this_arg: *mut c_void,
74         /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
75         /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
76         /// is unknown.
77         ///
78         /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
79         #[must_use]
80         pub get_utxo: extern "C" fn (this_arg: *const c_void, genesis_hash: *const [u8; 32], short_channel_id: u64) -> crate::c_types::derived::CResult_TxOutAccessErrorZ,
81         /// Frees any resources associated with this object given its this_arg pointer.
82         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
83         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
84 }
85 unsafe impl Send for Access {}
86 unsafe impl Sync for Access {}
87
88 use lightning::chain::Access as rustAccess;
89 impl rustAccess for Access {
90         fn get_utxo(&self, genesis_hash: &bitcoin::hash_types::BlockHash, short_channel_id: u64) -> Result<bitcoin::blockdata::transaction::TxOut, lightning::chain::AccessError> {
91                 let mut ret = (self.get_utxo)(self.this_arg, genesis_hash.as_inner(), short_channel_id);
92                 let mut local_ret = match ret.result_ok { true => Ok( { (*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)) }).into_native() })};
93                 local_ret
94         }
95 }
96
97 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
98 // directly as a Deref trait in higher-level structs:
99 impl std::ops::Deref for Access {
100         type Target = Self;
101         fn deref(&self) -> &Self {
102                 self
103         }
104 }
105 /// Calls the free function if one is set
106 #[no_mangle]
107 pub extern "C" fn Access_free(this_ptr: Access) { }
108 impl Drop for Access {
109         fn drop(&mut self) {
110                 if let Some(f) = self.free {
111                         f(self.this_arg);
112                 }
113         }
114 }
115 /// The `Listen` trait is used to notify when blocks have been connected or disconnected from the
116 /// chain.
117 ///
118 /// Useful when needing to replay chain data upon startup or as new chain events occur. Clients
119 /// sourcing chain data using a block-oriented API should prefer this interface over [`Confirm`].
120 /// Such clients fetch the entire header chain whereas clients using [`Confirm`] only fetch headers
121 /// when needed.
122 #[repr(C)]
123 pub struct Listen {
124         /// An opaque pointer which is passed to your function implementations as an argument.
125         /// This has no meaning in the LDK, and can be NULL or any other value.
126         pub this_arg: *mut c_void,
127         /// Notifies the listener that a block was added at the given height.
128         pub block_connected: extern "C" fn (this_arg: *const c_void, block: crate::c_types::u8slice, height: u32),
129         /// Notifies the listener that a block was removed at the given height.
130         pub block_disconnected: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32),
131         /// Frees any resources associated with this object given its this_arg pointer.
132         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
133         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
134 }
135
136 use lightning::chain::Listen as rustListen;
137 impl rustListen for Listen {
138         fn block_connected(&self, block: &bitcoin::blockdata::block::Block, height: u32) {
139                 let mut local_block = ::bitcoin::consensus::encode::serialize(block);
140                 (self.block_connected)(self.this_arg, crate::c_types::u8slice::from_slice(&local_block), height)
141         }
142         fn block_disconnected(&self, header: &bitcoin::blockdata::block::BlockHeader, height: u32) {
143                 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
144                 (self.block_disconnected)(self.this_arg, &local_header, height)
145         }
146 }
147
148 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
149 // directly as a Deref trait in higher-level structs:
150 impl std::ops::Deref for Listen {
151         type Target = Self;
152         fn deref(&self) -> &Self {
153                 self
154         }
155 }
156 /// Calls the free function if one is set
157 #[no_mangle]
158 pub extern "C" fn Listen_free(this_ptr: Listen) { }
159 impl Drop for Listen {
160         fn drop(&mut self) {
161                 if let Some(f) = self.free {
162                         f(self.this_arg);
163                 }
164         }
165 }
166 /// The `Confirm` trait is used to notify when transactions have been confirmed on chain or
167 /// unconfirmed during a chain reorganization.
168 ///
169 /// Clients sourcing chain data using a transaction-oriented API should prefer this interface over
170 /// [`Listen`]. For instance, an Electrum client may implement [`Filter`] by subscribing to activity
171 /// related to registered transactions and outputs. Upon notification, it would pass along the
172 /// matching transactions using this interface.
173 ///
174 /// # Use
175 ///
176 /// The intended use is as follows:
177 /// - Call [`transactions_confirmed`] to process any on-chain activity of interest.
178 /// - Call [`transaction_unconfirmed`] to process any transaction returned by [`get_relevant_txids`]
179 ///   that has been reorganized out of the chain.
180 /// - Call [`best_block_updated`] whenever a new chain tip becomes available.
181 ///
182 /// # Order
183 ///
184 /// Clients must call these methods in chain order. Specifically:
185 /// - Transactions confirmed in a block must be given before transactions confirmed in a later
186 ///   block.
187 /// - Dependent transactions within the same block must be given in topological order, possibly in
188 ///   separate calls.
189 /// - Unconfirmed transactions must be given after the original confirmations and before any
190 ///   reconfirmation.
191 ///
192 /// See individual method documentation for further details.
193 ///
194 /// [`transactions_confirmed`]: Self::transactions_confirmed
195 /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
196 /// [`best_block_updated`]: Self::best_block_updated
197 /// [`get_relevant_txids`]: Self::get_relevant_txids
198 #[repr(C)]
199 pub struct Confirm {
200         /// An opaque pointer which is passed to your function implementations as an argument.
201         /// This has no meaning in the LDK, and can be NULL or any other value.
202         pub this_arg: *mut c_void,
203         /// Processes transactions confirmed in a block with a given header and height.
204         ///
205         /// Should be called for any transactions registered by [`Filter::register_tx`] or any
206         /// transactions spending an output registered by [`Filter::register_output`]. Such transactions
207         /// appearing in the same block do not need to be included in the same call; instead, multiple
208         /// calls with additional transactions may be made so long as they are made in [chain order].
209         ///
210         /// May be called before or after [`best_block_updated`] for the corresponding block. However,
211         /// in the event of a chain reorganization, it must not be called with a `header` that is no
212         /// longer in the chain as of the last call to [`best_block_updated`].
213         ///
214         /// [chain order]: Confirm#Order
215         /// [`best_block_updated`]: Self::best_block_updated
216         pub transactions_confirmed: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], txdata: crate::c_types::derived::CVec_C2Tuple_usizeTransactionZZ, height: u32),
217         /// Processes a transaction that is no longer confirmed as result of a chain reorganization.
218         ///
219         /// Should be called for any transaction returned by [`get_relevant_txids`] if it has been
220         /// reorganized out of the best chain. Once called, the given transaction should not be returned
221         /// by [`get_relevant_txids`] unless it has been reconfirmed via [`transactions_confirmed`].
222         ///
223         /// [`get_relevant_txids`]: Self::get_relevant_txids
224         /// [`transactions_confirmed`]: Self::transactions_confirmed
225         pub transaction_unconfirmed: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32]),
226         /// Processes an update to the best header connected at the given height.
227         ///
228         /// Should be called when a new header is available but may be skipped for intermediary blocks
229         /// if they become available at the same time.
230         pub best_block_updated: extern "C" fn (this_arg: *const c_void, header: *const [u8; 80], height: u32),
231         /// Returns transactions that should be monitored for reorganization out of the chain.
232         ///
233         /// Should include any transactions passed to [`transactions_confirmed`] that have insufficient
234         /// confirmations to be safe from a chain reorganization. Should not include any transactions
235         /// passed to [`transaction_unconfirmed`] unless later reconfirmed.
236         ///
237         /// May be called to determine the subset of transactions that must still be monitored for
238         /// reorganization. Will be idempotent between calls but may change as a result of calls to the
239         /// other interface methods. Thus, this is useful to determine which transactions may need to be
240         /// given to [`transaction_unconfirmed`].
241         ///
242         /// [`transactions_confirmed`]: Self::transactions_confirmed
243         /// [`transaction_unconfirmed`]: Self::transaction_unconfirmed
244         #[must_use]
245         pub get_relevant_txids: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_TxidZ,
246         /// Frees any resources associated with this object given its this_arg pointer.
247         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
248         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
249 }
250
251 use lightning::chain::Confirm as rustConfirm;
252 impl rustConfirm for Confirm {
253         fn transactions_confirmed(&self, header: &bitcoin::blockdata::block::BlockHeader, txdata: &lightning::chain::transaction::TransactionData, height: u32) {
254                 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
255                 let mut local_txdata = Vec::new(); for item in txdata.iter() { local_txdata.push( { let (mut orig_txdata_0_0, mut orig_txdata_0_1) = *item; let mut local_txdata_0 = (orig_txdata_0_0, crate::c_types::Transaction::from_bitcoin(&orig_txdata_0_1)).into(); local_txdata_0 }); };
256                 (self.transactions_confirmed)(self.this_arg, &local_header, local_txdata.into(), height)
257         }
258         fn transaction_unconfirmed(&self, txid: &bitcoin::hash_types::Txid) {
259                 (self.transaction_unconfirmed)(self.this_arg, txid.as_inner())
260         }
261         fn best_block_updated(&self, header: &bitcoin::blockdata::block::BlockHeader, height: u32) {
262                 let mut local_header = { let mut s = [0u8; 80]; s[..].copy_from_slice(&::bitcoin::consensus::encode::serialize(header)); s };
263                 (self.best_block_updated)(self.this_arg, &local_header, height)
264         }
265         fn get_relevant_txids(&self) -> Vec<bitcoin::hash_types::Txid> {
266                 let mut ret = (self.get_relevant_txids)(self.this_arg);
267                 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { ::bitcoin::hash_types::Txid::from_slice(&item.data[..]).unwrap() }); };
268                 local_ret
269         }
270 }
271
272 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
273 // directly as a Deref trait in higher-level structs:
274 impl std::ops::Deref for Confirm {
275         type Target = Self;
276         fn deref(&self) -> &Self {
277                 self
278         }
279 }
280 /// Calls the free function if one is set
281 #[no_mangle]
282 pub extern "C" fn Confirm_free(this_ptr: Confirm) { }
283 impl Drop for Confirm {
284         fn drop(&mut self) {
285                 if let Some(f) = self.free {
286                         f(self.this_arg);
287                 }
288         }
289 }
290 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
291 /// blocks are connected and disconnected.
292 ///
293 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
294 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
295 /// channel state changes and HTLCs are resolved. See method documentation for specific
296 /// requirements.
297 ///
298 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
299 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
300 /// without taking any further action such as persisting the current state.
301 ///
302 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
303 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
304 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
305 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
306 /// multiple instances.
307 ///
308 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
309 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
310 /// [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
311 #[repr(C)]
312 pub struct Watch {
313         /// An opaque pointer which is passed to your function implementations as an argument.
314         /// This has no meaning in the LDK, and can be NULL or any other value.
315         pub this_arg: *mut c_void,
316         /// Watches a channel identified by `funding_txo` using `monitor`.
317         ///
318         /// Implementations are responsible for watching the chain for the funding transaction along
319         /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
320         /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
321         ///
322         /// [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
323         /// [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
324         /// [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
325         #[must_use]
326         pub watch_channel: extern "C" fn (this_arg: *const c_void, funding_txo: crate::lightning::chain::transaction::OutPoint, monitor: crate::lightning::chain::channelmonitor::ChannelMonitor) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
327         /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
328         ///
329         /// Implementations must call [`update_monitor`] with the given update. See
330         /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
331         ///
332         /// [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
333         /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
334         #[must_use]
335         pub update_channel: extern "C" fn (this_arg: *const c_void, funding_txo: crate::lightning::chain::transaction::OutPoint, update: crate::lightning::chain::channelmonitor::ChannelMonitorUpdate) -> crate::c_types::derived::CResult_NoneChannelMonitorUpdateErrZ,
336         /// Returns any monitor events since the last call. Subsequent calls must only return new
337         /// events.
338         #[must_use]
339         pub release_pending_monitor_events: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_MonitorEventZ,
340         /// Frees any resources associated with this object given its this_arg pointer.
341         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
342         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
343 }
344 unsafe impl Send for Watch {}
345 unsafe impl Sync for Watch {}
346
347 use lightning::chain::Watch as rustWatch;
348 impl rustWatch<crate::lightning::chain::keysinterface::Sign> for Watch {
349         fn watch_channel(&self, funding_txo: lightning::chain::transaction::OutPoint, monitor: lightning::chain::channelmonitor::ChannelMonitor<crate::lightning::chain::keysinterface::Sign>) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
350                 let mut ret = (self.watch_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::lightning::chain::channelmonitor::ChannelMonitor { inner: Box::into_raw(Box::new(monitor)), is_owned: true });
351                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
352                 local_ret
353         }
354         fn update_channel(&self, funding_txo: lightning::chain::transaction::OutPoint, update: lightning::chain::channelmonitor::ChannelMonitorUpdate) -> Result<(), lightning::chain::channelmonitor::ChannelMonitorUpdateErr> {
355                 let mut ret = (self.update_channel)(self.this_arg, crate::lightning::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(funding_txo)), is_owned: true }, crate::lightning::chain::channelmonitor::ChannelMonitorUpdate { inner: Box::into_raw(Box::new(update)), is_owned: true });
356                 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
357                 local_ret
358         }
359         fn release_pending_monitor_events(&self) -> Vec<lightning::chain::channelmonitor::MonitorEvent> {
360                 let mut ret = (self.release_pending_monitor_events)(self.this_arg);
361                 let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { item.into_native() }); };
362                 local_ret
363         }
364 }
365
366 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
367 // directly as a Deref trait in higher-level structs:
368 impl std::ops::Deref for Watch {
369         type Target = Self;
370         fn deref(&self) -> &Self {
371                 self
372         }
373 }
374 /// Calls the free function if one is set
375 #[no_mangle]
376 pub extern "C" fn Watch_free(this_ptr: Watch) { }
377 impl Drop for Watch {
378         fn drop(&mut self) {
379                 if let Some(f) = self.free {
380                         f(self.this_arg);
381                 }
382         }
383 }
384 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
385 /// channels.
386 ///
387 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
388 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
389 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
390 /// receiving full blocks from a chain source, any further filtering is unnecessary.
391 ///
392 /// After an output has been registered, subsequent block retrievals from the chain source must not
393 /// exclude any transactions matching the new criteria nor any in-block descendants of such
394 /// transactions.
395 ///
396 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
397 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
398 /// processed later. Then, in order to block until the data has been processed, any [`Watch`]
399 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
400 ///
401 /// [`TemporaryFailure`]: channelmonitor::ChannelMonitorUpdateErr::TemporaryFailure
402 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
403 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
404 #[repr(C)]
405 pub struct Filter {
406         /// An opaque pointer which is passed to your function implementations as an argument.
407         /// This has no meaning in the LDK, and can be NULL or any other value.
408         pub this_arg: *mut c_void,
409         /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
410         /// a spending condition.
411         pub register_tx: extern "C" fn (this_arg: *const c_void, txid: *const [u8; 32], script_pubkey: crate::c_types::u8slice),
412         /// Registers interest in spends of a transaction output.
413         ///
414         /// Optionally, when `output.block_hash` is set, should return any transaction spending the
415         /// output that is found in the corresponding block along with its index.
416         ///
417         /// This return value is useful for Electrum clients in order to supply in-block descendant
418         /// transactions which otherwise were not included. This is not necessary for other clients if
419         /// such descendant transactions were already included (e.g., when a BIP 157 client provides the
420         /// full block).
421         #[must_use]
422         pub register_output: extern "C" fn (this_arg: *const c_void, output: crate::lightning::chain::WatchedOutput) -> crate::c_types::derived::COption_C2Tuple_usizeTransactionZZ,
423         /// Frees any resources associated with this object given its this_arg pointer.
424         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
425         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
426 }
427 unsafe impl Send for Filter {}
428 unsafe impl Sync for Filter {}
429
430 use lightning::chain::Filter as rustFilter;
431 impl rustFilter for Filter {
432         fn register_tx(&self, txid: &bitcoin::hash_types::Txid, script_pubkey: &bitcoin::blockdata::script::Script) {
433                 (self.register_tx)(self.this_arg, txid.as_inner(), crate::c_types::u8slice::from_slice(&script_pubkey[..]))
434         }
435         fn register_output(&self, output: lightning::chain::WatchedOutput) -> Option<(usize, bitcoin::blockdata::transaction::Transaction)> {
436                 let mut ret = (self.register_output)(self.this_arg, crate::lightning::chain::WatchedOutput { inner: Box::into_raw(Box::new(output)), is_owned: true });
437                 let mut local_ret = if ret.is_some() { Some( { let (mut orig_ret_0_0, mut orig_ret_0_1) = ret.take().to_rust(); let mut local_ret_0 = (orig_ret_0_0, orig_ret_0_1.into_bitcoin()); local_ret_0 }) } else { None };
438                 local_ret
439         }
440 }
441
442 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
443 // directly as a Deref trait in higher-level structs:
444 impl std::ops::Deref for Filter {
445         type Target = Self;
446         fn deref(&self) -> &Self {
447                 self
448         }
449 }
450 /// Calls the free function if one is set
451 #[no_mangle]
452 pub extern "C" fn Filter_free(this_ptr: Filter) { }
453 impl Drop for Filter {
454         fn drop(&mut self) {
455                 if let Some(f) = self.free {
456                         f(self.this_arg);
457                 }
458         }
459 }
460
461 use lightning::chain::WatchedOutput as nativeWatchedOutputImport;
462 type nativeWatchedOutput = nativeWatchedOutputImport;
463
464 /// A transaction output watched by a [`ChannelMonitor`] for spends on-chain.
465 ///
466 /// Used to convey to a [`Filter`] such an output with a given spending condition. Any transaction
467 /// spending the output must be given to [`ChannelMonitor::block_connected`] either directly or via
468 /// the return value of [`Filter::register_output`].
469 ///
470 /// If `block_hash` is `Some`, this indicates the output was created in the corresponding block and
471 /// may have been spent there. See [`Filter::register_output`] for details.
472 ///
473 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
474 /// [`ChannelMonitor::block_connected`]: channelmonitor::ChannelMonitor::block_connected
475 #[must_use]
476 #[repr(C)]
477 pub struct WatchedOutput {
478         /// A pointer to the opaque Rust object.
479
480         /// Nearly everywhere, inner must be non-null, however in places where
481         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
482         pub inner: *mut nativeWatchedOutput,
483         /// Indicates that this is the only struct which contains the same pointer.
484
485         /// Rust functions which take ownership of an object provided via an argument require
486         /// this to be true and invalidate the object pointed to by inner.
487         pub is_owned: bool,
488 }
489
490 impl Drop for WatchedOutput {
491         fn drop(&mut self) {
492                 if self.is_owned && !<*mut nativeWatchedOutput>::is_null(self.inner) {
493                         let _ = unsafe { Box::from_raw(self.inner) };
494                 }
495         }
496 }
497 /// Frees any resources used by the WatchedOutput, if is_owned is set and inner is non-NULL.
498 #[no_mangle]
499 pub extern "C" fn WatchedOutput_free(this_obj: WatchedOutput) { }
500 #[allow(unused)]
501 /// Used only if an object of this type is returned as a trait impl by a method
502 extern "C" fn WatchedOutput_free_void(this_ptr: *mut c_void) {
503         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeWatchedOutput); }
504 }
505 #[allow(unused)]
506 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
507 impl WatchedOutput {
508         pub(crate) fn take_inner(mut self) -> *mut nativeWatchedOutput {
509                 assert!(self.is_owned);
510                 let ret = self.inner;
511                 self.inner = std::ptr::null_mut();
512                 ret
513         }
514 }
515 /// First block where the transaction output may have been spent.
516 #[no_mangle]
517 pub extern "C" fn WatchedOutput_get_block_hash(this_ptr: &WatchedOutput) -> crate::c_types::ThirtyTwoBytes {
518         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.block_hash;
519         let mut local_inner_val = if inner_val.is_none() { crate::c_types::ThirtyTwoBytes::null() } else {  { crate::c_types::ThirtyTwoBytes { data: (inner_val.unwrap()).into_inner() } } };
520         local_inner_val
521 }
522 /// First block where the transaction output may have been spent.
523 #[no_mangle]
524 pub extern "C" fn WatchedOutput_set_block_hash(this_ptr: &mut WatchedOutput, mut val: crate::c_types::ThirtyTwoBytes) {
525         let mut local_val = if val.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&val.data[..]).unwrap() }) };
526         unsafe { &mut *this_ptr.inner }.block_hash = local_val;
527 }
528 /// Outpoint identifying the transaction output.
529 #[no_mangle]
530 pub extern "C" fn WatchedOutput_get_outpoint(this_ptr: &WatchedOutput) -> crate::lightning::chain::transaction::OutPoint {
531         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint;
532         crate::lightning::chain::transaction::OutPoint { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false }
533 }
534 /// Outpoint identifying the transaction output.
535 #[no_mangle]
536 pub extern "C" fn WatchedOutput_set_outpoint(this_ptr: &mut WatchedOutput, mut val: crate::lightning::chain::transaction::OutPoint) {
537         unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) };
538 }
539 /// Spending condition of the transaction output.
540 #[no_mangle]
541 pub extern "C" fn WatchedOutput_get_script_pubkey(this_ptr: &WatchedOutput) -> crate::c_types::u8slice {
542         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script_pubkey;
543         crate::c_types::u8slice::from_slice(&inner_val[..])
544 }
545 /// Spending condition of the transaction output.
546 #[no_mangle]
547 pub extern "C" fn WatchedOutput_set_script_pubkey(this_ptr: &mut WatchedOutput, mut val: crate::c_types::derived::CVec_u8Z) {
548         unsafe { &mut *this_ptr.inner }.script_pubkey = ::bitcoin::blockdata::script::Script::from(val.into_rust());
549 }
550 /// Constructs a new WatchedOutput given each field
551 #[must_use]
552 #[no_mangle]
553 pub extern "C" fn WatchedOutput_new(mut block_hash_arg: crate::c_types::ThirtyTwoBytes, mut outpoint_arg: crate::lightning::chain::transaction::OutPoint, mut script_pubkey_arg: crate::c_types::derived::CVec_u8Z) -> WatchedOutput {
554         let mut local_block_hash_arg = if block_hash_arg.data == [0; 32] { None } else { Some( { ::bitcoin::hash_types::BlockHash::from_slice(&block_hash_arg.data[..]).unwrap() }) };
555         WatchedOutput { inner: Box::into_raw(Box::new(nativeWatchedOutput {
556                 block_hash: local_block_hash_arg,
557                 outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) },
558                 script_pubkey: ::bitcoin::blockdata::script::Script::from(script_pubkey_arg.into_rust()),
559         })), is_owned: true }
560 }