[bindings] update generated code
[rust-lightning] / lightning-c-bindings / src / chain / keysinterface.rs
1 //! keysinterface provides keys into rust-lightning and defines some useful enums which describe
2 //! spendable on-chain outputs which the user owns and is responsible for using just as any other
3 //! on-chain output which is theirs.
4
5 use std::ffi::c_void;
6 use bitcoin::hashes::Hash;
7 use crate::c_types::*;
8
9 /// When on-chain outputs are created by rust-lightning (which our counterparty is not able to
10 /// claim at any point in the future) an event is generated which you must track and be able to
11 /// spend on-chain. The information needed to do this is provided in this enum, including the
12 /// outpoint describing which txid and output index is available, the full output which exists at
13 /// that txid/index, and any keys or other information required to sign.
14 #[must_use]
15 #[derive(Clone)]
16 #[repr(C)]
17 pub enum SpendableOutputDescriptor {
18         /// An output to a script which was provided via KeysInterface, thus you should already know
19         /// how to spend it. No keys are provided as rust-lightning was never given any keys - only the
20         /// script_pubkey as it appears in the output.
21         /// These may include outputs from a transaction punishing our counterparty or claiming an HTLC
22         /// on-chain using the payment preimage or after it has timed out.
23         StaticOutput {
24                 outpoint: crate::chain::transaction::OutPoint,
25                 output: crate::c_types::TxOut,
26         },
27         /// An output to a P2WSH script which can be spent with a single signature after a CSV delay.
28         ///
29         /// The witness in the spending input should be:
30         /// <BIP 143 signature> <empty vector> (MINIMALIF standard rule) <provided witnessScript>
31         ///
32         /// Note that the nSequence field in the spending input must be set to to_self_delay
33         /// (which means the transaction is not broadcastable until at least to_self_delay
34         /// blocks after the outpoint confirms).
35         ///
36         /// These are generally the result of a \"revocable\" output to us, spendable only by us unless
37         /// it is an output from an old state which we broadcast (which should never happen).
38         ///
39         /// To derive the delayed_payment key which is used to sign for this input, you must pass the
40         /// holder delayed_payment_base_key (ie the private key which corresponds to the pubkey in
41         /// ChannelKeys::pubkeys().delayed_payment_basepoint) and the provided per_commitment_point to
42         /// chan_utils::derive_private_key. The public key can be generated without the secret key
43         /// using chan_utils::derive_public_key and only the delayed_payment_basepoint which appears in
44         /// ChannelKeys::pubkeys().
45         ///
46         /// To derive the revocation_pubkey provided here (which is used in the witness
47         /// script generation), you must pass the counterparty revocation_basepoint (which appears in the
48         /// call to ChannelKeys::on_accept) and the provided per_commitment point
49         /// to chan_utils::derive_public_revocation_key.
50         ///
51         /// The witness script which is hashed and included in the output script_pubkey may be
52         /// regenerated by passing the revocation_pubkey (derived as above), our delayed_payment pubkey
53         /// (derived as above), and the to_self_delay contained here to
54         /// chan_utils::get_revokeable_redeemscript.
55         DynamicOutputP2WSH {
56                 outpoint: crate::chain::transaction::OutPoint,
57                 per_commitment_point: crate::c_types::PublicKey,
58                 to_self_delay: u16,
59                 output: crate::c_types::TxOut,
60                 key_derivation_params: crate::c_types::derived::C2Tuple_u64u64Z,
61                 revocation_pubkey: crate::c_types::PublicKey,
62         },
63         /// An output to a P2WPKH, spendable exclusively by our payment key (ie the private key which
64         /// corresponds to the public key in ChannelKeys::pubkeys().payment_point).
65         /// The witness in the spending input, is, thus, simply:
66         /// <BIP 143 signature> <payment key>
67         ///
68         /// These are generally the result of our counterparty having broadcast the current state,
69         /// allowing us to claim the non-HTLC-encumbered outputs immediately.
70         StaticOutputCounterpartyPayment {
71                 outpoint: crate::chain::transaction::OutPoint,
72                 output: crate::c_types::TxOut,
73                 key_derivation_params: crate::c_types::derived::C2Tuple_u64u64Z,
74         },
75 }
76 use lightning::chain::keysinterface::SpendableOutputDescriptor as nativeSpendableOutputDescriptor;
77 impl SpendableOutputDescriptor {
78         #[allow(unused)]
79         pub(crate) fn to_native(&self) -> nativeSpendableOutputDescriptor {
80                 match self {
81                         SpendableOutputDescriptor::StaticOutput {ref outpoint, ref output, } => {
82                                 let mut outpoint_nonref = (*outpoint).clone();
83                                 let mut output_nonref = (*output).clone();
84                                 nativeSpendableOutputDescriptor::StaticOutput {
85                                         outpoint: *unsafe { Box::from_raw(outpoint_nonref.take_ptr()) },
86                                         output: output_nonref.into_rust(),
87                                 }
88                         },
89                         SpendableOutputDescriptor::DynamicOutputP2WSH {ref outpoint, ref per_commitment_point, ref to_self_delay, ref output, ref key_derivation_params, ref revocation_pubkey, } => {
90                                 let mut outpoint_nonref = (*outpoint).clone();
91                                 let mut per_commitment_point_nonref = (*per_commitment_point).clone();
92                                 let mut to_self_delay_nonref = (*to_self_delay).clone();
93                                 let mut output_nonref = (*output).clone();
94                                 let mut key_derivation_params_nonref = (*key_derivation_params).clone();
95                                 let (mut orig_key_derivation_params_nonref_0, mut orig_key_derivation_params_nonref_1) = key_derivation_params_nonref.to_rust(); let mut local_key_derivation_params_nonref = (orig_key_derivation_params_nonref_0, orig_key_derivation_params_nonref_1);
96                                 let mut revocation_pubkey_nonref = (*revocation_pubkey).clone();
97                                 nativeSpendableOutputDescriptor::DynamicOutputP2WSH {
98                                         outpoint: *unsafe { Box::from_raw(outpoint_nonref.take_ptr()) },
99                                         per_commitment_point: per_commitment_point_nonref.into_rust(),
100                                         to_self_delay: to_self_delay_nonref,
101                                         output: output_nonref.into_rust(),
102                                         key_derivation_params: local_key_derivation_params_nonref,
103                                         revocation_pubkey: revocation_pubkey_nonref.into_rust(),
104                                 }
105                         },
106                         SpendableOutputDescriptor::StaticOutputCounterpartyPayment {ref outpoint, ref output, ref key_derivation_params, } => {
107                                 let mut outpoint_nonref = (*outpoint).clone();
108                                 let mut output_nonref = (*output).clone();
109                                 let mut key_derivation_params_nonref = (*key_derivation_params).clone();
110                                 let (mut orig_key_derivation_params_nonref_0, mut orig_key_derivation_params_nonref_1) = key_derivation_params_nonref.to_rust(); let mut local_key_derivation_params_nonref = (orig_key_derivation_params_nonref_0, orig_key_derivation_params_nonref_1);
111                                 nativeSpendableOutputDescriptor::StaticOutputCounterpartyPayment {
112                                         outpoint: *unsafe { Box::from_raw(outpoint_nonref.take_ptr()) },
113                                         output: output_nonref.into_rust(),
114                                         key_derivation_params: local_key_derivation_params_nonref,
115                                 }
116                         },
117                 }
118         }
119         #[allow(unused)]
120         pub(crate) fn into_native(self) -> nativeSpendableOutputDescriptor {
121                 match self {
122                         SpendableOutputDescriptor::StaticOutput {mut outpoint, mut output, } => {
123                                 nativeSpendableOutputDescriptor::StaticOutput {
124                                         outpoint: *unsafe { Box::from_raw(outpoint.take_ptr()) },
125                                         output: output.into_rust(),
126                                 }
127                         },
128                         SpendableOutputDescriptor::DynamicOutputP2WSH {mut outpoint, mut per_commitment_point, mut to_self_delay, mut output, mut key_derivation_params, mut revocation_pubkey, } => {
129                                 let (mut orig_key_derivation_params_0, mut orig_key_derivation_params_1) = key_derivation_params.to_rust(); let mut local_key_derivation_params = (orig_key_derivation_params_0, orig_key_derivation_params_1);
130                                 nativeSpendableOutputDescriptor::DynamicOutputP2WSH {
131                                         outpoint: *unsafe { Box::from_raw(outpoint.take_ptr()) },
132                                         per_commitment_point: per_commitment_point.into_rust(),
133                                         to_self_delay: to_self_delay,
134                                         output: output.into_rust(),
135                                         key_derivation_params: local_key_derivation_params,
136                                         revocation_pubkey: revocation_pubkey.into_rust(),
137                                 }
138                         },
139                         SpendableOutputDescriptor::StaticOutputCounterpartyPayment {mut outpoint, mut output, mut key_derivation_params, } => {
140                                 let (mut orig_key_derivation_params_0, mut orig_key_derivation_params_1) = key_derivation_params.to_rust(); let mut local_key_derivation_params = (orig_key_derivation_params_0, orig_key_derivation_params_1);
141                                 nativeSpendableOutputDescriptor::StaticOutputCounterpartyPayment {
142                                         outpoint: *unsafe { Box::from_raw(outpoint.take_ptr()) },
143                                         output: output.into_rust(),
144                                         key_derivation_params: local_key_derivation_params,
145                                 }
146                         },
147                 }
148         }
149         #[allow(unused)]
150         pub(crate) fn from_native(native: &nativeSpendableOutputDescriptor) -> Self {
151                 match native {
152                         nativeSpendableOutputDescriptor::StaticOutput {ref outpoint, ref output, } => {
153                                 let mut outpoint_nonref = (*outpoint).clone();
154                                 let mut output_nonref = (*output).clone();
155                                 SpendableOutputDescriptor::StaticOutput {
156                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint_nonref)), is_owned: true },
157                                         output: crate::c_types::TxOut::from_rust(output_nonref),
158                                 }
159                         },
160                         nativeSpendableOutputDescriptor::DynamicOutputP2WSH {ref outpoint, ref per_commitment_point, ref to_self_delay, ref output, ref key_derivation_params, ref revocation_pubkey, } => {
161                                 let mut outpoint_nonref = (*outpoint).clone();
162                                 let mut per_commitment_point_nonref = (*per_commitment_point).clone();
163                                 let mut to_self_delay_nonref = (*to_self_delay).clone();
164                                 let mut output_nonref = (*output).clone();
165                                 let mut key_derivation_params_nonref = (*key_derivation_params).clone();
166                                 let (mut orig_key_derivation_params_nonref_0, mut orig_key_derivation_params_nonref_1) = key_derivation_params_nonref; let mut local_key_derivation_params_nonref = (orig_key_derivation_params_nonref_0, orig_key_derivation_params_nonref_1).into();
167                                 let mut revocation_pubkey_nonref = (*revocation_pubkey).clone();
168                                 SpendableOutputDescriptor::DynamicOutputP2WSH {
169                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint_nonref)), is_owned: true },
170                                         per_commitment_point: crate::c_types::PublicKey::from_rust(&per_commitment_point_nonref),
171                                         to_self_delay: to_self_delay_nonref,
172                                         output: crate::c_types::TxOut::from_rust(output_nonref),
173                                         key_derivation_params: local_key_derivation_params_nonref,
174                                         revocation_pubkey: crate::c_types::PublicKey::from_rust(&revocation_pubkey_nonref),
175                                 }
176                         },
177                         nativeSpendableOutputDescriptor::StaticOutputCounterpartyPayment {ref outpoint, ref output, ref key_derivation_params, } => {
178                                 let mut outpoint_nonref = (*outpoint).clone();
179                                 let mut output_nonref = (*output).clone();
180                                 let mut key_derivation_params_nonref = (*key_derivation_params).clone();
181                                 let (mut orig_key_derivation_params_nonref_0, mut orig_key_derivation_params_nonref_1) = key_derivation_params_nonref; let mut local_key_derivation_params_nonref = (orig_key_derivation_params_nonref_0, orig_key_derivation_params_nonref_1).into();
182                                 SpendableOutputDescriptor::StaticOutputCounterpartyPayment {
183                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint_nonref)), is_owned: true },
184                                         output: crate::c_types::TxOut::from_rust(output_nonref),
185                                         key_derivation_params: local_key_derivation_params_nonref,
186                                 }
187                         },
188                 }
189         }
190         #[allow(unused)]
191         pub(crate) fn native_into(native: nativeSpendableOutputDescriptor) -> Self {
192                 match native {
193                         nativeSpendableOutputDescriptor::StaticOutput {mut outpoint, mut output, } => {
194                                 SpendableOutputDescriptor::StaticOutput {
195                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint)), is_owned: true },
196                                         output: crate::c_types::TxOut::from_rust(output),
197                                 }
198                         },
199                         nativeSpendableOutputDescriptor::DynamicOutputP2WSH {mut outpoint, mut per_commitment_point, mut to_self_delay, mut output, mut key_derivation_params, mut revocation_pubkey, } => {
200                                 let (mut orig_key_derivation_params_0, mut orig_key_derivation_params_1) = key_derivation_params; let mut local_key_derivation_params = (orig_key_derivation_params_0, orig_key_derivation_params_1).into();
201                                 SpendableOutputDescriptor::DynamicOutputP2WSH {
202                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint)), is_owned: true },
203                                         per_commitment_point: crate::c_types::PublicKey::from_rust(&per_commitment_point),
204                                         to_self_delay: to_self_delay,
205                                         output: crate::c_types::TxOut::from_rust(output),
206                                         key_derivation_params: local_key_derivation_params,
207                                         revocation_pubkey: crate::c_types::PublicKey::from_rust(&revocation_pubkey),
208                                 }
209                         },
210                         nativeSpendableOutputDescriptor::StaticOutputCounterpartyPayment {mut outpoint, mut output, mut key_derivation_params, } => {
211                                 let (mut orig_key_derivation_params_0, mut orig_key_derivation_params_1) = key_derivation_params; let mut local_key_derivation_params = (orig_key_derivation_params_0, orig_key_derivation_params_1).into();
212                                 SpendableOutputDescriptor::StaticOutputCounterpartyPayment {
213                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint)), is_owned: true },
214                                         output: crate::c_types::TxOut::from_rust(output),
215                                         key_derivation_params: local_key_derivation_params,
216                                 }
217                         },
218                 }
219         }
220 }
221 #[no_mangle]
222 pub extern "C" fn SpendableOutputDescriptor_free(this_ptr: SpendableOutputDescriptor) { }
223 /// Set of lightning keys needed to operate a channel as described in BOLT 3.
224 ///
225 /// Signing services could be implemented on a hardware wallet. In this case,
226 /// the current ChannelKeys would be a front-end on top of a communication
227 /// channel connected to your secure device and lightning key material wouldn't
228 /// reside on a hot server. Nevertheless, a this deployment would still need
229 /// to trust the ChannelManager to avoid loss of funds as this latest component
230 /// could ask to sign commitment transaction with HTLCs paying to attacker pubkeys.
231 ///
232 /// A more secure iteration would be to use hashlock (or payment points) to pair
233 /// invoice/incoming HTLCs with outgoing HTLCs to implement a no-trust-ChannelManager
234 /// at the price of more state and computation on the hardware wallet side. In the future,
235 /// we are looking forward to design such interface.
236 ///
237 /// In any case, ChannelMonitor or fallback watchtowers are always going to be trusted
238 /// to act, as liveness and breach reply correctness are always going to be hard requirements
239 /// of LN security model, orthogonal of key management issues.
240 ///
241 /// If you're implementing a custom signer, you almost certainly want to implement
242 /// Readable/Writable to serialize out a unique reference to this set of keys so
243 /// that you can serialize the full ChannelManager object.
244 ///
245 #[repr(C)]
246 pub struct ChannelKeys {
247         pub this_arg: *mut c_void,
248         /// Gets the per-commitment point for a specific commitment number
249         ///
250         /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
251         #[must_use]
252         pub get_per_commitment_point: extern "C" fn (this_arg: *const c_void, idx: u64) -> crate::c_types::PublicKey,
253         /// Gets the commitment secret for a specific commitment number as part of the revocation process
254         ///
255         /// An external signer implementation should error here if the commitment was already signed
256         /// and should refuse to sign it in the future.
257         ///
258         /// May be called more than once for the same index.
259         ///
260         /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
261         /// TODO: return a Result so we can signal a validation error
262         #[must_use]
263         pub release_commitment_secret: extern "C" fn (this_arg: *const c_void, idx: u64) -> crate::c_types::ThirtyTwoBytes,
264         /// Gets the holder's channel public keys and basepoints
265         pub pubkeys: crate::ln::chan_utils::ChannelPublicKeys,
266         /// Fill in the pubkeys field as a reference to it will be given to Rust after this returns
267         /// Note that this takes a pointer to this object, not the this_ptr like other methods do
268         /// This function pointer may be NULL if pubkeys is filled in when this object is created and never needs updating.
269         pub set_pubkeys: Option<extern "C" fn(&ChannelKeys)>,
270         /// Gets arbitrary identifiers describing the set of keys which are provided back to you in
271         /// some SpendableOutputDescriptor types. These should be sufficient to identify this
272         /// ChannelKeys object uniquely and lookup or re-derive its keys.
273         #[must_use]
274         pub key_derivation_params: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::C2Tuple_u64u64Z,
275         /// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
276         ///
277         /// Note that if signing fails or is rejected, the channel will be force-closed.
278         #[must_use]
279         pub sign_counterparty_commitment: extern "C" fn (this_arg: *const c_void, feerate_per_kw: u32, commitment_tx: crate::c_types::Transaction, keys: &crate::ln::chan_utils::PreCalculatedTxCreationKeys, htlcs: crate::c_types::derived::CVec_HTLCOutputInCommitmentZ) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ,
280         /// Create a signature for a holder's commitment transaction. This will only ever be called with
281         /// the same holder_commitment_tx (or a copy thereof), though there are currently no guarantees
282         /// that it will not be called multiple times.
283         /// An external signer implementation should check that the commitment has not been revoked.
284         #[must_use]
285         pub sign_holder_commitment: extern "C" fn (this_arg: *const c_void, holder_commitment_tx: &crate::ln::chan_utils::HolderCommitmentTransaction) -> crate::c_types::derived::CResult_SignatureNoneZ,
286         /// Create a signature for each HTLC transaction spending a holder's commitment transaction.
287         ///
288         /// Unlike sign_holder_commitment, this may be called multiple times with *different*
289         /// holder_commitment_tx values. While this will never be called with a revoked
290         /// holder_commitment_tx, it is possible that it is called with the second-latest
291         /// holder_commitment_tx (only if we haven't yet revoked it) if some watchtower/secondary
292         /// ChannelMonitor decided to broadcast before it had been updated to the latest.
293         ///
294         /// Either an Err should be returned, or a Vec with one entry for each HTLC which exists in
295         /// holder_commitment_tx. For those HTLCs which have transaction_output_index set to None
296         /// (implying they were considered dust at the time the commitment transaction was negotiated),
297         /// a corresponding None should be included in the return value. All other positions in the
298         /// return value must contain a signature.
299         #[must_use]
300         pub sign_holder_commitment_htlc_transactions: extern "C" fn (this_arg: *const c_void, holder_commitment_tx: &crate::ln::chan_utils::HolderCommitmentTransaction) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ,
301         /// Create a signature for the given input in a transaction spending an HTLC or commitment
302         /// transaction output when our counterparty broadcasts an old state.
303         ///
304         /// A justice transaction may claim multiples outputs at the same time if timelocks are
305         /// similar, but only a signature for the input at index `input` should be signed for here.
306         /// It may be called multiples time for same output(s) if a fee-bump is needed with regards
307         /// to an upcoming timelock expiration.
308         ///
309         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
310         ///
311         /// per_commitment_key is revocation secret which was provided by our counterparty when they
312         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
313         /// not allow the spending of any funds by itself (you need our holder revocation_secret to do
314         /// so).
315         ///
316         /// htlc holds HTLC elements (hash, timelock) if the output being spent is a HTLC output, thus
317         /// changing the format of the witness script (which is committed to in the BIP 143
318         /// signatures).
319         #[must_use]
320         pub sign_justice_transaction: extern "C" fn (this_arg: *const c_void, justice_tx: crate::c_types::Transaction, input: usize, amount: u64, per_commitment_key: *const [u8; 32], htlc: &crate::ln::chan_utils::HTLCOutputInCommitment) -> crate::c_types::derived::CResult_SignatureNoneZ,
321         /// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
322         /// transaction, either offered or received.
323         ///
324         /// Such a transaction may claim multiples offered outputs at same time if we know the
325         /// preimage for each when we create it, but only the input at index `input` should be
326         /// signed for here. It may be called multiple times for same output(s) if a fee-bump is
327         /// needed with regards to an upcoming timelock expiration.
328         ///
329         /// Witness_script is either a offered or received script as defined in BOLT3 for HTLC
330         /// outputs.
331         ///
332         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
333         ///
334         /// Per_commitment_point is the dynamic point corresponding to the channel state
335         /// detected onchain. It has been generated by our counterparty and is used to derive
336         /// channel state keys, which are then included in the witness script and committed to in the
337         /// BIP 143 signature.
338         #[must_use]
339         pub sign_counterparty_htlc_transaction: extern "C" fn (this_arg: *const c_void, htlc_tx: crate::c_types::Transaction, input: usize, amount: u64, per_commitment_point: crate::c_types::PublicKey, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment) -> crate::c_types::derived::CResult_SignatureNoneZ,
340         /// Create a signature for a (proposed) closing transaction.
341         ///
342         /// Note that, due to rounding, there may be one \"missing\" satoshi, and either party may have
343         /// chosen to forgo their output as dust.
344         #[must_use]
345         pub sign_closing_transaction: extern "C" fn (this_arg: *const c_void, closing_tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_SignatureNoneZ,
346         /// Signs a channel announcement message with our funding key, proving it comes from one
347         /// of the channel participants.
348         ///
349         /// Note that if this fails or is rejected, the channel will not be publicly announced and
350         /// our counterparty may (though likely will not) close the channel on us for violating the
351         /// protocol.
352         #[must_use]
353         pub sign_channel_announcement: extern "C" fn (this_arg: *const c_void, msg: &crate::ln::msgs::UnsignedChannelAnnouncement) -> crate::c_types::derived::CResult_SignatureNoneZ,
354         /// Set the counterparty channel basepoints and counterparty_selected/holder_selected_contest_delay.
355         /// This is done immediately on incoming channels and as soon as the channel is accepted on outgoing channels.
356         ///
357         /// We bind holder_selected_contest_delay late here for API convenience.
358         ///
359         /// Will be called before any signatures are applied.
360         pub on_accept: extern "C" fn (this_arg: *mut c_void, channel_points: &crate::ln::chan_utils::ChannelPublicKeys, counterparty_selected_contest_delay: u16, holder_selected_contest_delay: u16),
361         pub clone: Option<extern "C" fn (this_arg: *const c_void) -> *mut c_void>,
362         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
363 }
364 unsafe impl Send for ChannelKeys {}
365 impl Clone for ChannelKeys {
366         fn clone(&self) -> Self {
367                 Self {
368                 this_arg: if let Some(f) = self.clone { (f)(self.this_arg) } else { self.this_arg },
369                         get_per_commitment_point: self.get_per_commitment_point.clone(),
370                         release_commitment_secret: self.release_commitment_secret.clone(),
371                         pubkeys: self.pubkeys.clone(),
372                         set_pubkeys: self.set_pubkeys.clone(),
373                         key_derivation_params: self.key_derivation_params.clone(),
374                         sign_counterparty_commitment: self.sign_counterparty_commitment.clone(),
375                         sign_holder_commitment: self.sign_holder_commitment.clone(),
376                         sign_holder_commitment_htlc_transactions: self.sign_holder_commitment_htlc_transactions.clone(),
377                         sign_justice_transaction: self.sign_justice_transaction.clone(),
378                         sign_counterparty_htlc_transaction: self.sign_counterparty_htlc_transaction.clone(),
379                         sign_closing_transaction: self.sign_closing_transaction.clone(),
380                         sign_channel_announcement: self.sign_channel_announcement.clone(),
381                         on_accept: self.on_accept.clone(),
382                         clone: self.clone.clone(),
383                         free: self.free.clone(),
384                 }
385         }
386 }
387
388 use lightning::chain::keysinterface::ChannelKeys as rustChannelKeys;
389 impl rustChannelKeys for ChannelKeys {
390         fn get_per_commitment_point<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, idx: u64, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> bitcoin::secp256k1::key::PublicKey {
391                 let mut ret = (self.get_per_commitment_point)(self.this_arg, idx);
392                 ret.into_rust()
393         }
394         fn release_commitment_secret(&self, idx: u64) -> [u8; 32] {
395                 let mut ret = (self.release_commitment_secret)(self.this_arg, idx);
396                 ret.data
397         }
398         fn pubkeys(&self) -> &lightning::ln::chan_utils::ChannelPublicKeys {
399                 if let Some(f) = self.set_pubkeys {
400                         (f)(self);
401                 }
402                 unsafe { &*self.pubkeys.inner }
403         }
404         fn key_derivation_params(&self) -> (u64, u64) {
405                 let mut ret = (self.key_derivation_params)(self.this_arg);
406                 let (mut orig_ret_0, mut orig_ret_1) = ret.to_rust(); let mut local_ret = (orig_ret_0, orig_ret_1);
407                 local_ret
408         }
409         fn sign_counterparty_commitment<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, feerate_per_kw: u32, commitment_tx: &bitcoin::blockdata::transaction::Transaction, keys: &lightning::ln::chan_utils::PreCalculatedTxCreationKeys, htlcs: &[&lightning::ln::chan_utils::HTLCOutputInCommitment], _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<(bitcoin::secp256k1::Signature, Vec<bitcoin::secp256k1::Signature>), ()> {
410                 let mut local_commitment_tx = ::bitcoin::consensus::encode::serialize(commitment_tx);
411                 let mut local_htlcs = Vec::new(); for item in htlcs.iter() { local_htlcs.push( { crate::ln::chan_utils::HTLCOutputInCommitment { inner: unsafe { ( (&(**item) as *const _) as *mut _) }, is_owned: false } }); };
412                 let mut ret = (self.sign_counterparty_commitment)(self.this_arg, feerate_per_kw, crate::c_types::Transaction::from_vec(local_commitment_tx), &crate::ln::chan_utils::PreCalculatedTxCreationKeys { inner: unsafe { (keys as *const _) as *mut _ }, is_owned: false }, local_htlcs.into());
413                 let mut local_ret = match ret.result_ok { true => Ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).to_rust(); let mut local_orig_ret_0_1 = Vec::new(); for mut item in orig_ret_0_1.into_rust().drain(..) { local_orig_ret_0_1.push( { item.into_rust() }); }; let mut local_ret_0 = (orig_ret_0_0.into_rust(), local_orig_ret_0_1); local_ret_0 }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
414                 local_ret
415         }
416         fn sign_holder_commitment<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, holder_commitment_tx: &lightning::ln::chan_utils::HolderCommitmentTransaction, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
417                 let mut ret = (self.sign_holder_commitment)(self.this_arg, &crate::ln::chan_utils::HolderCommitmentTransaction { inner: unsafe { (holder_commitment_tx as *const _) as *mut _ }, is_owned: false });
418                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
419                 local_ret
420         }
421         fn sign_holder_commitment_htlc_transactions<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, holder_commitment_tx: &lightning::ln::chan_utils::HolderCommitmentTransaction, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<Vec<Option<bitcoin::secp256k1::Signature>>, ()> {
422                 let mut ret = (self.sign_holder_commitment_htlc_transactions)(self.this_arg, &crate::ln::chan_utils::HolderCommitmentTransaction { inner: unsafe { (holder_commitment_tx as *const _) as *mut _ }, is_owned: false });
423                 let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = Vec::new(); for mut item in (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).into_rust().drain(..) { local_ret_0.push( { let mut local_ret_0_0 = if item.is_null() { None } else { Some( { item.into_rust() }) }; local_ret_0_0 }); }; local_ret_0 }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
424                 local_ret
425         }
426         fn sign_justice_transaction<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, justice_tx: &bitcoin::blockdata::transaction::Transaction, input: usize, amount: u64, per_commitment_key: &bitcoin::secp256k1::key::SecretKey, htlc: &Option<lightning::ln::chan_utils::HTLCOutputInCommitment>, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
427                 let mut local_justice_tx = ::bitcoin::consensus::encode::serialize(justice_tx);
428                 let mut local_htlc = &crate::ln::chan_utils::HTLCOutputInCommitment { inner: unsafe { (if htlc.is_none() { std::ptr::null() } else {  { (htlc.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
429                 let mut ret = (self.sign_justice_transaction)(self.this_arg, crate::c_types::Transaction::from_vec(local_justice_tx), input, amount, per_commitment_key.as_ref(), local_htlc);
430                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
431                 local_ret
432         }
433         fn sign_counterparty_htlc_transaction<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, htlc_tx: &bitcoin::blockdata::transaction::Transaction, input: usize, amount: u64, per_commitment_point: &bitcoin::secp256k1::key::PublicKey, htlc: &lightning::ln::chan_utils::HTLCOutputInCommitment, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
434                 let mut local_htlc_tx = ::bitcoin::consensus::encode::serialize(htlc_tx);
435                 let mut ret = (self.sign_counterparty_htlc_transaction)(self.this_arg, crate::c_types::Transaction::from_vec(local_htlc_tx), input, amount, crate::c_types::PublicKey::from_rust(&per_commitment_point), &crate::ln::chan_utils::HTLCOutputInCommitment { inner: unsafe { (htlc as *const _) as *mut _ }, is_owned: false });
436                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
437                 local_ret
438         }
439         fn sign_closing_transaction<T:bitcoin::secp256k1::Signing>(&self, closing_tx: &bitcoin::blockdata::transaction::Transaction, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
440                 let mut local_closing_tx = ::bitcoin::consensus::encode::serialize(closing_tx);
441                 let mut ret = (self.sign_closing_transaction)(self.this_arg, crate::c_types::Transaction::from_vec(local_closing_tx));
442                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
443                 local_ret
444         }
445         fn sign_channel_announcement<T:bitcoin::secp256k1::Signing>(&self, msg: &lightning::ln::msgs::UnsignedChannelAnnouncement, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
446                 let mut ret = (self.sign_channel_announcement)(self.this_arg, &crate::ln::msgs::UnsignedChannelAnnouncement { inner: unsafe { (msg as *const _) as *mut _ }, is_owned: false });
447                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(ret.contents.result.take_ptr()) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(ret.contents.err.take_ptr()) })*/ })};
448                 local_ret
449         }
450         fn on_accept(&mut self, channel_points: &lightning::ln::chan_utils::ChannelPublicKeys, counterparty_selected_contest_delay: u16, holder_selected_contest_delay: u16) {
451                 (self.on_accept)(self.this_arg, &crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { (channel_points as *const _) as *mut _ }, is_owned: false }, counterparty_selected_contest_delay, holder_selected_contest_delay)
452         }
453 }
454
455 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
456 // directly as a Deref trait in higher-level structs:
457 impl std::ops::Deref for ChannelKeys {
458         type Target = Self;
459         fn deref(&self) -> &Self {
460                 self
461         }
462 }
463 /// Calls the free function if one is set
464 #[no_mangle]
465 pub extern "C" fn ChannelKeys_free(this_ptr: ChannelKeys) { }
466 impl Drop for ChannelKeys {
467         fn drop(&mut self) {
468                 if let Some(f) = self.free {
469                         f(self.this_arg);
470                 }
471         }
472 }
473 /// A trait to describe an object which can get user secrets and key material.
474 #[repr(C)]
475 pub struct KeysInterface {
476         pub this_arg: *mut c_void,
477         /// Get node secret key (aka node_id or network_key)
478         #[must_use]
479         pub get_node_secret: extern "C" fn (this_arg: *const c_void) -> crate::c_types::SecretKey,
480         /// Get destination redeemScript to encumber static protocol exit points.
481         #[must_use]
482         pub get_destination_script: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
483         /// Get shutdown_pubkey to use as PublicKey at channel closure
484         #[must_use]
485         pub get_shutdown_pubkey: extern "C" fn (this_arg: *const c_void) -> crate::c_types::PublicKey,
486         /// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
487         /// restarted with some stale data!
488         #[must_use]
489         pub get_channel_keys: extern "C" fn (this_arg: *const c_void, inbound: bool, channel_value_satoshis: u64) -> crate::chain::keysinterface::ChannelKeys,
490         /// Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
491         /// onion packets and for temporary channel IDs. There is no requirement that these be
492         /// persisted anywhere, though they must be unique across restarts.
493         #[must_use]
494         pub get_secure_random_bytes: extern "C" fn (this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes,
495         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
496 }
497 unsafe impl Send for KeysInterface {}
498 unsafe impl Sync for KeysInterface {}
499
500 use lightning::chain::keysinterface::KeysInterface as rustKeysInterface;
501 impl rustKeysInterface for KeysInterface {
502         type ChanKeySigner = crate::chain::keysinterface::ChannelKeys;
503         fn get_node_secret(&self) -> bitcoin::secp256k1::key::SecretKey {
504                 let mut ret = (self.get_node_secret)(self.this_arg);
505                 ret.into_rust()
506         }
507         fn get_destination_script(&self) -> bitcoin::blockdata::script::Script {
508                 let mut ret = (self.get_destination_script)(self.this_arg);
509                 ::bitcoin::blockdata::script::Script::from(ret.into_rust())
510         }
511         fn get_shutdown_pubkey(&self) -> bitcoin::secp256k1::key::PublicKey {
512                 let mut ret = (self.get_shutdown_pubkey)(self.this_arg);
513                 ret.into_rust()
514         }
515         fn get_channel_keys(&self, inbound: bool, channel_value_satoshis: u64) -> Self::ChanKeySigner {
516                 let mut ret = (self.get_channel_keys)(self.this_arg, inbound, channel_value_satoshis);
517                 ret
518         }
519         fn get_secure_random_bytes(&self) -> [u8; 32] {
520                 let mut ret = (self.get_secure_random_bytes)(self.this_arg);
521                 ret.data
522         }
523 }
524
525 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
526 // directly as a Deref trait in higher-level structs:
527 impl std::ops::Deref for KeysInterface {
528         type Target = Self;
529         fn deref(&self) -> &Self {
530                 self
531         }
532 }
533 /// Calls the free function if one is set
534 #[no_mangle]
535 pub extern "C" fn KeysInterface_free(this_ptr: KeysInterface) { }
536 impl Drop for KeysInterface {
537         fn drop(&mut self) {
538                 if let Some(f) = self.free {
539                         f(self.this_arg);
540                 }
541         }
542 }
543
544 use lightning::chain::keysinterface::InMemoryChannelKeys as nativeInMemoryChannelKeysImport;
545 type nativeInMemoryChannelKeys = nativeInMemoryChannelKeysImport;
546
547 /// A simple implementation of ChannelKeys that just keeps the private keys in memory.
548 #[must_use]
549 #[repr(C)]
550 pub struct InMemoryChannelKeys {
551         /// Nearly everywhere, inner must be non-null, however in places where
552         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
553         pub inner: *mut nativeInMemoryChannelKeys,
554         pub is_owned: bool,
555 }
556
557 impl Drop for InMemoryChannelKeys {
558         fn drop(&mut self) {
559                 if self.is_owned && !self.inner.is_null() {
560                         let _ = unsafe { Box::from_raw(self.inner) };
561                 }
562         }
563 }
564 #[no_mangle]
565 pub extern "C" fn InMemoryChannelKeys_free(this_ptr: InMemoryChannelKeys) { }
566 #[allow(unused)]
567 /// Used only if an object of this type is returned as a trait impl by a method
568 extern "C" fn InMemoryChannelKeys_free_void(this_ptr: *mut c_void) {
569         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInMemoryChannelKeys); }
570 }
571 #[allow(unused)]
572 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
573 impl InMemoryChannelKeys {
574         pub(crate) fn take_ptr(mut self) -> *mut nativeInMemoryChannelKeys {
575                 assert!(self.is_owned);
576                 let ret = self.inner;
577                 self.inner = std::ptr::null_mut();
578                 ret
579         }
580 }
581 impl Clone for InMemoryChannelKeys {
582         fn clone(&self) -> Self {
583                 Self {
584                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
585                         is_owned: true,
586                 }
587         }
588 }
589 #[allow(unused)]
590 /// Used only if an object of this type is returned as a trait impl by a method
591 pub(crate) extern "C" fn InMemoryChannelKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
592         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInMemoryChannelKeys)).clone() })) as *mut c_void
593 }
594 /// Private key of anchor tx
595 #[no_mangle]
596 pub extern "C" fn InMemoryChannelKeys_get_funding_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
597         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_key;
598         (*inner_val).as_ref()
599 }
600 /// Private key of anchor tx
601 #[no_mangle]
602 pub extern "C" fn InMemoryChannelKeys_set_funding_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
603         unsafe { &mut *this_ptr.inner }.funding_key = val.into_rust();
604 }
605 /// Holder secret key for blinded revocation pubkey
606 #[no_mangle]
607 pub extern "C" fn InMemoryChannelKeys_get_revocation_base_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
608         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_base_key;
609         (*inner_val).as_ref()
610 }
611 /// Holder secret key for blinded revocation pubkey
612 #[no_mangle]
613 pub extern "C" fn InMemoryChannelKeys_set_revocation_base_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
614         unsafe { &mut *this_ptr.inner }.revocation_base_key = val.into_rust();
615 }
616 /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions
617 #[no_mangle]
618 pub extern "C" fn InMemoryChannelKeys_get_payment_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
619         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_key;
620         (*inner_val).as_ref()
621 }
622 /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions
623 #[no_mangle]
624 pub extern "C" fn InMemoryChannelKeys_set_payment_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
625         unsafe { &mut *this_ptr.inner }.payment_key = val.into_rust();
626 }
627 /// Holder secret key used in HTLC tx
628 #[no_mangle]
629 pub extern "C" fn InMemoryChannelKeys_get_delayed_payment_base_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
630         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_base_key;
631         (*inner_val).as_ref()
632 }
633 /// Holder secret key used in HTLC tx
634 #[no_mangle]
635 pub extern "C" fn InMemoryChannelKeys_set_delayed_payment_base_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
636         unsafe { &mut *this_ptr.inner }.delayed_payment_base_key = val.into_rust();
637 }
638 /// Holder htlc secret key used in commitment tx htlc outputs
639 #[no_mangle]
640 pub extern "C" fn InMemoryChannelKeys_get_htlc_base_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
641         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_base_key;
642         (*inner_val).as_ref()
643 }
644 /// Holder htlc secret key used in commitment tx htlc outputs
645 #[no_mangle]
646 pub extern "C" fn InMemoryChannelKeys_set_htlc_base_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
647         unsafe { &mut *this_ptr.inner }.htlc_base_key = val.into_rust();
648 }
649 /// Commitment seed
650 #[no_mangle]
651 pub extern "C" fn InMemoryChannelKeys_get_commitment_seed(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
652         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.commitment_seed;
653         &(*inner_val)
654 }
655 /// Commitment seed
656 #[no_mangle]
657 pub extern "C" fn InMemoryChannelKeys_set_commitment_seed(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::ThirtyTwoBytes) {
658         unsafe { &mut *this_ptr.inner }.commitment_seed = val.data;
659 }
660 /// Create a new InMemoryChannelKeys
661 #[must_use]
662 #[no_mangle]
663 pub extern "C" fn InMemoryChannelKeys_new(mut funding_key: crate::c_types::SecretKey, mut revocation_base_key: crate::c_types::SecretKey, mut payment_key: crate::c_types::SecretKey, mut delayed_payment_base_key: crate::c_types::SecretKey, mut htlc_base_key: crate::c_types::SecretKey, mut commitment_seed: crate::c_types::ThirtyTwoBytes, mut channel_value_satoshis: u64, mut key_derivation_params: crate::c_types::derived::C2Tuple_u64u64Z) -> crate::chain::keysinterface::InMemoryChannelKeys {
664         let (mut orig_key_derivation_params_0, mut orig_key_derivation_params_1) = key_derivation_params.to_rust(); let mut local_key_derivation_params = (orig_key_derivation_params_0, orig_key_derivation_params_1);
665         let mut ret = lightning::chain::keysinterface::InMemoryChannelKeys::new(&bitcoin::secp256k1::Secp256k1::new(), funding_key.into_rust(), revocation_base_key.into_rust(), payment_key.into_rust(), delayed_payment_base_key.into_rust(), htlc_base_key.into_rust(), commitment_seed.data, channel_value_satoshis, local_key_derivation_params);
666         crate::chain::keysinterface::InMemoryChannelKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
667 }
668
669 /// Counterparty pubkeys.
670 /// Will panic if on_accept wasn't called.
671 #[must_use]
672 #[no_mangle]
673 pub extern "C" fn InMemoryChannelKeys_counterparty_pubkeys(this_arg: &InMemoryChannelKeys) -> crate::ln::chan_utils::ChannelPublicKeys {
674         let mut ret = unsafe { &*this_arg.inner }.counterparty_pubkeys();
675         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
676 }
677
678 /// The contest_delay value specified by our counterparty and applied on holder-broadcastable
679 /// transactions, ie the amount of time that we have to wait to recover our funds if we
680 /// broadcast a transaction. You'll likely want to pass this to the
681 /// ln::chan_utils::build*_transaction functions when signing holder's transactions.
682 /// Will panic if on_accept wasn't called.
683 #[must_use]
684 #[no_mangle]
685 pub extern "C" fn InMemoryChannelKeys_counterparty_selected_contest_delay(this_arg: &InMemoryChannelKeys) -> u16 {
686         let mut ret = unsafe { &*this_arg.inner }.counterparty_selected_contest_delay();
687         ret
688 }
689
690 /// The contest_delay value specified by us and applied on transactions broadcastable
691 /// by our counterparty, ie the amount of time that they have to wait to recover their funds
692 /// if they broadcast a transaction.
693 /// Will panic if on_accept wasn't called.
694 #[must_use]
695 #[no_mangle]
696 pub extern "C" fn InMemoryChannelKeys_holder_selected_contest_delay(this_arg: &InMemoryChannelKeys) -> u16 {
697         let mut ret = unsafe { &*this_arg.inner }.holder_selected_contest_delay();
698         ret
699 }
700
701 #[no_mangle]
702 pub extern "C" fn InMemoryChannelKeys_as_ChannelKeys(this_arg: *const InMemoryChannelKeys) -> crate::chain::keysinterface::ChannelKeys {
703         crate::chain::keysinterface::ChannelKeys {
704                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
705                 free: None,
706                 get_per_commitment_point: InMemoryChannelKeys_ChannelKeys_get_per_commitment_point,
707                 release_commitment_secret: InMemoryChannelKeys_ChannelKeys_release_commitment_secret,
708
709                 pubkeys: crate::ln::chan_utils::ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true },
710                 set_pubkeys: Some(InMemoryChannelKeys_ChannelKeys_set_pubkeys),
711                 key_derivation_params: InMemoryChannelKeys_ChannelKeys_key_derivation_params,
712                 sign_counterparty_commitment: InMemoryChannelKeys_ChannelKeys_sign_counterparty_commitment,
713                 sign_holder_commitment: InMemoryChannelKeys_ChannelKeys_sign_holder_commitment,
714                 sign_holder_commitment_htlc_transactions: InMemoryChannelKeys_ChannelKeys_sign_holder_commitment_htlc_transactions,
715                 sign_justice_transaction: InMemoryChannelKeys_ChannelKeys_sign_justice_transaction,
716                 sign_counterparty_htlc_transaction: InMemoryChannelKeys_ChannelKeys_sign_counterparty_htlc_transaction,
717                 sign_closing_transaction: InMemoryChannelKeys_ChannelKeys_sign_closing_transaction,
718                 sign_channel_announcement: InMemoryChannelKeys_ChannelKeys_sign_channel_announcement,
719                 on_accept: InMemoryChannelKeys_ChannelKeys_on_accept,
720                 clone: Some(InMemoryChannelKeys_clone_void),
721         }
722 }
723 use lightning::chain::keysinterface::ChannelKeys as ChannelKeysTraitImport;
724 #[must_use]
725 extern "C" fn InMemoryChannelKeys_ChannelKeys_get_per_commitment_point(this_arg: *const c_void, mut idx: u64) -> crate::c_types::PublicKey {
726         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.get_per_commitment_point(idx, &bitcoin::secp256k1::Secp256k1::new());
727         crate::c_types::PublicKey::from_rust(&ret)
728 }
729 #[must_use]
730 extern "C" fn InMemoryChannelKeys_ChannelKeys_release_commitment_secret(this_arg: *const c_void, mut idx: u64) -> crate::c_types::ThirtyTwoBytes {
731         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.release_commitment_secret(idx);
732         crate::c_types::ThirtyTwoBytes { data: ret }
733 }
734 #[must_use]
735 extern "C" fn InMemoryChannelKeys_ChannelKeys_pubkeys(this_arg: *const c_void) -> crate::ln::chan_utils::ChannelPublicKeys {
736         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.pubkeys();
737         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
738 }
739 extern "C" fn InMemoryChannelKeys_ChannelKeys_set_pubkeys(trait_self_arg: &ChannelKeys) {
740         // This is a bit race-y in the general case, but for our specific use-cases today, we're safe
741         // Specifically, we must ensure that the first time we're called it can never be in parallel
742         if trait_self_arg.pubkeys.inner.is_null() {
743                 unsafe { &mut *(trait_self_arg as *const ChannelKeys  as *mut ChannelKeys) }.pubkeys = InMemoryChannelKeys_ChannelKeys_pubkeys(trait_self_arg.this_arg);
744         }
745 }
746 #[must_use]
747 extern "C" fn InMemoryChannelKeys_ChannelKeys_key_derivation_params(this_arg: *const c_void) -> crate::c_types::derived::C2Tuple_u64u64Z {
748         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.key_derivation_params();
749         let (mut orig_ret_0, mut orig_ret_1) = ret; let mut local_ret = (orig_ret_0, orig_ret_1).into();
750         local_ret
751 }
752 #[must_use]
753 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_counterparty_commitment(this_arg: *const c_void, mut feerate_per_kw: u32, mut commitment_tx: crate::c_types::Transaction, pre_keys: &crate::ln::chan_utils::PreCalculatedTxCreationKeys, mut htlcs: crate::c_types::derived::CVec_HTLCOutputInCommitmentZ) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ {
754         let mut local_htlcs = Vec::new(); for mut item in htlcs.as_slice().iter() { local_htlcs.push( { unsafe { &*item.inner } }); };
755         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_counterparty_commitment(feerate_per_kw, &commitment_tx.into_bitcoin(), unsafe { &*pre_keys.inner }, &local_htlcs[..], &bitcoin::secp256k1::Secp256k1::new());
756         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_orig_ret_0_1 = Vec::new(); for item in orig_ret_0_1.drain(..) { local_orig_ret_0_1.push( { crate::c_types::Signature::from_rust(&item) }); }; let mut local_ret_0 = (crate::c_types::Signature::from_rust(&orig_ret_0_0), local_orig_ret_0_1.into()).into(); local_ret_0 }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
757         local_ret
758 }
759 #[must_use]
760 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_holder_commitment(this_arg: *const c_void, holder_commitment_tx: &crate::ln::chan_utils::HolderCommitmentTransaction) -> crate::c_types::derived::CResult_SignatureNoneZ {
761         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_holder_commitment(unsafe { &*holder_commitment_tx.inner }, &bitcoin::secp256k1::Secp256k1::new());
762         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
763         local_ret
764 }
765 #[must_use]
766 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_holder_commitment_htlc_transactions(this_arg: *const c_void, holder_commitment_tx: &crate::ln::chan_utils::HolderCommitmentTransaction) -> crate::c_types::derived::CResult_CVec_SignatureZNoneZ {
767         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_holder_commitment_htlc_transactions(unsafe { &*holder_commitment_tx.inner }, &bitcoin::secp256k1::Secp256k1::new());
768         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*/ }) };
769         local_ret
770 }
771 #[must_use]
772 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_justice_transaction(this_arg: *const c_void, mut justice_tx: crate::c_types::Transaction, mut input: usize, mut amount: u64, per_commitment_key: *const [u8; 32], htlc: &crate::ln::chan_utils::HTLCOutputInCommitment) -> crate::c_types::derived::CResult_SignatureNoneZ {
773         let mut local_htlc = if htlc.inner.is_null() { None } else { Some((* { unsafe { &*htlc.inner } }).clone()) };
774         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_justice_transaction(&justice_tx.into_bitcoin(), input, amount, &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *per_commitment_key}[..]).unwrap(), &local_htlc, &bitcoin::secp256k1::Secp256k1::new());
775         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
776         local_ret
777 }
778 #[must_use]
779 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_counterparty_htlc_transaction(this_arg: *const c_void, mut htlc_tx: crate::c_types::Transaction, mut input: usize, mut amount: u64, mut per_commitment_point: crate::c_types::PublicKey, htlc: &crate::ln::chan_utils::HTLCOutputInCommitment) -> crate::c_types::derived::CResult_SignatureNoneZ {
780         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_counterparty_htlc_transaction(&htlc_tx.into_bitcoin(), input, amount, &per_commitment_point.into_rust(), unsafe { &*htlc.inner }, &bitcoin::secp256k1::Secp256k1::new());
781         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
782         local_ret
783 }
784 #[must_use]
785 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_closing_transaction(this_arg: *const c_void, mut closing_tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_SignatureNoneZ {
786         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_closing_transaction(&closing_tx.into_bitcoin(), &bitcoin::secp256k1::Secp256k1::new());
787         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
788         local_ret
789 }
790 #[must_use]
791 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_channel_announcement(this_arg: *const c_void, msg: &crate::ln::msgs::UnsignedChannelAnnouncement) -> crate::c_types::derived::CResult_SignatureNoneZ {
792         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_channel_announcement(unsafe { &*msg.inner }, &bitcoin::secp256k1::Secp256k1::new());
793         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }) };
794         local_ret
795 }
796 extern "C" fn InMemoryChannelKeys_ChannelKeys_on_accept(this_arg: *mut c_void, channel_pubkeys: &crate::ln::chan_utils::ChannelPublicKeys, mut counterparty_selected_contest_delay: u16, mut holder_selected_contest_delay: u16) {
797         unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.on_accept(unsafe { &*channel_pubkeys.inner }, counterparty_selected_contest_delay, holder_selected_contest_delay)
798 }
799
800 #[no_mangle]
801 pub extern "C" fn InMemoryChannelKeys_write(obj: *const InMemoryChannelKeys) -> crate::c_types::derived::CVec_u8Z {
802         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
803 }
804 #[no_mangle]
805 pub extern "C" fn InMemoryChannelKeys_read(ser: crate::c_types::u8slice) -> InMemoryChannelKeys {
806         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
807                 InMemoryChannelKeys { inner: Box::into_raw(Box::new(res)), is_owned: true }
808         } else {
809                 InMemoryChannelKeys { inner: std::ptr::null_mut(), is_owned: true }
810         }
811 }
812
813 use lightning::chain::keysinterface::KeysManager as nativeKeysManagerImport;
814 type nativeKeysManager = nativeKeysManagerImport;
815
816 /// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
817 /// and derives keys from that.
818 ///
819 /// Your node_id is seed/0'
820 /// ChannelMonitor closes may use seed/1'
821 /// Cooperative closes may use seed/2'
822 /// The two close keys may be needed to claim on-chain funds!
823 #[must_use]
824 #[repr(C)]
825 pub struct KeysManager {
826         /// Nearly everywhere, inner must be non-null, however in places where
827         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
828         pub inner: *mut nativeKeysManager,
829         pub is_owned: bool,
830 }
831
832 impl Drop for KeysManager {
833         fn drop(&mut self) {
834                 if self.is_owned && !self.inner.is_null() {
835                         let _ = unsafe { Box::from_raw(self.inner) };
836                 }
837         }
838 }
839 #[no_mangle]
840 pub extern "C" fn KeysManager_free(this_ptr: KeysManager) { }
841 #[allow(unused)]
842 /// Used only if an object of this type is returned as a trait impl by a method
843 extern "C" fn KeysManager_free_void(this_ptr: *mut c_void) {
844         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeKeysManager); }
845 }
846 #[allow(unused)]
847 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
848 impl KeysManager {
849         pub(crate) fn take_ptr(mut self) -> *mut nativeKeysManager {
850                 assert!(self.is_owned);
851                 let ret = self.inner;
852                 self.inner = std::ptr::null_mut();
853                 ret
854         }
855 }
856 /// Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
857 /// CSRNG is busted) this may panic (but more importantly, you will possibly lose funds).
858 /// starting_time isn't strictly required to actually be a time, but it must absolutely,
859 /// without a doubt, be unique to this instance. ie if you start multiple times with the same
860 /// seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
861 /// simply use the current time (with very high precision).
862 ///
863 /// The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
864 /// obviously, starting_time should be unique every time you reload the library - it is only
865 /// used to generate new ephemeral key data (which will be stored by the individual channel if
866 /// necessary).
867 ///
868 /// Note that the seed is required to recover certain on-chain funds independent of
869 /// ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
870 /// channel, and some on-chain during-closing funds.
871 ///
872 /// Note that until the 0.1 release there is no guarantee of backward compatibility between
873 /// versions. Once the library is more fully supported, the docs will be updated to include a
874 /// detailed description of the guarantee.
875 #[must_use]
876 #[no_mangle]
877 pub extern "C" fn KeysManager_new(seed: *const [u8; 32], mut network: crate::bitcoin::network::Network, mut starting_time_secs: u64, mut starting_time_nanos: u32) -> KeysManager {
878         let mut ret = lightning::chain::keysinterface::KeysManager::new(unsafe { &*seed}, network.into_bitcoin(), starting_time_secs, starting_time_nanos);
879         KeysManager { inner: Box::into_raw(Box::new(ret)), is_owned: true }
880 }
881
882 /// Derive an old set of ChannelKeys for per-channel secrets based on a key derivation
883 /// parameters.
884 /// Key derivation parameters are accessible through a per-channel secrets
885 /// ChannelKeys::key_derivation_params and is provided inside DynamicOuputP2WSH in case of
886 /// onchain output detection for which a corresponding delayed_payment_key must be derived.
887 #[must_use]
888 #[no_mangle]
889 pub extern "C" fn KeysManager_derive_channel_keys(this_arg: &KeysManager, mut channel_value_satoshis: u64, mut params_1: u64, mut params_2: u64) -> crate::chain::keysinterface::InMemoryChannelKeys {
890         let mut ret = unsafe { &*this_arg.inner }.derive_channel_keys(channel_value_satoshis, params_1, params_2);
891         crate::chain::keysinterface::InMemoryChannelKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
892 }
893
894 #[no_mangle]
895 pub extern "C" fn KeysManager_as_KeysInterface(this_arg: *const KeysManager) -> crate::chain::keysinterface::KeysInterface {
896         crate::chain::keysinterface::KeysInterface {
897                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
898                 free: None,
899                 get_node_secret: KeysManager_KeysInterface_get_node_secret,
900                 get_destination_script: KeysManager_KeysInterface_get_destination_script,
901                 get_shutdown_pubkey: KeysManager_KeysInterface_get_shutdown_pubkey,
902                 get_channel_keys: KeysManager_KeysInterface_get_channel_keys,
903                 get_secure_random_bytes: KeysManager_KeysInterface_get_secure_random_bytes,
904         }
905 }
906 use lightning::chain::keysinterface::KeysInterface as KeysInterfaceTraitImport;
907 #[must_use]
908 extern "C" fn KeysManager_KeysInterface_get_node_secret(this_arg: *const c_void) -> crate::c_types::SecretKey {
909         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_node_secret();
910         crate::c_types::SecretKey::from_rust(ret)
911 }
912 #[must_use]
913 extern "C" fn KeysManager_KeysInterface_get_destination_script(this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z {
914         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_destination_script();
915         ret.into_bytes().into()
916 }
917 #[must_use]
918 extern "C" fn KeysManager_KeysInterface_get_shutdown_pubkey(this_arg: *const c_void) -> crate::c_types::PublicKey {
919         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_shutdown_pubkey();
920         crate::c_types::PublicKey::from_rust(&ret)
921 }
922 #[must_use]
923 extern "C" fn KeysManager_KeysInterface_get_channel_keys(this_arg: *const c_void, mut _inbound: bool, mut channel_value_satoshis: u64) -> crate::chain::keysinterface::ChannelKeys {
924         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_channel_keys(_inbound, channel_value_satoshis);
925         let mut rust_obj = InMemoryChannelKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true };
926         let mut ret = InMemoryChannelKeys_as_ChannelKeys(&rust_obj);
927         // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
928         rust_obj.inner = std::ptr::null_mut();
929         ret.free = Some(InMemoryChannelKeys_free_void);
930         ret
931
932 }
933 #[must_use]
934 extern "C" fn KeysManager_KeysInterface_get_secure_random_bytes(this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes {
935         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_secure_random_bytes();
936         crate::c_types::ThirtyTwoBytes { data: ret }
937 }
938