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