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