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 /// 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(per_commitment_point: crate::c_types::PublicKey, 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 everyhwere, 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
136 /// The broadcaster's per-commitment public key which was used to derive the other keys.
138 pub extern "C" fn TxCreationKeys_get_per_commitment_point(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
139 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.per_commitment_point;
140 crate::c_types::PublicKey::from_rust(&(*inner_val))
142 /// The broadcaster's per-commitment public key which was used to derive the other keys.
144 pub extern "C" fn TxCreationKeys_set_per_commitment_point(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
145 unsafe { &mut *this_ptr.inner }.per_commitment_point = val.into_rust();
147 /// The revocation key which is used to allow the broadcaster of the commitment
148 /// transaction to provide their counterparty the ability to punish them if they broadcast
151 pub extern "C" fn TxCreationKeys_get_revocation_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
152 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_key;
153 crate::c_types::PublicKey::from_rust(&(*inner_val))
155 /// The revocation key which is used to allow the broadcaster of the commitment
156 /// transaction to provide their counterparty the ability to punish them if they broadcast
159 pub extern "C" fn TxCreationKeys_set_revocation_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
160 unsafe { &mut *this_ptr.inner }.revocation_key = val.into_rust();
162 /// Broadcaster's HTLC Key
164 pub extern "C" fn TxCreationKeys_get_broadcaster_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
165 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key;
166 crate::c_types::PublicKey::from_rust(&(*inner_val))
168 /// Broadcaster's HTLC Key
170 pub extern "C" fn TxCreationKeys_set_broadcaster_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
171 unsafe { &mut *this_ptr.inner }.broadcaster_htlc_key = val.into_rust();
173 /// Countersignatory's HTLC Key
175 pub extern "C" fn TxCreationKeys_get_countersignatory_htlc_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
176 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key;
177 crate::c_types::PublicKey::from_rust(&(*inner_val))
179 /// Countersignatory's HTLC Key
181 pub extern "C" fn TxCreationKeys_set_countersignatory_htlc_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
182 unsafe { &mut *this_ptr.inner }.countersignatory_htlc_key = val.into_rust();
184 /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
186 pub extern "C" fn TxCreationKeys_get_broadcaster_delayed_payment_key(this_ptr: &TxCreationKeys) -> crate::c_types::PublicKey {
187 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key;
188 crate::c_types::PublicKey::from_rust(&(*inner_val))
190 /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
192 pub extern "C" fn TxCreationKeys_set_broadcaster_delayed_payment_key(this_ptr: &mut TxCreationKeys, mut val: crate::c_types::PublicKey) {
193 unsafe { &mut *this_ptr.inner }.broadcaster_delayed_payment_key = val.into_rust();
197 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 {
198 TxCreationKeys { inner: Box::into_raw(Box::new(nativeTxCreationKeys {
199 per_commitment_point: per_commitment_point_arg.into_rust(),
200 revocation_key: revocation_key_arg.into_rust(),
201 broadcaster_htlc_key: broadcaster_htlc_key_arg.into_rust(),
202 countersignatory_htlc_key: countersignatory_htlc_key_arg.into_rust(),
203 broadcaster_delayed_payment_key: broadcaster_delayed_payment_key_arg.into_rust(),
204 })), is_owned: true }
207 pub extern "C" fn TxCreationKeys_write(obj: *const TxCreationKeys) -> crate::c_types::derived::CVec_u8Z {
208 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
211 pub extern "C" fn TxCreationKeys_read(ser: crate::c_types::u8slice) -> TxCreationKeys {
212 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
213 TxCreationKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
215 TxCreationKeys { inner: std::ptr::null_mut(), is_owned: true }
219 use lightning::ln::chan_utils::PreCalculatedTxCreationKeys as nativePreCalculatedTxCreationKeysImport;
220 type nativePreCalculatedTxCreationKeys = nativePreCalculatedTxCreationKeysImport;
222 /// The per-commitment point and a set of pre-calculated public keys used for transaction creation
224 /// The pre-calculated keys are an optimization, because ChannelKeys has enough
225 /// information to re-derive them.
228 pub struct PreCalculatedTxCreationKeys {
229 /// Nearly everyhwere, inner must be non-null, however in places where
230 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
231 pub inner: *mut nativePreCalculatedTxCreationKeys,
235 impl Drop for PreCalculatedTxCreationKeys {
237 if self.is_owned && !self.inner.is_null() {
238 let _ = unsafe { Box::from_raw(self.inner) };
243 pub extern "C" fn PreCalculatedTxCreationKeys_free(this_ptr: PreCalculatedTxCreationKeys) { }
245 /// Used only if an object of this type is returned as a trait impl by a method
246 extern "C" fn PreCalculatedTxCreationKeys_free_void(this_ptr: *mut c_void) {
247 unsafe { let _ = Box::from_raw(this_ptr as *mut nativePreCalculatedTxCreationKeys); }
250 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
251 impl PreCalculatedTxCreationKeys {
252 pub(crate) fn take_ptr(mut self) -> *mut nativePreCalculatedTxCreationKeys {
253 assert!(self.is_owned);
254 let ret = self.inner;
255 self.inner = std::ptr::null_mut();
259 /// Create a new PreCalculatedTxCreationKeys from TxCreationKeys
262 pub extern "C" fn PreCalculatedTxCreationKeys_new(mut keys: crate::ln::chan_utils::TxCreationKeys) -> PreCalculatedTxCreationKeys {
263 let mut ret = lightning::ln::chan_utils::PreCalculatedTxCreationKeys::new(*unsafe { Box::from_raw(keys.take_ptr()) });
264 PreCalculatedTxCreationKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
267 /// The pre-calculated transaction creation public keys.
268 /// An external validating signer should not trust these keys.
271 pub extern "C" fn PreCalculatedTxCreationKeys_trust_key_derivation(this_arg: &PreCalculatedTxCreationKeys) -> crate::ln::chan_utils::TxCreationKeys {
272 let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation();
273 crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
276 /// The transaction per-commitment point
279 pub extern "C" fn PreCalculatedTxCreationKeys_per_commitment_point(this_arg: &PreCalculatedTxCreationKeys) -> crate::c_types::PublicKey {
280 let mut ret = unsafe { &*this_arg.inner }.per_commitment_point();
281 crate::c_types::PublicKey::from_rust(&*ret)
285 use lightning::ln::chan_utils::ChannelPublicKeys as nativeChannelPublicKeysImport;
286 type nativeChannelPublicKeys = nativeChannelPublicKeysImport;
288 /// One counterparty's public keys which do not change over the life of a channel.
291 pub struct ChannelPublicKeys {
292 /// Nearly everyhwere, inner must be non-null, however in places where
293 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
294 pub inner: *mut nativeChannelPublicKeys,
298 impl Drop for ChannelPublicKeys {
300 if self.is_owned && !self.inner.is_null() {
301 let _ = unsafe { Box::from_raw(self.inner) };
306 pub extern "C" fn ChannelPublicKeys_free(this_ptr: ChannelPublicKeys) { }
308 /// Used only if an object of this type is returned as a trait impl by a method
309 extern "C" fn ChannelPublicKeys_free_void(this_ptr: *mut c_void) {
310 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelPublicKeys); }
313 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
314 impl ChannelPublicKeys {
315 pub(crate) fn take_ptr(mut self) -> *mut nativeChannelPublicKeys {
316 assert!(self.is_owned);
317 let ret = self.inner;
318 self.inner = std::ptr::null_mut();
322 impl Clone for ChannelPublicKeys {
323 fn clone(&self) -> Self {
325 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
331 /// Used only if an object of this type is returned as a trait impl by a method
332 pub(crate) extern "C" fn ChannelPublicKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
333 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelPublicKeys)).clone() })) as *mut c_void
335 /// The public key which is used to sign all commitment transactions, as it appears in the
336 /// on-chain channel lock-in 2-of-2 multisig output.
338 pub extern "C" fn ChannelPublicKeys_get_funding_pubkey(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
339 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_pubkey;
340 crate::c_types::PublicKey::from_rust(&(*inner_val))
342 /// The public key which is used to sign all commitment transactions, as it appears in the
343 /// on-chain channel lock-in 2-of-2 multisig output.
345 pub extern "C" fn ChannelPublicKeys_set_funding_pubkey(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
346 unsafe { &mut *this_ptr.inner }.funding_pubkey = val.into_rust();
348 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
349 /// revocation keys. This is combined with the per-commitment-secret generated by the
350 /// counterparty to create a secret which the counterparty can reveal to revoke previous
353 pub extern "C" fn ChannelPublicKeys_get_revocation_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
354 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_basepoint;
355 crate::c_types::PublicKey::from_rust(&(*inner_val))
357 /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
358 /// revocation keys. This is combined with the per-commitment-secret generated by the
359 /// counterparty to create a secret which the counterparty can reveal to revoke previous
362 pub extern "C" fn ChannelPublicKeys_set_revocation_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
363 unsafe { &mut *this_ptr.inner }.revocation_basepoint = val.into_rust();
365 /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
366 /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
367 /// static across every commitment transaction.
369 pub extern "C" fn ChannelPublicKeys_get_payment_point(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
370 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_point;
371 crate::c_types::PublicKey::from_rust(&(*inner_val))
373 /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
374 /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
375 /// static across every commitment transaction.
377 pub extern "C" fn ChannelPublicKeys_set_payment_point(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
378 unsafe { &mut *this_ptr.inner }.payment_point = val.into_rust();
380 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
381 /// public key which receives non-HTLC-encumbered funds which are only available for spending
382 /// after some delay (or can be claimed via the revocation path).
384 pub extern "C" fn ChannelPublicKeys_get_delayed_payment_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
385 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint;
386 crate::c_types::PublicKey::from_rust(&(*inner_val))
388 /// The base point which is used (with derive_public_key) to derive a per-commitment payment
389 /// public key which receives non-HTLC-encumbered funds which are only available for spending
390 /// after some delay (or can be claimed via the revocation path).
392 pub extern "C" fn ChannelPublicKeys_set_delayed_payment_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
393 unsafe { &mut *this_ptr.inner }.delayed_payment_basepoint = val.into_rust();
395 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
396 /// which is used to encumber HTLC-in-flight outputs.
398 pub extern "C" fn ChannelPublicKeys_get_htlc_basepoint(this_ptr: &ChannelPublicKeys) -> crate::c_types::PublicKey {
399 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_basepoint;
400 crate::c_types::PublicKey::from_rust(&(*inner_val))
402 /// The base point which is used (with derive_public_key) to derive a per-commitment public key
403 /// which is used to encumber HTLC-in-flight outputs.
405 pub extern "C" fn ChannelPublicKeys_set_htlc_basepoint(this_ptr: &mut ChannelPublicKeys, mut val: crate::c_types::PublicKey) {
406 unsafe { &mut *this_ptr.inner }.htlc_basepoint = val.into_rust();
410 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 {
411 ChannelPublicKeys { inner: Box::into_raw(Box::new(nativeChannelPublicKeys {
412 funding_pubkey: funding_pubkey_arg.into_rust(),
413 revocation_basepoint: revocation_basepoint_arg.into_rust(),
414 payment_point: payment_point_arg.into_rust(),
415 delayed_payment_basepoint: delayed_payment_basepoint_arg.into_rust(),
416 htlc_basepoint: htlc_basepoint_arg.into_rust(),
417 })), is_owned: true }
420 pub extern "C" fn ChannelPublicKeys_write(obj: *const ChannelPublicKeys) -> crate::c_types::derived::CVec_u8Z {
421 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
424 pub extern "C" fn ChannelPublicKeys_read(ser: crate::c_types::u8slice) -> ChannelPublicKeys {
425 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
426 ChannelPublicKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
428 ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true }
431 /// Create a new TxCreationKeys from channel base points and the per-commitment point
434 pub extern "C" fn TxCreationKeys_derive_new(per_commitment_point: crate::c_types::PublicKey, broadcaster_delayed_payment_base: crate::c_types::PublicKey, broadcaster_htlc_base: crate::c_types::PublicKey, countersignatory_revocation_base: crate::c_types::PublicKey, countersignatory_htlc_base: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_TxCreationKeysSecpErrorZ {
435 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());
436 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) }) };
440 /// A script either spendable by the revocation
441 /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
442 /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
444 pub extern "C" fn get_revokeable_redeemscript(revocation_key: crate::c_types::PublicKey, mut contest_delay: u16, broadcaster_delayed_payment_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
445 let mut ret = lightning::ln::chan_utils::get_revokeable_redeemscript(&revocation_key.into_rust(), contest_delay, &broadcaster_delayed_payment_key.into_rust());
446 ret.into_bytes().into()
450 use lightning::ln::chan_utils::HTLCOutputInCommitment as nativeHTLCOutputInCommitmentImport;
451 type nativeHTLCOutputInCommitment = nativeHTLCOutputInCommitmentImport;
453 /// Information about an HTLC as it appears in a commitment transaction
456 pub struct HTLCOutputInCommitment {
457 /// Nearly everyhwere, inner must be non-null, however in places where
458 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
459 pub inner: *mut nativeHTLCOutputInCommitment,
463 impl Drop for HTLCOutputInCommitment {
465 if self.is_owned && !self.inner.is_null() {
466 let _ = unsafe { Box::from_raw(self.inner) };
471 pub extern "C" fn HTLCOutputInCommitment_free(this_ptr: HTLCOutputInCommitment) { }
473 /// Used only if an object of this type is returned as a trait impl by a method
474 extern "C" fn HTLCOutputInCommitment_free_void(this_ptr: *mut c_void) {
475 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHTLCOutputInCommitment); }
478 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
479 impl HTLCOutputInCommitment {
480 pub(crate) fn take_ptr(mut self) -> *mut nativeHTLCOutputInCommitment {
481 assert!(self.is_owned);
482 let ret = self.inner;
483 self.inner = std::ptr::null_mut();
487 impl Clone for HTLCOutputInCommitment {
488 fn clone(&self) -> Self {
490 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
496 /// Used only if an object of this type is returned as a trait impl by a method
497 pub(crate) extern "C" fn HTLCOutputInCommitment_clone_void(this_ptr: *const c_void) -> *mut c_void {
498 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHTLCOutputInCommitment)).clone() })) as *mut c_void
500 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
501 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
502 /// need to compare this value to whether the commitment transaction in question is that of
503 /// the counterparty or our own.
505 pub extern "C" fn HTLCOutputInCommitment_get_offered(this_ptr: &HTLCOutputInCommitment) -> bool {
506 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.offered;
509 /// Whether the HTLC was \"offered\" (ie outbound in relation to this commitment transaction).
510 /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
511 /// need to compare this value to whether the commitment transaction in question is that of
512 /// the counterparty or our own.
514 pub extern "C" fn HTLCOutputInCommitment_set_offered(this_ptr: &mut HTLCOutputInCommitment, mut val: bool) {
515 unsafe { &mut *this_ptr.inner }.offered = val;
517 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
518 /// this divided by 1000.
520 pub extern "C" fn HTLCOutputInCommitment_get_amount_msat(this_ptr: &HTLCOutputInCommitment) -> u64 {
521 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.amount_msat;
524 /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
525 /// this divided by 1000.
527 pub extern "C" fn HTLCOutputInCommitment_set_amount_msat(this_ptr: &mut HTLCOutputInCommitment, mut val: u64) {
528 unsafe { &mut *this_ptr.inner }.amount_msat = val;
530 /// The CLTV lock-time at which this HTLC expires.
532 pub extern "C" fn HTLCOutputInCommitment_get_cltv_expiry(this_ptr: &HTLCOutputInCommitment) -> u32 {
533 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry;
536 /// The CLTV lock-time at which this HTLC expires.
538 pub extern "C" fn HTLCOutputInCommitment_set_cltv_expiry(this_ptr: &mut HTLCOutputInCommitment, mut val: u32) {
539 unsafe { &mut *this_ptr.inner }.cltv_expiry = val;
541 /// The hash of the preimage which unlocks this HTLC.
543 pub extern "C" fn HTLCOutputInCommitment_get_payment_hash(this_ptr: &HTLCOutputInCommitment) -> *const [u8; 32] {
544 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_hash;
547 /// The hash of the preimage which unlocks this HTLC.
549 pub extern "C" fn HTLCOutputInCommitment_set_payment_hash(this_ptr: &mut HTLCOutputInCommitment, mut val: crate::c_types::ThirtyTwoBytes) {
550 unsafe { &mut *this_ptr.inner }.payment_hash = ::lightning::ln::channelmanager::PaymentHash(val.data);
553 pub extern "C" fn HTLCOutputInCommitment_write(obj: *const HTLCOutputInCommitment) -> crate::c_types::derived::CVec_u8Z {
554 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
557 pub extern "C" fn HTLCOutputInCommitment_read(ser: crate::c_types::u8slice) -> HTLCOutputInCommitment {
558 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
559 HTLCOutputInCommitment { inner: Box::into_raw(Box::new(res)), is_owned: true }
561 HTLCOutputInCommitment { inner: std::ptr::null_mut(), is_owned: true }
564 /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
565 /// does not need to have its previous_output_index filled.
567 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 {
568 let mut ret = lightning::ln::chan_utils::get_htlc_redeemscript(unsafe { &*htlc.inner }, unsafe { &*keys.inner });
569 ret.into_bytes().into()
572 /// Gets the redeemscript for a funding output from the two funding public keys.
573 /// Note that the order of funding public keys does not matter.
575 pub extern "C" fn make_funding_redeemscript(broadcaster: crate::c_types::PublicKey, countersignatory: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
576 let mut ret = lightning::ln::chan_utils::make_funding_redeemscript(&broadcaster.into_rust(), &countersignatory.into_rust());
577 ret.into_bytes().into()
580 /// panics if htlc.transaction_output_index.is_none()!
582 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, broadcaster_delayed_payment_key: crate::c_types::PublicKey, revocation_key: crate::c_types::PublicKey) -> crate::c_types::derived::CVec_u8Z {
583 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());
584 let mut local_ret = ::bitcoin::consensus::encode::serialize(&ret);
589 use lightning::ln::chan_utils::HolderCommitmentTransaction as nativeHolderCommitmentTransactionImport;
590 type nativeHolderCommitmentTransaction = nativeHolderCommitmentTransactionImport;
592 /// We use this to track holder commitment transactions and put off signing them until we are ready
593 /// to broadcast. This class can be used inside a signer implementation to generate a signature
594 /// given the relevant secret key.
597 pub struct HolderCommitmentTransaction {
598 /// Nearly everyhwere, inner must be non-null, however in places where
599 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
600 pub inner: *mut nativeHolderCommitmentTransaction,
604 impl Drop for HolderCommitmentTransaction {
606 if self.is_owned && !self.inner.is_null() {
607 let _ = unsafe { Box::from_raw(self.inner) };
612 pub extern "C" fn HolderCommitmentTransaction_free(this_ptr: HolderCommitmentTransaction) { }
614 /// Used only if an object of this type is returned as a trait impl by a method
615 extern "C" fn HolderCommitmentTransaction_free_void(this_ptr: *mut c_void) {
616 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeHolderCommitmentTransaction); }
619 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
620 impl HolderCommitmentTransaction {
621 pub(crate) fn take_ptr(mut self) -> *mut nativeHolderCommitmentTransaction {
622 assert!(self.is_owned);
623 let ret = self.inner;
624 self.inner = std::ptr::null_mut();
628 impl Clone for HolderCommitmentTransaction {
629 fn clone(&self) -> Self {
631 inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
637 /// Used only if an object of this type is returned as a trait impl by a method
638 pub(crate) extern "C" fn HolderCommitmentTransaction_clone_void(this_ptr: *const c_void) -> *mut c_void {
639 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeHolderCommitmentTransaction)).clone() })) as *mut c_void
641 /// The commitment transaction itself, in unsigned form.
643 pub extern "C" fn HolderCommitmentTransaction_get_unsigned_tx(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
644 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.unsigned_tx;
645 let mut local_inner_val = ::bitcoin::consensus::encode::serialize(inner_val);
646 local_inner_val.into()
648 /// The commitment transaction itself, in unsigned form.
650 pub extern "C" fn HolderCommitmentTransaction_set_unsigned_tx(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_u8Z) {
651 unsafe { &mut *this_ptr.inner }.unsigned_tx = ::bitcoin::consensus::encode::deserialize(&val.into_rust()[..]).unwrap();
653 /// Our counterparty's signature for the transaction, above.
655 pub extern "C" fn HolderCommitmentTransaction_get_counterparty_sig(this_ptr: &HolderCommitmentTransaction) -> crate::c_types::Signature {
656 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.counterparty_sig;
657 crate::c_types::Signature::from_rust(&(*inner_val))
659 /// Our counterparty's signature for the transaction, above.
661 pub extern "C" fn HolderCommitmentTransaction_set_counterparty_sig(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::Signature) {
662 unsafe { &mut *this_ptr.inner }.counterparty_sig = val.into_rust();
664 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
665 /// controlled by the channel initiator.
667 pub extern "C" fn HolderCommitmentTransaction_get_feerate_per_kw(this_ptr: &HolderCommitmentTransaction) -> u32 {
668 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.feerate_per_kw;
671 /// The feerate paid per 1000-weight-unit in this commitment transaction. This value is
672 /// controlled by the channel initiator.
674 pub extern "C" fn HolderCommitmentTransaction_set_feerate_per_kw(this_ptr: &mut HolderCommitmentTransaction, mut val: u32) {
675 unsafe { &mut *this_ptr.inner }.feerate_per_kw = val;
677 /// The HTLCs and counterparty htlc signatures which were included in this commitment transaction.
679 /// Note that this includes all HTLCs, including ones which were considered dust and not
680 /// actually included in the transaction as it appears on-chain, but who's value is burned as
681 /// fees and not included in the to_holder or to_counterparty outputs.
683 /// The counterparty HTLC signatures in the second element will always be set for non-dust HTLCs, ie
684 /// those for which transaction_output_index.is_some().
686 pub extern "C" fn HolderCommitmentTransaction_set_per_htlc(this_ptr: &mut HolderCommitmentTransaction, mut val: crate::c_types::derived::CVec_C2Tuple_HTLCOutputInCommitmentSignatureZZ) {
687 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 }); };
688 unsafe { &mut *this_ptr.inner }.per_htlc = local_val;
690 /// Generate a new HolderCommitmentTransaction based on a raw commitment transaction,
691 /// counterparty signature and both parties keys.
693 /// The unsigned transaction outputs must be consistent with htlc_data. This function
694 /// only checks that the shape and amounts are consistent, but does not check the scriptPubkey.
697 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, holder_funding_key: crate::c_types::PublicKey, 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 {
698 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 }); };
699 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(), &holder_funding_key.into_rust(), &counterparty_funding_key.into_rust(), *unsafe { Box::from_raw(keys.take_ptr()) }, feerate_per_kw, local_htlc_data);
700 crate::ln::chan_utils::HolderCommitmentTransaction { inner: Box::into_raw(Box::new(ret)), is_owned: true }
703 /// The pre-calculated transaction creation public keys.
704 /// An external validating signer should not trust these keys.
707 pub extern "C" fn HolderCommitmentTransaction_trust_key_derivation(this_arg: &HolderCommitmentTransaction) -> crate::ln::chan_utils::TxCreationKeys {
708 let mut ret = unsafe { &*this_arg.inner }.trust_key_derivation();
709 crate::ln::chan_utils::TxCreationKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
712 /// Get the txid of the holder commitment transaction contained in this
713 /// HolderCommitmentTransaction
716 pub extern "C" fn HolderCommitmentTransaction_txid(this_arg: &HolderCommitmentTransaction) -> crate::c_types::ThirtyTwoBytes {
717 let mut ret = unsafe { &*this_arg.inner }.txid();
718 crate::c_types::ThirtyTwoBytes { data: ret.into_inner() }
721 /// Gets holder signature for the contained commitment transaction given holder funding private key.
723 /// Funding key is your key included in the 2-2 funding_outpoint lock. Should be provided
724 /// by your ChannelKeys.
725 /// Funding redeemscript is script locking funding_outpoint. This is the mutlsig script
726 /// between your own funding key and your counterparty's. Currently, this is provided in
727 /// ChannelKeys::sign_holder_commitment() calls directly.
728 /// Channel value is amount locked in funding_outpoint.
731 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 {
732 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());
733 crate::c_types::Signature::from_rust(&ret)
736 /// Get a signature for each HTLC which was included in the commitment transaction (ie for
737 /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
739 /// The returned Vec has one entry for each HTLC, and in the same order. For HTLCs which were
740 /// considered dust and not included, a None entry exists, for all others a signature is
744 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 {
745 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());
746 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*/ }) };
751 pub extern "C" fn HolderCommitmentTransaction_write(obj: *const HolderCommitmentTransaction) -> crate::c_types::derived::CVec_u8Z {
752 crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
755 pub extern "C" fn HolderCommitmentTransaction_read(ser: crate::c_types::u8slice) -> HolderCommitmentTransaction {
756 if let Ok(res) = crate::c_types::deserialize_obj(ser) {
757 HolderCommitmentTransaction { inner: Box::into_raw(Box::new(res)), is_owned: true }
759 HolderCommitmentTransaction { inner: std::ptr::null_mut(), is_owned: true }