Merge pull request #801 from TheBlueMatt/2021-02-789-bindings
[rust-lightning] / lightning-c-bindings / src / ln / chan_utils.rs
1 //! Various utilities for building scripts and deriving keys related to channels. These are
2 //! largely of interest for those implementing chain::keysinterface::ChannelKeys message signing
3 //! by hand.
4
5 use std::ffi::c_void;
6 use bitcoin::hashes::Hash;
7 use crate::c_types::*;
8
9 /// Build the commitment secret from the seed and the commitment number
10 #[no_mangle]
11 pub extern "C" fn build_commitment_secret(commitment_seed: *const [u8; 32], mut idx: u64) -> crate::c_types::ThirtyTwoBytes {
12         let mut ret = lightning::ln::chan_utils::build_commitment_secret(unsafe { &*commitment_seed}, idx);
13         crate::c_types::ThirtyTwoBytes { data: ret }
14 }
15
16 /// Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
17 /// from the base secret and the per_commitment_point.
18 ///
19 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
20 /// generated (ie our own).
21 #[no_mangle]
22 pub extern "C" fn derive_private_key(mut per_commitment_point: crate::c_types::PublicKey, base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeyErrorZ {
23         let mut ret = lightning::ln::chan_utils::derive_private_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *base_secret}[..]).unwrap());
24         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
25         local_ret
26 }
27
28 /// Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
29 /// from the base point and the per_commitment_key. This is the public equivalent of
30 /// derive_private_key - using only public keys to derive a public key instead of private keys.
31 ///
32 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
33 /// generated (ie our own).
34 #[no_mangle]
35 pub extern "C" fn derive_public_key(mut per_commitment_point: crate::c_types::PublicKey, mut base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeyErrorZ {
36         let mut ret = lightning::ln::chan_utils::derive_public_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &base_point.into_rust());
37         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
38         local_ret
39 }
40
41 /// Derives a per-commitment-transaction revocation key from its constituent parts.
42 ///
43 /// Only the cheating participant owns a valid witness to propagate a revoked 
44 /// commitment transaction, thus per_commitment_secret always come from cheater
45 /// and revocation_base_secret always come from punisher, which is the broadcaster
46 /// of the transaction spending with this key knowledge.
47 ///
48 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
49 /// generated (ie our own).
50 #[no_mangle]
51 pub extern "C" fn derive_private_revocation_key(per_commitment_secret: *const [u8; 32], countersignatory_revocation_base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeyErrorZ {
52         let mut ret = lightning::ln::chan_utils::derive_private_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *per_commitment_secret}[..]).unwrap(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *countersignatory_revocation_base_secret}[..]).unwrap());
53         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
54         local_ret
55 }
56
57 /// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
58 /// the public equivalend of derive_private_revocation_key - using only public keys to derive a
59 /// public key instead of private keys.
60 ///
61 /// Only the cheating participant owns a valid witness to propagate a revoked 
62 /// commitment transaction, thus per_commitment_point always come from cheater
63 /// and revocation_base_point always come from punisher, which is the broadcaster
64 /// of the transaction spending with this key knowledge.
65 ///
66 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
67 /// generated (ie our own).
68 #[no_mangle]
69 pub extern "C" fn derive_public_revocation_key(mut per_commitment_point: crate::c_types::PublicKey, mut countersignatory_revocation_base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeyErrorZ {
70         let mut ret = lightning::ln::chan_utils::derive_public_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &countersignatory_revocation_base_point.into_rust());
71         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
72         local_ret
73 }
74
75
76 use lightning::ln::chan_utils::TxCreationKeys as nativeTxCreationKeysImport;
77 type nativeTxCreationKeys = nativeTxCreationKeysImport;
78
79 /// The set of public keys which are used in the creation of one commitment transaction.
80 /// These are derived from the channel base keys and per-commitment data.
81 ///
82 /// A broadcaster key is provided from potential broadcaster of the computed transaction.
83 /// A countersignatory key is coming from a protocol participant unable to broadcast the
84 /// transaction.
85 ///
86 /// These keys are assumed to be good, either because the code derived them from
87 /// channel basepoints via the new function, or they were obtained via
88 /// CommitmentTransaction.trust().keys() because we trusted the source of the
89 /// pre-calculated keys.
90 #[must_use]
91 #[repr(C)]
92 pub struct TxCreationKeys {
93         /// Nearly everywhere, inner must be non-null, however in places where
94         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
95         pub inner: *mut nativeTxCreationKeys,
96         pub is_owned: bool,
97 }
98
99 impl Drop for TxCreationKeys {
100         fn drop(&mut self) {
101                 if self.is_owned && !self.inner.is_null() {
102                         let _ = unsafe { Box::from_raw(self.inner) };
103                 }
104         }
105 }
106 #[no_mangle]
107 pub extern "C" fn TxCreationKeys_free(this_ptr: TxCreationKeys) { }
108 #[allow(unused)]
109 /// Used only if an object of this type is returned as a trait impl by a method
110 extern "C" fn TxCreationKeys_free_void(this_ptr: *mut c_void) {
111         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTxCreationKeys); }
112 }
113 #[allow(unused)]
114 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
115 impl TxCreationKeys {
116         pub(crate) fn take_inner(mut self) -> *mut nativeTxCreationKeys {
117                 assert!(self.is_owned);
118                 let ret = self.inner;
119                 self.inner = std::ptr::null_mut();
120                 ret
121         }
122 }
123 /// The broadcaster's per-commitment public key which was used to derive the other keys.
124 #[no_mangle]
125 pub extern "C" fn TxCreationKeys_get_per_commitment_point(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
126         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.per_commitment_point;
127         crate::c_types::PublicKey::from_rust(&(*inner_val))
128 }
129 /// The broadcaster's per-commitment public key which was used to derive the other keys.
130 #[no_mangle]
131 pub extern "C" fn TxCreationKeys_set_per_commitment_point(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
132         unsafe { &mut *this_ptr.inner }.per_commitment_point = val.into_rust();
133 }
134 /// The revocation key which is used to allow the broadcaster of the commitment
135 /// transaction to provide their counterparty the ability to punish them if they broadcast
136 /// an old state.
137 #[no_mangle]
138 pub extern "C" fn TxCreationKeys_get_revocation_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
139         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_key;
140         crate::c_types::PublicKey::from_rust(&(*inner_val))
141 }
142 /// The revocation key which is used to allow the broadcaster of the commitment
143 /// transaction to provide their counterparty the ability to punish them if they broadcast
144 /// an old state.
145 #[no_mangle]
146 pub extern "C" fn TxCreationKeys_set_revocation_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
147         unsafe { &mut *this_ptr.inner }.revocation_key = val.into_rust();
148 }
149 /// Broadcaster's HTLC Key
150 #[no_mangle]
151 pub extern "C" fn TxCreationKeys_get_broadcaster_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
152         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key;
153         crate::c_types::PublicKey::from_rust(&(*inner_val))
154 }
155 /// Broadcaster's HTLC Key
156 #[no_mangle]
157 pub extern "C" fn TxCreationKeys_set_broadcaster_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
158         unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key = val.into_rust();
159 }
160 /// Countersignatory's HTLC Key
161 #[no_mangle]
162 pub extern "C" fn TxCreationKeys_get_countersignatory_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
163         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key;
164         crate::c_types::PublicKey::from_rust(&(*inner_val))
165 }
166 /// Countersignatory's HTLC Key
167 #[no_mangle]
168 pub extern "C" fn TxCreationKeys_set_countersignatory_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
169         unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key = val.into_rust();
170 }
171 /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
172 #[no_mangle]
173 pub extern "C" fn TxCreationKeys_get_broadcaster_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
174         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key;
175         crate::c_types::PublicKey::from_rust(&(*inner_val))
176 }
177 /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
178 #[no_mangle]
179 pub extern "C" fn TxCreationKeys_set_broadcaster_delayed_payment_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
180         unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key = val.into_rust();
181 }
182 #[must_use]
183 #[no_mangle]
184 pub extern "C" fn TxCreationKeys_new(mut per_commitment_point_arg: crate::c_types::PublicKey, mut revocation_key_arg: crate::c_types::PublicKey, mut broadcaster_htlc_key_arg: crate::c_types::PublicKey, mut countersignatory_htlc_key_arg: crate::c_types::PublicKey, mut broadcaster_delayed_payment_key_arg: crate::c_types::PublicKey) -> TxCreationKeys {
185         TxCreationKeys { inner: Box::into_raw(Box::new(nativeTxCreationKeys {
186                 per_commitment_point: per_commitment_point_arg.into_rust(),
187                 revocation_key: revocation_key_arg.into_rust(),
188                 broadcaster_htlc_key: broadcaster_htlc_key_arg.into_rust(),
189                 countersignatory_htlc_key: countersignatory_htlc_key_arg.into_rust(),
190                 broadcaster_delayed_payment_key: broadcaster_delayed_payment_key_arg.into_rust(),
191         })), is_owned: true }
192 }
193 impl Clone for TxCreationKeys {
194         fn clone(&self) -> Self {
195                 Self {
196                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
197                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
198                         is_owned: true,
199                 }
200         }
201 }
202 #[allow(unused)]
203 /// Used only if an object of this type is returned as a trait impl by a method
204 pub(crate) extern "C" fn TxCreationKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
205         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeTxCreationKeys)).clone() })) as *mut c_void
206 }
207 #[no_mangle]
208 pub extern "C" fn TxCreationKeys_clone(orig: &TxCreationKeys) -> TxCreationKeys {
209         orig.clone()
210 }
211 #[no_mangle]
212 pub extern "C" fn TxCreationKeys_write(obj: &TxCreationKeys) -> crate::c_types::derived::CVec_u8Z {
213         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
214 }
215 #[no_mangle]
216 pub(crate) extern "C" fn TxCreationKeys_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
217         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeTxCreationKeys) })
218 }
219 #[no_mangle]
220 pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_TxCreationKeysDecodeErrorZ {
221         let res = crate::c_types::deserialize_obj(ser);
222         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
223         local_res
224 }
225
226 use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport;
227 type nativeChannelPublicKeys = nativeChannelPublicKeysImport;
228
229 /// One counterparty's public keys which do not change over the life of a channel.
230 #[must_use]
231 #[repr(C)]
232 pub struct ChannelPublicKeys {
233         /// Nearly everywhere, inner must be non-null, however in places where
234         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
235         pub inner: *mut nativeChannelPublicKeys,
236         pub is_owned: bool,
237 }
238
239 impl Drop for ChannelPublicKeys {
240         fn drop(&mut self) {
241                 if self.is_owned && !self.inner.is_null() {
242                         let _ = unsafe { Box::from_raw(self.inner) };
243                 }
244         }
245 }
246 #[no_mangle]
247 pub extern "C" fn ChannelPublicKeys_free(this_ptr: ChannelPublicKeys) { }
248 #[allow(unused)]
249 /// Used only if an object of this type is returned as a trait impl by a method
250 extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) {
251         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelPublicKeys); }
252 }
253 #[allow(unused)]
254 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
255 impl ChannelPublicKeys {
256         pub(crate) fn take_inner(mut self) -> *mut nativeChannelPublicKeys {
257                 assert!(self.is_owned);
258                 let ret = self.inner;
259                 self.inner = std::ptr::null_mut();
260                 ret
261         }
262 }
263 /// The public key which is used to sign all commitment transactions, as it appears in the
264 /// on-chain channel lock-in 2-of-2 multisig output.
265 #[no_mangle]
266 pub extern "C" fn ChannelPublicKeys_get_funding_pubkey(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
267         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_pubkey;
268         crate::c_types::PublicKey::from_rust(&(*inner_val))
269 }
270 /// The public key which is used to sign all commitment transactions, as it appears in the
271 /// on-chain channel lock-in 2-of-2 multisig output.
272 #[no_mangle]
273 pub extern "C" fn ChannelPublicKeys_set_funding_pubkey(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
274         unsafe { &mut *this_ptr.inner }.funding_pubkey = val.into_rust();
275 }
276 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
277 /// revocation keys. This is combined with the per-commitment-secret generated by the
278 /// counterparty to create a secret which the counterparty can reveal to revoke previous
279 /// states.
280 #[no_mangle]
281 pub extern "C" fn ChannelPublicKeys_get_revocation_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
282         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_basepoint;
283         crate::c_types::PublicKey::from_rust(&(*inner_val))
284 }
285 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
286 /// revocation keys. This is combined with the per-commitment-secret generated by the
287 /// counterparty to create a secret which the counterparty can reveal to revoke previous
288 /// states.
289 #[no_mangle]
290 pub extern "C" fn ChannelPublicKeys_set_revocation_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
291         unsafe { &mut *this_ptr.inner }.revocation_basepoint = val.into_rust();
292 }
293 /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
294 /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
295 /// static across every commitment transaction.
296 #[no_mangle]
297 pub extern "C" fn ChannelPublicKeys_get_payment_point(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
298         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_point;
299         crate::c_types::PublicKey::from_rust(&(*inner_val))
300 }
301 /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
302 /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
303 /// static across every commitment transaction.
304 #[no_mangle]
305 pub extern "C" fn ChannelPublicKeys_set_payment_point(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
306         unsafe { &mut *this_ptr.inner }.payment_point = val.into_rust();
307 }
308 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
309 /// public key which receives non-HTLC-encumbered funds which are only available for spending
310 /// after some delay (or can be claimed via the revocation path).
311 #[no_mangle]
312 pub extern "C" fn ChannelPublicKeys_get_delayed_payment_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
313         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint;
314         crate::c_types::PublicKey::from_rust(&(*inner_val))
315 }
316 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
317 /// public key which receives non-HTLC-encumbered funds which are only available for spending
318 /// after some delay (or can be claimed via the revocation path).
319 #[no_mangle]
320 pub extern "C" fn ChannelPublicKeys_set_delayed_payment_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
321         unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint = val.into_rust();
322 }
323 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
324 /// which is used to encumber HTLC-in-flight outputs.
325 #[no_mangle]
326 pub extern "C" fn ChannelPublicKeys_get_htlc_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
327         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_basepoint;
328         crate::c_types::PublicKey::from_rust(&(*inner_val))
329 }
330 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
331 /// which is used to encumber HTLC-in-flight outputs.
332 #[no_mangle]
333 pub extern "C" fn ChannelPublicKeys_set_htlc_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
334         unsafe { &mut *this_ptr.inner }.htlc_basepoint = val.into_rust();
335 }
336 #[must_use]
337 #[no_mangle]
338 pub extern "C" fn ChannelPublicKeys_new(mut funding_pubkey_arg: crate::c_types::PublicKey, mut revocation_basepoint_arg: crate::c_types::PublicKey, mut payment_point_arg: crate::c_types::PublicKey, mut delayed_payment_basepoint_arg: crate::c_types::PublicKey, mut htlc_basepoint_arg: crate::c_types::PublicKey) -> ChannelPublicKeys {
339         ChannelPublicKeys { inner: Box::into_raw(Box::new(nativeChannelPublicKeys {
340                 funding_pubkey: funding_pubkey_arg.into_rust(),
341                 revocation_basepoint: revocation_basepoint_arg.into_rust(),
342                 payment_point: payment_point_arg.into_rust(),
343                 delayed_payment_basepoint: delayed_payment_basepoint_arg.into_rust(),
344                 htlc_basepoint: htlc_basepoint_arg.into_rust(),
345         })), is_owned: true }
346 }
347 impl Clone for ChannelPublicKeys {
348         fn clone(&self) -> Self {
349                 Self {
350                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
351                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
352                         is_owned: true,
353                 }
354         }
355 }
356 #[allow(unused)]
357 /// Used only if an object of this type is returned as a trait impl by a method
358 pub(crate) extern "C" fn ChannelPublicKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
359         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelPublicKeys)).clone() })) as *mut c_void
360 }
361 #[no_mangle]
362 pub extern "C" fn ChannelPublicKeys_clone(orig: &ChannelPublicKeys) -> ChannelPublicKeys {
363         orig.clone()
364 }
365 #[no_mangle]
366 pub extern "C" fn ChannelPublicKeys_write(obj: &ChannelPublicKeys) -> crate::c_types::derived::CVec_u8Z {
367         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
368 }
369 #[no_mangle]
370 pub(crate) extern "C" fn ChannelPublicKeys_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
371         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelPublicKeys) })
372 }
373 #[no_mangle]
374 pub extern "C" fn ChannelPublicKeys_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelPublicKeysDecodeErrorZ {
375         let res = crate::c_types::deserialize_obj(ser);
376         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::ChannelPublicKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
377         local_res
378 }
379 /// Create per-state keys from channel base points and the per-commitment point.
380 /// Key set is asymmetric and can't be used as part of counter-signatory set of transactions.
381 #[must_use]
382 #[no_mangle]
383 pub extern "C" fn TxCreationKeys_derive_new(mut per_commitment_point: crate::c_types::PublicKey, mut broadcaster_delayed_payment_base: crate::c_types::PublicKey, mut broadcaster_htlc_base: crate::c_types::PublicKey, mut countersignatory_revocation_base: crate::c_types::PublicKey, mut countersignatory_htlc_base: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_TxCreationKeysErrorZ {
384         let mut ret = lightning::ln::chan_utils::TxCreationKeys::derive_new(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &broadcaster_delayed_payment_base.into_rust(), &broadcaster_htlc_base.into_rust(), &countersignatory_revocation_base.into_rust(), &countersignatory_htlc_base.into_rust());
385         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
386         local_ret
387 }
388
389 /// Generate per-state keys from channel static keys.
390 /// Key set is asymmetric and can't be used as part of counter-signatory set of transactions.
391 #[must_use]
392 #[no_mangle]
393 pub extern "C" fn TxCreationKeys_from_channel_static_keys(mut per_commitment_point: crate::c_types::PublicKey, broadcaster_keys: &crate::ln::chan_utils::ChannelPublicKeys, countersignatory_keys: &crate::ln::chan_utils::ChannelPublicKeys) -> crate::c_types::derived::CResult_TxCreationKeysErrorZ {
394         let mut ret = lightning::ln::chan_utils::TxCreationKeys::from_channel_static_keys(&per_commitment_point.into_rust(), unsafe { &*broadcaster_keys.inner }, unsafe { &*countersignatory_keys.inner }, &bitcoin::secp256k1::Secp256k1::new());
395         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TxCreationKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }).into() };
396         local_ret
397 }
398
399
400 #[no_mangle]
401 pub static REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH: usize = lightning::ln::chan_utils::REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH;
402 /// A script either spendable by the revocation
403 /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
404 /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
405 #[no_mangle]
406 pub extern "C" fn get_revokeable_redeemscript(mut revocation_key: crate::c_types::PublicKey, mut contest_delay: u16, mut broadcaster_delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
407         let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), contest_delay, &broadcaster_delayed_payment_key.into_rust());
408         ret.into_bytes().into()
409 }
410
411
412 use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport;
413 type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport;
414
415 /// Information about an HTLC as it appears in a commitment transaction
416 #[must_use]
417 #[repr(C)]
418 pub struct HTLCOutputInCommitment {
419         /// Nearly everywhere, inner must be non-null, however in places where
420         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
421         pub inner: *mut nativeHTLCOutputInCommitment,
422         pub is_owned: bool,
423 }
424
425 impl Drop for HTLCOutputInCommitment {
426         fn drop(&mut self) {
427                 if self.is_owned && !self.inner.is_null() {
428                         let _ = unsafe { Box::from_raw(self.inner) };
429                 }
430         }
431 }
432 #[no_mangle]
433 pub extern "C" fn HTLCOutputInCommitment_free(this_ptr: HTLCOutputInCommitment) { }
434 #[allow(unused)]
435 /// Used only if an object of this type is returned as a trait impl by a method
436 extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) {
437         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCOutputInCommitment); }
438 }
439 #[allow(unused)]
440 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
441 impl HTLCOutputInCommitment {
442         pub(crate) fn take_inner(mut self) -> *mut nativeHTLCOutputInCommitment {
443                 assert!(self.is_owned);
444                 let ret = self.inner;
445                 self.inner = std::ptr::null_mut();
446                 ret
447         }
448 }
449 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
450 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
451 /// need to compare this value to whether the commitment transaction in question is that of
452 /// the counterparty or our own.
453 #[no_mangle]
454 pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool {
455         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered;
456         (*inner_val)
457 }
458 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
459 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
460 /// need to compare this value to whether the commitment transaction in question is that of
461 /// the counterparty or our own.
462 #[no_mangle]
463 pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputInCommitment, mut val: bool) {
464         unsafe { &mut *this_ptr.inner }.offered = val;
465 }
466 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
467 /// this divided by 1000.
468 #[no_mangle]
469 pub extern "C" fn HTLCOutputInCommitment_get_amount_msat(this_ptr: &HTLCOutputInCommitment) -> u64 {
470         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.amount_msat;
471         (*inner_val)
472 }
473 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
474 /// this divided by 1000.
475 #[no_mangle]
476 pub extern "C" fn HTLCOutputInCommitment_set_amount_msat(this_ptr: &mut HTLCOutputInCommitment, mut val: u64) {
477         unsafe { &mut *this_ptr.inner }.amount_msat = val;
478 }
479 /// The CLTV lock-time at which this HTLC expires.
480 #[no_mangle]
481 pub extern "C" fn HTLCOutputInCommitment_get_cltv_expiry(this_ptr: &HTLCOutputInCommitment) -> u32 {
482         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry;
483         (*inner_val)
484 }
485 /// The CLTV lock-time at which this HTLC expires.
486 #[no_mangle]
487 pub extern "C" fn HTLCOutputInCommitment_set_cltv_expiry(this_ptr: &mut HTLCOutputInCommitment, mut val: u32) {
488         unsafe { &mut *this_ptr.inner }.cltv_expiry = val;
489 }
490 /// The hash of the preimage which unlocks this HTLC.
491 #[no_mangle]
492 pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputInCommitment) -> *const [u8; 32] {
493         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_hash;
494         &(*inner_val).0
495 }
496 /// The hash of the preimage which unlocks this HTLC.
497 #[no_mangle]
498 pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) {
499         unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data);
500 }
501 impl Clone for HTLCOutputInCommitment {
502         fn clone(&self) -> Self {
503                 Self {
504                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
505                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
506                         is_owned: true,
507                 }
508         }
509 }
510 #[allow(unused)]
511 /// Used only if an object of this type is returned as a trait impl by a method
512 pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void {
513         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void
514 }
515 #[no_mangle]
516 pub extern "C" fn HTLCOutputInCommitment_clone(orig: &HTLCOutputInCommitment) -> HTLCOutputInCommitment {
517         orig.clone()
518 }
519 #[no_mangle]
520 pub extern "C" fn HTLCOutputInCommitment_write(obj: &HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z {
521         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
522 }
523 #[no_mangle]
524 pub(crate) extern "C" fn HTLCOutputInCommitment_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
525         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHTLCOutputInCommitment) })
526 }
527 #[no_mangle]
528 pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HTLCOutputInCommitmentDecodeErrorZ {
529         let res = crate::c_types::deserialize_obj(ser);
530         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::HTLCOutputInCommitment { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
531         local_res
532 }
533 /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
534 /// does not need to have its previous_output_index filled.
535 #[no_mangle]
536 pub extern "C" fn get_htlc_redeemscript(htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, keys: &crate::ln::chan_utils::TxCreationKeys) -> crate::c_types::derived::CVec_u8Z {
537         let mut ret = lightning::ln::chan_utils::get_htlc_redeemscript(unsafe { &*htlc.inner }, unsafe { &*keys.inner });
538         ret.into_bytes().into()
539 }
540
541 /// Gets the redeemscript for a funding output from the two funding public keys.
542 /// Note that the order of funding public keys does not matter.
543 #[no_mangle]
544 pub extern "C" fn make_funding_redeemscript(mut broadcaster: crate::c_types::PublicKey, mut countersignatory: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
545         let mut ret = lightning::ln::chan_utils::make_funding_redeemscript(&broadcaster.into_rust(), &countersignatory.into_rust());
546         ret.into_bytes().into()
547 }
548
549 /// panics if htlc.transaction_output_index.is_none()!
550 #[no_mangle]
551 pub extern "C" fn build_htlc_transaction(prev_hash: *const [u8; 32], mut feerate_per_kw: u32, mut contest_delay: u16, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, mut broadcaster_delayed_payment_key: crate::c_types::PublicKey, mut revocation_key: crate::c_types::PublicKey) -> crate::c_types::Transaction {
552         let mut ret = lightning::ln::chan_utils::build_htlc_transaction(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*prev_hash }[..]).unwrap(), feerate_per_kw, contest_delay, unsafe { &*htlc.inner }, &broadcaster_delayed_payment_key.into_rust(), &revocation_key.into_rust());
553         let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret);
554         crate::c_types::Transaction::from_vec(local_ret)
555 }
556
557
558 use lightning::ln::chan_utils::ChannelTransactionParameters as nativeChannelTransactionParametersImport;
559 type nativeChannelTransactionParameters = nativeChannelTransactionParametersImport;
560
561 /// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction).
562 /// The fields are organized by holder/counterparty.
563 ///
564 /// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
565 /// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
566 #[must_use]
567 #[repr(C)]
568 pub struct ChannelTransactionParameters {
569         /// Nearly everywhere, inner must be non-null, however in places where
570         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
571         pub inner: *mut nativeChannelTransactionParameters,
572         pub is_owned: bool,
573 }
574
575 impl Drop for ChannelTransactionParameters {
576         fn drop(&mut self) {
577                 if self.is_owned && !self.inner.is_null() {
578                         let _ = unsafe { Box::from_raw(self.inner) };
579                 }
580         }
581 }
582 #[no_mangle]
583 pub extern "C" fn ChannelTransactionParameters_free(this_ptr: ChannelTransactionParameters) { }
584 #[allow(unused)]
585 /// Used only if an object of this type is returned as a trait impl by a method
586 extern "C" fn ChannelTransactionParameters_free_void(this_ptr: *mut c_void) {
587         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelTransactionParameters); }
588 }
589 #[allow(unused)]
590 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
591 impl ChannelTransactionParameters {
592         pub(crate) fn take_inner(mut self) -> *mut nativeChannelTransactionParameters {
593                 assert!(self.is_owned);
594                 let ret = self.inner;
595                 self.inner = std::ptr::null_mut();
596                 ret
597         }
598 }
599 /// Holder public keys
600 #[no_mangle]
601 pub extern "C" fn ChannelTransactionParameters_get_holder_pubkeys(this_ptr: &ChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
602         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.holder_pubkeys;
603         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
604 }
605 /// Holder public keys
606 #[no_mangle]
607 pub extern "C" fn ChannelTransactionParameters_set_holder_pubkeys(this_ptr: &mut ChannelTransactionParameters, mut val: crate::ln::chan_utils::ChannelPublicKeys) {
608         unsafe { &mut *this_ptr.inner }.holder_pubkeys = *unsafe { Box::from_raw(val.take_inner()) };
609 }
610 /// The contest delay selected by the holder, which applies to counterparty-broadcast transactions
611 #[no_mangle]
612 pub extern "C" fn ChannelTransactionParameters_get_holder_selected_contest_delay(this_ptr: &ChannelTransactionParameters) -> u16 {
613         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.holder_selected_contest_delay;
614         (*inner_val)
615 }
616 /// The contest delay selected by the holder, which applies to counterparty-broadcast transactions
617 #[no_mangle]
618 pub extern "C" fn ChannelTransactionParameters_set_holder_selected_contest_delay(this_ptr: &mut ChannelTransactionParameters, mut val: u16) {
619         unsafe { &mut *this_ptr.inner }.holder_selected_contest_delay = val;
620 }
621 /// Whether the holder is the initiator of this channel.
622 /// This is an input to the commitment number obscure factor computation.
623 #[no_mangle]
624 pub extern "C" fn ChannelTransactionParameters_get_is_outbound_from_holder(this_ptr: &ChannelTransactionParameters) -> bool {
625         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.is_outbound_from_holder;
626         (*inner_val)
627 }
628 /// Whether the holder is the initiator of this channel.
629 /// This is an input to the commitment number obscure factor computation.
630 #[no_mangle]
631 pub extern "C" fn ChannelTransactionParameters_set_is_outbound_from_holder(this_ptr: &mut ChannelTransactionParameters, mut val: bool) {
632         unsafe { &mut *this_ptr.inner }.is_outbound_from_holder = val;
633 }
634 /// The late-bound counterparty channel transaction parameters.
635 /// These parameters are populated at the point in the protocol where the counterparty provides them.
636 #[no_mangle]
637 pub extern "C" fn ChannelTransactionParameters_get_counterparty_parameters(this_ptr: &ChannelTransactionParameters) -> crate::ln::chan_utils::CounterpartyChannelTransactionParameters {
638         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_parameters;
639         let mut local_inner_val = crate::ln::chan_utils::CounterpartyChannelTransactionParameters { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
640         local_inner_val
641 }
642 /// The late-bound counterparty channel transaction parameters.
643 /// These parameters are populated at the point in the protocol where the counterparty provides them.
644 #[no_mangle]
645 pub extern "C" fn ChannelTransactionParameters_set_counterparty_parameters(this_ptr: &mut ChannelTransactionParameters, mut val: crate::ln::chan_utils::CounterpartyChannelTransactionParameters) {
646         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
647         unsafe { &mut *this_ptr.inner }.counterparty_parameters = local_val;
648 }
649 /// The late-bound funding outpoint
650 #[no_mangle]
651 pub extern "C" fn ChannelTransactionParameters_get_funding_outpoint(this_ptr: &ChannelTransactionParameters) -> crate::chain::transaction::OutPoint {
652         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_outpoint;
653         let mut local_inner_val = crate::chain::transaction::OutPoint { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
654         local_inner_val
655 }
656 /// The late-bound funding outpoint
657 #[no_mangle]
658 pub extern "C" fn ChannelTransactionParameters_set_funding_outpoint(this_ptr: &mut ChannelTransactionParameters, mut val: crate::chain::transaction::OutPoint) {
659         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
660         unsafe { &mut *this_ptr.inner }.funding_outpoint = local_val;
661 }
662 #[must_use]
663 #[no_mangle]
664 pub extern "C" fn ChannelTransactionParameters_new(mut holder_pubkeys_arg: crate::ln::chan_utils::ChannelPublicKeys, mut holder_selected_contest_delay_arg: u16, mut is_outbound_from_holder_arg: bool, mut counterparty_parameters_arg: crate::ln::chan_utils::CounterpartyChannelTransactionParameters, mut funding_outpoint_arg: crate::chain::transaction::OutPoint) -> ChannelTransactionParameters {
665         let mut local_counterparty_parameters_arg = if counterparty_parameters_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(counterparty_parameters_arg.take_inner()) } }) };
666         let mut local_funding_outpoint_arg = if funding_outpoint_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(funding_outpoint_arg.take_inner()) } }) };
667         ChannelTransactionParameters { inner: Box::into_raw(Box::new(nativeChannelTransactionParameters {
668                 holder_pubkeys: *unsafe { Box::from_raw(holder_pubkeys_arg.take_inner()) },
669                 holder_selected_contest_delay: holder_selected_contest_delay_arg,
670                 is_outbound_from_holder: is_outbound_from_holder_arg,
671                 counterparty_parameters: local_counterparty_parameters_arg,
672                 funding_outpoint: local_funding_outpoint_arg,
673         })), is_owned: true }
674 }
675 impl Clone for ChannelTransactionParameters {
676         fn clone(&self) -> Self {
677                 Self {
678                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
679                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
680                         is_owned: true,
681                 }
682         }
683 }
684 #[allow(unused)]
685 /// Used only if an object of this type is returned as a trait impl by a method
686 pub(crate) extern "C" fn ChannelTransactionParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
687         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelTransactionParameters)).clone() })) as *mut c_void
688 }
689 #[no_mangle]
690 pub extern "C" fn ChannelTransactionParameters_clone(orig: &ChannelTransactionParameters) -> ChannelTransactionParameters {
691         orig.clone()
692 }
693
694 use lightning::ln::chan_utils::CounterpartyChannelTransactionParameters as nativeCounterpartyChannelTransactionParametersImport;
695 type nativeCounterpartyChannelTransactionParameters = nativeCounterpartyChannelTransactionParametersImport;
696
697 /// Late-bound per-channel counterparty data used to build transactions.
698 #[must_use]
699 #[repr(C)]
700 pub struct CounterpartyChannelTransactionParameters {
701         /// Nearly everywhere, inner must be non-null, however in places where
702         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
703         pub inner: *mut nativeCounterpartyChannelTransactionParameters,
704         pub is_owned: bool,
705 }
706
707 impl Drop for CounterpartyChannelTransactionParameters {
708         fn drop(&mut self) {
709                 if self.is_owned && !self.inner.is_null() {
710                         let _ = unsafe { Box::from_raw(self.inner) };
711                 }
712         }
713 }
714 #[no_mangle]
715 pub extern "C" fn CounterpartyChannelTransactionParameters_free(this_ptr: CounterpartyChannelTransactionParameters) { }
716 #[allow(unused)]
717 /// Used only if an object of this type is returned as a trait impl by a method
718 extern "C" fn CounterpartyChannelTransactionParameters_free_void(this_ptr: *mut c_void) {
719         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCounterpartyChannelTransactionParameters); }
720 }
721 #[allow(unused)]
722 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
723 impl CounterpartyChannelTransactionParameters {
724         pub(crate) fn take_inner(mut self) -> *mut nativeCounterpartyChannelTransactionParameters {
725                 assert!(self.is_owned);
726                 let ret = self.inner;
727                 self.inner = std::ptr::null_mut();
728                 ret
729         }
730 }
731 /// Counter-party public keys
732 #[no_mangle]
733 pub extern "C" fn CounterpartyChannelTransactionParameters_get_pubkeys(this_ptr: &CounterpartyChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
734         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.pubkeys;
735         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
736 }
737 /// Counter-party public keys
738 #[no_mangle]
739 pub extern "C" fn CounterpartyChannelTransactionParameters_set_pubkeys(this_ptr: &mut CounterpartyChannelTransactionParameters, mut val: crate::ln::chan_utils::ChannelPublicKeys) {
740         unsafe { &mut *this_ptr.inner }.pubkeys = *unsafe { Box::from_raw(val.take_inner()) };
741 }
742 /// The contest delay selected by the counterparty, which applies to holder-broadcast transactions
743 #[no_mangle]
744 pub extern "C" fn CounterpartyChannelTransactionParameters_get_selected_contest_delay(this_ptr: &CounterpartyChannelTransactionParameters) -> u16 {
745         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.selected_contest_delay;
746         (*inner_val)
747 }
748 /// The contest delay selected by the counterparty, which applies to holder-broadcast transactions
749 #[no_mangle]
750 pub extern "C" fn CounterpartyChannelTransactionParameters_set_selected_contest_delay(this_ptr: &mut CounterpartyChannelTransactionParameters, mut val: u16) {
751         unsafe { &mut *this_ptr.inner }.selected_contest_delay = val;
752 }
753 #[must_use]
754 #[no_mangle]
755 pub extern "C" fn CounterpartyChannelTransactionParameters_new(mut pubkeys_arg: crate::ln::chan_utils::ChannelPublicKeys, mut selected_contest_delay_arg: u16) -> CounterpartyChannelTransactionParameters {
756         CounterpartyChannelTransactionParameters { inner: Box::into_raw(Box::new(nativeCounterpartyChannelTransactionParameters {
757                 pubkeys: *unsafe { Box::from_raw(pubkeys_arg.take_inner()) },
758                 selected_contest_delay: selected_contest_delay_arg,
759         })), is_owned: true }
760 }
761 impl Clone for CounterpartyChannelTransactionParameters {
762         fn clone(&self) -> Self {
763                 Self {
764                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
765                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
766                         is_owned: true,
767                 }
768         }
769 }
770 #[allow(unused)]
771 /// Used only if an object of this type is returned as a trait impl by a method
772 pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
773         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeCounterpartyChannelTransactionParameters)).clone() })) as *mut c_void
774 }
775 #[no_mangle]
776 pub extern "C" fn CounterpartyChannelTransactionParameters_clone(orig: &CounterpartyChannelTransactionParameters) -> CounterpartyChannelTransactionParameters {
777         orig.clone()
778 }
779 /// Whether the late bound parameters are populated.
780 #[must_use]
781 #[no_mangle]
782 pub extern "C" fn ChannelTransactionParameters_is_populated(this_arg: &ChannelTransactionParameters) -> bool {
783         let mut ret = unsafe { &*this_arg.inner }.is_populated();
784         ret
785 }
786
787 /// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
788 /// given that the holder is the broadcaster.
789 ///
790 /// self.is_populated() must be true before calling this function.
791 #[must_use]
792 #[no_mangle]
793 pub extern "C" fn ChannelTransactionParameters_as_holder_broadcastable(this_arg: &ChannelTransactionParameters) -> crate::ln::chan_utils::DirectedChannelTransactionParameters {
794         let mut ret = unsafe { &*this_arg.inner }.as_holder_broadcastable();
795         crate::ln::chan_utils::DirectedChannelTransactionParameters { inner: Box::into_raw(Box::new(ret)), is_owned: true }
796 }
797
798 /// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
799 /// given that the counterparty is the broadcaster.
800 ///
801 /// self.is_populated() must be true before calling this function.
802 #[must_use]
803 #[no_mangle]
804 pub extern "C" fn ChannelTransactionParameters_as_counterparty_broadcastable(this_arg: &ChannelTransactionParameters) -> crate::ln::chan_utils::DirectedChannelTransactionParameters {
805         let mut ret = unsafe { &*this_arg.inner }.as_counterparty_broadcastable();
806         crate::ln::chan_utils::DirectedChannelTransactionParameters { inner: Box::into_raw(Box::new(ret)), is_owned: true }
807 }
808
809 #[no_mangle]
810 pub extern "C" fn CounterpartyChannelTransactionParameters_write(obj: &CounterpartyChannelTransactionParameters) -> crate::c_types::derived::CVec_u8Z {
811         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
812 }
813 #[no_mangle]
814 pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
815         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeCounterpartyChannelTransactionParameters) })
816 }
817 #[no_mangle]
818 pub extern "C" fn CounterpartyChannelTransactionParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_CounterpartyChannelTransactionParametersDecodeErrorZ {
819         let res = crate::c_types::deserialize_obj(ser);
820         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::CounterpartyChannelTransactionParameters { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
821         local_res
822 }
823 #[no_mangle]
824 pub extern "C" fn ChannelTransactionParameters_write(obj: &ChannelTransactionParameters) -> crate::c_types::derived::CVec_u8Z {
825         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
826 }
827 #[no_mangle]
828 pub(crate) extern "C" fn ChannelTransactionParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
829         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelTransactionParameters) })
830 }
831 #[no_mangle]
832 pub extern "C" fn ChannelTransactionParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelTransactionParametersDecodeErrorZ {
833         let res = crate::c_types::deserialize_obj(ser);
834         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::ChannelTransactionParameters { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
835         local_res
836 }
837
838 use lightning::ln::chan_utils::DirectedChannelTransactionParameters as nativeDirectedChannelTransactionParametersImport;
839 type nativeDirectedChannelTransactionParameters = nativeDirectedChannelTransactionParametersImport<'static>;
840
841 /// Static channel fields used to build transactions given per-commitment fields, organized by
842 /// broadcaster/countersignatory.
843 ///
844 /// This is derived from the holder/counterparty-organized ChannelTransactionParameters via the
845 /// as_holder_broadcastable and as_counterparty_broadcastable functions.
846 #[must_use]
847 #[repr(C)]
848 pub struct DirectedChannelTransactionParameters {
849         /// Nearly everywhere, inner must be non-null, however in places where
850         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
851         pub inner: *mut nativeDirectedChannelTransactionParameters,
852         pub is_owned: bool,
853 }
854
855 impl Drop for DirectedChannelTransactionParameters {
856         fn drop(&mut self) {
857                 if self.is_owned && !self.inner.is_null() {
858                         let _ = unsafe { Box::from_raw(self.inner) };
859                 }
860         }
861 }
862 #[no_mangle]
863 pub extern "C" fn DirectedChannelTransactionParameters_free(this_ptr: DirectedChannelTransactionParameters) { }
864 #[allow(unused)]
865 /// Used only if an object of this type is returned as a trait impl by a method
866 extern "C" fn DirectedChannelTransactionParameters_free_void(this_ptr: *mut c_void) {
867         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDirectedChannelTransactionParameters); }
868 }
869 #[allow(unused)]
870 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
871 impl DirectedChannelTransactionParameters {
872         pub(crate) fn take_inner(mut self) -> *mut nativeDirectedChannelTransactionParameters {
873                 assert!(self.is_owned);
874                 let ret = self.inner;
875                 self.inner = std::ptr::null_mut();
876                 ret
877         }
878 }
879 /// Get the channel pubkeys for the broadcaster
880 #[must_use]
881 #[no_mangle]
882 pub extern "C" fn DirectedChannelTransactionParameters_broadcaster_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
883         let mut ret = unsafe { &*this_arg.inner }.broadcaster_pubkeys();
884         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
885 }
886
887 /// Get the channel pubkeys for the countersignatory
888 #[must_use]
889 #[no_mangle]
890 pub extern "C" fn DirectedChannelTransactionParameters_countersignatory_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
891         let mut ret = unsafe { &*this_arg.inner }.countersignatory_pubkeys();
892         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
893 }
894
895 /// Get the contest delay applicable to the transactions.
896 /// Note that the contest delay was selected by the countersignatory.
897 #[must_use]
898 #[no_mangle]
899 pub extern "C" fn DirectedChannelTransactionParameters_contest_delay(this_arg: &DirectedChannelTransactionParameters) -> u16 {
900         let mut ret = unsafe { &*this_arg.inner }.contest_delay();
901         ret
902 }
903
904 /// Whether the channel is outbound from the broadcaster.
905 ///
906 /// The boolean representing the side that initiated the channel is
907 /// an input to the commitment number obscure factor computation.
908 #[must_use]
909 #[no_mangle]
910 pub extern "C" fn DirectedChannelTransactionParameters_is_outbound(this_arg: &DirectedChannelTransactionParameters) -> bool {
911         let mut ret = unsafe { &*this_arg.inner }.is_outbound();
912         ret
913 }
914
915 /// The funding outpoint
916 #[must_use]
917 #[no_mangle]
918 pub extern "C" fn DirectedChannelTransactionParameters_funding_outpoint(this_arg: &DirectedChannelTransactionParameters) -> crate::chain::transaction::OutPoint {
919         let mut ret = unsafe { &*this_arg.inner }.funding_outpoint();
920         crate::c_types::bitcoin_to_C_outpoint(ret)
921 }
922
923
924 use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport;
925 type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport;
926
927 /// Information needed to build and sign a holder's commitment transaction.
928 ///
929 /// The transaction is only signed once we are ready to broadcast.
930 #[must_use]
931 #[repr(C)]
932 pub struct HolderCommitmentTransaction {
933         /// Nearly everywhere, inner must be non-null, however in places where
934         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
935         pub inner: *mut nativeHolderCommitmentTransaction,
936         pub is_owned: bool,
937 }
938
939 impl Drop for HolderCommitmentTransaction {
940         fn drop(&mut self) {
941                 if self.is_owned && !self.inner.is_null() {
942                         let _ = unsafe { Box::from_raw(self.inner) };
943                 }
944         }
945 }
946 #[no_mangle]
947 pub extern "C" fn HolderCommitmentTransaction_free(this_ptr: HolderCommitmentTransaction) { }
948 #[allow(unused)]
949 /// Used only if an object of this type is returned as a trait impl by a method
950 extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) {
951         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHolderCommitmentTransaction); }
952 }
953 #[allow(unused)]
954 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
955 impl HolderCommitmentTransaction {
956         pub(crate) fn take_inner(mut self) -> *mut nativeHolderCommitmentTransaction {
957                 assert!(self.is_owned);
958                 let ret = self.inner;
959                 self.inner = std::ptr::null_mut();
960                 ret
961         }
962 }
963 /// Our counterparty's signature for the transaction
964 #[no_mangle]
965 pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature {
966         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig;
967         crate::c_types::Signature::from_rust(&(*inner_val))
968 }
969 /// Our counterparty's signature for the transaction
970 #[no_mangle]
971 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) {
972         unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust();
973 }
974 /// All non-dust counterparty HTLC signatures, in the order they appear in the transaction
975 #[no_mangle]
976 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_htlc_sigs(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_SignatureZ) {
977         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item.into_rust() }); };
978         unsafe { &mut *this_ptr.inner }.counterparty_htlc_sigs = local_val;
979 }
980 impl Clone for HolderCommitmentTransaction {
981         fn clone(&self) -> Self {
982                 Self {
983                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
984                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
985                         is_owned: true,
986                 }
987         }
988 }
989 #[allow(unused)]
990 /// Used only if an object of this type is returned as a trait impl by a method
991 pub(crate) extern "C" fn HolderCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
992         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHolderCommitmentTransaction)).clone() })) as *mut c_void
993 }
994 #[no_mangle]
995 pub extern "C" fn HolderCommitmentTransaction_clone(orig: &HolderCommitmentTransaction) -> HolderCommitmentTransaction {
996         orig.clone()
997 }
998 #[no_mangle]
999 pub extern "C" fn HolderCommitmentTransaction_write(obj: &HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
1000         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1001 }
1002 #[no_mangle]
1003 pub(crate) extern "C" fn HolderCommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1004         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHolderCommitmentTransaction) })
1005 }
1006 #[no_mangle]
1007 pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HolderCommitmentTransactionDecodeErrorZ {
1008         let res = crate::c_types::deserialize_obj(ser);
1009         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::HolderCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
1010         local_res
1011 }
1012 /// Create a new holder transaction with the given counterparty signatures.
1013 /// The funding keys are used to figure out which signature should go first when building the transaction for broadcast.
1014 #[must_use]
1015 #[no_mangle]
1016 pub extern "C" fn HolderCommitmentTransaction_new(mut commitment_tx: crate::ln::chan_utils::CommitmentTransaction, mut counterparty_sig: crate::c_types::Signature, mut counterparty_htlc_sigs: crate::c_types::derived::CVec_SignatureZ, mut holder_funding_key: crate::c_types::PublicKey, mut counterparty_funding_key: crate::c_types::PublicKey) -> HolderCommitmentTransaction {
1017         let mut local_counterparty_htlc_sigs = Vec::new(); for mut item in counterparty_htlc_sigs.into_rust().drain(..) { local_counterparty_htlc_sigs.push( { item.into_rust() }); };
1018         let mut ret = lightning::ln::chan_utils::HolderCommitmentTransaction::new(*unsafe { Box::from_raw(commitment_tx.take_inner()) }, counterparty_sig.into_rust(), local_counterparty_htlc_sigs, &holder_funding_key.into_rust(), &counterparty_funding_key.into_rust());
1019         HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1020 }
1021
1022
1023 use lightning::ln::chan_utils::BuiltCommitmentTransaction as nativeBuiltCommitmentTransactionImport;
1024 type nativeBuiltCommitmentTransaction = nativeBuiltCommitmentTransactionImport;
1025
1026 /// A pre-built Bitcoin commitment transaction and its txid.
1027 #[must_use]
1028 #[repr(C)]
1029 pub struct BuiltCommitmentTransaction {
1030         /// Nearly everywhere, inner must be non-null, however in places where
1031         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1032         pub inner: *mut nativeBuiltCommitmentTransaction,
1033         pub is_owned: bool,
1034 }
1035
1036 impl Drop for BuiltCommitmentTransaction {
1037         fn drop(&mut self) {
1038                 if self.is_owned && !self.inner.is_null() {
1039                         let _ = unsafe { Box::from_raw(self.inner) };
1040                 }
1041         }
1042 }
1043 #[no_mangle]
1044 pub extern "C" fn BuiltCommitmentTransaction_free(this_ptr: BuiltCommitmentTransaction) { }
1045 #[allow(unused)]
1046 /// Used only if an object of this type is returned as a trait impl by a method
1047 extern "C" fn BuiltCommitmentTransaction_free_void(this_ptr: *mut c_void) {
1048         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBuiltCommitmentTransaction); }
1049 }
1050 #[allow(unused)]
1051 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1052 impl BuiltCommitmentTransaction {
1053         pub(crate) fn take_inner(mut self) -> *mut nativeBuiltCommitmentTransaction {
1054                 assert!(self.is_owned);
1055                 let ret = self.inner;
1056                 self.inner = std::ptr::null_mut();
1057                 ret
1058         }
1059 }
1060 /// The commitment transaction
1061 #[no_mangle]
1062 pub extern "C" fn BuiltCommitmentTransaction_get_transaction(this_ptr: &BuiltCommitmentTransaction) -> crate::c_types::Transaction {
1063         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.transaction;
1064         let mut local_inner_val = ::bitcoin::consensus::encode::serialize(inner_val);
1065         crate::c_types::Transaction::from_vec(local_inner_val)
1066 }
1067 /// The commitment transaction
1068 #[no_mangle]
1069 pub extern "C" fn BuiltCommitmentTransaction_set_transaction(this_ptr: &mut BuiltCommitmentTransaction, mut val: crate::c_types::Transaction) {
1070         unsafe { &mut *this_ptr.inner }.transaction = val.into_bitcoin();
1071 }
1072 /// The txid for the commitment transaction.
1073 ///
1074 /// This is provided as a performance optimization, instead of calling transaction.txid()
1075 /// multiple times.
1076 #[no_mangle]
1077 pub extern "C" fn BuiltCommitmentTransaction_get_txid(this_ptr: &BuiltCommitmentTransaction) -> *const [u8; 32] {
1078         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.txid;
1079         (*inner_val).as_inner()
1080 }
1081 /// The txid for the commitment transaction.
1082 ///
1083 /// This is provided as a performance optimization, instead of calling transaction.txid()
1084 /// multiple times.
1085 #[no_mangle]
1086 pub extern "C" fn BuiltCommitmentTransaction_set_txid(this_ptr: &mut BuiltCommitmentTransaction, mut val: crate::c_types::ThirtyTwoBytes) {
1087         unsafe { &mut *this_ptr.inner }.txid = ::bitcoin::hash_types::Txid::from_slice(&val.data[..]).unwrap();
1088 }
1089 #[must_use]
1090 #[no_mangle]
1091 pub extern "C" fn BuiltCommitmentTransaction_new(mut transaction_arg: crate::c_types::Transaction, mut txid_arg: crate::c_types::ThirtyTwoBytes) -> BuiltCommitmentTransaction {
1092         BuiltCommitmentTransaction { inner: Box::into_raw(Box::new(nativeBuiltCommitmentTransaction {
1093                 transaction: transaction_arg.into_bitcoin(),
1094                 txid: ::bitcoin::hash_types::Txid::from_slice(&txid_arg.data[..]).unwrap(),
1095         })), is_owned: true }
1096 }
1097 impl Clone for BuiltCommitmentTransaction {
1098         fn clone(&self) -> Self {
1099                 Self {
1100                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
1101                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
1102                         is_owned: true,
1103                 }
1104         }
1105 }
1106 #[allow(unused)]
1107 /// Used only if an object of this type is returned as a trait impl by a method
1108 pub(crate) extern "C" fn BuiltCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
1109         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBuiltCommitmentTransaction)).clone() })) as *mut c_void
1110 }
1111 #[no_mangle]
1112 pub extern "C" fn BuiltCommitmentTransaction_clone(orig: &BuiltCommitmentTransaction) -> BuiltCommitmentTransaction {
1113         orig.clone()
1114 }
1115 #[no_mangle]
1116 pub extern "C" fn BuiltCommitmentTransaction_write(obj: &BuiltCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
1117         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1118 }
1119 #[no_mangle]
1120 pub(crate) extern "C" fn BuiltCommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1121         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBuiltCommitmentTransaction) })
1122 }
1123 #[no_mangle]
1124 pub extern "C" fn BuiltCommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BuiltCommitmentTransactionDecodeErrorZ {
1125         let res = crate::c_types::deserialize_obj(ser);
1126         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::BuiltCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
1127         local_res
1128 }
1129 /// Get the SIGHASH_ALL sighash value of the transaction.
1130 ///
1131 /// This can be used to verify a signature.
1132 #[must_use]
1133 #[no_mangle]
1134 pub extern "C" fn BuiltCommitmentTransaction_get_sighash_all(this_arg: &BuiltCommitmentTransaction, mut funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::ThirtyTwoBytes {
1135         let mut ret = unsafe { &*this_arg.inner }.get_sighash_all(&::bitcoin::blockdata::script::Script::from(Vec::from(funding_redeemscript.to_slice())), channel_value_satoshis);
1136         crate::c_types::ThirtyTwoBytes { data: ret.as_ref().clone() }
1137 }
1138
1139 /// Sign a transaction, either because we are counter-signing the counterparty's transaction or
1140 /// because we are about to broadcast a holder transaction.
1141 #[must_use]
1142 #[no_mangle]
1143 pub extern "C" fn BuiltCommitmentTransaction_sign(this_arg: &BuiltCommitmentTransaction, funding_key: *const [u8; 32], mut funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::Signature {
1144         let mut ret = unsafe { &*this_arg.inner }.sign(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *funding_key}[..]).unwrap(), &::bitcoin::blockdata::script::Script::from(Vec::from(funding_redeemscript.to_slice())), channel_value_satoshis, &bitcoin::secp256k1::Secp256k1::new());
1145         crate::c_types::Signature::from_rust(&ret)
1146 }
1147
1148
1149 use lightning::ln::chan_utils::CommitmentTransaction as nativeCommitmentTransactionImport;
1150 type nativeCommitmentTransaction = nativeCommitmentTransactionImport;
1151
1152 /// This class tracks the per-transaction information needed to build a commitment transaction and to
1153 /// actually build it and sign.  It is used for holder transactions that we sign only when needed
1154 /// and for transactions we sign for the counterparty.
1155 ///
1156 /// This class can be used inside a signer implementation to generate a signature given the relevant
1157 /// secret key.
1158 #[must_use]
1159 #[repr(C)]
1160 pub struct CommitmentTransaction {
1161         /// Nearly everywhere, inner must be non-null, however in places where
1162         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1163         pub inner: *mut nativeCommitmentTransaction,
1164         pub is_owned: bool,
1165 }
1166
1167 impl Drop for CommitmentTransaction {
1168         fn drop(&mut self) {
1169                 if self.is_owned && !self.inner.is_null() {
1170                         let _ = unsafe { Box::from_raw(self.inner) };
1171                 }
1172         }
1173 }
1174 #[no_mangle]
1175 pub extern "C" fn CommitmentTransaction_free(this_ptr: CommitmentTransaction) { }
1176 #[allow(unused)]
1177 /// Used only if an object of this type is returned as a trait impl by a method
1178 extern "C" fn CommitmentTransaction_free_void(this_ptr: *mut c_void) {
1179         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCommitmentTransaction); }
1180 }
1181 #[allow(unused)]
1182 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1183 impl CommitmentTransaction {
1184         pub(crate) fn take_inner(mut self) -> *mut nativeCommitmentTransaction {
1185                 assert!(self.is_owned);
1186                 let ret = self.inner;
1187                 self.inner = std::ptr::null_mut();
1188                 ret
1189         }
1190 }
1191 impl Clone for CommitmentTransaction {
1192         fn clone(&self) -> Self {
1193                 Self {
1194                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
1195                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
1196                         is_owned: true,
1197                 }
1198         }
1199 }
1200 #[allow(unused)]
1201 /// Used only if an object of this type is returned as a trait impl by a method
1202 pub(crate) extern "C" fn CommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
1203         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeCommitmentTransaction)).clone() })) as *mut c_void
1204 }
1205 #[no_mangle]
1206 pub extern "C" fn CommitmentTransaction_clone(orig: &CommitmentTransaction) -> CommitmentTransaction {
1207         orig.clone()
1208 }
1209 #[no_mangle]
1210 pub extern "C" fn CommitmentTransaction_write(obj: &CommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
1211         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1212 }
1213 #[no_mangle]
1214 pub(crate) extern "C" fn CommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1215         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeCommitmentTransaction) })
1216 }
1217 #[no_mangle]
1218 pub extern "C" fn CommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_CommitmentTransactionDecodeErrorZ {
1219         let res = crate::c_types::deserialize_obj(ser);
1220         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::CommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
1221         local_res
1222 }
1223 /// The backwards-counting commitment number
1224 #[must_use]
1225 #[no_mangle]
1226 pub extern "C" fn CommitmentTransaction_commitment_number(this_arg: &CommitmentTransaction) -> u64 {
1227         let mut ret = unsafe { &*this_arg.inner }.commitment_number();
1228         ret
1229 }
1230
1231 /// The value to be sent to the broadcaster
1232 #[must_use]
1233 #[no_mangle]
1234 pub extern "C" fn CommitmentTransaction_to_broadcaster_value_sat(this_arg: &CommitmentTransaction) -> u64 {
1235         let mut ret = unsafe { &*this_arg.inner }.to_broadcaster_value_sat();
1236         ret
1237 }
1238
1239 /// The value to be sent to the counterparty
1240 #[must_use]
1241 #[no_mangle]
1242 pub extern "C" fn CommitmentTransaction_to_countersignatory_value_sat(this_arg: &CommitmentTransaction) -> u64 {
1243         let mut ret = unsafe { &*this_arg.inner }.to_countersignatory_value_sat();
1244         ret
1245 }
1246
1247 /// The feerate paid per 1000-weight-unit in this commitment transaction.
1248 #[must_use]
1249 #[no_mangle]
1250 pub extern "C" fn CommitmentTransaction_feerate_per_kw(this_arg: &CommitmentTransaction) -> u32 {
1251         let mut ret = unsafe { &*this_arg.inner }.feerate_per_kw();
1252         ret
1253 }
1254
1255 /// Trust our pre-built transaction and derived transaction creation public keys.
1256 ///
1257 /// Applies a wrapper which allows access to these fields.
1258 ///
1259 /// This should only be used if you fully trust the builder of this object.  It should not
1260 ///\tbe used by an external signer - instead use the verify function.
1261 #[must_use]
1262 #[no_mangle]
1263 pub extern "C" fn CommitmentTransaction_trust(this_arg: &CommitmentTransaction) -> crate::ln::chan_utils::TrustedCommitmentTransaction {
1264         let mut ret = unsafe { &*this_arg.inner }.trust();
1265         crate::ln::chan_utils::TrustedCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1266 }
1267
1268 /// Verify our pre-built transaction and derived transaction creation public keys.
1269 ///
1270 /// Applies a wrapper which allows access to these fields.
1271 ///
1272 /// An external validating signer must call this method before signing
1273 /// or using the built transaction.
1274 #[must_use]
1275 #[no_mangle]
1276 pub extern "C" fn CommitmentTransaction_verify(this_arg: &CommitmentTransaction, channel_parameters: &crate::ln::chan_utils::DirectedChannelTransactionParameters, broadcaster_keys: &crate::ln::chan_utils::ChannelPublicKeys, countersignatory_keys: &crate::ln::chan_utils::ChannelPublicKeys) -> crate::c_types::derived::CResult_TrustedCommitmentTransactionNoneZ {
1277         let mut ret = unsafe { &*this_arg.inner }.verify(unsafe { &*channel_parameters.inner }, unsafe { &*broadcaster_keys.inner }, unsafe { &*countersignatory_keys.inner }, &bitcoin::secp256k1::Secp256k1::new());
1278         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::ln::chan_utils::TrustedCommitmentTransaction { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
1279         local_ret
1280 }
1281
1282
1283 use lightning::ln::chan_utils::TrustedCommitmentTransaction as nativeTrustedCommitmentTransactionImport;
1284 type nativeTrustedCommitmentTransaction = nativeTrustedCommitmentTransactionImport<'static>;
1285
1286 /// A wrapper on CommitmentTransaction indicating that the derived fields (the built bitcoin
1287 /// transaction and the transaction creation keys) are trusted.
1288 ///
1289 /// See trust() and verify() functions on CommitmentTransaction.
1290 ///
1291 /// This structure implements Deref.
1292 #[must_use]
1293 #[repr(C)]
1294 pub struct TrustedCommitmentTransaction {
1295         /// Nearly everywhere, inner must be non-null, however in places where
1296         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1297         pub inner: *mut nativeTrustedCommitmentTransaction,
1298         pub is_owned: bool,
1299 }
1300
1301 impl Drop for TrustedCommitmentTransaction {
1302         fn drop(&mut self) {
1303                 if self.is_owned && !self.inner.is_null() {
1304                         let _ = unsafe { Box::from_raw(self.inner) };
1305                 }
1306         }
1307 }
1308 #[no_mangle]
1309 pub extern "C" fn TrustedCommitmentTransaction_free(this_ptr: TrustedCommitmentTransaction) { }
1310 #[allow(unused)]
1311 /// Used only if an object of this type is returned as a trait impl by a method
1312 extern "C" fn TrustedCommitmentTransaction_free_void(this_ptr: *mut c_void) {
1313         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTrustedCommitmentTransaction); }
1314 }
1315 #[allow(unused)]
1316 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1317 impl TrustedCommitmentTransaction {
1318         pub(crate) fn take_inner(mut self) -> *mut nativeTrustedCommitmentTransaction {
1319                 assert!(self.is_owned);
1320                 let ret = self.inner;
1321                 self.inner = std::ptr::null_mut();
1322                 ret
1323         }
1324 }
1325 /// The transaction ID of the built Bitcoin transaction
1326 #[must_use]
1327 #[no_mangle]
1328 pub extern "C" fn TrustedCommitmentTransaction_txid(this_arg: &TrustedCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes {
1329         let mut ret = unsafe { &*this_arg.inner }.txid();
1330         crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
1331 }
1332
1333 /// The pre-built Bitcoin commitment transaction
1334 #[must_use]
1335 #[no_mangle]
1336 pub extern "C" fn TrustedCommitmentTransaction_built_transaction(this_arg: &TrustedCommitmentTransaction) -> crate::ln::chan_utils::BuiltCommitmentTransaction {
1337         let mut ret = unsafe { &*this_arg.inner }.built_transaction();
1338         crate::ln::chan_utils::BuiltCommitmentTransaction { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
1339 }
1340
1341 /// The pre-calculated transaction creation public keys.
1342 #[must_use]
1343 #[no_mangle]
1344 pub extern "C" fn TrustedCommitmentTransaction_keys(this_arg: &TrustedCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys {
1345         let mut ret = unsafe { &*this_arg.inner }.keys();
1346         crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
1347 }
1348
1349 /// Get a signature for each HTLC which was included in the commitment transaction (ie for
1350 /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
1351 ///
1352 /// The returned Vec has one entry for each HTLC, and in the same order.
1353 #[must_use]
1354 #[no_mangle]
1355 pub extern "C" fn TrustedCommitmentTransaction_get_htlc_sigs(this_arg: &TrustedCommitmentTransaction, htlc_base_key: *const [u8; 32], channel_parameters: &crate::ln::chan_utils::DirectedChannelTransactionParameters) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ {
1356         let mut ret = unsafe { &*this_arg.inner }.get_htlc_sigs(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *htlc_base_key}[..]).unwrap(), unsafe { &*channel_parameters.inner }, &bitcoin::secp256k1::Secp256k1::new());
1357         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { crate::c_types::Signature::from_rust(&item) }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
1358         local_ret
1359 }
1360
1361 /// Get the transaction number obscure factor
1362 #[no_mangle]
1363 pub extern "C" fn get_commitment_transaction_number_obscure_factor(mut broadcaster_payment_basepoint: crate::c_types::PublicKey, mut countersignatory_payment_basepoint: crate::c_types::PublicKey, mut outbound_from_broadcaster: bool) -> u64 {
1364         let mut ret = lightning::ln::chan_utils::get_commitment_transaction_number_obscure_factor(&broadcaster_payment_basepoint.into_rust(), &countersignatory_payment_basepoint.into_rust(), outbound_from_broadcaster);
1365         ret
1366 }
1367