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
6 use bitcoin::hashes::Hash;
9 /// Build the commitment secret from the seed and the commitment number
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 }
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.
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).
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) }) };
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.
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).
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) }) };
41 /// Derives a per-commitment-transaction revocation key from its constituent parts.
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).
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) }) };
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.
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).
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) }) };
66 use lightning::ln::chan_utils::TxCreationKeys as nativeTxCreationKeysImport;
67 type nativeTxCreationKeys = nativeTxCreationKeysImport;
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.
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.
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,
85 impl Drop for TxCreationKeys {
87 if self.is_owned && !self.inner.is_null() {
88 let _ = unsafe { Box::from_raw(self.inner) };
93 pub extern "C" fn TxCreationKeys_free(this_ptr: TxCreationKeys) { }
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); }
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();
109 impl Clone for TxCreationKeys {
110 fn clone(&self) -> Self {
112 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
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
122 /// The per-commitment public key which was used to derive the other keys.
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))
128 /// The per-commitment public key which was used to derive the other keys.
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();
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.
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))
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.
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();
146 /// Broadcaster's HTLC Key
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))
152 /// Broadcaster's HTLC Key
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();
157 /// Countersignatory's HTLC Key
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))
163 /// Countersignatory's HTLC Key
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();
168 /// Payment Key (which isn't allowed to be spent from for some delay)
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))
174 /// Payment Key (which isn't allowed to be spent from for some delay)
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();
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 }
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) })
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 }
199 TxCreationKeys { inner: std::ptr::null_mut(), is_owned: true }
203 use lightning::ln::chan_utils::PreCalculatedTxCreationKeys as nativePreCalculatedTxCreationKeysImport;
204 type nativePreCalculatedTxCreationKeys = nativePreCalculatedTxCreationKeysImport;
206 /// The per-commitment point and a set of pre-calculated public keys used for transaction creation
208 /// The pre-calculated keys are an optimization, because ChannelKeys has enough
209 /// information to re-derive them.
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,
219 impl Drop for PreCalculatedTxCreationKeys {
221 if self.is_owned && !self.inner.is_null() {
222 let _ = unsafe { Box::from_raw(self.inner) };
227 pub extern "C" fn PreCalculatedTxCreationKeys_free(this_ptr: PreCalculatedTxCreationKeys) { }
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); }
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();
243 /// Create a new PreCalculatedTxCreationKeys from TxCreationKeys
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 }
251 /// The pre-calculated transaction creation public keys.
252 /// An external validating signer should not trust these keys.
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 }
260 /// The transaction per-commitment point
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)
269 use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport;
270 type nativeChannelPublicKeys = nativeChannelPublicKeysImport;
272 /// One counterparty's public keys which do not change over the life of a channel.
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,
282 impl Drop for ChannelPublicKeys {
284 if self.is_owned && !self.inner.is_null() {
285 let _ = unsafe { Box::from_raw(self.inner) };
290 pub extern "C" fn ChannelPublicKeys_free(this_ptr: ChannelPublicKeys) { }
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); }
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();
306 impl Clone for ChannelPublicKeys {
307 fn clone(&self) -> Self {
309 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
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
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.
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))
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.
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();
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
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))
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
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();
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
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))
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
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();
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).
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))
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).
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();
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.
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))
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.
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();
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 }
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) })
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 }
412 ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true }
415 /// Create a new TxCreationKeys from channel base points and the per-commitment point
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) }) };
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.
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()
434 use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport;
435 type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport;
437 /// Information about an HTLC as it appears in a commitment transaction
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,
447 impl Drop for HTLCOutputInCommitment {
449 if self.is_owned && !self.inner.is_null() {
450 let _ = unsafe { Box::from_raw(self.inner) };
455 pub extern "C" fn HTLCOutputInCommitment_free(this_ptr: HTLCOutputInCommitment) { }
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); }
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();
471 impl Clone for HTLCOutputInCommitment {
472 fn clone(&self) -> Self {
474 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
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
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.
489 pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool {
490 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered;
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.
498 pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputInCommitment, mut val: bool) {
499 unsafe { &mut *this_ptr.inner }.offered = val;
501 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
502 /// this divided by 1000.
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;
508 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
509 /// this divided by 1000.
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;
514 /// The CLTV lock-time at which this HTLC expires.
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;
520 /// The CLTV lock-time at which this HTLC expires.
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;
525 /// The hash of the preimage which unlocks this HTLC.
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;
531 /// The hash of the preimage which unlocks this HTLC.
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);
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) })
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 }
545 HTLCOutputInCommitment { inner: std::ptr::null_mut(), is_owned: true }
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.
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()
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.
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()
564 /// panics if htlc.transaction_output_index.is_none()!
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);
573 use lightning::ln::chan_utils::LocalCommitmentTransaction as nativeLocalCommitmentTransactionImport;
574 type nativeLocalCommitmentTransaction = nativeLocalCommitmentTransactionImport;
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.
581 pub struct LocalCommitmentTransaction {
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 nativeLocalCommitmentTransaction,
588 impl Drop for LocalCommitmentTransaction {
590 if self.is_owned && !self.inner.is_null() {
591 let _ = unsafe { Box::from_raw(self.inner) };
596 pub extern "C" fn LocalCommitmentTransaction_free(this_ptr: LocalCommitmentTransaction) { }
598 /// Used only if an object of this type is returned as a trait impl by a method
599 extern "C" fn LocalCommitmentTransaction_free_void(this_ptr: *mut c_void) {
600 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeLocalCommitmentTransaction); }
603 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
604 impl LocalCommitmentTransaction {
605 pub(crate) fn take_ptr(mut self) -> *mut nativeLocalCommitmentTransaction {
606 assert!(self.is_owned);
607 let ret = self.inner;
608 self.inner = std::ptr::null_mut();
612 impl Clone for LocalCommitmentTransaction {
613 fn clone(&self) -> Self {
615 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
621 /// Used only if an object of this type is returned as a trait impl by a method
622 pub(crate) extern "C" fn LocalCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
623 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeLocalCommitmentTransaction)).clone() })) as *mut c_void
625 /// The commitment transaction itself, in unsigned form.
627 pub extern "C" fn LocalCommitmentTransaction_get_unsigned_tx(this_ptr: &LocalCommitmentTransaction) -> 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()
632 /// The commitment transaction itself, in unsigned form.
634 pub extern "C" fn LocalCommitmentTransaction_set_unsigned_tx(this_ptr: &mut LocalCommitmentTransaction, mut val: crate::c_types::derived::CVec_u8Z) {
635 unsafe { &mut *this_ptr.inner }.unsigned_tx = ::bitcoin::consensus::encode::deserialize(&val.into_rust()[..]).unwrap();
637 /// Our counterparty's signature for the transaction, above.
639 pub extern "C" fn LocalCommitmentTransaction_get_their_sig(this_ptr: &LocalCommitmentTransaction) -> crate::c_types::Signature {
640 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.their_sig;
641 crate::c_types::Signature::from_rust(&(*inner_val))
643 /// Our counterparty's signature for the transaction, above.
645 pub extern "C" fn LocalCommitmentTransaction_set_their_sig(this_ptr: &mut LocalCommitmentTransaction, mut val: crate::c_types::Signature) {
646 unsafe { &mut *this_ptr.inner }.their_sig = val.into_rust();
648 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
649 /// controlled by the channel initiator.
651 pub extern "C" fn LocalCommitmentTransaction_get_feerate_per_kw(this_ptr: &LocalCommitmentTransaction) -> u32 {
652 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.feerate_per_kw;
655 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
656 /// controlled by the channel initiator.
658 pub extern "C" fn LocalCommitmentTransaction_set_feerate_per_kw(this_ptr: &mut LocalCommitmentTransaction, mut val: u32) {
659 unsafe { &mut *this_ptr.inner }.feerate_per_kw = val;
661 /// The HTLCs and remote htlc signatures which were included in this commitment transaction.
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.
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().
670 pub extern "C" fn LocalCommitmentTransaction_set_per_htlc(this_ptr: &mut LocalCommitmentTransaction, 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;
674 /// Generate a new LocalCommitmentTransaction based on a raw commitment transaction,
675 /// remote signature and both parties keys.
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.
681 pub extern "C" fn LocalCommitmentTransaction_new_missing_local_sig(mut unsigned_tx: crate::c_types::derived::CVec_u8Z, mut their_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::LocalCommitmentTransaction {
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::LocalCommitmentTransaction::new_missing_local_sig(::bitcoin::consensus::encode::deserialize(&unsigned_tx.into_rust()[..]).unwrap(), their_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::LocalCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
687 /// The pre-calculated transaction creation public keys.
688 /// An external validating signer should not trust these keys.
691 pub extern "C" fn LocalCommitmentTransaction_trust_key_derivation(this_arg: &LocalCommitmentTransaction) -> 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 }
696 /// Get the txid of the local commitment transaction contained in this
697 /// LocalCommitmentTransaction
700 pub extern "C" fn LocalCommitmentTransaction_txid(this_arg: &LocalCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes {
701 let mut ret = unsafe { &*this_arg.inner }.txid();
702 crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
705 /// Gets our signature for the contained commitment transaction given our funding private key.
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.
715 pub extern "C" fn LocalCommitmentTransaction_get_local_sig(this_arg: &LocalCommitmentTransaction, 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_local_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)
720 /// Get a signature for each HTLC which was included in the commitment transaction (ie for
721 /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
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
728 pub extern "C" fn LocalCommitmentTransaction_get_htlc_sigs(this_arg: &LocalCommitmentTransaction, 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*/ }) };
735 pub extern "C" fn LocalCommitmentTransaction_write(obj: *const LocalCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
736 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
739 pub extern "C" fn LocalCommitmentTransaction_read(ser: crate::c_types::u8slice) -> LocalCommitmentTransaction {
740 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
741 LocalCommitmentTransaction { inner: Box::into_raw(Box::new(res)), is_owned: true }
743 LocalCommitmentTransaction { inner: std::ptr::null_mut(), is_owned: true }