Overhaul LocalCommitmentTx to new nomenclature
[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(per_commitment_point: crate::c_types::PublicKey, base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeySecpErrorZ {
23         let mut ret = lightning::ln::chan_utils::derive_private_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *base_secret}[..]).unwrap());
24         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
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(per_commitment_point: crate::c_types::PublicKey, base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeySecpErrorZ {
36         let mut ret = lightning::ln::chan_utils::derive_public_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &base_point.into_rust());
37         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
38         local_ret
39 }
40
41 /// Derives a per-commitment-transaction revocation key from its constituent parts.
42 ///
43 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
44 /// generated (ie our own).
45 #[no_mangle]
46 pub extern "C" fn derive_private_revocation_key(per_commitment_secret: *const [u8; 32], revocation_base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeySecpErrorZ {
47         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 { *revocation_base_secret}[..]).unwrap());
48         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
49         local_ret
50 }
51
52 /// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
53 /// the public equivalend of derive_private_revocation_key - using only public keys to derive a
54 /// public key instead of private keys.
55 ///
56 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
57 /// generated (ie our own).
58 #[no_mangle]
59 pub extern "C" fn derive_public_revocation_key(per_commitment_point: crate::c_types::PublicKey, revocation_base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeySecpErrorZ {
60         let mut ret = lightning::ln::chan_utils::derive_public_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &revocation_base_point.into_rust());
61         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
62         local_ret
63 }
64
65
66 use lightning::ln::chan_utils::TxCreationKeys as nativeTxCreationKeysImport;
67 type nativeTxCreationKeys = nativeTxCreationKeysImport;
68
69 /// The set of public keys which are used in the creation of one commitment transaction.
70 /// These are derived from the channel base keys and per-commitment data.
71 ///
72 /// These keys are assumed to be good, either because the code derived them from
73 /// channel basepoints via the new function, or they were obtained via
74 /// PreCalculatedTxCreationKeys.trust_key_derivation because we trusted the source of the
75 /// pre-calculated keys.
76 #[must_use]
77 #[repr(C)]
78 pub struct TxCreationKeys {
79         /// Nearly everyhwere, inner must be non-null, however in places where
80         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
81         pub inner: *mut nativeTxCreationKeys,
82         pub is_owned: bool,
83 }
84
85 impl Drop for TxCreationKeys {
86         fn drop(&mut self) {
87                 if self.is_owned && !self.inner.is_null() {
88                         let _ = unsafe { Box::from_raw(self.inner) };
89                 }
90         }
91 }
92 #[no_mangle]
93 pub extern "C" fn TxCreationKeys_free(this_ptr: TxCreationKeys) { }
94 #[allow(unused)]
95 /// Used only if an object of this type is returned as a trait impl by a method
96 extern "C" fn TxCreationKeys_free_void(this_ptr: *mut c_void) {
97         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTxCreationKeys); }
98 }
99 #[allow(unused)]
100 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
101 impl TxCreationKeys {
102         pub(crate) fn take_ptr(mut self) -> *mut nativeTxCreationKeys {
103                 assert!(self.is_owned);
104                 let ret = self.inner;
105                 self.inner = std::ptr::null_mut();
106                 ret
107         }
108 }
109 impl Clone for TxCreationKeys {
110         fn clone(&self) -> Self {
111                 Self {
112                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
113                         is_owned: true,
114                 }
115         }
116 }
117 #[allow(unused)]
118 /// Used only if an object of this type is returned as a trait impl by a method
119 pub(crate) extern "C" fn TxCreationKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
120         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeTxCreationKeys)).clone() })) as *mut c_void
121 }
122 /// The 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 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 owner of the commitment transaction to
134 /// provide their counterparty the ability to punish them if they broadcast an old state.
135 #[no_mangle]
136 pub extern "C" fn TxCreationKeys_get_revocation_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
137         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_key;
138         crate::c_types::PublicKey::from_rust(&(*inner_val))
139 }
140 /// The revocation key which is used to allow the owner of the commitment transaction to
141 /// provide their counterparty the ability to punish them if they broadcast an old state.
142 #[no_mangle]
143 pub extern "C" fn TxCreationKeys_set_revocation_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
144         unsafe { &mut *this_ptr.inner }.revocation_key = val.into_rust();
145 }
146 /// Broadcaster's HTLC Key
147 #[no_mangle]
148 pub extern "C" fn TxCreationKeys_get_broadcaster_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
149         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key;
150         crate::c_types::PublicKey::from_rust(&(*inner_val))
151 }
152 /// Broadcaster's HTLC Key
153 #[no_mangle]
154 pub extern "C" fn TxCreationKeys_set_broadcaster_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
155         unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key = val.into_rust();
156 }
157 /// Countersignatory's HTLC Key
158 #[no_mangle]
159 pub extern "C" fn TxCreationKeys_get_countersignatory_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
160         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key;
161         crate::c_types::PublicKey::from_rust(&(*inner_val))
162 }
163 /// Countersignatory's HTLC Key
164 #[no_mangle]
165 pub extern "C" fn TxCreationKeys_set_countersignatory_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
166         unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key = val.into_rust();
167 }
168 /// Payment Key (which isn't allowed to be spent from for some delay)
169 #[no_mangle]
170 pub extern "C" fn TxCreationKeys_get_broadcaster_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
171         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key;
172         crate::c_types::PublicKey::from_rust(&(*inner_val))
173 }
174 /// Payment Key (which isn't allowed to be spent from for some delay)
175 #[no_mangle]
176 pub extern "C" fn TxCreationKeys_set_broadcaster_delayed_payment_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
177         unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key = val.into_rust();
178 }
179 #[must_use]
180 #[no_mangle]
181 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 {
182         TxCreationKeys { inner: Box::into_raw(Box::new(nativeTxCreationKeys {
183                 per_commitment_point: per_commitment_point_arg.into_rust(),
184                 revocation_key: revocation_key_arg.into_rust(),
185                 broadcaster_htlc_key: broadcaster_htlc_key_arg.into_rust(),
186                 countersignatory_htlc_key: countersignatory_htlc_key_arg.into_rust(),
187                 broadcaster_delayed_payment_key: broadcaster_delayed_payment_key_arg.into_rust(),
188         })), is_owned: true }
189 }
190 #[no_mangle]
191 pub extern "C" fn TxCreationKeys_write(obj: *const TxCreationKeys) -> crate::c_types::derived::CVec_u8Z {
192         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
193 }
194 #[no_mangle]
195 pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> TxCreationKeys {
196         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
197                 TxCreationKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
198         } else {
199                 TxCreationKeys { inner: std::ptr::null_mut(), is_owned: true }
200         }
201 }
202
203 use lightning::ln::chan_utils::PreCalculatedTxCreationKeys as nativePreCalculatedTxCreationKeysImport;
204 type nativePreCalculatedTxCreationKeys = nativePreCalculatedTxCreationKeysImport;
205
206 /// The per-commitment point and a set of pre-calculated public keys used for transaction creation
207 /// in the signer.
208 /// The pre-calculated keys are an optimization, because ChannelKeys has enough
209 /// information to re-derive them.
210 #[must_use]
211 #[repr(C)]
212 pub struct PreCalculatedTxCreationKeys {
213         /// Nearly everyhwere, inner must be non-null, however in places where
214         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
215         pub inner: *mut nativePreCalculatedTxCreationKeys,
216         pub is_owned: bool,
217 }
218
219 impl Drop for PreCalculatedTxCreationKeys {
220         fn drop(&mut self) {
221                 if self.is_owned && !self.inner.is_null() {
222                         let _ = unsafe { Box::from_raw(self.inner) };
223                 }
224         }
225 }
226 #[no_mangle]
227 pub extern "C" fn PreCalculatedTxCreationKeys_free(this_ptr: PreCalculatedTxCreationKeys) { }
228 #[allow(unused)]
229 /// Used only if an object of this type is returned as a trait impl by a method
230 extern "C" fn PreCalculatedTxCreationKeys_free_void(this_ptr: *mut c_void) {
231         unsafe { let _ = Box::from_raw(this_ptr as *mut nativePreCalculatedTxCreationKeys); }
232 }
233 #[allow(unused)]
234 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
235 impl PreCalculatedTxCreationKeys {
236         pub(crate) fn take_ptr(mut self) -> *mut nativePreCalculatedTxCreationKeys {
237                 assert!(self.is_owned);
238                 let ret = self.inner;
239                 self.inner = std::ptr::null_mut();
240                 ret
241         }
242 }
243 /// Create a new PreCalculatedTxCreationKeys from TxCreationKeys
244 #[must_use]
245 #[no_mangle]
246 pub extern "C" fn PreCalculatedTxCreationKeys_new(mut keys: crate::ln::chan_utils::TxCreationKeys) -> PreCalculatedTxCreationKeys {
247         let mut ret = lightning::ln::chan_utils::PreCalculatedTxCreationKeys::new(*unsafe { Box::from_raw(keys.take_ptr()) });
248         PreCalculatedTxCreationKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
249 }
250
251 /// The pre-calculated transaction creation public keys.
252 /// An external validating signer should not trust these keys.
253 #[must_use]
254 #[no_mangle]
255 pub extern "C" fn PreCalculatedTxCreationKeys_trust_key_derivation(this_arg: &PreCalculatedTxCreationKeys) -> crate::ln::chan_utils::TxCreationKeys {
256         let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation();
257         crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
258 }
259
260 /// The transaction per-commitment point
261 #[must_use]
262 #[no_mangle]
263 pub extern "C" fn PreCalculatedTxCreationKeys_per_commitment_point(this_arg: &PreCalculatedTxCreationKeys) -> crate::c_types::PublicKey {
264         let mut ret = unsafe { &*this_arg.inner }.per_commitment_point();
265         crate::c_types::PublicKey::from_rust(&*ret)
266 }
267
268
269 use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport;
270 type nativeChannelPublicKeys = nativeChannelPublicKeysImport;
271
272 /// One counterparty's public keys which do not change over the life of a channel.
273 #[must_use]
274 #[repr(C)]
275 pub struct ChannelPublicKeys {
276         /// Nearly everyhwere, inner must be non-null, however in places where
277         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
278         pub inner: *mut nativeChannelPublicKeys,
279         pub is_owned: bool,
280 }
281
282 impl Drop for ChannelPublicKeys {
283         fn drop(&mut self) {
284                 if self.is_owned && !self.inner.is_null() {
285                         let _ = unsafe { Box::from_raw(self.inner) };
286                 }
287         }
288 }
289 #[no_mangle]
290 pub extern "C" fn ChannelPublicKeys_free(this_ptr: ChannelPublicKeys) { }
291 #[allow(unused)]
292 /// Used only if an object of this type is returned as a trait impl by a method
293 extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) {
294         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelPublicKeys); }
295 }
296 #[allow(unused)]
297 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
298 impl ChannelPublicKeys {
299         pub(crate) fn take_ptr(mut self) -> *mut nativeChannelPublicKeys {
300                 assert!(self.is_owned);
301                 let ret = self.inner;
302                 self.inner = std::ptr::null_mut();
303                 ret
304         }
305 }
306 impl Clone for ChannelPublicKeys {
307         fn clone(&self) -> Self {
308                 Self {
309                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
310                         is_owned: true,
311                 }
312         }
313 }
314 #[allow(unused)]
315 /// Used only if an object of this type is returned as a trait impl by a method
316 pub(crate) extern "C" fn ChannelPublicKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
317         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelPublicKeys)).clone() })) as *mut c_void
318 }
319 /// The public key which is used to sign all commitment transactions, as it appears in the
320 /// on-chain channel lock-in 2-of-2 multisig output.
321 #[no_mangle]
322 pub extern "C" fn ChannelPublicKeys_get_funding_pubkey(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
323         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_pubkey;
324         crate::c_types::PublicKey::from_rust(&(*inner_val))
325 }
326 /// The public key which is used to sign all commitment transactions, as it appears in the
327 /// on-chain channel lock-in 2-of-2 multisig output.
328 #[no_mangle]
329 pub extern "C" fn ChannelPublicKeys_set_funding_pubkey(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
330         unsafe { &mut *this_ptr.inner }.funding_pubkey = val.into_rust();
331 }
332 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
333 /// revocation keys. This is combined with the per-commitment-secret generated by the
334 /// counterparty to create a secret which the counterparty can reveal to revoke previous
335 /// states.
336 #[no_mangle]
337 pub extern "C" fn ChannelPublicKeys_get_revocation_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
338         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_basepoint;
339         crate::c_types::PublicKey::from_rust(&(*inner_val))
340 }
341 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
342 /// revocation keys. This is combined with the per-commitment-secret generated by the
343 /// counterparty to create a secret which the counterparty can reveal to revoke previous
344 /// states.
345 #[no_mangle]
346 pub extern "C" fn ChannelPublicKeys_set_revocation_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
347         unsafe { &mut *this_ptr.inner }.revocation_basepoint = val.into_rust();
348 }
349 /// The public key which receives our immediately spendable primary channel balance in
350 /// remote-broadcasted commitment transactions. This key is static across every commitment
351 /// transaction.
352 #[no_mangle]
353 pub extern "C" fn ChannelPublicKeys_get_payment_point(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
354         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_point;
355         crate::c_types::PublicKey::from_rust(&(*inner_val))
356 }
357 /// The public key which receives our immediately spendable primary channel balance in
358 /// remote-broadcasted commitment transactions. This key is static across every commitment
359 /// transaction.
360 #[no_mangle]
361 pub extern "C" fn ChannelPublicKeys_set_payment_point(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
362         unsafe { &mut *this_ptr.inner }.payment_point = val.into_rust();
363 }
364 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
365 /// public key which receives non-HTLC-encumbered funds which are only available for spending
366 /// after some delay (or can be claimed via the revocation path).
367 #[no_mangle]
368 pub extern "C" fn ChannelPublicKeys_get_delayed_payment_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
369         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint;
370         crate::c_types::PublicKey::from_rust(&(*inner_val))
371 }
372 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
373 /// public key which receives non-HTLC-encumbered funds which are only available for spending
374 /// after some delay (or can be claimed via the revocation path).
375 #[no_mangle]
376 pub extern "C" fn ChannelPublicKeys_set_delayed_payment_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
377         unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint = val.into_rust();
378 }
379 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
380 /// which is used to encumber HTLC-in-flight outputs.
381 #[no_mangle]
382 pub extern "C" fn ChannelPublicKeys_get_htlc_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
383         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_basepoint;
384         crate::c_types::PublicKey::from_rust(&(*inner_val))
385 }
386 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
387 /// which is used to encumber HTLC-in-flight outputs.
388 #[no_mangle]
389 pub extern "C" fn ChannelPublicKeys_set_htlc_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
390         unsafe { &mut *this_ptr.inner }.htlc_basepoint = val.into_rust();
391 }
392 #[must_use]
393 #[no_mangle]
394 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 {
395         ChannelPublicKeys { inner: Box::into_raw(Box::new(nativeChannelPublicKeys {
396                 funding_pubkey: funding_pubkey_arg.into_rust(),
397                 revocation_basepoint: revocation_basepoint_arg.into_rust(),
398                 payment_point: payment_point_arg.into_rust(),
399                 delayed_payment_basepoint: delayed_payment_basepoint_arg.into_rust(),
400                 htlc_basepoint: htlc_basepoint_arg.into_rust(),
401         })), is_owned: true }
402 }
403 #[no_mangle]
404 pub extern "C" fn ChannelPublicKeys_write(obj: *const ChannelPublicKeys) -> crate::c_types::derived::CVec_u8Z {
405         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
406 }
407 #[no_mangle]
408 pub extern "C" fn ChannelPublicKeys_read(ser: crate::c_types::u8slice) -> ChannelPublicKeys {
409         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
410                 ChannelPublicKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
411         } else {
412                 ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true }
413         }
414 }
415 /// Create a new TxCreationKeys from channel base points and the per-commitment point
416 #[must_use]
417 #[no_mangle]
418 pub extern "C" fn TxCreationKeys_derive_new(per_commitment_point: crate::c_types::PublicKey, a_delayed_payment_base: crate::c_types::PublicKey, a_htlc_base: crate::c_types::PublicKey, b_revocation_base: crate::c_types::PublicKey, b_htlc_base: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_TxCreationKeysSecpErrorZ {
419         let mut ret = lightning::ln::chan_utils::TxCreationKeys::derive_new(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &a_delayed_payment_base.into_rust(), &a_htlc_base.into_rust(), &b_revocation_base.into_rust(), &b_htlc_base.into_rust());
420         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 } }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
421         local_ret
422 }
423
424 /// A script either spendable by the revocation
425 /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
426 /// Encumbering a `to_local` output on a commitment transaction or 2nd-stage HTLC transactions.
427 #[no_mangle]
428 pub extern "C" fn get_revokeable_redeemscript(revocation_key: crate::c_types::PublicKey, mut to_self_delay: u16, broadcaster_delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
429         let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), to_self_delay, &broadcaster_delayed_payment_key.into_rust());
430         ret.into_bytes().into()
431 }
432
433
434 use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport;
435 type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport;
436
437 /// Information about an HTLC as it appears in a commitment transaction
438 #[must_use]
439 #[repr(C)]
440 pub struct HTLCOutputInCommitment {
441         /// Nearly everyhwere, inner must be non-null, however in places where
442         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
443         pub inner: *mut nativeHTLCOutputInCommitment,
444         pub is_owned: bool,
445 }
446
447 impl Drop for HTLCOutputInCommitment {
448         fn drop(&mut self) {
449                 if self.is_owned && !self.inner.is_null() {
450                         let _ = unsafe { Box::from_raw(self.inner) };
451                 }
452         }
453 }
454 #[no_mangle]
455 pub extern "C" fn HTLCOutputInCommitment_free(this_ptr: HTLCOutputInCommitment) { }
456 #[allow(unused)]
457 /// Used only if an object of this type is returned as a trait impl by a method
458 extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) {
459         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCOutputInCommitment); }
460 }
461 #[allow(unused)]
462 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
463 impl HTLCOutputInCommitment {
464         pub(crate) fn take_ptr(mut self) -> *mut nativeHTLCOutputInCommitment {
465                 assert!(self.is_owned);
466                 let ret = self.inner;
467                 self.inner = std::ptr::null_mut();
468                 ret
469         }
470 }
471 impl Clone for HTLCOutputInCommitment {
472         fn clone(&self) -> Self {
473                 Self {
474                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
475                         is_owned: true,
476                 }
477         }
478 }
479 #[allow(unused)]
480 /// Used only if an object of this type is returned as a trait impl by a method
481 pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void {
482         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void
483 }
484 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
485 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
486 /// need to compare this value to whether the commitment transaction in question is that of
487 /// the remote party or our own.
488 #[no_mangle]
489 pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool {
490         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered;
491         (*inner_val)
492 }
493 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
494 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
495 /// need to compare this value to whether the commitment transaction in question is that of
496 /// the remote party or our own.
497 #[no_mangle]
498 pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputInCommitment, mut val: bool) {
499         unsafe { &mut *this_ptr.inner }.offered = val;
500 }
501 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
502 /// this divided by 1000.
503 #[no_mangle]
504 pub extern "C" fn HTLCOutputInCommitment_get_amount_msat(this_ptr: &HTLCOutputInCommitment) -> u64 {
505         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.amount_msat;
506         (*inner_val)
507 }
508 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
509 /// this divided by 1000.
510 #[no_mangle]
511 pub extern "C" fn HTLCOutputInCommitment_set_amount_msat(this_ptr: &mut HTLCOutputInCommitment, mut val: u64) {
512         unsafe { &mut *this_ptr.inner }.amount_msat = val;
513 }
514 /// The CLTV lock-time at which this HTLC expires.
515 #[no_mangle]
516 pub extern "C" fn HTLCOutputInCommitment_get_cltv_expiry(this_ptr: &HTLCOutputInCommitment) -> u32 {
517         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry;
518         (*inner_val)
519 }
520 /// The CLTV lock-time at which this HTLC expires.
521 #[no_mangle]
522 pub extern "C" fn HTLCOutputInCommitment_set_cltv_expiry(this_ptr: &mut HTLCOutputInCommitment, mut val: u32) {
523         unsafe { &mut *this_ptr.inner }.cltv_expiry = val;
524 }
525 /// The hash of the preimage which unlocks this HTLC.
526 #[no_mangle]
527 pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputInCommitment) -> *const [u8; 32] {
528         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_hash;
529         &(*inner_val).0
530 }
531 /// The hash of the preimage which unlocks this HTLC.
532 #[no_mangle]
533 pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) {
534         unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data);
535 }
536 #[no_mangle]
537 pub extern "C" fn HTLCOutputInCommitment_write(obj: *const HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z {
538         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
539 }
540 #[no_mangle]
541 pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> HTLCOutputInCommitment {
542         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
543                 HTLCOutputInCommitment { inner: Box::into_raw(Box::new(res)), is_owned: true }
544         } else {
545                 HTLCOutputInCommitment { inner: std::ptr::null_mut(), is_owned: true }
546         }
547 }
548 /// note here that 'a_revocation_key' is generated using b_revocation_basepoint and a's
549 /// commitment secret. 'htlc' does *not* need to have its previous_output_index filled.
550 #[no_mangle]
551 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 {
552         let mut ret = lightning::ln::chan_utils::get_htlc_redeemscript(unsafe { &*htlc.inner }, unsafe { &*keys.inner });
553         ret.into_bytes().into()
554 }
555
556 /// Gets the redeemscript for a funding output from the two funding public keys.
557 /// Note that the order of funding public keys does not matter.
558 #[no_mangle]
559 pub extern "C" fn make_funding_redeemscript(a: crate::c_types::PublicKey, b: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
560         let mut ret = lightning::ln::chan_utils::make_funding_redeemscript(&a.into_rust(), &b.into_rust());
561         ret.into_bytes().into()
562 }
563
564 /// panics if htlc.transaction_output_index.is_none()!
565 #[no_mangle]
566 pub extern "C" fn build_htlc_transaction(prev_hash: *const [u8; 32], mut feerate_per_kw: u32, mut to_self_delay: u16, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment, broadcaster_delayed_payment_key: crate::c_types::PublicKey, revocation_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
567         let mut ret = lightning::ln::chan_utils::build_htlc_transaction(&::bitcoin::hash_types::Txid::from_slice(&unsafe { &*prev_hash }[..]).unwrap(), feerate_per_kw, to_self_delay, unsafe { &*htlc.inner }, &broadcaster_delayed_payment_key.into_rust(), &revocation_key.into_rust());
568         let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret);
569         local_ret.into()
570 }
571
572
573 use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport;
574 type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport;
575
576 /// We use this to track local commitment transactions and put off signing them until we are ready
577 /// to broadcast. This class can be used inside a signer implementation to generate a signature
578 /// given the relevant secret key.
579 #[must_use]
580 #[repr(C)]
581 pub struct HolderCommitmentTransaction {
582         /// Nearly everyhwere, inner must be non-null, however in places where
583         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
584         pub inner: *mut nativeHolderCommitmentTransaction,
585         pub is_owned: bool,
586 }
587
588 impl Drop for HolderCommitmentTransaction {
589         fn drop(&mut self) {
590                 if self.is_owned && !self.inner.is_null() {
591                         let _ = unsafe { Box::from_raw(self.inner) };
592                 }
593         }
594 }
595 #[no_mangle]
596 pub extern "C" fn HolderCommitmentTransaction_free(this_ptr: HolderCommitmentTransaction) { }
597 #[allow(unused)]
598 /// Used only if an object of this type is returned as a trait impl by a method
599 extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) {
600         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHolderCommitmentTransaction); }
601 }
602 #[allow(unused)]
603 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
604 impl HolderCommitmentTransaction {
605         pub(crate) fn take_ptr(mut self) -> *mut nativeHolderCommitmentTransaction {
606                 assert!(self.is_owned);
607                 let ret = self.inner;
608                 self.inner = std::ptr::null_mut();
609                 ret
610         }
611 }
612 impl Clone for HolderCommitmentTransaction {
613         fn clone(&self) -> Self {
614                 Self {
615                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
616                         is_owned: true,
617                 }
618         }
619 }
620 #[allow(unused)]
621 /// Used only if an object of this type is returned as a trait impl by a method
622 pub(crate) extern "C" fn HolderCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
623         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHolderCommitmentTransaction)).clone() })) as *mut c_void
624 }
625 /// The commitment transaction itself, in unsigned form.
626 #[no_mangle]
627 pub extern "C" fn HolderCommitmentTransaction_get_unsigned_tx(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
628         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.unsigned_tx;
629         let mut local_inner_val = ::bitcoin::consensus::encode::serialize(inner_val);
630         local_inner_val.into()
631 }
632 /// The commitment transaction itself, in unsigned form.
633 #[no_mangle]
634 pub extern "C" fn HolderCommitmentTransaction_set_unsigned_tx(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_u8Z) {
635         unsafe { &mut *this_ptr.inner }.unsigned_tx = ::bitcoin::consensus::encode::deserialize(&val.into_rust()[..]).unwrap();
636 }
637 /// Our counterparty's signature for the transaction, above.
638 #[no_mangle]
639 pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature {
640         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig;
641         crate::c_types::Signature::from_rust(&(*inner_val))
642 }
643 /// Our counterparty's signature for the transaction, above.
644 #[no_mangle]
645 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) {
646         unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust();
647 }
648 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
649 /// controlled by the channel initiator.
650 #[no_mangle]
651 pub extern "C" fn HolderCommitmentTransaction_get_feerate_per_kw(this_ptr: &HolderCommitmentTransaction) -> u32 {
652         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.feerate_per_kw;
653         (*inner_val)
654 }
655 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
656 /// controlled by the channel initiator.
657 #[no_mangle]
658 pub extern "C" fn HolderCommitmentTransaction_set_feerate_per_kw(this_ptr: &mut HolderCommitmentTransaction, mut val: u32) {
659         unsafe { &mut *this_ptr.inner }.feerate_per_kw = val;
660 }
661 /// The HTLCs and remote htlc signatures which were included in this commitment transaction.
662 ///
663 /// Note that this includes all HTLCs, including ones which were considered dust and not
664 /// actually included in the transaction as it appears on-chain, but who's value is burned as
665 /// fees and not included in the to_local or to_remote outputs.
666 ///
667 /// The remote HTLC signatures in the second element will always be set for non-dust HTLCs, ie
668 /// those for which transaction_output_index.is_some().
669 #[no_mangle]
670 pub extern "C" fn HolderCommitmentTransaction_set_per_htlc(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_C2Tuple_HTLCOutputInCommitmentSignatureZZ) {
671         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { let (mut orig_val_0_0, mut orig_val_0_1) = item.to_rust(); let mut local_orig_val_0_1 = if orig_val_0_1.is_null() { None } else { Some( { orig_val_0_1.into_rust() }) }; let mut local_val_0 = (*unsafe { Box::from_raw(orig_val_0_0.take_ptr()) }, local_orig_val_0_1); local_val_0 }); };
672         unsafe { &mut *this_ptr.inner }.per_htlc = local_val;
673 }
674 /// Generate a new HolderCommitmentTransaction based on a raw commitment transaction,
675 /// remote signature and both parties keys.
676 ///
677 /// The unsigned transaction outputs must be consistent with htlc_data.  This function
678 /// only checks that the shape and amounts are consistent, but does not check the scriptPubkey.
679 #[must_use]
680 #[no_mangle]
681 pub extern "C" fn HolderCommitmentTransaction_new_missing_holder_sig(mut unsigned_tx: crate::c_types::derived::CVec_u8Z, mut counterparty_sig: crate::c_types::Signature, our_funding_key: crate::c_types::PublicKey, their_funding_key: crate::c_types::PublicKey, mut local_keys: crate::ln::chan_utils::TxCreationKeys, mut feerate_per_kw: u32, mut htlc_data: crate::c_types::derived::CVec_C2Tuple_HTLCOutputInCommitmentSignatureZZ) -> crate::ln::chan_utils::HolderCommitmentTransaction {
682         let mut local_htlc_data = Vec::new(); for mut item in htlc_data.into_rust().drain(..) { local_htlc_data.push( { let (mut orig_htlc_data_0_0, mut orig_htlc_data_0_1) = item.to_rust(); let mut local_orig_htlc_data_0_1 = if orig_htlc_data_0_1.is_null() { None } else { Some( { orig_htlc_data_0_1.into_rust() }) }; let mut local_htlc_data_0 = (*unsafe { Box::from_raw(orig_htlc_data_0_0.take_ptr()) }, local_orig_htlc_data_0_1); local_htlc_data_0 }); };
683         let mut ret = lightning::ln::chan_utils::HolderCommitmentTransaction::new_missing_holder_sig(::bitcoin::consensus::encode::deserialize(&unsigned_tx.into_rust()[..]).unwrap(), counterparty_sig.into_rust(), &our_funding_key.into_rust(), &their_funding_key.into_rust(), *unsafe { Box::from_raw(local_keys.take_ptr()) }, feerate_per_kw, local_htlc_data);
684         crate::ln::chan_utils::HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
685 }
686
687 /// The pre-calculated transaction creation public keys.
688 /// An external validating signer should not trust these keys.
689 #[must_use]
690 #[no_mangle]
691 pub extern "C" fn HolderCommitmentTransaction_trust_key_derivation(this_arg: &HolderCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys {
692         let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation();
693         crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
694 }
695
696 /// Get the txid of the local commitment transaction contained in this
697 /// HolderCommitmentTransaction
698 #[must_use]
699 #[no_mangle]
700 pub extern "C" fn HolderCommitmentTransaction_txid(this_arg: &HolderCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes {
701         let mut ret = unsafe { &*this_arg.inner }.txid();
702         crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
703 }
704
705 /// Gets our signature for the contained commitment transaction given our funding private key.
706 ///
707 /// Funding key is your key included in the 2-2 funding_outpoint lock. Should be provided
708 /// by your ChannelKeys.
709 /// Funding redeemscript is script locking funding_outpoint. This is the mutlsig script
710 /// between your own funding key and your counterparty's. Currently, this is provided in
711 /// ChannelKeys::sign_local_commitment() calls directly.
712 /// Channel value is amount locked in funding_outpoint.
713 #[must_use]
714 #[no_mangle]
715 pub extern "C" fn HolderCommitmentTransaction_get_holder_sig(this_arg: &HolderCommitmentTransaction, funding_key: *const [u8; 32], funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::Signature {
716         let mut ret = unsafe { &*this_arg.inner }.get_holder_sig(&::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());
717         crate::c_types::Signature::from_rust(&ret)
718 }
719
720 /// Get a signature for each HTLC which was included in the commitment transaction (ie for
721 /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
722 ///
723 /// The returned Vec has one entry for each HTLC, and in the same order. For HTLCs which were
724 /// considered dust and not included, a None entry exists, for all others a signature is
725 /// included.
726 #[must_use]
727 #[no_mangle]
728 pub extern "C" fn HolderCommitmentTransaction_get_htlc_sigs(this_arg: &HolderCommitmentTransaction, htlc_base_key: *const [u8; 32], mut local_csv: u16) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ {
729         let mut ret = unsafe { &*this_arg.inner }.get_htlc_sigs(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *htlc_base_key}[..]).unwrap(), local_csv, &bitcoin::secp256k1::Secp256k1::new());
730         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( { let mut local_ret_0_0 = if item.is_none() { crate::c_types::Signature::null() } else {  { crate::c_types::Signature::from_rust(&(item.unwrap())) } }; local_ret_0_0 }); }; local_ret_0.into() }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
731         local_ret
732 }
733
734 #[no_mangle]
735 pub extern "C" fn HolderCommitmentTransaction_write(obj: *const HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
736         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
737 }
738 #[no_mangle]
739 pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> HolderCommitmentTransaction {
740         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
741                 HolderCommitmentTransaction { inner: Box::into_raw(Box::new(res)), is_owned: true }
742         } else {
743                 HolderCommitmentTransaction { inner: std::ptr::null_mut(), is_owned: true }
744         }
745 }