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(mut per_commitment_point: crate::c_types::PublicKey, base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeySecpErrorZ {
23 let mut ret = lightning::ln::chan_utils::derive_private_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *base_secret}[..]).unwrap());
24 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }), 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(mut per_commitment_point: crate::c_types::PublicKey, mut base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeySecpErrorZ {
36 let mut ret = lightning::ln::chan_utils::derive_public_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &base_point.into_rust());
37 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }), 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 /// Only the cheating participant owns a valid witness to propagate a revoked
44 /// commitment transaction, thus per_commitment_secret always come from cheater
45 /// and revocation_base_secret always come from punisher, which is the broadcaster
46 /// of the transaction spending with this key knowledge.
48 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
49 /// generated (ie our own).
51 pub extern "C" fn derive_private_revocation_key(per_commitment_secret: *const [u8; 32], countersignatory_revocation_base_secret: *const [u8; 32]) -> crate::c_types::derived::CResult_SecretKeySecpErrorZ {
52 let mut ret = lightning::ln::chan_utils::derive_private_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *per_commitment_secret}[..]).unwrap(), &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *countersignatory_revocation_base_secret}[..]).unwrap());
53 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::SecretKey::from_rust(o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
57 /// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
58 /// the public equivalend of derive_private_revocation_key - using only public keys to derive a
59 /// public key instead of private keys.
61 /// Only the cheating participant owns a valid witness to propagate a revoked
62 /// commitment transaction, thus per_commitment_point always come from cheater
63 /// and revocation_base_point always come from punisher, which is the broadcaster
64 /// of the transaction spending with this key knowledge.
66 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
67 /// generated (ie our own).
69 pub extern "C" fn derive_public_revocation_key(mut per_commitment_point: crate::c_types::PublicKey, mut countersignatory_revocation_base_point: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_PublicKeySecpErrorZ {
70 let mut ret = lightning::ln::chan_utils::derive_public_revocation_key(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &countersignatory_revocation_base_point.into_rust());
71 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::PublicKey::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::c_types::Secp256k1Error::from_rust(e) }) };
76 use lightning::ln::chan_utils::TxCreationKeys as nativeTxCreationKeysImport;
77 type nativeTxCreationKeys = nativeTxCreationKeysImport;
79 /// The set of public keys which are used in the creation of one commitment transaction.
80 /// These are derived from the channel base keys and per-commitment data.
82 /// A broadcaster key is provided from potential broadcaster of the computed transaction.
83 /// A countersignatory key is coming from a protocol participant unable to broadcast the
86 /// These keys are assumed to be good, either because the code derived them from
87 /// channel basepoints via the new function, or they were obtained via
88 /// PreCalculatedTxCreationKeys.trust_key_derivation because we trusted the source of the
89 /// pre-calculated keys.
92 pub struct TxCreationKeys {
93 /// Nearly everywhere, inner must be non-null, however in places where
94 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
95 pub inner: *mut nativeTxCreationKeys,
99 impl Drop for TxCreationKeys {
101 if self.is_owned && !self.inner.is_null() {
102 let _ = unsafe { Box::from_raw(self.inner) };
107 pub extern "C" fn TxCreationKeys_free(this_ptr: TxCreationKeys) { }
109 /// Used only if an object of this type is returned as a trait impl by a method
110 extern "C" fn TxCreationKeys_free_void(this_ptr: *mut c_void) {
111 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeTxCreationKeys); }
114 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
115 impl TxCreationKeys {
116 pub(crate) fn take_ptr(mut self) -> *mut nativeTxCreationKeys {
117 assert!(self.is_owned);
118 let ret = self.inner;
119 self.inner = std::ptr::null_mut();
123 impl Clone for TxCreationKeys {
124 fn clone(&self) -> Self {
126 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
132 /// Used only if an object of this type is returned as a trait impl by a method
133 pub(crate) extern "C" fn TxCreationKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
134 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeTxCreationKeys)).clone() })) as *mut c_void
137 pub extern "C" fn TxCreationKeys_clone(orig: &TxCreationKeys) -> TxCreationKeys {
138 TxCreationKeys { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
140 /// The broadcaster's per-commitment public key which was used to derive the other keys.
142 pub extern "C" fn TxCreationKeys_get_per_commitment_point(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
143 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.per_commitment_point;
144 crate::c_types::PublicKey::from_rust(&(*inner_val))
146 /// The broadcaster's per-commitment public key which was used to derive the other keys.
148 pub extern "C" fn TxCreationKeys_set_per_commitment_point(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
149 unsafe { &mut *this_ptr.inner }.per_commitment_point = val.into_rust();
151 /// The revocation key which is used to allow the broadcaster of the commitment
152 /// transaction to provide their counterparty the ability to punish them if they broadcast
155 pub extern "C" fn TxCreationKeys_get_revocation_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
156 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_key;
157 crate::c_types::PublicKey::from_rust(&(*inner_val))
159 /// The revocation key which is used to allow the broadcaster of the commitment
160 /// transaction to provide their counterparty the ability to punish them if they broadcast
163 pub extern "C" fn TxCreationKeys_set_revocation_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
164 unsafe { &mut *this_ptr.inner }.revocation_key = val.into_rust();
166 /// Broadcaster's HTLC Key
168 pub extern "C" fn TxCreationKeys_get_broadcaster_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
169 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key;
170 crate::c_types::PublicKey::from_rust(&(*inner_val))
172 /// Broadcaster's HTLC Key
174 pub extern "C" fn TxCreationKeys_set_broadcaster_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
175 unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key = val.into_rust();
177 /// Countersignatory's HTLC Key
179 pub extern "C" fn TxCreationKeys_get_countersignatory_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
180 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key;
181 crate::c_types::PublicKey::from_rust(&(*inner_val))
183 /// Countersignatory's HTLC Key
185 pub extern "C" fn TxCreationKeys_set_countersignatory_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
186 unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key = val.into_rust();
188 /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
190 pub extern "C" fn TxCreationKeys_get_broadcaster_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
191 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key;
192 crate::c_types::PublicKey::from_rust(&(*inner_val))
194 /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
196 pub extern "C" fn TxCreationKeys_set_broadcaster_delayed_payment_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
197 unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key = val.into_rust();
201 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 {
202 TxCreationKeys { inner: Box::into_raw(Box::new(nativeTxCreationKeys {
203 per_commitment_point: per_commitment_point_arg.into_rust(),
204 revocation_key: revocation_key_arg.into_rust(),
205 broadcaster_htlc_key: broadcaster_htlc_key_arg.into_rust(),
206 countersignatory_htlc_key: countersignatory_htlc_key_arg.into_rust(),
207 broadcaster_delayed_payment_key: broadcaster_delayed_payment_key_arg.into_rust(),
208 })), is_owned: true }
211 pub extern "C" fn TxCreationKeys_write(obj: *const TxCreationKeys) -> crate::c_types::derived::CVec_u8Z {
212 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
215 pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> TxCreationKeys {
216 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
217 TxCreationKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
219 TxCreationKeys { inner: std::ptr::null_mut(), is_owned: true }
223 use lightning::ln::chan_utils::PreCalculatedTxCreationKeys as nativePreCalculatedTxCreationKeysImport;
224 type nativePreCalculatedTxCreationKeys = nativePreCalculatedTxCreationKeysImport;
226 /// The per-commitment point and a set of pre-calculated public keys used for transaction creation
228 /// The pre-calculated keys are an optimization, because ChannelKeys has enough
229 /// information to re-derive them.
232 pub struct PreCalculatedTxCreationKeys {
233 /// Nearly everywhere, inner must be non-null, however in places where
234 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
235 pub inner: *mut nativePreCalculatedTxCreationKeys,
239 impl Drop for PreCalculatedTxCreationKeys {
241 if self.is_owned && !self.inner.is_null() {
242 let _ = unsafe { Box::from_raw(self.inner) };
247 pub extern "C" fn PreCalculatedTxCreationKeys_free(this_ptr: PreCalculatedTxCreationKeys) { }
249 /// Used only if an object of this type is returned as a trait impl by a method
250 extern "C" fn PreCalculatedTxCreationKeys_free_void(this_ptr: *mut c_void) {
251 unsafe { let _ = Box::from_raw(this_ptr as *mut nativePreCalculatedTxCreationKeys); }
254 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
255 impl PreCalculatedTxCreationKeys {
256 pub(crate) fn take_ptr(mut self) -> *mut nativePreCalculatedTxCreationKeys {
257 assert!(self.is_owned);
258 let ret = self.inner;
259 self.inner = std::ptr::null_mut();
263 impl Clone for PreCalculatedTxCreationKeys {
264 fn clone(&self) -> Self {
266 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
272 /// Used only if an object of this type is returned as a trait impl by a method
273 pub(crate) extern "C" fn PreCalculatedTxCreationKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
274 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePreCalculatedTxCreationKeys)).clone() })) as *mut c_void
277 pub extern "C" fn PreCalculatedTxCreationKeys_clone(orig: &PreCalculatedTxCreationKeys) -> PreCalculatedTxCreationKeys {
278 PreCalculatedTxCreationKeys { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
280 /// Create a new PreCalculatedTxCreationKeys from TxCreationKeys
283 pub extern "C" fn PreCalculatedTxCreationKeys_new(mut keys: crate::ln::chan_utils::TxCreationKeys) -> PreCalculatedTxCreationKeys {
284 let mut ret = lightning::ln::chan_utils::PreCalculatedTxCreationKeys::new(*unsafe { Box::from_raw(keys.take_ptr()) });
285 PreCalculatedTxCreationKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
288 /// The pre-calculated transaction creation public keys.
289 /// An external validating signer should not trust these keys.
292 pub extern "C" fn PreCalculatedTxCreationKeys_trust_key_derivation(this_arg: &PreCalculatedTxCreationKeys) -> crate::ln::chan_utils::TxCreationKeys {
293 let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation();
294 crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
297 /// The transaction per-commitment point
300 pub extern "C" fn PreCalculatedTxCreationKeys_per_commitment_point(this_arg: &PreCalculatedTxCreationKeys) -> crate::c_types::PublicKey {
301 let mut ret = unsafe { &*this_arg.inner }.per_commitment_point();
302 crate::c_types::PublicKey::from_rust(&*ret)
306 use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport;
307 type nativeChannelPublicKeys = nativeChannelPublicKeysImport;
309 /// One counterparty's public keys which do not change over the life of a channel.
312 pub struct ChannelPublicKeys {
313 /// Nearly everywhere, inner must be non-null, however in places where
314 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
315 pub inner: *mut nativeChannelPublicKeys,
319 impl Drop for ChannelPublicKeys {
321 if self.is_owned && !self.inner.is_null() {
322 let _ = unsafe { Box::from_raw(self.inner) };
327 pub extern "C" fn ChannelPublicKeys_free(this_ptr: ChannelPublicKeys) { }
329 /// Used only if an object of this type is returned as a trait impl by a method
330 extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) {
331 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelPublicKeys); }
334 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
335 impl ChannelPublicKeys {
336 pub(crate) fn take_ptr(mut self) -> *mut nativeChannelPublicKeys {
337 assert!(self.is_owned);
338 let ret = self.inner;
339 self.inner = std::ptr::null_mut();
343 impl Clone for ChannelPublicKeys {
344 fn clone(&self) -> Self {
346 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
352 /// Used only if an object of this type is returned as a trait impl by a method
353 pub(crate) extern "C" fn ChannelPublicKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
354 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelPublicKeys)).clone() })) as *mut c_void
357 pub extern "C" fn ChannelPublicKeys_clone(orig: &ChannelPublicKeys) -> ChannelPublicKeys {
358 ChannelPublicKeys { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
360 /// The public key which is used to sign all commitment transactions, as it appears in the
361 /// on-chain channel lock-in 2-of-2 multisig output.
363 pub extern "C" fn ChannelPublicKeys_get_funding_pubkey(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
364 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_pubkey;
365 crate::c_types::PublicKey::from_rust(&(*inner_val))
367 /// The public key which is used to sign all commitment transactions, as it appears in the
368 /// on-chain channel lock-in 2-of-2 multisig output.
370 pub extern "C" fn ChannelPublicKeys_set_funding_pubkey(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
371 unsafe { &mut *this_ptr.inner }.funding_pubkey = val.into_rust();
373 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
374 /// revocation keys. This is combined with the per-commitment-secret generated by the
375 /// counterparty to create a secret which the counterparty can reveal to revoke previous
378 pub extern "C" fn ChannelPublicKeys_get_revocation_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
379 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_basepoint;
380 crate::c_types::PublicKey::from_rust(&(*inner_val))
382 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
383 /// revocation keys. This is combined with the per-commitment-secret generated by the
384 /// counterparty to create a secret which the counterparty can reveal to revoke previous
387 pub extern "C" fn ChannelPublicKeys_set_revocation_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
388 unsafe { &mut *this_ptr.inner }.revocation_basepoint = val.into_rust();
390 /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
391 /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
392 /// static across every commitment transaction.
394 pub extern "C" fn ChannelPublicKeys_get_payment_point(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
395 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_point;
396 crate::c_types::PublicKey::from_rust(&(*inner_val))
398 /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
399 /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
400 /// static across every commitment transaction.
402 pub extern "C" fn ChannelPublicKeys_set_payment_point(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
403 unsafe { &mut *this_ptr.inner }.payment_point = val.into_rust();
405 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
406 /// public key which receives non-HTLC-encumbered funds which are only available for spending
407 /// after some delay (or can be claimed via the revocation path).
409 pub extern "C" fn ChannelPublicKeys_get_delayed_payment_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
410 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint;
411 crate::c_types::PublicKey::from_rust(&(*inner_val))
413 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
414 /// public key which receives non-HTLC-encumbered funds which are only available for spending
415 /// after some delay (or can be claimed via the revocation path).
417 pub extern "C" fn ChannelPublicKeys_set_delayed_payment_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
418 unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint = val.into_rust();
420 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
421 /// which is used to encumber HTLC-in-flight outputs.
423 pub extern "C" fn ChannelPublicKeys_get_htlc_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
424 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_basepoint;
425 crate::c_types::PublicKey::from_rust(&(*inner_val))
427 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
428 /// which is used to encumber HTLC-in-flight outputs.
430 pub extern "C" fn ChannelPublicKeys_set_htlc_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
431 unsafe { &mut *this_ptr.inner }.htlc_basepoint = val.into_rust();
435 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 {
436 ChannelPublicKeys { inner: Box::into_raw(Box::new(nativeChannelPublicKeys {
437 funding_pubkey: funding_pubkey_arg.into_rust(),
438 revocation_basepoint: revocation_basepoint_arg.into_rust(),
439 payment_point: payment_point_arg.into_rust(),
440 delayed_payment_basepoint: delayed_payment_basepoint_arg.into_rust(),
441 htlc_basepoint: htlc_basepoint_arg.into_rust(),
442 })), is_owned: true }
445 pub extern "C" fn ChannelPublicKeys_write(obj: *const ChannelPublicKeys) -> crate::c_types::derived::CVec_u8Z {
446 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
449 pub extern "C" fn ChannelPublicKeys_read(ser: crate::c_types::u8slice) -> ChannelPublicKeys {
450 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
451 ChannelPublicKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
453 ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true }
456 /// Create a new TxCreationKeys from channel base points and the per-commitment point
459 pub extern "C" fn TxCreationKeys_derive_new(mut per_commitment_point: crate::c_types::PublicKey, mut broadcaster_delayed_payment_base: crate::c_types::PublicKey, mut broadcaster_htlc_base: crate::c_types::PublicKey, mut countersignatory_revocation_base: crate::c_types::PublicKey, mut countersignatory_htlc_base: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_TxCreationKeysSecpErrorZ {
460 let mut ret = lightning::ln::chan_utils::TxCreationKeys::derive_new(&bitcoin::secp256k1::Secp256k1::new(), &per_commitment_point.into_rust(), &broadcaster_delayed_payment_base.into_rust(), &broadcaster_htlc_base.into_rust(), &countersignatory_revocation_base.into_rust(), &countersignatory_htlc_base.into_rust());
461 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) }) };
465 /// A script either spendable by the revocation
466 /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
467 /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
469 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 {
470 let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), contest_delay, &broadcaster_delayed_payment_key.into_rust());
471 ret.into_bytes().into()
475 use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport;
476 type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport;
478 /// Information about an HTLC as it appears in a commitment transaction
481 pub struct HTLCOutputInCommitment {
482 /// Nearly everywhere, inner must be non-null, however in places where
483 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
484 pub inner: *mut nativeHTLCOutputInCommitment,
488 impl Drop for HTLCOutputInCommitment {
490 if self.is_owned && !self.inner.is_null() {
491 let _ = unsafe { Box::from_raw(self.inner) };
496 pub extern "C" fn HTLCOutputInCommitment_free(this_ptr: HTLCOutputInCommitment) { }
498 /// Used only if an object of this type is returned as a trait impl by a method
499 extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) {
500 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCOutputInCommitment); }
503 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
504 impl HTLCOutputInCommitment {
505 pub(crate) fn take_ptr(mut self) -> *mut nativeHTLCOutputInCommitment {
506 assert!(self.is_owned);
507 let ret = self.inner;
508 self.inner = std::ptr::null_mut();
512 impl Clone for HTLCOutputInCommitment {
513 fn clone(&self) -> Self {
515 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
521 /// Used only if an object of this type is returned as a trait impl by a method
522 pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void {
523 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void
526 pub extern "C" fn HTLCOutputInCommitment_clone(orig: &HTLCOutputInCommitment) -> HTLCOutputInCommitment {
527 HTLCOutputInCommitment { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
529 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
530 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
531 /// need to compare this value to whether the commitment transaction in question is that of
532 /// the counterparty or our own.
534 pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool {
535 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered;
538 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
539 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
540 /// need to compare this value to whether the commitment transaction in question is that of
541 /// the counterparty or our own.
543 pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputInCommitment, mut val: bool) {
544 unsafe { &mut *this_ptr.inner }.offered = val;
546 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
547 /// this divided by 1000.
549 pub extern "C" fn HTLCOutputInCommitment_get_amount_msat(this_ptr: &HTLCOutputInCommitment) -> u64 {
550 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.amount_msat;
553 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
554 /// this divided by 1000.
556 pub extern "C" fn HTLCOutputInCommitment_set_amount_msat(this_ptr: &mut HTLCOutputInCommitment, mut val: u64) {
557 unsafe { &mut *this_ptr.inner }.amount_msat = val;
559 /// The CLTV lock-time at which this HTLC expires.
561 pub extern "C" fn HTLCOutputInCommitment_get_cltv_expiry(this_ptr: &HTLCOutputInCommitment) -> u32 {
562 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry;
565 /// The CLTV lock-time at which this HTLC expires.
567 pub extern "C" fn HTLCOutputInCommitment_set_cltv_expiry(this_ptr: &mut HTLCOutputInCommitment, mut val: u32) {
568 unsafe { &mut *this_ptr.inner }.cltv_expiry = val;
570 /// The hash of the preimage which unlocks this HTLC.
572 pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputInCommitment) -> *const [u8; 32] {
573 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_hash;
576 /// The hash of the preimage which unlocks this HTLC.
578 pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) {
579 unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data);
582 pub extern "C" fn HTLCOutputInCommitment_write(obj: *const HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z {
583 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
586 pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> HTLCOutputInCommitment {
587 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
588 HTLCOutputInCommitment { inner: Box::into_raw(Box::new(res)), is_owned: true }
590 HTLCOutputInCommitment { inner: std::ptr::null_mut(), is_owned: true }
593 /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
594 /// does not need to have its previous_output_index filled.
596 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 {
597 let mut ret = lightning::ln::chan_utils::get_htlc_redeemscript(unsafe { &*htlc.inner }, unsafe { &*keys.inner });
598 ret.into_bytes().into()
601 /// Gets the redeemscript for a funding output from the two funding public keys.
602 /// Note that the order of funding public keys does not matter.
604 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 {
605 let mut ret = lightning::ln::chan_utils::make_funding_redeemscript(&broadcaster.into_rust(), &countersignatory.into_rust());
606 ret.into_bytes().into()
609 /// panics if htlc.transaction_output_index.is_none()!
611 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 {
612 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());
613 let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret);
614 crate::c_types::Transaction::from_vec(local_ret)
618 use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport;
619 type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport;
621 /// We use this to track holder commitment transactions and put off signing them until we are ready
622 /// to broadcast. This class can be used inside a signer implementation to generate a signature
623 /// given the relevant secret key.
626 pub struct HolderCommitmentTransaction {
627 /// Nearly everywhere, inner must be non-null, however in places where
628 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
629 pub inner: *mut nativeHolderCommitmentTransaction,
633 impl Drop for HolderCommitmentTransaction {
635 if self.is_owned && !self.inner.is_null() {
636 let _ = unsafe { Box::from_raw(self.inner) };
641 pub extern "C" fn HolderCommitmentTransaction_free(this_ptr: HolderCommitmentTransaction) { }
643 /// Used only if an object of this type is returned as a trait impl by a method
644 extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) {
645 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHolderCommitmentTransaction); }
648 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
649 impl HolderCommitmentTransaction {
650 pub(crate) fn take_ptr(mut self) -> *mut nativeHolderCommitmentTransaction {
651 assert!(self.is_owned);
652 let ret = self.inner;
653 self.inner = std::ptr::null_mut();
657 impl Clone for HolderCommitmentTransaction {
658 fn clone(&self) -> Self {
660 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
666 /// Used only if an object of this type is returned as a trait impl by a method
667 pub(crate) extern "C" fn HolderCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
668 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHolderCommitmentTransaction)).clone() })) as *mut c_void
671 pub extern "C" fn HolderCommitmentTransaction_clone(orig: &HolderCommitmentTransaction) -> HolderCommitmentTransaction {
672 HolderCommitmentTransaction { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
674 /// The commitment transaction itself, in unsigned form.
676 pub extern "C" fn HolderCommitmentTransaction_get_unsigned_tx(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Transaction {
677 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.unsigned_tx;
678 let mut local_inner_val = ::bitcoin::consensus::encode::serialize(inner_val);
679 crate::c_types::Transaction::from_vec(local_inner_val)
681 /// The commitment transaction itself, in unsigned form.
683 pub extern "C" fn HolderCommitmentTransaction_set_unsigned_tx(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Transaction) {
684 unsafe { &mut *this_ptr.inner }.unsigned_tx = val.into_bitcoin();
686 /// Our counterparty's signature for the transaction, above.
688 pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature {
689 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig;
690 crate::c_types::Signature::from_rust(&(*inner_val))
692 /// Our counterparty's signature for the transaction, above.
694 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) {
695 unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust();
697 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
698 /// controlled by the channel initiator.
700 pub extern "C" fn HolderCommitmentTransaction_get_feerate_per_kw(this_ptr: &HolderCommitmentTransaction) -> u32 {
701 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.feerate_per_kw;
704 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
705 /// controlled by the channel initiator.
707 pub extern "C" fn HolderCommitmentTransaction_set_feerate_per_kw(this_ptr: &mut HolderCommitmentTransaction, mut val: u32) {
708 unsafe { &mut *this_ptr.inner }.feerate_per_kw = val;
710 /// The HTLCs and counterparty htlc signatures which were included in this commitment transaction.
712 /// Note that this includes all HTLCs, including ones which were considered dust and not
713 /// actually included in the transaction as it appears on-chain, but who's value is burned as
714 /// fees and not included in the to_holder or to_counterparty outputs.
716 /// The counterparty HTLC signatures in the second element will always be set for non-dust HTLCs, ie
717 /// those for which transaction_output_index.is_some().
719 pub extern "C" fn HolderCommitmentTransaction_set_per_htlc(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_C2Tuple_HTLCOutputInCommitmentSignatureZZ) {
720 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 }); };
721 unsafe { &mut *this_ptr.inner }.per_htlc = local_val;
723 /// Generate a new HolderCommitmentTransaction based on a raw commitment transaction,
724 /// counterparty signature and both parties keys.
726 /// The unsigned transaction outputs must be consistent with htlc_data. This function
727 /// only checks that the shape and amounts are consistent, but does not check the scriptPubkey.
730 pub extern "C" fn HolderCommitmentTransaction_new_missing_holder_sig(mut unsigned_tx: crate::c_types::Transaction, mut counterparty_sig: crate::c_types::Signature, mut holder_funding_key: crate::c_types::PublicKey, mut counterparty_funding_key: crate::c_types::PublicKey, mut 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 {
731 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 }); };
732 let mut ret = lightning::ln::chan_utils::HolderCommitmentTransaction::new_missing_holder_sig(unsigned_tx.into_bitcoin(), counterparty_sig.into_rust(), &holder_funding_key.into_rust(), &counterparty_funding_key.into_rust(), *unsafe { Box::from_raw(keys.take_ptr()) }, feerate_per_kw, local_htlc_data);
733 crate::ln::chan_utils::HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
736 /// The pre-calculated transaction creation public keys.
737 /// An external validating signer should not trust these keys.
740 pub extern "C" fn HolderCommitmentTransaction_trust_key_derivation(this_arg: &HolderCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys {
741 let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation();
742 crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
745 /// Get the txid of the holder commitment transaction contained in this
746 /// HolderCommitmentTransaction
749 pub extern "C" fn HolderCommitmentTransaction_txid(this_arg: &HolderCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes {
750 let mut ret = unsafe { &*this_arg.inner }.txid();
751 crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
754 /// Gets holder signature for the contained commitment transaction given holder funding private key.
756 /// Funding key is your key included in the 2-2 funding_outpoint lock. Should be provided
757 /// by your ChannelKeys.
758 /// Funding redeemscript is script locking funding_outpoint. This is the mutlsig script
759 /// between your own funding key and your counterparty's. Currently, this is provided in
760 /// ChannelKeys::sign_holder_commitment() calls directly.
761 /// Channel value is amount locked in funding_outpoint.
764 pub extern "C" fn HolderCommitmentTransaction_get_holder_sig(this_arg: &HolderCommitmentTransaction, funding_key: *const [u8; 32], mut funding_redeemscript: crate::c_types::u8slice, mut channel_value_satoshis: u64) -> crate::c_types::Signature {
765 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());
766 crate::c_types::Signature::from_rust(&ret)
769 /// Get a signature for each HTLC which was included in the commitment transaction (ie for
770 /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
772 /// The returned Vec has one entry for each HTLC, and in the same order. For HTLCs which were
773 /// considered dust and not included, a None entry exists, for all others a signature is
777 pub extern "C" fn HolderCommitmentTransaction_get_htlc_sigs(this_arg: &HolderCommitmentTransaction, htlc_base_key: *const [u8; 32], mut counterparty_selected_contest_delay: u16) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ {
778 let mut ret = unsafe { &*this_arg.inner }.get_htlc_sigs(&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *htlc_base_key}[..]).unwrap(), counterparty_selected_contest_delay, &bitcoin::secp256k1::Secp256k1::new());
779 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*/ }) };
784 pub extern "C" fn HolderCommitmentTransaction_write(obj: *const HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
785 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
788 pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> HolderCommitmentTransaction {
789 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
790 HolderCommitmentTransaction { inner: Box::into_raw(Box::new(res)), is_owned: true }
792 HolderCommitmentTransaction { inner: std::ptr::null_mut(), is_owned: true }