2783d803d40af766f206842dc9ed8d459d607a87
[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 /// A script either spendable by the revocation
400 /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
401 /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
402 #[no_mangle]
403 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 {
404         let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), contest_delay, &broadcaster_delayed_payment_key.into_rust());
405         ret.into_bytes().into()
406 }
407
408
409 use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport;
410 type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport;
411
412 /// Information about an HTLC as it appears in a commitment transaction
413 #[must_use]
414 #[repr(C)]
415 pub struct HTLCOutputInCommitment {
416         /// Nearly everywhere, inner must be non-null, however in places where
417         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
418         pub inner: *mut nativeHTLCOutputInCommitment,
419         pub is_owned: bool,
420 }
421
422 impl Drop for HTLCOutputInCommitment {
423         fn drop(&mut self) {
424                 if self.is_owned && !self.inner.is_null() {
425                         let _ = unsafe { Box::from_raw(self.inner) };
426                 }
427         }
428 }
429 #[no_mangle]
430 pub extern "C" fn HTLCOutputInCommitment_free(this_ptr: HTLCOutputInCommitment) { }
431 #[allow(unused)]
432 /// Used only if an object of this type is returned as a trait impl by a method
433 extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) {
434         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCOutputInCommitment); }
435 }
436 #[allow(unused)]
437 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
438 impl HTLCOutputInCommitment {
439         pub(crate) fn take_inner(mut self) -> *mut nativeHTLCOutputInCommitment {
440                 assert!(self.is_owned);
441                 let ret = self.inner;
442                 self.inner = std::ptr::null_mut();
443                 ret
444         }
445 }
446 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
447 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
448 /// need to compare this value to whether the commitment transaction in question is that of
449 /// the counterparty or our own.
450 #[no_mangle]
451 pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool {
452         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered;
453         (*inner_val)
454 }
455 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
456 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
457 /// need to compare this value to whether the commitment transaction in question is that of
458 /// the counterparty or our own.
459 #[no_mangle]
460 pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputInCommitment, mut val: bool) {
461         unsafe { &mut *this_ptr.inner }.offered = val;
462 }
463 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
464 /// this divided by 1000.
465 #[no_mangle]
466 pub extern "C" fn HTLCOutputInCommitment_get_amount_msat(this_ptr: &HTLCOutputInCommitment) -> u64 {
467         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.amount_msat;
468         (*inner_val)
469 }
470 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
471 /// this divided by 1000.
472 #[no_mangle]
473 pub extern "C" fn HTLCOutputInCommitment_set_amount_msat(this_ptr: &mut HTLCOutputInCommitment, mut val: u64) {
474         unsafe { &mut *this_ptr.inner }.amount_msat = val;
475 }
476 /// The CLTV lock-time at which this HTLC expires.
477 #[no_mangle]
478 pub extern "C" fn HTLCOutputInCommitment_get_cltv_expiry(this_ptr: &HTLCOutputInCommitment) -> u32 {
479         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry;
480         (*inner_val)
481 }
482 /// The CLTV lock-time at which this HTLC expires.
483 #[no_mangle]
484 pub extern "C" fn HTLCOutputInCommitment_set_cltv_expiry(this_ptr: &mut HTLCOutputInCommitment, mut val: u32) {
485         unsafe { &mut *this_ptr.inner }.cltv_expiry = val;
486 }
487 /// The hash of the preimage which unlocks this HTLC.
488 #[no_mangle]
489 pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputInCommitment) -> *const [u8; 32] {
490         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_hash;
491         &(*inner_val).0
492 }
493 /// The hash of the preimage which unlocks this HTLC.
494 #[no_mangle]
495 pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) {
496         unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data);
497 }
498 impl Clone for HTLCOutputInCommitment {
499         fn clone(&self) -> Self {
500                 Self {
501                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
502                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
503                         is_owned: true,
504                 }
505         }
506 }
507 #[allow(unused)]
508 /// Used only if an object of this type is returned as a trait impl by a method
509 pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void {
510         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void
511 }
512 #[no_mangle]
513 pub extern "C" fn HTLCOutputInCommitment_clone(orig: &HTLCOutputInCommitment) -> HTLCOutputInCommitment {
514         orig.clone()
515 }
516 #[no_mangle]
517 pub extern "C" fn HTLCOutputInCommitment_write(obj: &HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z {
518         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
519 }
520 #[no_mangle]
521 pub(crate) extern "C" fn HTLCOutputInCommitment_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
522         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHTLCOutputInCommitment) })
523 }
524 #[no_mangle]
525 pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HTLCOutputInCommitmentDecodeErrorZ {
526         let res = crate::c_types::deserialize_obj(ser);
527         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() };
528         local_res
529 }
530 /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
531 /// does not need to have its previous_output_index filled.
532 #[no_mangle]
533 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 {
534         let mut ret = lightning::ln::chan_utils::get_htlc_redeemscript(unsafe { &*htlc.inner }, unsafe { &*keys.inner });
535         ret.into_bytes().into()
536 }
537
538 /// Gets the redeemscript for a funding output from the two funding public keys.
539 /// Note that the order of funding public keys does not matter.
540 #[no_mangle]
541 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 {
542         let mut ret = lightning::ln::chan_utils::make_funding_redeemscript(&broadcaster.into_rust(), &countersignatory.into_rust());
543         ret.into_bytes().into()
544 }
545
546 /// panics if htlc.transaction_output_index.is_none()!
547 #[no_mangle]
548 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 {
549         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());
550         let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret);
551         crate::c_types::Transaction::from_vec(local_ret)
552 }
553
554
555 use lightning::ln::chan_utils::ChannelTransactionParameters as nativeChannelTransactionParametersImport;
556 type nativeChannelTransactionParameters = nativeChannelTransactionParametersImport;
557
558 /// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction).
559 /// The fields are organized by holder/counterparty.
560 ///
561 /// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
562 /// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
563 #[must_use]
564 #[repr(C)]
565 pub struct ChannelTransactionParameters {
566         /// Nearly everywhere, inner must be non-null, however in places where
567         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
568         pub inner: *mut nativeChannelTransactionParameters,
569         pub is_owned: bool,
570 }
571
572 impl Drop for ChannelTransactionParameters {
573         fn drop(&mut self) {
574                 if self.is_owned && !self.inner.is_null() {
575                         let _ = unsafe { Box::from_raw(self.inner) };
576                 }
577         }
578 }
579 #[no_mangle]
580 pub extern "C" fn ChannelTransactionParameters_free(this_ptr: ChannelTransactionParameters) { }
581 #[allow(unused)]
582 /// Used only if an object of this type is returned as a trait impl by a method
583 extern "C" fn ChannelTransactionParameters_free_void(this_ptr: *mut c_void) {
584         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelTransactionParameters); }
585 }
586 #[allow(unused)]
587 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
588 impl ChannelTransactionParameters {
589         pub(crate) fn take_inner(mut self) -> *mut nativeChannelTransactionParameters {
590                 assert!(self.is_owned);
591                 let ret = self.inner;
592                 self.inner = std::ptr::null_mut();
593                 ret
594         }
595 }
596 /// Holder public keys
597 #[no_mangle]
598 pub extern "C" fn ChannelTransactionParameters_get_holder_pubkeys(this_ptr: &ChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
599         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.holder_pubkeys;
600         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
601 }
602 /// Holder public keys
603 #[no_mangle]
604 pub extern "C" fn ChannelTransactionParameters_set_holder_pubkeys(this_ptr: &mut ChannelTransactionParameters, mut val: crate::ln::chan_utils::ChannelPublicKeys) {
605         unsafe { &mut *this_ptr.inner }.holder_pubkeys = *unsafe { Box::from_raw(val.take_inner()) };
606 }
607 /// The contest delay selected by the holder, which applies to counterparty-broadcast transactions
608 #[no_mangle]
609 pub extern "C" fn ChannelTransactionParameters_get_holder_selected_contest_delay(this_ptr: &ChannelTransactionParameters) -> u16 {
610         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.holder_selected_contest_delay;
611         (*inner_val)
612 }
613 /// The contest delay selected by the holder, which applies to counterparty-broadcast transactions
614 #[no_mangle]
615 pub extern "C" fn ChannelTransactionParameters_set_holder_selected_contest_delay(this_ptr: &mut ChannelTransactionParameters, mut val: u16) {
616         unsafe { &mut *this_ptr.inner }.holder_selected_contest_delay = val;
617 }
618 /// Whether the holder is the initiator of this channel.
619 /// This is an input to the commitment number obscure factor computation.
620 #[no_mangle]
621 pub extern "C" fn ChannelTransactionParameters_get_is_outbound_from_holder(this_ptr: &ChannelTransactionParameters) -> bool {
622         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.is_outbound_from_holder;
623         (*inner_val)
624 }
625 /// Whether the holder is the initiator of this channel.
626 /// This is an input to the commitment number obscure factor computation.
627 #[no_mangle]
628 pub extern "C" fn ChannelTransactionParameters_set_is_outbound_from_holder(this_ptr: &mut ChannelTransactionParameters, mut val: bool) {
629         unsafe { &mut *this_ptr.inner }.is_outbound_from_holder = val;
630 }
631 /// The late-bound counterparty channel transaction parameters.
632 /// These parameters are populated at the point in the protocol where the counterparty provides them.
633 #[no_mangle]
634 pub extern "C" fn ChannelTransactionParameters_get_counterparty_parameters(this_ptr: &ChannelTransactionParameters) -> crate::ln::chan_utils::CounterpartyChannelTransactionParameters {
635         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_parameters;
636         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 };
637         local_inner_val
638 }
639 /// The late-bound counterparty channel transaction parameters.
640 /// These parameters are populated at the point in the protocol where the counterparty provides them.
641 #[no_mangle]
642 pub extern "C" fn ChannelTransactionParameters_set_counterparty_parameters(this_ptr: &mut ChannelTransactionParameters, mut val: crate::ln::chan_utils::CounterpartyChannelTransactionParameters) {
643         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
644         unsafe { &mut *this_ptr.inner }.counterparty_parameters = local_val;
645 }
646 /// The late-bound funding outpoint
647 #[no_mangle]
648 pub extern "C" fn ChannelTransactionParameters_get_funding_outpoint(this_ptr: &ChannelTransactionParameters) -> crate::chain::transaction::OutPoint {
649         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_outpoint;
650         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 };
651         local_inner_val
652 }
653 /// The late-bound funding outpoint
654 #[no_mangle]
655 pub extern "C" fn ChannelTransactionParameters_set_funding_outpoint(this_ptr: &mut ChannelTransactionParameters, mut val: crate::chain::transaction::OutPoint) {
656         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
657         unsafe { &mut *this_ptr.inner }.funding_outpoint = local_val;
658 }
659 #[must_use]
660 #[no_mangle]
661 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 {
662         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()) } }) };
663         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()) } }) };
664         ChannelTransactionParameters { inner: Box::into_raw(Box::new(nativeChannelTransactionParameters {
665                 holder_pubkeys: *unsafe { Box::from_raw(holder_pubkeys_arg.take_inner()) },
666                 holder_selected_contest_delay: holder_selected_contest_delay_arg,
667                 is_outbound_from_holder: is_outbound_from_holder_arg,
668                 counterparty_parameters: local_counterparty_parameters_arg,
669                 funding_outpoint: local_funding_outpoint_arg,
670         })), is_owned: true }
671 }
672 impl Clone for ChannelTransactionParameters {
673         fn clone(&self) -> Self {
674                 Self {
675                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
676                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
677                         is_owned: true,
678                 }
679         }
680 }
681 #[allow(unused)]
682 /// Used only if an object of this type is returned as a trait impl by a method
683 pub(crate) extern "C" fn ChannelTransactionParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
684         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelTransactionParameters)).clone() })) as *mut c_void
685 }
686 #[no_mangle]
687 pub extern "C" fn ChannelTransactionParameters_clone(orig: &ChannelTransactionParameters) -> ChannelTransactionParameters {
688         orig.clone()
689 }
690
691 use lightning::ln::chan_utils::CounterpartyChannelTransactionParameters as nativeCounterpartyChannelTransactionParametersImport;
692 type nativeCounterpartyChannelTransactionParameters = nativeCounterpartyChannelTransactionParametersImport;
693
694 /// Late-bound per-channel counterparty data used to build transactions.
695 #[must_use]
696 #[repr(C)]
697 pub struct CounterpartyChannelTransactionParameters {
698         /// Nearly everywhere, inner must be non-null, however in places where
699         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
700         pub inner: *mut nativeCounterpartyChannelTransactionParameters,
701         pub is_owned: bool,
702 }
703
704 impl Drop for CounterpartyChannelTransactionParameters {
705         fn drop(&mut self) {
706                 if self.is_owned && !self.inner.is_null() {
707                         let _ = unsafe { Box::from_raw(self.inner) };
708                 }
709         }
710 }
711 #[no_mangle]
712 pub extern "C" fn CounterpartyChannelTransactionParameters_free(this_ptr: CounterpartyChannelTransactionParameters) { }
713 #[allow(unused)]
714 /// Used only if an object of this type is returned as a trait impl by a method
715 extern "C" fn CounterpartyChannelTransactionParameters_free_void(this_ptr: *mut c_void) {
716         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCounterpartyChannelTransactionParameters); }
717 }
718 #[allow(unused)]
719 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
720 impl CounterpartyChannelTransactionParameters {
721         pub(crate) fn take_inner(mut self) -> *mut nativeCounterpartyChannelTransactionParameters {
722                 assert!(self.is_owned);
723                 let ret = self.inner;
724                 self.inner = std::ptr::null_mut();
725                 ret
726         }
727 }
728 /// Counter-party public keys
729 #[no_mangle]
730 pub extern "C" fn CounterpartyChannelTransactionParameters_get_pubkeys(this_ptr: &CounterpartyChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
731         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.pubkeys;
732         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
733 }
734 /// Counter-party public keys
735 #[no_mangle]
736 pub extern "C" fn CounterpartyChannelTransactionParameters_set_pubkeys(this_ptr: &mut CounterpartyChannelTransactionParameters, mut val: crate::ln::chan_utils::ChannelPublicKeys) {
737         unsafe { &mut *this_ptr.inner }.pubkeys = *unsafe { Box::from_raw(val.take_inner()) };
738 }
739 /// The contest delay selected by the counterparty, which applies to holder-broadcast transactions
740 #[no_mangle]
741 pub extern "C" fn CounterpartyChannelTransactionParameters_get_selected_contest_delay(this_ptr: &CounterpartyChannelTransactionParameters) -> u16 {
742         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.selected_contest_delay;
743         (*inner_val)
744 }
745 /// The contest delay selected by the counterparty, which applies to holder-broadcast transactions
746 #[no_mangle]
747 pub extern "C" fn CounterpartyChannelTransactionParameters_set_selected_contest_delay(this_ptr: &mut CounterpartyChannelTransactionParameters, mut val: u16) {
748         unsafe { &mut *this_ptr.inner }.selected_contest_delay = val;
749 }
750 #[must_use]
751 #[no_mangle]
752 pub extern "C" fn CounterpartyChannelTransactionParameters_new(mut pubkeys_arg: crate::ln::chan_utils::ChannelPublicKeys, mut selected_contest_delay_arg: u16) -> CounterpartyChannelTransactionParameters {
753         CounterpartyChannelTransactionParameters { inner: Box::into_raw(Box::new(nativeCounterpartyChannelTransactionParameters {
754                 pubkeys: *unsafe { Box::from_raw(pubkeys_arg.take_inner()) },
755                 selected_contest_delay: selected_contest_delay_arg,
756         })), is_owned: true }
757 }
758 impl Clone for CounterpartyChannelTransactionParameters {
759         fn clone(&self) -> Self {
760                 Self {
761                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
762                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
763                         is_owned: true,
764                 }
765         }
766 }
767 #[allow(unused)]
768 /// Used only if an object of this type is returned as a trait impl by a method
769 pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
770         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeCounterpartyChannelTransactionParameters)).clone() })) as *mut c_void
771 }
772 #[no_mangle]
773 pub extern "C" fn CounterpartyChannelTransactionParameters_clone(orig: &CounterpartyChannelTransactionParameters) -> CounterpartyChannelTransactionParameters {
774         orig.clone()
775 }
776 /// Whether the late bound parameters are populated.
777 #[must_use]
778 #[no_mangle]
779 pub extern "C" fn ChannelTransactionParameters_is_populated(this_arg: &ChannelTransactionParameters) -> bool {
780         let mut ret = unsafe { &*this_arg.inner }.is_populated();
781         ret
782 }
783
784 /// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
785 /// given that the holder is the broadcaster.
786 ///
787 /// self.is_populated() must be true before calling this function.
788 #[must_use]
789 #[no_mangle]
790 pub extern "C" fn ChannelTransactionParameters_as_holder_broadcastable(this_arg: &ChannelTransactionParameters) -> crate::ln::chan_utils::DirectedChannelTransactionParameters {
791         let mut ret = unsafe { &*this_arg.inner }.as_holder_broadcastable();
792         crate::ln::chan_utils::DirectedChannelTransactionParameters { inner: Box::into_raw(Box::new(ret)), is_owned: true }
793 }
794
795 /// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
796 /// given that the counterparty is the broadcaster.
797 ///
798 /// self.is_populated() must be true before calling this function.
799 #[must_use]
800 #[no_mangle]
801 pub extern "C" fn ChannelTransactionParameters_as_counterparty_broadcastable(this_arg: &ChannelTransactionParameters) -> crate::ln::chan_utils::DirectedChannelTransactionParameters {
802         let mut ret = unsafe { &*this_arg.inner }.as_counterparty_broadcastable();
803         crate::ln::chan_utils::DirectedChannelTransactionParameters { inner: Box::into_raw(Box::new(ret)), is_owned: true }
804 }
805
806 #[no_mangle]
807 pub extern "C" fn CounterpartyChannelTransactionParameters_write(obj: &CounterpartyChannelTransactionParameters) -> crate::c_types::derived::CVec_u8Z {
808         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
809 }
810 #[no_mangle]
811 pub(crate) extern "C" fn CounterpartyChannelTransactionParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
812         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeCounterpartyChannelTransactionParameters) })
813 }
814 #[no_mangle]
815 pub extern "C" fn CounterpartyChannelTransactionParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_CounterpartyChannelTransactionParametersDecodeErrorZ {
816         let res = crate::c_types::deserialize_obj(ser);
817         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() };
818         local_res
819 }
820 #[no_mangle]
821 pub extern "C" fn ChannelTransactionParameters_write(obj: &ChannelTransactionParameters) -> crate::c_types::derived::CVec_u8Z {
822         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
823 }
824 #[no_mangle]
825 pub(crate) extern "C" fn ChannelTransactionParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
826         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelTransactionParameters) })
827 }
828 #[no_mangle]
829 pub extern "C" fn ChannelTransactionParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelTransactionParametersDecodeErrorZ {
830         let res = crate::c_types::deserialize_obj(ser);
831         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() };
832         local_res
833 }
834
835 use lightning::ln::chan_utils::DirectedChannelTransactionParameters as nativeDirectedChannelTransactionParametersImport;
836 type nativeDirectedChannelTransactionParameters = nativeDirectedChannelTransactionParametersImport<'static>;
837
838 /// Static channel fields used to build transactions given per-commitment fields, organized by
839 /// broadcaster/countersignatory.
840 ///
841 /// This is derived from the holder/counterparty-organized ChannelTransactionParameters via the
842 /// as_holder_broadcastable and as_counterparty_broadcastable functions.
843 #[must_use]
844 #[repr(C)]
845 pub struct DirectedChannelTransactionParameters {
846         /// Nearly everywhere, inner must be non-null, however in places where
847         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
848         pub inner: *mut nativeDirectedChannelTransactionParameters,
849         pub is_owned: bool,
850 }
851
852 impl Drop for DirectedChannelTransactionParameters {
853         fn drop(&mut self) {
854                 if self.is_owned && !self.inner.is_null() {
855                         let _ = unsafe { Box::from_raw(self.inner) };
856                 }
857         }
858 }
859 #[no_mangle]
860 pub extern "C" fn DirectedChannelTransactionParameters_free(this_ptr: DirectedChannelTransactionParameters) { }
861 #[allow(unused)]
862 /// Used only if an object of this type is returned as a trait impl by a method
863 extern "C" fn DirectedChannelTransactionParameters_free_void(this_ptr: *mut c_void) {
864         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDirectedChannelTransactionParameters); }
865 }
866 #[allow(unused)]
867 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
868 impl DirectedChannelTransactionParameters {
869         pub(crate) fn take_inner(mut self) -> *mut nativeDirectedChannelTransactionParameters {
870                 assert!(self.is_owned);
871                 let ret = self.inner;
872                 self.inner = std::ptr::null_mut();
873                 ret
874         }
875 }
876 /// Get the channel pubkeys for the broadcaster
877 #[must_use]
878 #[no_mangle]
879 pub extern "C" fn DirectedChannelTransactionParameters_broadcaster_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
880         let mut ret = unsafe { &*this_arg.inner }.broadcaster_pubkeys();
881         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
882 }
883
884 /// Get the channel pubkeys for the countersignatory
885 #[must_use]
886 #[no_mangle]
887 pub extern "C" fn DirectedChannelTransactionParameters_countersignatory_pubkeys(this_arg: &DirectedChannelTransactionParameters) -> crate::ln::chan_utils::ChannelPublicKeys {
888         let mut ret = unsafe { &*this_arg.inner }.countersignatory_pubkeys();
889         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
890 }
891
892 /// Get the contest delay applicable to the transactions.
893 /// Note that the contest delay was selected by the countersignatory.
894 #[must_use]
895 #[no_mangle]
896 pub extern "C" fn DirectedChannelTransactionParameters_contest_delay(this_arg: &DirectedChannelTransactionParameters) -> u16 {
897         let mut ret = unsafe { &*this_arg.inner }.contest_delay();
898         ret
899 }
900
901 /// Whether the channel is outbound from the broadcaster.
902 ///
903 /// The boolean representing the side that initiated the channel is
904 /// an input to the commitment number obscure factor computation.
905 #[must_use]
906 #[no_mangle]
907 pub extern "C" fn DirectedChannelTransactionParameters_is_outbound(this_arg: &DirectedChannelTransactionParameters) -> bool {
908         let mut ret = unsafe { &*this_arg.inner }.is_outbound();
909         ret
910 }
911
912 /// The funding outpoint
913 #[must_use]
914 #[no_mangle]
915 pub extern "C" fn DirectedChannelTransactionParameters_funding_outpoint(this_arg: &DirectedChannelTransactionParameters) -> crate::chain::transaction::OutPoint {
916         let mut ret = unsafe { &*this_arg.inner }.funding_outpoint();
917         crate::c_types::bitcoin_to_C_outpoint(ret)
918 }
919
920
921 use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport;
922 type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport;
923
924 /// Information needed to build and sign a holder's commitment transaction.
925 ///
926 /// The transaction is only signed once we are ready to broadcast.
927 #[must_use]
928 #[repr(C)]
929 pub struct HolderCommitmentTransaction {
930         /// Nearly everywhere, inner must be non-null, however in places where
931         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
932         pub inner: *mut nativeHolderCommitmentTransaction,
933         pub is_owned: bool,
934 }
935
936 impl Drop for HolderCommitmentTransaction {
937         fn drop(&mut self) {
938                 if self.is_owned && !self.inner.is_null() {
939                         let _ = unsafe { Box::from_raw(self.inner) };
940                 }
941         }
942 }
943 #[no_mangle]
944 pub extern "C" fn HolderCommitmentTransaction_free(this_ptr: HolderCommitmentTransaction) { }
945 #[allow(unused)]
946 /// Used only if an object of this type is returned as a trait impl by a method
947 extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) {
948         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHolderCommitmentTransaction); }
949 }
950 #[allow(unused)]
951 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
952 impl HolderCommitmentTransaction {
953         pub(crate) fn take_inner(mut self) -> *mut nativeHolderCommitmentTransaction {
954                 assert!(self.is_owned);
955                 let ret = self.inner;
956                 self.inner = std::ptr::null_mut();
957                 ret
958         }
959 }
960 /// Our counterparty's signature for the transaction
961 #[no_mangle]
962 pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature {
963         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig;
964         crate::c_types::Signature::from_rust(&(*inner_val))
965 }
966 /// Our counterparty's signature for the transaction
967 #[no_mangle]
968 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) {
969         unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust();
970 }
971 /// All non-dust counterparty HTLC signatures, in the order they appear in the transaction
972 #[no_mangle]
973 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_htlc_sigs(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_SignatureZ) {
974         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item.into_rust() }); };
975         unsafe { &mut *this_ptr.inner }.counterparty_htlc_sigs = local_val;
976 }
977 impl Clone for HolderCommitmentTransaction {
978         fn clone(&self) -> Self {
979                 Self {
980                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
981                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
982                         is_owned: true,
983                 }
984         }
985 }
986 #[allow(unused)]
987 /// Used only if an object of this type is returned as a trait impl by a method
988 pub(crate) extern "C" fn HolderCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
989         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHolderCommitmentTransaction)).clone() })) as *mut c_void
990 }
991 #[no_mangle]
992 pub extern "C" fn HolderCommitmentTransaction_clone(orig: &HolderCommitmentTransaction) -> HolderCommitmentTransaction {
993         orig.clone()
994 }
995 #[no_mangle]
996 pub extern "C" fn HolderCommitmentTransaction_write(obj: &HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
997         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
998 }
999 #[no_mangle]
1000 pub(crate) extern "C" fn HolderCommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1001         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHolderCommitmentTransaction) })
1002 }
1003 #[no_mangle]
1004 pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HolderCommitmentTransactionDecodeErrorZ {
1005         let res = crate::c_types::deserialize_obj(ser);
1006         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() };
1007         local_res
1008 }
1009 /// Create a new holder transaction with the given counterparty signatures.
1010 /// The funding keys are used to figure out which signature should go first when building the transaction for broadcast.
1011 #[must_use]
1012 #[no_mangle]
1013 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 {
1014         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() }); };
1015         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());
1016         HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1017 }
1018
1019
1020 use lightning::ln::chan_utils::BuiltCommitmentTransaction as nativeBuiltCommitmentTransactionImport;
1021 type nativeBuiltCommitmentTransaction = nativeBuiltCommitmentTransactionImport;
1022
1023 /// A pre-built Bitcoin commitment transaction and its txid.
1024 #[must_use]
1025 #[repr(C)]
1026 pub struct BuiltCommitmentTransaction {
1027         /// Nearly everywhere, inner must be non-null, however in places where
1028         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1029         pub inner: *mut nativeBuiltCommitmentTransaction,
1030         pub is_owned: bool,
1031 }
1032
1033 impl Drop for BuiltCommitmentTransaction {
1034         fn drop(&mut self) {
1035                 if self.is_owned && !self.inner.is_null() {
1036                         let _ = unsafe { Box::from_raw(self.inner) };
1037                 }
1038         }
1039 }
1040 #[no_mangle]
1041 pub extern "C" fn BuiltCommitmentTransaction_free(this_ptr: BuiltCommitmentTransaction) { }
1042 #[allow(unused)]
1043 /// Used only if an object of this type is returned as a trait impl by a method
1044 extern "C" fn BuiltCommitmentTransaction_free_void(this_ptr: *mut c_void) {
1045         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeBuiltCommitmentTransaction); }
1046 }
1047 #[allow(unused)]
1048 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1049 impl BuiltCommitmentTransaction {
1050         pub(crate) fn take_inner(mut self) -> *mut nativeBuiltCommitmentTransaction {
1051                 assert!(self.is_owned);
1052                 let ret = self.inner;
1053                 self.inner = std::ptr::null_mut();
1054                 ret
1055         }
1056 }
1057 /// The commitment transaction
1058 #[no_mangle]
1059 pub extern "C" fn BuiltCommitmentTransaction_get_transaction(this_ptr: &BuiltCommitmentTransaction) -> crate::c_types::Transaction {
1060         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.transaction;
1061         let mut local_inner_val = ::bitcoin::consensus::encode::serialize(inner_val);
1062         crate::c_types::Transaction::from_vec(local_inner_val)
1063 }
1064 /// The commitment transaction
1065 #[no_mangle]
1066 pub extern "C" fn BuiltCommitmentTransaction_set_transaction(this_ptr: &mut BuiltCommitmentTransaction, mut val: crate::c_types::Transaction) {
1067         unsafe { &mut *this_ptr.inner }.transaction = val.into_bitcoin();
1068 }
1069 /// The txid for the commitment transaction.
1070 ///
1071 /// This is provided as a performance optimization, instead of calling transaction.txid()
1072 /// multiple times.
1073 #[no_mangle]
1074 pub extern "C" fn BuiltCommitmentTransaction_get_txid(this_ptr: &BuiltCommitmentTransaction) -> *const [u8; 32] {
1075         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.txid;
1076         (*inner_val).as_inner()
1077 }
1078 /// The txid for the commitment transaction.
1079 ///
1080 /// This is provided as a performance optimization, instead of calling transaction.txid()
1081 /// multiple times.
1082 #[no_mangle]
1083 pub extern "C" fn BuiltCommitmentTransaction_set_txid(this_ptr: &mut BuiltCommitmentTransaction, mut val: crate::c_types::ThirtyTwoBytes) {
1084         unsafe { &mut *this_ptr.inner }.txid = ::bitcoin::hash_types::Txid::from_slice(&val.data[..]).unwrap();
1085 }
1086 #[must_use]
1087 #[no_mangle]
1088 pub extern "C" fn BuiltCommitmentTransaction_new(mut transaction_arg: crate::c_types::Transaction, mut txid_arg: crate::c_types::ThirtyTwoBytes) -> BuiltCommitmentTransaction {
1089         BuiltCommitmentTransaction { inner: Box::into_raw(Box::new(nativeBuiltCommitmentTransaction {
1090                 transaction: transaction_arg.into_bitcoin(),
1091                 txid: ::bitcoin::hash_types::Txid::from_slice(&txid_arg.data[..]).unwrap(),
1092         })), is_owned: true }
1093 }
1094 impl Clone for BuiltCommitmentTransaction {
1095         fn clone(&self) -> Self {
1096                 Self {
1097                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
1098                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
1099                         is_owned: true,
1100                 }
1101         }
1102 }
1103 #[allow(unused)]
1104 /// Used only if an object of this type is returned as a trait impl by a method
1105 pub(crate) extern "C" fn BuiltCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
1106         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeBuiltCommitmentTransaction)).clone() })) as *mut c_void
1107 }
1108 #[no_mangle]
1109 pub extern "C" fn BuiltCommitmentTransaction_clone(orig: &BuiltCommitmentTransaction) -> BuiltCommitmentTransaction {
1110         orig.clone()
1111 }
1112 #[no_mangle]
1113 pub extern "C" fn BuiltCommitmentTransaction_write(obj: &BuiltCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
1114         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1115 }
1116 #[no_mangle]
1117 pub(crate) extern "C" fn BuiltCommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1118         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBuiltCommitmentTransaction) })
1119 }
1120 #[no_mangle]
1121 pub extern "C" fn BuiltCommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BuiltCommitmentTransactionDecodeErrorZ {
1122         let res = crate::c_types::deserialize_obj(ser);
1123         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() };
1124         local_res
1125 }
1126 /// Get the SIGHASH_ALL sighash value of the transaction.
1127 ///
1128 /// This can be used to verify a signature.
1129 #[must_use]
1130 #[no_mangle]
1131 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 {
1132         let mut ret = unsafe { &*this_arg.inner }.get_sighash_all(&::bitcoin::blockdata::script::Script::from(Vec::from(funding_redeemscript.to_slice())), channel_value_satoshis);
1133         crate::c_types::ThirtyTwoBytes { data: ret.as_ref().clone() }
1134 }
1135
1136 /// Sign a transaction, either because we are counter-signing the counterparty's transaction or
1137 /// because we are about to broadcast a holder transaction.
1138 #[must_use]
1139 #[no_mangle]
1140 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 {
1141         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());
1142         crate::c_types::Signature::from_rust(&ret)
1143 }
1144
1145
1146 use lightning::ln::chan_utils::CommitmentTransaction as nativeCommitmentTransactionImport;
1147 type nativeCommitmentTransaction = nativeCommitmentTransactionImport;
1148
1149 /// This class tracks the per-transaction information needed to build a commitment transaction and to
1150 /// actually build it and sign.  It is used for holder transactions that we sign only when needed
1151 /// and for transactions we sign for the counterparty.
1152 ///
1153 /// This class can be used inside a signer implementation to generate a signature given the relevant
1154 /// secret key.
1155 #[must_use]
1156 #[repr(C)]
1157 pub struct CommitmentTransaction {
1158         /// Nearly everywhere, inner must be non-null, however in places where
1159         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1160         pub inner: *mut nativeCommitmentTransaction,
1161         pub is_owned: bool,
1162 }
1163
1164 impl Drop for CommitmentTransaction {
1165         fn drop(&mut self) {
1166                 if self.is_owned && !self.inner.is_null() {
1167                         let _ = unsafe { Box::from_raw(self.inner) };
1168                 }
1169         }
1170 }
1171 #[no_mangle]
1172 pub extern "C" fn CommitmentTransaction_free(this_ptr: CommitmentTransaction) { }
1173 #[allow(unused)]
1174 /// Used only if an object of this type is returned as a trait impl by a method
1175 extern "C" fn CommitmentTransaction_free_void(this_ptr: *mut c_void) {
1176         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeCommitmentTransaction); }
1177 }
1178 #[allow(unused)]
1179 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1180 impl CommitmentTransaction {
1181         pub(crate) fn take_inner(mut self) -> *mut nativeCommitmentTransaction {
1182                 assert!(self.is_owned);
1183                 let ret = self.inner;
1184                 self.inner = std::ptr::null_mut();
1185                 ret
1186         }
1187 }
1188 impl Clone for CommitmentTransaction {
1189         fn clone(&self) -> Self {
1190                 Self {
1191                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
1192                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
1193                         is_owned: true,
1194                 }
1195         }
1196 }
1197 #[allow(unused)]
1198 /// Used only if an object of this type is returned as a trait impl by a method
1199 pub(crate) extern "C" fn CommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
1200         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeCommitmentTransaction)).clone() })) as *mut c_void
1201 }
1202 #[no_mangle]
1203 pub extern "C" fn CommitmentTransaction_clone(orig: &CommitmentTransaction) -> CommitmentTransaction {
1204         orig.clone()
1205 }
1206 #[no_mangle]
1207 pub extern "C" fn CommitmentTransaction_write(obj: &CommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
1208         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1209 }
1210 #[no_mangle]
1211 pub(crate) extern "C" fn CommitmentTransaction_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1212         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeCommitmentTransaction) })
1213 }
1214 #[no_mangle]
1215 pub extern "C" fn CommitmentTransaction_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_CommitmentTransactionDecodeErrorZ {
1216         let res = crate::c_types::deserialize_obj(ser);
1217         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() };
1218         local_res
1219 }
1220 /// The backwards-counting commitment number
1221 #[must_use]
1222 #[no_mangle]
1223 pub extern "C" fn CommitmentTransaction_commitment_number(this_arg: &CommitmentTransaction) -> u64 {
1224         let mut ret = unsafe { &*this_arg.inner }.commitment_number();
1225         ret
1226 }
1227
1228 /// The value to be sent to the broadcaster
1229 #[must_use]
1230 #[no_mangle]
1231 pub extern "C" fn CommitmentTransaction_to_broadcaster_value_sat(this_arg: &CommitmentTransaction) -> u64 {
1232         let mut ret = unsafe { &*this_arg.inner }.to_broadcaster_value_sat();
1233         ret
1234 }
1235
1236 /// The value to be sent to the counterparty
1237 #[must_use]
1238 #[no_mangle]
1239 pub extern "C" fn CommitmentTransaction_to_countersignatory_value_sat(this_arg: &CommitmentTransaction) -> u64 {
1240         let mut ret = unsafe { &*this_arg.inner }.to_countersignatory_value_sat();
1241         ret
1242 }
1243
1244 /// The feerate paid per 1000-weight-unit in this commitment transaction.
1245 #[must_use]
1246 #[no_mangle]
1247 pub extern "C" fn CommitmentTransaction_feerate_per_kw(this_arg: &CommitmentTransaction) -> u32 {
1248         let mut ret = unsafe { &*this_arg.inner }.feerate_per_kw();
1249         ret
1250 }
1251
1252 /// Trust our pre-built transaction and derived transaction creation public keys.
1253 ///
1254 /// Applies a wrapper which allows access to these fields.
1255 ///
1256 /// This should only be used if you fully trust the builder of this object.  It should not
1257 ///\tbe used by an external signer - instead use the verify function.
1258 #[must_use]
1259 #[no_mangle]
1260 pub extern "C" fn CommitmentTransaction_trust(this_arg: &CommitmentTransaction) -> crate::ln::chan_utils::TrustedCommitmentTransaction {
1261         let mut ret = unsafe { &*this_arg.inner }.trust();
1262         crate::ln::chan_utils::TrustedCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1263 }
1264
1265 /// Verify our pre-built transaction and derived transaction creation public keys.
1266 ///
1267 /// Applies a wrapper which allows access to these fields.
1268 ///
1269 /// An external validating signer must call this method before signing
1270 /// or using the built transaction.
1271 #[must_use]
1272 #[no_mangle]
1273 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 {
1274         let mut ret = unsafe { &*this_arg.inner }.verify(unsafe { &*channel_parameters.inner }, unsafe { &*broadcaster_keys.inner }, unsafe { &*countersignatory_keys.inner }, &bitcoin::secp256k1::Secp256k1::new());
1275         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() };
1276         local_ret
1277 }
1278
1279
1280 use lightning::ln::chan_utils::TrustedCommitmentTransaction as nativeTrustedCommitmentTransactionImport;
1281 type nativeTrustedCommitmentTransaction = nativeTrustedCommitmentTransactionImport<'static>;
1282
1283 /// A wrapper on CommitmentTransaction indicating that the derived fields (the built bitcoin
1284 /// transaction and the transaction creation keys) are trusted.
1285 ///
1286 /// See trust() and verify() functions on CommitmentTransaction.
1287 ///
1288 /// This structure implements Deref.
1289 #[must_use]
1290 #[repr(C)]
1291 pub struct TrustedCommitmentTransaction {
1292         /// Nearly everywhere, inner must be non-null, however in places where
1293         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1294         pub inner: *mut nativeTrustedCommitmentTransaction,
1295         pub is_owned: bool,
1296 }
1297
1298 impl Drop for TrustedCommitmentTransaction {
1299         fn drop(&mut self) {
1300                 if self.is_owned && !self.inner.is_null() {
1301                         let _ = unsafe { Box::from_raw(self.inner) };
1302                 }
1303         }
1304 }
1305 #[no_mangle]
1306 pub extern "C" fn TrustedCommitmentTransaction_free(this_ptr: TrustedCommitmentTransaction) { }
1307 #[allow(unused)]
1308 /// Used only if an object of this type is returned as a trait impl by a method
1309 extern "C" fn TrustedCommitmentTransaction_free_void(this_ptr: *mut c_void) {
1310         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTrustedCommitmentTransaction); }
1311 }
1312 #[allow(unused)]
1313 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1314 impl TrustedCommitmentTransaction {
1315         pub(crate) fn take_inner(mut self) -> *mut nativeTrustedCommitmentTransaction {
1316                 assert!(self.is_owned);
1317                 let ret = self.inner;
1318                 self.inner = std::ptr::null_mut();
1319                 ret
1320         }
1321 }
1322 /// The transaction ID of the built Bitcoin transaction
1323 #[must_use]
1324 #[no_mangle]
1325 pub extern "C" fn TrustedCommitmentTransaction_txid(this_arg: &TrustedCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes {
1326         let mut ret = unsafe { &*this_arg.inner }.txid();
1327         crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
1328 }
1329
1330 /// The pre-built Bitcoin commitment transaction
1331 #[must_use]
1332 #[no_mangle]
1333 pub extern "C" fn TrustedCommitmentTransaction_built_transaction(this_arg: &TrustedCommitmentTransaction) -> crate::ln::chan_utils::BuiltCommitmentTransaction {
1334         let mut ret = unsafe { &*this_arg.inner }.built_transaction();
1335         crate::ln::chan_utils::BuiltCommitmentTransaction { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
1336 }
1337
1338 /// The pre-calculated transaction creation public keys.
1339 #[must_use]
1340 #[no_mangle]
1341 pub extern "C" fn TrustedCommitmentTransaction_keys(this_arg: &TrustedCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys {
1342         let mut ret = unsafe { &*this_arg.inner }.keys();
1343         crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
1344 }
1345
1346 /// Get a signature for each HTLC which was included in the commitment transaction (ie for
1347 /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
1348 ///
1349 /// The returned Vec has one entry for each HTLC, and in the same order.
1350 #[must_use]
1351 #[no_mangle]
1352 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 {
1353         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());
1354         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() };
1355         local_ret
1356 }
1357
1358 /// Get the transaction number obscure factor
1359 #[no_mangle]
1360 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 {
1361         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);
1362         ret
1363 }
1364