- bindings updates
[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
10 use lightning::chain::keysinterface::DelayedPaymentOutputDescriptor as nativeDelayedPaymentOutputDescriptorImport;
11 type nativeDelayedPaymentOutputDescriptor = nativeDelayedPaymentOutputDescriptorImport;
12
13 /// Information about a spendable output to a P2WSH script. See
14 /// SpendableOutputDescriptor::DelayedPaymentOutput for more details on how to spend this.
15 #[must_use]
16 #[repr(C)]
17 pub struct DelayedPaymentOutputDescriptor {
18         /// Nearly everywhere, inner must be non-null, however in places where
19         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
20         pub inner: *mut nativeDelayedPaymentOutputDescriptor,
21         pub is_owned: bool,
22 }
23
24 impl Drop for DelayedPaymentOutputDescriptor {
25         fn drop(&mut self) {
26                 if self.is_owned && !self.inner.is_null() {
27                         let _ = unsafe { Box::from_raw(self.inner) };
28                 }
29         }
30 }
31 #[no_mangle]
32 pub extern "C" fn DelayedPaymentOutputDescriptor_free(this_ptr: DelayedPaymentOutputDescriptor) { }
33 #[allow(unused)]
34 /// Used only if an object of this type is returned as a trait impl by a method
35 extern "C" fn DelayedPaymentOutputDescriptor_free_void(this_ptr: *mut c_void) {
36         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDelayedPaymentOutputDescriptor); }
37 }
38 #[allow(unused)]
39 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
40 impl DelayedPaymentOutputDescriptor {
41         pub(crate) fn take_inner(mut self) -> *mut nativeDelayedPaymentOutputDescriptor {
42                 assert!(self.is_owned);
43                 let ret = self.inner;
44                 self.inner = std::ptr::null_mut();
45                 ret
46         }
47 }
48 /// The outpoint which is spendable
49 #[no_mangle]
50 pub extern "C" fn DelayedPaymentOutputDescriptor_get_outpoint(this_ptr: &DelayedPaymentOutputDescriptor) -> crate::chain::transaction::OutPoint {
51         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint;
52         crate::chain::transaction::OutPoint { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
53 }
54 /// The outpoint which is spendable
55 #[no_mangle]
56 pub extern "C" fn DelayedPaymentOutputDescriptor_set_outpoint(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: crate::chain::transaction::OutPoint) {
57         unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) };
58 }
59 /// Per commitment point to derive delayed_payment_key by key holder
60 #[no_mangle]
61 pub extern "C" fn DelayedPaymentOutputDescriptor_get_per_commitment_point(this_ptr: &DelayedPaymentOutputDescriptor) -> crate::c_types::PublicKey {
62         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.per_commitment_point;
63         crate::c_types::PublicKey::from_rust(&(*inner_val))
64 }
65 /// Per commitment point to derive delayed_payment_key by key holder
66 #[no_mangle]
67 pub extern "C" fn DelayedPaymentOutputDescriptor_set_per_commitment_point(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: crate::c_types::PublicKey) {
68         unsafe { &mut *this_ptr.inner }.per_commitment_point = val.into_rust();
69 }
70 /// The nSequence value which must be set in the spending input to satisfy the OP_CSV in
71 /// the witness_script.
72 #[no_mangle]
73 pub extern "C" fn DelayedPaymentOutputDescriptor_get_to_self_delay(this_ptr: &DelayedPaymentOutputDescriptor) -> u16 {
74         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.to_self_delay;
75         (*inner_val)
76 }
77 /// The nSequence value which must be set in the spending input to satisfy the OP_CSV in
78 /// the witness_script.
79 #[no_mangle]
80 pub extern "C" fn DelayedPaymentOutputDescriptor_set_to_self_delay(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: u16) {
81         unsafe { &mut *this_ptr.inner }.to_self_delay = val;
82 }
83 /// The output which is referenced by the given outpoint
84 #[no_mangle]
85 pub extern "C" fn DelayedPaymentOutputDescriptor_set_output(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: crate::c_types::TxOut) {
86         unsafe { &mut *this_ptr.inner }.output = val.into_rust();
87 }
88 /// The revocation point specific to the commitment transaction which was broadcast. Used to
89 /// derive the witnessScript for this output.
90 #[no_mangle]
91 pub extern "C" fn DelayedPaymentOutputDescriptor_get_revocation_pubkey(this_ptr: &DelayedPaymentOutputDescriptor) -> crate::c_types::PublicKey {
92         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_pubkey;
93         crate::c_types::PublicKey::from_rust(&(*inner_val))
94 }
95 /// The revocation point specific to the commitment transaction which was broadcast. Used to
96 /// derive the witnessScript for this output.
97 #[no_mangle]
98 pub extern "C" fn DelayedPaymentOutputDescriptor_set_revocation_pubkey(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: crate::c_types::PublicKey) {
99         unsafe { &mut *this_ptr.inner }.revocation_pubkey = val.into_rust();
100 }
101 /// Arbitrary identification information returned by a call to
102 /// `ChannelKeys::channel_keys_id()`. This may be useful in re-deriving keys used in
103 /// the channel to spend the output.
104 #[no_mangle]
105 pub extern "C" fn DelayedPaymentOutputDescriptor_get_channel_keys_id(this_ptr: &DelayedPaymentOutputDescriptor) -> *const [u8; 32] {
106         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_keys_id;
107         &(*inner_val)
108 }
109 /// Arbitrary identification information returned by a call to
110 /// `ChannelKeys::channel_keys_id()`. This may be useful in re-deriving keys used in
111 /// the channel to spend the output.
112 #[no_mangle]
113 pub extern "C" fn DelayedPaymentOutputDescriptor_set_channel_keys_id(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: crate::c_types::ThirtyTwoBytes) {
114         unsafe { &mut *this_ptr.inner }.channel_keys_id = val.data;
115 }
116 /// The value of the channel which this output originated from, possibly indirectly.
117 #[no_mangle]
118 pub extern "C" fn DelayedPaymentOutputDescriptor_get_channel_value_satoshis(this_ptr: &DelayedPaymentOutputDescriptor) -> u64 {
119         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_value_satoshis;
120         (*inner_val)
121 }
122 /// The value of the channel which this output originated from, possibly indirectly.
123 #[no_mangle]
124 pub extern "C" fn DelayedPaymentOutputDescriptor_set_channel_value_satoshis(this_ptr: &mut DelayedPaymentOutputDescriptor, mut val: u64) {
125         unsafe { &mut *this_ptr.inner }.channel_value_satoshis = val;
126 }
127 #[must_use]
128 #[no_mangle]
129 pub extern "C" fn DelayedPaymentOutputDescriptor_new(mut outpoint_arg: crate::chain::transaction::OutPoint, mut per_commitment_point_arg: crate::c_types::PublicKey, mut to_self_delay_arg: u16, mut output_arg: crate::c_types::TxOut, mut revocation_pubkey_arg: crate::c_types::PublicKey, mut channel_keys_id_arg: crate::c_types::ThirtyTwoBytes, mut channel_value_satoshis_arg: u64) -> DelayedPaymentOutputDescriptor {
130         DelayedPaymentOutputDescriptor { inner: Box::into_raw(Box::new(nativeDelayedPaymentOutputDescriptor {
131                 outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) },
132                 per_commitment_point: per_commitment_point_arg.into_rust(),
133                 to_self_delay: to_self_delay_arg,
134                 output: output_arg.into_rust(),
135                 revocation_pubkey: revocation_pubkey_arg.into_rust(),
136                 channel_keys_id: channel_keys_id_arg.data,
137                 channel_value_satoshis: channel_value_satoshis_arg,
138         })), is_owned: true }
139 }
140 impl Clone for DelayedPaymentOutputDescriptor {
141         fn clone(&self) -> Self {
142                 Self {
143                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
144                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
145                         is_owned: true,
146                 }
147         }
148 }
149 #[allow(unused)]
150 /// Used only if an object of this type is returned as a trait impl by a method
151 pub(crate) extern "C" fn DelayedPaymentOutputDescriptor_clone_void(this_ptr: *const c_void) -> *mut c_void {
152         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeDelayedPaymentOutputDescriptor)).clone() })) as *mut c_void
153 }
154 #[no_mangle]
155 pub extern "C" fn DelayedPaymentOutputDescriptor_clone(orig: &DelayedPaymentOutputDescriptor) -> DelayedPaymentOutputDescriptor {
156         orig.clone()
157 }
158
159 use lightning::chain::keysinterface::StaticPaymentOutputDescriptor as nativeStaticPaymentOutputDescriptorImport;
160 type nativeStaticPaymentOutputDescriptor = nativeStaticPaymentOutputDescriptorImport;
161
162 /// Information about a spendable output to our \"payment key\". See
163 /// SpendableOutputDescriptor::StaticPaymentOutput for more details on how to spend this.
164 #[must_use]
165 #[repr(C)]
166 pub struct StaticPaymentOutputDescriptor {
167         /// Nearly everywhere, inner must be non-null, however in places where
168         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
169         pub inner: *mut nativeStaticPaymentOutputDescriptor,
170         pub is_owned: bool,
171 }
172
173 impl Drop for StaticPaymentOutputDescriptor {
174         fn drop(&mut self) {
175                 if self.is_owned && !self.inner.is_null() {
176                         let _ = unsafe { Box::from_raw(self.inner) };
177                 }
178         }
179 }
180 #[no_mangle]
181 pub extern "C" fn StaticPaymentOutputDescriptor_free(this_ptr: StaticPaymentOutputDescriptor) { }
182 #[allow(unused)]
183 /// Used only if an object of this type is returned as a trait impl by a method
184 extern "C" fn StaticPaymentOutputDescriptor_free_void(this_ptr: *mut c_void) {
185         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeStaticPaymentOutputDescriptor); }
186 }
187 #[allow(unused)]
188 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
189 impl StaticPaymentOutputDescriptor {
190         pub(crate) fn take_inner(mut self) -> *mut nativeStaticPaymentOutputDescriptor {
191                 assert!(self.is_owned);
192                 let ret = self.inner;
193                 self.inner = std::ptr::null_mut();
194                 ret
195         }
196 }
197 /// The outpoint which is spendable
198 #[no_mangle]
199 pub extern "C" fn StaticPaymentOutputDescriptor_get_outpoint(this_ptr: &StaticPaymentOutputDescriptor) -> crate::chain::transaction::OutPoint {
200         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.outpoint;
201         crate::chain::transaction::OutPoint { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
202 }
203 /// The outpoint which is spendable
204 #[no_mangle]
205 pub extern "C" fn StaticPaymentOutputDescriptor_set_outpoint(this_ptr: &mut StaticPaymentOutputDescriptor, mut val: crate::chain::transaction::OutPoint) {
206         unsafe { &mut *this_ptr.inner }.outpoint = *unsafe { Box::from_raw(val.take_inner()) };
207 }
208 /// The output which is referenced by the given outpoint
209 #[no_mangle]
210 pub extern "C" fn StaticPaymentOutputDescriptor_set_output(this_ptr: &mut StaticPaymentOutputDescriptor, mut val: crate::c_types::TxOut) {
211         unsafe { &mut *this_ptr.inner }.output = val.into_rust();
212 }
213 /// Arbitrary identification information returned by a call to
214 /// `ChannelKeys::channel_keys_id()`. This may be useful in re-deriving keys used in
215 /// the channel to spend the output.
216 #[no_mangle]
217 pub extern "C" fn StaticPaymentOutputDescriptor_get_channel_keys_id(this_ptr: &StaticPaymentOutputDescriptor) -> *const [u8; 32] {
218         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_keys_id;
219         &(*inner_val)
220 }
221 /// Arbitrary identification information returned by a call to
222 /// `ChannelKeys::channel_keys_id()`. This may be useful in re-deriving keys used in
223 /// the channel to spend the output.
224 #[no_mangle]
225 pub extern "C" fn StaticPaymentOutputDescriptor_set_channel_keys_id(this_ptr: &mut StaticPaymentOutputDescriptor, mut val: crate::c_types::ThirtyTwoBytes) {
226         unsafe { &mut *this_ptr.inner }.channel_keys_id = val.data;
227 }
228 /// The value of the channel which this transactions spends.
229 #[no_mangle]
230 pub extern "C" fn StaticPaymentOutputDescriptor_get_channel_value_satoshis(this_ptr: &StaticPaymentOutputDescriptor) -> u64 {
231         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_value_satoshis;
232         (*inner_val)
233 }
234 /// The value of the channel which this transactions spends.
235 #[no_mangle]
236 pub extern "C" fn StaticPaymentOutputDescriptor_set_channel_value_satoshis(this_ptr: &mut StaticPaymentOutputDescriptor, mut val: u64) {
237         unsafe { &mut *this_ptr.inner }.channel_value_satoshis = val;
238 }
239 #[must_use]
240 #[no_mangle]
241 pub extern "C" fn StaticPaymentOutputDescriptor_new(mut outpoint_arg: crate::chain::transaction::OutPoint, mut output_arg: crate::c_types::TxOut, mut channel_keys_id_arg: crate::c_types::ThirtyTwoBytes, mut channel_value_satoshis_arg: u64) -> StaticPaymentOutputDescriptor {
242         StaticPaymentOutputDescriptor { inner: Box::into_raw(Box::new(nativeStaticPaymentOutputDescriptor {
243                 outpoint: *unsafe { Box::from_raw(outpoint_arg.take_inner()) },
244                 output: output_arg.into_rust(),
245                 channel_keys_id: channel_keys_id_arg.data,
246                 channel_value_satoshis: channel_value_satoshis_arg,
247         })), is_owned: true }
248 }
249 impl Clone for StaticPaymentOutputDescriptor {
250         fn clone(&self) -> Self {
251                 Self {
252                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
253                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
254                         is_owned: true,
255                 }
256         }
257 }
258 #[allow(unused)]
259 /// Used only if an object of this type is returned as a trait impl by a method
260 pub(crate) extern "C" fn StaticPaymentOutputDescriptor_clone_void(this_ptr: *const c_void) -> *mut c_void {
261         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeStaticPaymentOutputDescriptor)).clone() })) as *mut c_void
262 }
263 #[no_mangle]
264 pub extern "C" fn StaticPaymentOutputDescriptor_clone(orig: &StaticPaymentOutputDescriptor) -> StaticPaymentOutputDescriptor {
265         orig.clone()
266 }
267
268 use lightning::chain::keysinterface::SpendableOutputDescriptor as nativeSpendableOutputDescriptorImport;
269 type nativeSpendableOutputDescriptor = nativeSpendableOutputDescriptorImport;
270
271 /// When on-chain outputs are created by rust-lightning (which our counterparty is not able to
272 /// claim at any point in the future) an event is generated which you must track and be able to
273 /// spend on-chain. The information needed to do this is provided in this enum, including the
274 /// outpoint describing which txid and output index is available, the full output which exists at
275 /// that txid/index, and any keys or other information required to sign.
276 #[must_use]
277 #[repr(C)]
278 pub struct SpendableOutputDescriptor {
279         /// Nearly everywhere, inner must be non-null, however in places where
280         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
281         pub inner: *mut nativeSpendableOutputDescriptor,
282         pub is_owned: bool,
283 }
284
285 impl Drop for SpendableOutputDescriptor {
286         fn drop(&mut self) {
287                 if self.is_owned && !self.inner.is_null() {
288                         let _ = unsafe { Box::from_raw(self.inner) };
289                 }
290         }
291 }
292 #[no_mangle]
293 pub extern "C" fn SpendableOutputDescriptor_free(this_ptr: SpendableOutputDescriptor) { }
294 #[allow(unused)]
295 /// Used only if an object of this type is returned as a trait impl by a method
296 extern "C" fn SpendableOutputDescriptor_free_void(this_ptr: *mut c_void) {
297         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeSpendableOutputDescriptor); }
298 }
299 #[allow(unused)]
300 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
301 impl SpendableOutputDescriptor {
302         pub(crate) fn take_inner(mut self) -> *mut nativeSpendableOutputDescriptor {
303                 assert!(self.is_owned);
304                 let ret = self.inner;
305                 self.inner = std::ptr::null_mut();
306                 ret
307         }
308 }
309 impl Clone for SpendableOutputDescriptor {
310         fn clone(&self) -> Self {
311                 Self {
312                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
313                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
314                         is_owned: true,
315                 }
316         }
317 }
318 #[allow(unused)]
319 /// Used only if an object of this type is returned as a trait impl by a method
320 pub(crate) extern "C" fn SpendableOutputDescriptor_clone_void(this_ptr: *const c_void) -> *mut c_void {
321         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeSpendableOutputDescriptor)).clone() })) as *mut c_void
322 }
323 #[no_mangle]
324 pub extern "C" fn SpendableOutputDescriptor_clone(orig: &SpendableOutputDescriptor) -> SpendableOutputDescriptor {
325         orig.clone()
326 }
327 #[no_mangle]
328 pub extern "C" fn SpendableOutputDescriptor_write(obj: &SpendableOutputDescriptor) -> crate::c_types::derived::CVec_u8Z {
329         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
330 }
331 #[no_mangle]
332 pub(crate) extern "C" fn SpendableOutputDescriptor_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
333         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeSpendableOutputDescriptor) })
334 }
335 #[no_mangle]
336 pub extern "C" fn SpendableOutputDescriptor_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_SpendableOutputDescriptorDecodeErrorZ {
337         let res = crate::c_types::deserialize_obj(ser);
338         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::chain::keysinterface::SpendableOutputDescriptor { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
339         local_res
340 }
341 /// Set of lightning keys needed to operate a channel as described in BOLT 3.
342 ///
343 /// Signing services could be implemented on a hardware wallet. In this case,
344 /// the current ChannelKeys would be a front-end on top of a communication
345 /// channel connected to your secure device and lightning key material wouldn't
346 /// reside on a hot server. Nevertheless, a this deployment would still need
347 /// to trust the ChannelManager to avoid loss of funds as this latest component
348 /// could ask to sign commitment transaction with HTLCs paying to attacker pubkeys.
349 ///
350 /// A more secure iteration would be to use hashlock (or payment points) to pair
351 /// invoice/incoming HTLCs with outgoing HTLCs to implement a no-trust-ChannelManager
352 /// at the price of more state and computation on the hardware wallet side. In the future,
353 /// we are looking forward to design such interface.
354 ///
355 /// In any case, ChannelMonitor or fallback watchtowers are always going to be trusted
356 /// to act, as liveness and breach reply correctness are always going to be hard requirements
357 /// of LN security model, orthogonal of key management issues.
358 #[repr(C)]
359 pub struct ChannelKeys {
360         pub this_arg: *mut c_void,
361         /// Gets the per-commitment point for a specific commitment number
362         ///
363         /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
364         #[must_use]
365         pub get_per_commitment_point: extern "C" fn (this_arg: *const c_void, idx: u64) -> crate::c_types::PublicKey,
366         /// Gets the commitment secret for a specific commitment number as part of the revocation process
367         ///
368         /// An external signer implementation should error here if the commitment was already signed
369         /// and should refuse to sign it in the future.
370         ///
371         /// May be called more than once for the same index.
372         ///
373         /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
374         #[must_use]
375         pub release_commitment_secret: extern "C" fn (this_arg: *const c_void, idx: u64) -> crate::c_types::ThirtyTwoBytes,
376         /// Gets the holder's channel public keys and basepoints
377         pub pubkeys: crate::ln::chan_utils::ChannelPublicKeys,
378         /// Fill in the pubkeys field as a reference to it will be given to Rust after this returns
379         /// Note that this takes a pointer to this object, not the this_ptr like other methods do
380         /// This function pointer may be NULL if pubkeys is filled in when this object is created and never needs updating.
381         pub set_pubkeys: Option<extern "C" fn(&ChannelKeys)>,
382         /// Gets an arbitrary identifier describing the set of keys which are provided back to you in
383         /// some SpendableOutputDescriptor types. This should be sufficient to identify this
384         /// ChannelKeys object uniquely and lookup or re-derive its keys.
385         #[must_use]
386         pub channel_keys_id: extern "C" fn (this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes,
387         /// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
388         ///
389         /// Note that if signing fails or is rejected, the channel will be force-closed.
390         #[must_use]
391         pub sign_counterparty_commitment: extern "C" fn (this_arg: *const c_void, commitment_tx: &crate::ln::chan_utils::CommitmentTransaction) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ,
392         /// Create a signatures for a holder's commitment transaction and its claiming HTLC transactions.
393         /// This will only ever be called with a non-revoked commitment_tx.  This will be called with the
394         /// latest commitment_tx when we initiate a force-close.
395         /// This will be called with the previous latest, just to get claiming HTLC signatures, if we are
396         /// reacting to a ChannelMonitor replica that decided to broadcast before it had been updated to
397         /// the latest.
398         /// This may be called multiple times for the same transaction.
399         ///
400         /// An external signer implementation should check that the commitment has not been revoked.
401         ///
402         /// May return Err if key derivation fails.  Callers, such as ChannelMonitor, will panic in such a case.
403         #[must_use]
404         pub sign_holder_commitment_and_htlcs: extern "C" fn (this_arg: *const c_void, commitment_tx: &crate::ln::chan_utils::HolderCommitmentTransaction) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ,
405         /// Create a signature for the given input in a transaction spending an HTLC or commitment
406         /// transaction output when our counterparty broadcasts an old state.
407         ///
408         /// A justice transaction may claim multiples outputs at the same time if timelocks are
409         /// similar, but only a signature for the input at index `input` should be signed for here.
410         /// It may be called multiples time for same output(s) if a fee-bump is needed with regards
411         /// to an upcoming timelock expiration.
412         ///
413         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
414         ///
415         /// per_commitment_key is revocation secret which was provided by our counterparty when they
416         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
417         /// not allow the spending of any funds by itself (you need our holder revocation_secret to do
418         /// so).
419         ///
420         /// htlc holds HTLC elements (hash, timelock) if the output being spent is a HTLC output, thus
421         /// changing the format of the witness script (which is committed to in the BIP 143
422         /// signatures).
423         #[must_use]
424         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,
425         /// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
426         /// transaction, either offered or received.
427         ///
428         /// Such a transaction may claim multiples offered outputs at same time if we know the
429         /// preimage for each when we create it, but only the input at index `input` should be
430         /// signed for here. It may be called multiple times for same output(s) if a fee-bump is
431         /// needed with regards to an upcoming timelock expiration.
432         ///
433         /// Witness_script is either a offered or received script as defined in BOLT3 for HTLC
434         /// outputs.
435         ///
436         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
437         ///
438         /// Per_commitment_point is the dynamic point corresponding to the channel state
439         /// detected onchain. It has been generated by our counterparty and is used to derive
440         /// channel state keys, which are then included in the witness script and committed to in the
441         /// BIP 143 signature.
442         #[must_use]
443         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,
444         /// Create a signature for a (proposed) closing transaction.
445         ///
446         /// Note that, due to rounding, there may be one \"missing\" satoshi, and either party may have
447         /// chosen to forgo their output as dust.
448         #[must_use]
449         pub sign_closing_transaction: extern "C" fn (this_arg: *const c_void, closing_tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_SignatureNoneZ,
450         /// Signs a channel announcement message with our funding key, proving it comes from one
451         /// of the channel participants.
452         ///
453         /// Note that if this fails or is rejected, the channel will not be publicly announced and
454         /// our counterparty may (though likely will not) close the channel on us for violating the
455         /// protocol.
456         #[must_use]
457         pub sign_channel_announcement: extern "C" fn (this_arg: *const c_void, msg: &crate::ln::msgs::UnsignedChannelAnnouncement) -> crate::c_types::derived::CResult_SignatureNoneZ,
458         /// Set the counterparty static channel data, including basepoints,
459         /// counterparty_selected/holder_selected_contest_delay and funding outpoint.
460         /// This is done as soon as the funding outpoint is known.  Since these are static channel data,
461         /// they MUST NOT be allowed to change to different values once set.
462         ///
463         /// channel_parameters.is_populated() MUST be true.
464         ///
465         /// We bind holder_selected_contest_delay late here for API convenience.
466         ///
467         /// Will be called before any signatures are applied.
468         pub ready_channel: extern "C" fn (this_arg: *mut c_void, channel_parameters: &crate::ln::chan_utils::ChannelTransactionParameters),
469         pub clone: Option<extern "C" fn (this_arg: *const c_void) -> *mut c_void>,
470         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
471         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
472 }
473 unsafe impl Send for ChannelKeys {}
474 #[no_mangle]
475 pub extern "C" fn ChannelKeys_clone(orig: &ChannelKeys) -> ChannelKeys {
476         ChannelKeys {
477                 this_arg: if let Some(f) = orig.clone { (f)(orig.this_arg) } else { orig.this_arg },
478                 get_per_commitment_point: orig.get_per_commitment_point.clone(),
479                 release_commitment_secret: orig.release_commitment_secret.clone(),
480                 pubkeys: orig.pubkeys.clone(),
481                 set_pubkeys: orig.set_pubkeys.clone(),
482                 channel_keys_id: orig.channel_keys_id.clone(),
483                 sign_counterparty_commitment: orig.sign_counterparty_commitment.clone(),
484                 sign_holder_commitment_and_htlcs: orig.sign_holder_commitment_and_htlcs.clone(),
485                 sign_justice_transaction: orig.sign_justice_transaction.clone(),
486                 sign_counterparty_htlc_transaction: orig.sign_counterparty_htlc_transaction.clone(),
487                 sign_closing_transaction: orig.sign_closing_transaction.clone(),
488                 sign_channel_announcement: orig.sign_channel_announcement.clone(),
489                 ready_channel: orig.ready_channel.clone(),
490                 clone: orig.clone.clone(),
491                 write: orig.write.clone(),
492                 free: orig.free.clone(),
493         }
494 }
495 impl Clone for ChannelKeys {
496         fn clone(&self) -> Self {
497                 ChannelKeys_clone(self)
498         }
499 }
500 impl lightning::util::ser::Writeable for ChannelKeys {
501         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
502                 let vec = (self.write)(self.this_arg);
503                 w.write_all(vec.as_slice())
504         }
505 }
506
507 use lightning::chain::keysinterface::ChannelKeys as rustChannelKeys;
508 impl rustChannelKeys for ChannelKeys {
509         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 {
510                 let mut ret = (self.get_per_commitment_point)(self.this_arg, idx);
511                 ret.into_rust()
512         }
513         fn release_commitment_secret(&self, idx: u64) -> [u8; 32] {
514                 let mut ret = (self.release_commitment_secret)(self.this_arg, idx);
515                 ret.data
516         }
517         fn pubkeys(&self) -> &lightning::ln::chan_utils::ChannelPublicKeys {
518                 if let Some(f) = self.set_pubkeys {
519                         (f)(self);
520                 }
521                 unsafe { &*self.pubkeys.inner }
522         }
523         fn channel_keys_id(&self) -> [u8; 32] {
524                 let mut ret = (self.channel_keys_id)(self.this_arg);
525                 ret.data
526         }
527         fn sign_counterparty_commitment<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, commitment_tx: &lightning::ln::chan_utils::CommitmentTransaction, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<(bitcoin::secp256k1::Signature, Vec<bitcoin::secp256k1::Signature>), ()> {
528                 let mut ret = (self.sign_counterparty_commitment)(self.this_arg, &crate::ln::chan_utils::CommitmentTransaction { inner: unsafe { (commitment_tx as *const _) as *mut _ }, is_owned: false });
529                 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(<*mut _>::take_ptr(&mut ret.contents.result)) }).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(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
530                 local_ret
531         }
532         fn sign_holder_commitment_and_htlcs<T:bitcoin::secp256k1::Signing + bitcoin::secp256k1::Verification>(&self, commitment_tx: &lightning::ln::chan_utils::HolderCommitmentTransaction, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<(bitcoin::secp256k1::Signature, Vec<bitcoin::secp256k1::Signature>), ()> {
533                 let mut ret = (self.sign_holder_commitment_and_htlcs)(self.this_arg, &crate::ln::chan_utils::HolderCommitmentTransaction { inner: unsafe { (commitment_tx as *const _) as *mut _ }, is_owned: false });
534                 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(<*mut _>::take_ptr(&mut ret.contents.result)) }).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(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
535                 local_ret
536         }
537         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, ()> {
538                 let mut local_justice_tx = ::bitcoin::consensus::encode::serialize(justice_tx);
539                 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 };
540                 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);
541                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
542                 local_ret
543         }
544         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, ()> {
545                 let mut local_htlc_tx = ::bitcoin::consensus::encode::serialize(htlc_tx);
546                 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 });
547                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
548                 local_ret
549         }
550         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, ()> {
551                 let mut local_closing_tx = ::bitcoin::consensus::encode::serialize(closing_tx);
552                 let mut ret = (self.sign_closing_transaction)(self.this_arg, crate::c_types::Transaction::from_vec(local_closing_tx));
553                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
554                 local_ret
555         }
556         fn sign_channel_announcement<T:bitcoin::secp256k1::Signing>(&self, msg: &lightning::ln::msgs::UnsignedChannelAnnouncement, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
557                 let mut ret = (self.sign_channel_announcement)(self.this_arg, &crate::ln::msgs::UnsignedChannelAnnouncement { inner: unsafe { (msg as *const _) as *mut _ }, is_owned: false });
558                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).into_rust() }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })};
559                 local_ret
560         }
561         fn ready_channel(&mut self, channel_parameters: &lightning::ln::chan_utils::ChannelTransactionParameters) {
562                 (self.ready_channel)(self.this_arg, &crate::ln::chan_utils::ChannelTransactionParameters { inner: unsafe { (channel_parameters as *const _) as *mut _ }, is_owned: false })
563         }
564 }
565
566 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
567 // directly as a Deref trait in higher-level structs:
568 impl std::ops::Deref for ChannelKeys {
569         type Target = Self;
570         fn deref(&self) -> &Self {
571                 self
572         }
573 }
574 /// Calls the free function if one is set
575 #[no_mangle]
576 pub extern "C" fn ChannelKeys_free(this_ptr: ChannelKeys) { }
577 impl Drop for ChannelKeys {
578         fn drop(&mut self) {
579                 if let Some(f) = self.free {
580                         f(self.this_arg);
581                 }
582         }
583 }
584 /// A trait to describe an object which can get user secrets and key material.
585 #[repr(C)]
586 pub struct KeysInterface {
587         pub this_arg: *mut c_void,
588         /// Get node secret key (aka node_id or network_key).
589         ///
590         /// This method must return the same value each time it is called.
591         #[must_use]
592         pub get_node_secret: extern "C" fn (this_arg: *const c_void) -> crate::c_types::SecretKey,
593         /// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
594         ///
595         /// This method should return a different value each time it is called, to avoid linking
596         /// on-chain funds across channels as controlled to the same user.
597         #[must_use]
598         pub get_destination_script: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
599         /// Get a public key which we will send funds to (in the form of a P2WPKH output) when closing
600         /// a channel.
601         ///
602         /// This method should return a different value each time it is called, to avoid linking
603         /// on-chain funds across channels as controlled to the same user.
604         #[must_use]
605         pub get_shutdown_pubkey: extern "C" fn (this_arg: *const c_void) -> crate::c_types::PublicKey,
606         /// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
607         /// restarted with some stale data!
608         ///
609         /// This method must return a different value each time it is called.
610         #[must_use]
611         pub get_channel_keys: extern "C" fn (this_arg: *const c_void, inbound: bool, channel_value_satoshis: u64) -> crate::chain::keysinterface::ChannelKeys,
612         /// Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
613         /// onion packets and for temporary channel IDs. There is no requirement that these be
614         /// persisted anywhere, though they must be unique across restarts.
615         ///
616         /// This method must return a different value each time it is called.
617         #[must_use]
618         pub get_secure_random_bytes: extern "C" fn (this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes,
619         /// Reads a `ChanKeySigner` for this `KeysInterface` from the given input stream.
620         /// This is only called during deserialization of other objects which contain
621         /// `ChannelKeys`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
622         /// The bytes are exactly those which `<Self::ChanKeySigner as Writeable>::write()` writes, and
623         /// contain no versioning scheme. You may wish to include your own version prefix and ensure
624         /// you've read all of the provided bytes to ensure no corruption occurred.
625         #[must_use]
626         pub read_chan_signer: extern "C" fn (this_arg: *const c_void, reader: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelKeysDecodeErrorZ,
627         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
628 }
629 unsafe impl Send for KeysInterface {}
630 unsafe impl Sync for KeysInterface {}
631
632 use lightning::chain::keysinterface::KeysInterface as rustKeysInterface;
633 impl rustKeysInterface for KeysInterface {
634         type ChanKeySigner = crate::chain::keysinterface::ChannelKeys;
635         fn get_node_secret(&self) -> bitcoin::secp256k1::key::SecretKey {
636                 let mut ret = (self.get_node_secret)(self.this_arg);
637                 ret.into_rust()
638         }
639         fn get_destination_script(&self) -> bitcoin::blockdata::script::Script {
640                 let mut ret = (self.get_destination_script)(self.this_arg);
641                 ::bitcoin::blockdata::script::Script::from(ret.into_rust())
642         }
643         fn get_shutdown_pubkey(&self) -> bitcoin::secp256k1::key::PublicKey {
644                 let mut ret = (self.get_shutdown_pubkey)(self.this_arg);
645                 ret.into_rust()
646         }
647         fn get_channel_keys(&self, inbound: bool, channel_value_satoshis: u64) -> crate::chain::keysinterface::ChannelKeys {
648                 let mut ret = (self.get_channel_keys)(self.this_arg, inbound, channel_value_satoshis);
649                 ret
650         }
651         fn get_secure_random_bytes(&self) -> [u8; 32] {
652                 let mut ret = (self.get_secure_random_bytes)(self.this_arg);
653                 ret.data
654         }
655         fn read_chan_signer(&self, reader: &[u8]) -> Result<crate::chain::keysinterface::ChannelKeys, lightning::ln::msgs::DecodeError> {
656                 let mut local_reader = crate::c_types::u8slice::from_slice(reader);
657                 let mut ret = (self.read_chan_signer)(self.this_arg, local_reader);
658                 let mut local_ret = match ret.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }) }), false => Err( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })};
659                 local_ret
660         }
661 }
662
663 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
664 // directly as a Deref trait in higher-level structs:
665 impl std::ops::Deref for KeysInterface {
666         type Target = Self;
667         fn deref(&self) -> &Self {
668                 self
669         }
670 }
671 /// Calls the free function if one is set
672 #[no_mangle]
673 pub extern "C" fn KeysInterface_free(this_ptr: KeysInterface) { }
674 impl Drop for KeysInterface {
675         fn drop(&mut self) {
676                 if let Some(f) = self.free {
677                         f(self.this_arg);
678                 }
679         }
680 }
681
682 use lightning::chain::keysinterface::InMemoryChannelKeys as nativeInMemoryChannelKeysImport;
683 type nativeInMemoryChannelKeys = nativeInMemoryChannelKeysImport;
684
685 /// A simple implementation of ChannelKeys that just keeps the private keys in memory.
686 ///
687 /// This implementation performs no policy checks and is insufficient by itself as
688 /// a secure external signer.
689 #[must_use]
690 #[repr(C)]
691 pub struct InMemoryChannelKeys {
692         /// Nearly everywhere, inner must be non-null, however in places where
693         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
694         pub inner: *mut nativeInMemoryChannelKeys,
695         pub is_owned: bool,
696 }
697
698 impl Drop for InMemoryChannelKeys {
699         fn drop(&mut self) {
700                 if self.is_owned && !self.inner.is_null() {
701                         let _ = unsafe { Box::from_raw(self.inner) };
702                 }
703         }
704 }
705 #[no_mangle]
706 pub extern "C" fn InMemoryChannelKeys_free(this_ptr: InMemoryChannelKeys) { }
707 #[allow(unused)]
708 /// Used only if an object of this type is returned as a trait impl by a method
709 extern "C" fn InMemoryChannelKeys_free_void(this_ptr: *mut c_void) {
710         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInMemoryChannelKeys); }
711 }
712 #[allow(unused)]
713 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
714 impl InMemoryChannelKeys {
715         pub(crate) fn take_inner(mut self) -> *mut nativeInMemoryChannelKeys {
716                 assert!(self.is_owned);
717                 let ret = self.inner;
718                 self.inner = std::ptr::null_mut();
719                 ret
720         }
721 }
722 /// Private key of anchor tx
723 #[no_mangle]
724 pub extern "C" fn InMemoryChannelKeys_get_funding_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
725         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_key;
726         (*inner_val).as_ref()
727 }
728 /// Private key of anchor tx
729 #[no_mangle]
730 pub extern "C" fn InMemoryChannelKeys_set_funding_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
731         unsafe { &mut *this_ptr.inner }.funding_key = val.into_rust();
732 }
733 /// Holder secret key for blinded revocation pubkey
734 #[no_mangle]
735 pub extern "C" fn InMemoryChannelKeys_get_revocation_base_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
736         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_base_key;
737         (*inner_val).as_ref()
738 }
739 /// Holder secret key for blinded revocation pubkey
740 #[no_mangle]
741 pub extern "C" fn InMemoryChannelKeys_set_revocation_base_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
742         unsafe { &mut *this_ptr.inner }.revocation_base_key = val.into_rust();
743 }
744 /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions
745 #[no_mangle]
746 pub extern "C" fn InMemoryChannelKeys_get_payment_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
747         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_key;
748         (*inner_val).as_ref()
749 }
750 /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions
751 #[no_mangle]
752 pub extern "C" fn InMemoryChannelKeys_set_payment_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
753         unsafe { &mut *this_ptr.inner }.payment_key = val.into_rust();
754 }
755 /// Holder secret key used in HTLC tx
756 #[no_mangle]
757 pub extern "C" fn InMemoryChannelKeys_get_delayed_payment_base_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
758         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_base_key;
759         (*inner_val).as_ref()
760 }
761 /// Holder secret key used in HTLC tx
762 #[no_mangle]
763 pub extern "C" fn InMemoryChannelKeys_set_delayed_payment_base_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
764         unsafe { &mut *this_ptr.inner }.delayed_payment_base_key = val.into_rust();
765 }
766 /// Holder htlc secret key used in commitment tx htlc outputs
767 #[no_mangle]
768 pub extern "C" fn InMemoryChannelKeys_get_htlc_base_key(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
769         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_base_key;
770         (*inner_val).as_ref()
771 }
772 /// Holder htlc secret key used in commitment tx htlc outputs
773 #[no_mangle]
774 pub extern "C" fn InMemoryChannelKeys_set_htlc_base_key(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::SecretKey) {
775         unsafe { &mut *this_ptr.inner }.htlc_base_key = val.into_rust();
776 }
777 /// Commitment seed
778 #[no_mangle]
779 pub extern "C" fn InMemoryChannelKeys_get_commitment_seed(this_ptr: &InMemoryChannelKeys) -> *const [u8; 32] {
780         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.commitment_seed;
781         &(*inner_val)
782 }
783 /// Commitment seed
784 #[no_mangle]
785 pub extern "C" fn InMemoryChannelKeys_set_commitment_seed(this_ptr: &mut InMemoryChannelKeys, mut val: crate::c_types::ThirtyTwoBytes) {
786         unsafe { &mut *this_ptr.inner }.commitment_seed = val.data;
787 }
788 impl Clone for InMemoryChannelKeys {
789         fn clone(&self) -> Self {
790                 Self {
791                         inner: if self.inner.is_null() { std::ptr::null_mut() } else {
792                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
793                         is_owned: true,
794                 }
795         }
796 }
797 #[allow(unused)]
798 /// Used only if an object of this type is returned as a trait impl by a method
799 pub(crate) extern "C" fn InMemoryChannelKeys_clone_void(this_ptr: *const c_void) -> *mut c_void {
800         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInMemoryChannelKeys)).clone() })) as *mut c_void
801 }
802 #[no_mangle]
803 pub extern "C" fn InMemoryChannelKeys_clone(orig: &InMemoryChannelKeys) -> InMemoryChannelKeys {
804         orig.clone()
805 }
806 /// Create a new InMemoryChannelKeys
807 #[must_use]
808 #[no_mangle]
809 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 channel_keys_id: crate::c_types::ThirtyTwoBytes) -> crate::chain::keysinterface::InMemoryChannelKeys {
810         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, channel_keys_id.data);
811         crate::chain::keysinterface::InMemoryChannelKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
812 }
813
814 /// Counterparty pubkeys.
815 /// Will panic if ready_channel wasn't called.
816 #[must_use]
817 #[no_mangle]
818 pub extern "C" fn InMemoryChannelKeys_counterparty_pubkeys(this_arg: &InMemoryChannelKeys) -> crate::ln::chan_utils::ChannelPublicKeys {
819         let mut ret = unsafe { &*this_arg.inner }.counterparty_pubkeys();
820         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
821 }
822
823 /// The contest_delay value specified by our counterparty and applied on holder-broadcastable
824 /// transactions, ie the amount of time that we have to wait to recover our funds if we
825 /// broadcast a transaction.
826 /// Will panic if ready_channel wasn't called.
827 #[must_use]
828 #[no_mangle]
829 pub extern "C" fn InMemoryChannelKeys_counterparty_selected_contest_delay(this_arg: &InMemoryChannelKeys) -> u16 {
830         let mut ret = unsafe { &*this_arg.inner }.counterparty_selected_contest_delay();
831         ret
832 }
833
834 /// The contest_delay value specified by us and applied on transactions broadcastable
835 /// by our counterparty, ie the amount of time that they have to wait to recover their funds
836 /// if they broadcast a transaction.
837 /// Will panic if ready_channel wasn't called.
838 #[must_use]
839 #[no_mangle]
840 pub extern "C" fn InMemoryChannelKeys_holder_selected_contest_delay(this_arg: &InMemoryChannelKeys) -> u16 {
841         let mut ret = unsafe { &*this_arg.inner }.holder_selected_contest_delay();
842         ret
843 }
844
845 /// Whether the holder is the initiator
846 /// Will panic if ready_channel wasn't called.
847 #[must_use]
848 #[no_mangle]
849 pub extern "C" fn InMemoryChannelKeys_is_outbound(this_arg: &InMemoryChannelKeys) -> bool {
850         let mut ret = unsafe { &*this_arg.inner }.is_outbound();
851         ret
852 }
853
854 /// Funding outpoint
855 /// Will panic if ready_channel wasn't called.
856 #[must_use]
857 #[no_mangle]
858 pub extern "C" fn InMemoryChannelKeys_funding_outpoint(this_arg: &InMemoryChannelKeys) -> crate::chain::transaction::OutPoint {
859         let mut ret = unsafe { &*this_arg.inner }.funding_outpoint();
860         crate::chain::transaction::OutPoint { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
861 }
862
863 /// Obtain a ChannelTransactionParameters for this channel, to be used when verifying or
864 /// building transactions.
865 ///
866 /// Will panic if ready_channel wasn't called.
867 #[must_use]
868 #[no_mangle]
869 pub extern "C" fn InMemoryChannelKeys_get_channel_parameters(this_arg: &InMemoryChannelKeys) -> crate::ln::chan_utils::ChannelTransactionParameters {
870         let mut ret = unsafe { &*this_arg.inner }.get_channel_parameters();
871         crate::ln::chan_utils::ChannelTransactionParameters { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
872 }
873
874 /// Sign the single input of spend_tx at index `input_idx` which spends the output
875 /// described by descriptor, returning the witness stack for the input.
876 ///
877 /// Returns an Err if the input at input_idx does not exist, has a non-empty script_sig,
878 /// or is not spending the outpoint described by `descriptor.outpoint`.
879 #[must_use]
880 #[no_mangle]
881 pub extern "C" fn InMemoryChannelKeys_sign_counterparty_payment_input(this_arg: &InMemoryChannelKeys, mut spend_tx: crate::c_types::Transaction, mut input_idx: usize, descriptor: &crate::chain::keysinterface::StaticPaymentOutputDescriptor) -> crate::c_types::derived::CResult_CVec_CVec_u8ZZNoneZ {
882         let mut ret = unsafe { &*this_arg.inner }.sign_counterparty_payment_input(&spend_tx.into_bitcoin(), input_idx, unsafe { &*descriptor.inner }, &bitcoin::secp256k1::Secp256k1::new());
883         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let mut local_ret_0_0 = Vec::new(); for mut item in item.drain(..) { local_ret_0_0.push( { item }); }; local_ret_0_0.into() }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
884         local_ret
885 }
886
887 /// Sign the single input of spend_tx at index `input_idx` which spends the output
888 /// described by descriptor, returning the witness stack for the input.
889 ///
890 /// Returns an Err if the input at input_idx does not exist, has a non-empty script_sig,
891 /// is not spending the outpoint described by `descriptor.outpoint`, or does not have a
892 /// sequence set to `descriptor.to_self_delay`.
893 #[must_use]
894 #[no_mangle]
895 pub extern "C" fn InMemoryChannelKeys_sign_dynamic_p2wsh_input(this_arg: &InMemoryChannelKeys, mut spend_tx: crate::c_types::Transaction, mut input_idx: usize, descriptor: &crate::chain::keysinterface::DelayedPaymentOutputDescriptor) -> crate::c_types::derived::CResult_CVec_CVec_u8ZZNoneZ {
896         let mut ret = unsafe { &*this_arg.inner }.sign_dynamic_p2wsh_input(&spend_tx.into_bitcoin(), input_idx, unsafe { &*descriptor.inner }, &bitcoin::secp256k1::Secp256k1::new());
897         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let mut local_ret_0_0 = Vec::new(); for mut item in item.drain(..) { local_ret_0_0.push( { item }); }; local_ret_0_0.into() }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
898         local_ret
899 }
900
901 impl From<nativeInMemoryChannelKeys> for crate::chain::keysinterface::ChannelKeys {
902         fn from(obj: nativeInMemoryChannelKeys) -> Self {
903                 let mut rust_obj = InMemoryChannelKeys { inner: Box::into_raw(Box::new(obj)), is_owned: true };
904                 let mut ret = InMemoryChannelKeys_as_ChannelKeys(&rust_obj);
905                 // 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
906                 rust_obj.inner = std::ptr::null_mut();
907                 ret.free = Some(InMemoryChannelKeys_free_void);
908                 ret
909         }
910 }
911 #[no_mangle]
912 pub extern "C" fn InMemoryChannelKeys_as_ChannelKeys(this_arg: &InMemoryChannelKeys) -> crate::chain::keysinterface::ChannelKeys {
913         crate::chain::keysinterface::ChannelKeys {
914                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
915                 free: None,
916                 get_per_commitment_point: InMemoryChannelKeys_ChannelKeys_get_per_commitment_point,
917                 release_commitment_secret: InMemoryChannelKeys_ChannelKeys_release_commitment_secret,
918
919                 pubkeys: crate::ln::chan_utils::ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true },
920                 set_pubkeys: Some(InMemoryChannelKeys_ChannelKeys_set_pubkeys),
921                 channel_keys_id: InMemoryChannelKeys_ChannelKeys_channel_keys_id,
922                 sign_counterparty_commitment: InMemoryChannelKeys_ChannelKeys_sign_counterparty_commitment,
923                 sign_holder_commitment_and_htlcs: InMemoryChannelKeys_ChannelKeys_sign_holder_commitment_and_htlcs,
924                 sign_justice_transaction: InMemoryChannelKeys_ChannelKeys_sign_justice_transaction,
925                 sign_counterparty_htlc_transaction: InMemoryChannelKeys_ChannelKeys_sign_counterparty_htlc_transaction,
926                 sign_closing_transaction: InMemoryChannelKeys_ChannelKeys_sign_closing_transaction,
927                 sign_channel_announcement: InMemoryChannelKeys_ChannelKeys_sign_channel_announcement,
928                 ready_channel: InMemoryChannelKeys_ChannelKeys_ready_channel,
929                 clone: Some(InMemoryChannelKeys_clone_void),
930                 write: InMemoryChannelKeys_write_void,
931         }
932 }
933 use lightning::chain::keysinterface::ChannelKeys as ChannelKeysTraitImport;
934 #[must_use]
935 extern "C" fn InMemoryChannelKeys_ChannelKeys_get_per_commitment_point(this_arg: *const c_void, mut idx: u64) -> crate::c_types::PublicKey {
936         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.get_per_commitment_point(idx, &bitcoin::secp256k1::Secp256k1::new());
937         crate::c_types::PublicKey::from_rust(&ret)
938 }
939 #[must_use]
940 extern "C" fn InMemoryChannelKeys_ChannelKeys_release_commitment_secret(this_arg: *const c_void, mut idx: u64) -> crate::c_types::ThirtyTwoBytes {
941         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.release_commitment_secret(idx);
942         crate::c_types::ThirtyTwoBytes { data: ret }
943 }
944 #[must_use]
945 extern "C" fn InMemoryChannelKeys_ChannelKeys_pubkeys(this_arg: *const c_void) -> crate::ln::chan_utils::ChannelPublicKeys {
946         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.pubkeys();
947         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
948 }
949 extern "C" fn InMemoryChannelKeys_ChannelKeys_set_pubkeys(trait_self_arg: &ChannelKeys) {
950         // This is a bit race-y in the general case, but for our specific use-cases today, we're safe
951         // Specifically, we must ensure that the first time we're called it can never be in parallel
952         if trait_self_arg.pubkeys.inner.is_null() {
953                 unsafe { &mut *(trait_self_arg as *const ChannelKeys  as *mut ChannelKeys) }.pubkeys = InMemoryChannelKeys_ChannelKeys_pubkeys(trait_self_arg.this_arg);
954         }
955 }
956 #[must_use]
957 extern "C" fn InMemoryChannelKeys_ChannelKeys_channel_keys_id(this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes {
958         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.channel_keys_id();
959         crate::c_types::ThirtyTwoBytes { data: ret }
960 }
961 #[must_use]
962 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_counterparty_commitment(this_arg: *const c_void, commitment_tx: &crate::ln::chan_utils::CommitmentTransaction) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ {
963         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_counterparty_commitment(unsafe { &*commitment_tx.inner }, &bitcoin::secp256k1::Secp256k1::new());
964         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 mut 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 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
965         local_ret
966 }
967 #[must_use]
968 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_holder_commitment_and_htlcs(this_arg: *const c_void, commitment_tx: &crate::ln::chan_utils::HolderCommitmentTransaction) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ {
969         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_holder_commitment_and_htlcs(unsafe { &*commitment_tx.inner }, &bitcoin::secp256k1::Secp256k1::new());
970         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 mut 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 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
971         local_ret
972 }
973 #[must_use]
974 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 {
975         let mut local_htlc = if htlc.inner.is_null() { None } else { Some((* { unsafe { &*htlc.inner } }).clone()) };
976         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());
977         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
978         local_ret
979 }
980 #[must_use]
981 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 {
982         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());
983         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
984         local_ret
985 }
986 #[must_use]
987 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 {
988         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_closing_transaction(&closing_tx.into_bitcoin(), &bitcoin::secp256k1::Secp256k1::new());
989         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
990         local_ret
991 }
992 #[must_use]
993 extern "C" fn InMemoryChannelKeys_ChannelKeys_sign_channel_announcement(this_arg: *const c_void, msg: &crate::ln::msgs::UnsignedChannelAnnouncement) -> crate::c_types::derived::CResult_SignatureNoneZ {
994         let mut ret = unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.sign_channel_announcement(unsafe { &*msg.inner }, &bitcoin::secp256k1::Secp256k1::new());
995         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::Signature::from_rust(&o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
996         local_ret
997 }
998 extern "C" fn InMemoryChannelKeys_ChannelKeys_ready_channel(this_arg: *mut c_void, channel_parameters: &crate::ln::chan_utils::ChannelTransactionParameters) {
999         unsafe { &mut *(this_arg as *mut nativeInMemoryChannelKeys) }.ready_channel(unsafe { &*channel_parameters.inner })
1000 }
1001
1002 #[no_mangle]
1003 pub extern "C" fn InMemoryChannelKeys_write(obj: &InMemoryChannelKeys) -> crate::c_types::derived::CVec_u8Z {
1004         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1005 }
1006 #[no_mangle]
1007 pub(crate) extern "C" fn InMemoryChannelKeys_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1008         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInMemoryChannelKeys) })
1009 }
1010 #[no_mangle]
1011 pub extern "C" fn InMemoryChannelKeys_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InMemoryChannelKeysDecodeErrorZ {
1012         let res = crate::c_types::deserialize_obj(ser);
1013         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::chain::keysinterface::InMemoryChannelKeys { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
1014         local_res
1015 }
1016
1017 use lightning::chain::keysinterface::KeysManager as nativeKeysManagerImport;
1018 type nativeKeysManager = nativeKeysManagerImport;
1019
1020 /// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
1021 /// and derives keys from that.
1022 ///
1023 /// Your node_id is seed/0'
1024 /// ChannelMonitor closes may use seed/1'
1025 /// Cooperative closes may use seed/2'
1026 /// The two close keys may be needed to claim on-chain funds!
1027 #[must_use]
1028 #[repr(C)]
1029 pub struct KeysManager {
1030         /// Nearly everywhere, inner must be non-null, however in places where
1031         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1032         pub inner: *mut nativeKeysManager,
1033         pub is_owned: bool,
1034 }
1035
1036 impl Drop for KeysManager {
1037         fn drop(&mut self) {
1038                 if self.is_owned && !self.inner.is_null() {
1039                         let _ = unsafe { Box::from_raw(self.inner) };
1040                 }
1041         }
1042 }
1043 #[no_mangle]
1044 pub extern "C" fn KeysManager_free(this_ptr: KeysManager) { }
1045 #[allow(unused)]
1046 /// Used only if an object of this type is returned as a trait impl by a method
1047 extern "C" fn KeysManager_free_void(this_ptr: *mut c_void) {
1048         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeKeysManager); }
1049 }
1050 #[allow(unused)]
1051 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1052 impl KeysManager {
1053         pub(crate) fn take_inner(mut self) -> *mut nativeKeysManager {
1054                 assert!(self.is_owned);
1055                 let ret = self.inner;
1056                 self.inner = std::ptr::null_mut();
1057                 ret
1058         }
1059 }
1060 /// Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
1061 /// CSRNG is busted) this may panic (but more importantly, you will possibly lose funds).
1062 /// starting_time isn't strictly required to actually be a time, but it must absolutely,
1063 /// without a doubt, be unique to this instance. ie if you start multiple times with the same
1064 /// seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
1065 /// simply use the current time (with very high precision).
1066 ///
1067 /// The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
1068 /// obviously, starting_time should be unique every time you reload the library - it is only
1069 /// used to generate new ephemeral key data (which will be stored by the individual channel if
1070 /// necessary).
1071 ///
1072 /// Note that the seed is required to recover certain on-chain funds independent of
1073 /// ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
1074 /// channel, and some on-chain during-closing funds.
1075 ///
1076 /// Note that until the 0.1 release there is no guarantee of backward compatibility between
1077 /// versions. Once the library is more fully supported, the docs will be updated to include a
1078 /// detailed description of the guarantee.
1079 #[must_use]
1080 #[no_mangle]
1081 pub extern "C" fn KeysManager_new(seed: *const [u8; 32], mut starting_time_secs: u64, mut starting_time_nanos: u32) -> KeysManager {
1082         let mut ret = lightning::chain::keysinterface::KeysManager::new(unsafe { &*seed}, starting_time_secs, starting_time_nanos);
1083         KeysManager { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1084 }
1085
1086 /// Derive an old set of ChannelKeys for per-channel secrets based on a key derivation
1087 /// parameters.
1088 /// Key derivation parameters are accessible through a per-channel secrets
1089 /// ChannelKeys::channel_keys_id and is provided inside DynamicOuputP2WSH in case of
1090 /// onchain output detection for which a corresponding delayed_payment_key must be derived.
1091 #[must_use]
1092 #[no_mangle]
1093 pub extern "C" fn KeysManager_derive_channel_keys(this_arg: &KeysManager, mut channel_value_satoshis: u64, params: *const [u8; 32]) -> crate::chain::keysinterface::InMemoryChannelKeys {
1094         let mut ret = unsafe { &*this_arg.inner }.derive_channel_keys(channel_value_satoshis, unsafe { &*params});
1095         crate::chain::keysinterface::InMemoryChannelKeys { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1096 }
1097
1098 /// Creates a Transaction which spends the given descriptors to the given outputs, plus an
1099 /// output to the given change destination (if sufficient change value remains). The
1100 /// transaction will have a feerate, at least, of the given value.
1101 ///
1102 /// Returns `Err(())` if the output value is greater than the input value minus required fee or
1103 /// if a descriptor was duplicated.
1104 ///
1105 /// We do not enforce that outputs meet the dust limit or that any output scripts are standard.
1106 ///
1107 /// May panic if the `SpendableOutputDescriptor`s were not generated by Channels which used
1108 /// this KeysManager or one of the `InMemoryChannelKeys` created by this KeysManager.
1109 #[must_use]
1110 #[no_mangle]
1111 pub extern "C" fn KeysManager_spend_spendable_outputs(this_arg: &KeysManager, mut descriptors: crate::c_types::derived::CVec_SpendableOutputDescriptorZ, mut outputs: crate::c_types::derived::CVec_TxOutZ, mut change_destination_script: crate::c_types::derived::CVec_u8Z, mut feerate_sat_per_1000_weight: u32) -> crate::c_types::derived::CResult_TransactionNoneZ {
1112         let mut local_descriptors = Vec::new(); for mut item in descriptors.as_slice().iter() { local_descriptors.push( { unsafe { &*item.inner } }); };
1113         let mut local_outputs = Vec::new(); for mut item in outputs.into_rust().drain(..) { local_outputs.push( { item.into_rust() }); };
1114         let mut ret = unsafe { &*this_arg.inner }.spend_spendable_outputs(&local_descriptors[..], local_outputs, ::bitcoin::blockdata::script::Script::from(change_destination_script.into_rust()), feerate_sat_per_1000_weight, &bitcoin::secp256k1::Secp256k1::new());
1115         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = ::bitcoin::consensus::encode::serialize(&o); crate::c_types::Transaction::from_vec(local_ret_0) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { 0u8 /*e*/ }).into() };
1116         local_ret
1117 }
1118
1119 impl From<nativeKeysManager> for crate::chain::keysinterface::KeysInterface {
1120         fn from(obj: nativeKeysManager) -> Self {
1121                 let mut rust_obj = KeysManager { inner: Box::into_raw(Box::new(obj)), is_owned: true };
1122                 let mut ret = KeysManager_as_KeysInterface(&rust_obj);
1123                 // 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
1124                 rust_obj.inner = std::ptr::null_mut();
1125                 ret.free = Some(KeysManager_free_void);
1126                 ret
1127         }
1128 }
1129 #[no_mangle]
1130 pub extern "C" fn KeysManager_as_KeysInterface(this_arg: &KeysManager) -> crate::chain::keysinterface::KeysInterface {
1131         crate::chain::keysinterface::KeysInterface {
1132                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
1133                 free: None,
1134                 get_node_secret: KeysManager_KeysInterface_get_node_secret,
1135                 get_destination_script: KeysManager_KeysInterface_get_destination_script,
1136                 get_shutdown_pubkey: KeysManager_KeysInterface_get_shutdown_pubkey,
1137                 get_channel_keys: KeysManager_KeysInterface_get_channel_keys,
1138                 get_secure_random_bytes: KeysManager_KeysInterface_get_secure_random_bytes,
1139                 read_chan_signer: KeysManager_KeysInterface_read_chan_signer,
1140         }
1141 }
1142 use lightning::chain::keysinterface::KeysInterface as KeysInterfaceTraitImport;
1143 #[must_use]
1144 extern "C" fn KeysManager_KeysInterface_get_node_secret(this_arg: *const c_void) -> crate::c_types::SecretKey {
1145         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_node_secret();
1146         crate::c_types::SecretKey::from_rust(ret)
1147 }
1148 #[must_use]
1149 extern "C" fn KeysManager_KeysInterface_get_destination_script(this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1150         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_destination_script();
1151         ret.into_bytes().into()
1152 }
1153 #[must_use]
1154 extern "C" fn KeysManager_KeysInterface_get_shutdown_pubkey(this_arg: *const c_void) -> crate::c_types::PublicKey {
1155         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_shutdown_pubkey();
1156         crate::c_types::PublicKey::from_rust(&ret)
1157 }
1158 #[must_use]
1159 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 {
1160         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_channel_keys(_inbound, channel_value_satoshis);
1161         ret.into()
1162 }
1163 #[must_use]
1164 extern "C" fn KeysManager_KeysInterface_get_secure_random_bytes(this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes {
1165         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.get_secure_random_bytes();
1166         crate::c_types::ThirtyTwoBytes { data: ret }
1167 }
1168 #[must_use]
1169 extern "C" fn KeysManager_KeysInterface_read_chan_signer(this_arg: *const c_void, mut reader: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelKeysDecodeErrorZ {
1170         let mut ret = unsafe { &mut *(this_arg as *mut nativeKeysManager) }.read_chan_signer(reader.to_slice());
1171         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
1172         local_ret
1173 }
1174