abd779693d19fc365f56246acb084f6639c3fbcd
[ldk-c-bindings] / 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 && !<*mut nativeDelayedPaymentOutputDescriptor>::is_null(self.inner) {
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 /// `Sign::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 /// `Sign::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 <*mut nativeDelayedPaymentOutputDescriptor>::is_null(self.inner) { 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 && !<*mut nativeStaticPaymentOutputDescriptor>::is_null(self.inner) {
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 /// `Sign::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 /// `Sign::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 <*mut nativeStaticPaymentOutputDescriptor>::is_null(self.inner) { 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 /// When on-chain outputs are created by rust-lightning (which our counterparty is not able to
268 /// claim at any point in the future) an event is generated which you must track and be able to
269 /// spend on-chain. The information needed to do this is provided in this enum, including the
270 /// outpoint describing which txid and output index is available, the full output which exists at
271 /// that txid/index, and any keys or other information required to sign.
272 #[must_use]
273 #[derive(Clone)]
274 #[repr(C)]
275 pub enum SpendableOutputDescriptor {
276         /// An output to a script which was provided via KeysInterface directly, either from
277         /// `get_destination_script()` or `get_shutdown_pubkey()`, thus you should already know how to
278         /// spend it. No secret keys are provided as rust-lightning was never given any key.
279         /// These may include outputs from a transaction punishing our counterparty or claiming an HTLC
280         /// on-chain using the payment preimage or after it has timed out.
281         StaticOutput {
282                 outpoint: crate::chain::transaction::OutPoint,
283                 output: crate::c_types::TxOut,
284         },
285         /// An output to a P2WSH script which can be spent with a single signature after a CSV delay.
286         ///
287         /// The witness in the spending input should be:
288         /// <BIP 143 signature> <empty vector> (MINIMALIF standard rule) <provided witnessScript>
289         ///
290         /// Note that the nSequence field in the spending input must be set to to_self_delay
291         /// (which means the transaction is not broadcastable until at least to_self_delay
292         /// blocks after the outpoint confirms).
293         ///
294         /// These are generally the result of a \"revocable\" output to us, spendable only by us unless
295         /// it is an output from an old state which we broadcast (which should never happen).
296         ///
297         /// To derive the delayed_payment key which is used to sign for this input, you must pass the
298         /// holder delayed_payment_base_key (ie the private key which corresponds to the pubkey in
299         /// Sign::pubkeys().delayed_payment_basepoint) and the provided per_commitment_point to
300         /// chan_utils::derive_private_key. The public key can be generated without the secret key
301         /// using chan_utils::derive_public_key and only the delayed_payment_basepoint which appears in
302         /// Sign::pubkeys().
303         ///
304         /// To derive the revocation_pubkey provided here (which is used in the witness
305         /// script generation), you must pass the counterparty revocation_basepoint (which appears in the
306         /// call to Sign::ready_channel) and the provided per_commitment point
307         /// to chan_utils::derive_public_revocation_key.
308         ///
309         /// The witness script which is hashed and included in the output script_pubkey may be
310         /// regenerated by passing the revocation_pubkey (derived as above), our delayed_payment pubkey
311         /// (derived as above), and the to_self_delay contained here to
312         /// chan_utils::get_revokeable_redeemscript.
313         DelayedPaymentOutput(crate::chain::keysinterface::DelayedPaymentOutputDescriptor),
314         /// An output to a P2WPKH, spendable exclusively by our payment key (ie the private key which
315         /// corresponds to the public key in Sign::pubkeys().payment_point).
316         /// The witness in the spending input, is, thus, simply:
317         /// <BIP 143 signature> <payment key>
318         ///
319         /// These are generally the result of our counterparty having broadcast the current state,
320         /// allowing us to claim the non-HTLC-encumbered outputs immediately.
321         StaticPaymentOutput(crate::chain::keysinterface::StaticPaymentOutputDescriptor),
322 }
323 use lightning::chain::keysinterface::SpendableOutputDescriptor as nativeSpendableOutputDescriptor;
324 impl SpendableOutputDescriptor {
325         #[allow(unused)]
326         pub(crate) fn to_native(&self) -> nativeSpendableOutputDescriptor {
327                 match self {
328                         SpendableOutputDescriptor::StaticOutput {ref outpoint, ref output, } => {
329                                 let mut outpoint_nonref = (*outpoint).clone();
330                                 let mut output_nonref = (*output).clone();
331                                 nativeSpendableOutputDescriptor::StaticOutput {
332                                         outpoint: *unsafe { Box::from_raw(outpoint_nonref.take_inner()) },
333                                         output: output_nonref.into_rust(),
334                                 }
335                         },
336                         SpendableOutputDescriptor::DelayedPaymentOutput (ref a, ) => {
337                                 let mut a_nonref = (*a).clone();
338                                 nativeSpendableOutputDescriptor::DelayedPaymentOutput (
339                                         *unsafe { Box::from_raw(a_nonref.take_inner()) },
340                                 )
341                         },
342                         SpendableOutputDescriptor::StaticPaymentOutput (ref a, ) => {
343                                 let mut a_nonref = (*a).clone();
344                                 nativeSpendableOutputDescriptor::StaticPaymentOutput (
345                                         *unsafe { Box::from_raw(a_nonref.take_inner()) },
346                                 )
347                         },
348                 }
349         }
350         #[allow(unused)]
351         pub(crate) fn into_native(self) -> nativeSpendableOutputDescriptor {
352                 match self {
353                         SpendableOutputDescriptor::StaticOutput {mut outpoint, mut output, } => {
354                                 nativeSpendableOutputDescriptor::StaticOutput {
355                                         outpoint: *unsafe { Box::from_raw(outpoint.take_inner()) },
356                                         output: output.into_rust(),
357                                 }
358                         },
359                         SpendableOutputDescriptor::DelayedPaymentOutput (mut a, ) => {
360                                 nativeSpendableOutputDescriptor::DelayedPaymentOutput (
361                                         *unsafe { Box::from_raw(a.take_inner()) },
362                                 )
363                         },
364                         SpendableOutputDescriptor::StaticPaymentOutput (mut a, ) => {
365                                 nativeSpendableOutputDescriptor::StaticPaymentOutput (
366                                         *unsafe { Box::from_raw(a.take_inner()) },
367                                 )
368                         },
369                 }
370         }
371         #[allow(unused)]
372         pub(crate) fn from_native(native: &nativeSpendableOutputDescriptor) -> Self {
373                 match native {
374                         nativeSpendableOutputDescriptor::StaticOutput {ref outpoint, ref output, } => {
375                                 let mut outpoint_nonref = (*outpoint).clone();
376                                 let mut output_nonref = (*output).clone();
377                                 SpendableOutputDescriptor::StaticOutput {
378                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint_nonref)), is_owned: true },
379                                         output: crate::c_types::TxOut::from_rust(output_nonref),
380                                 }
381                         },
382                         nativeSpendableOutputDescriptor::DelayedPaymentOutput (ref a, ) => {
383                                 let mut a_nonref = (*a).clone();
384                                 SpendableOutputDescriptor::DelayedPaymentOutput (
385                                         crate::chain::keysinterface::DelayedPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a_nonref)), is_owned: true },
386                                 )
387                         },
388                         nativeSpendableOutputDescriptor::StaticPaymentOutput (ref a, ) => {
389                                 let mut a_nonref = (*a).clone();
390                                 SpendableOutputDescriptor::StaticPaymentOutput (
391                                         crate::chain::keysinterface::StaticPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a_nonref)), is_owned: true },
392                                 )
393                         },
394                 }
395         }
396         #[allow(unused)]
397         pub(crate) fn native_into(native: nativeSpendableOutputDescriptor) -> Self {
398                 match native {
399                         nativeSpendableOutputDescriptor::StaticOutput {mut outpoint, mut output, } => {
400                                 SpendableOutputDescriptor::StaticOutput {
401                                         outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint)), is_owned: true },
402                                         output: crate::c_types::TxOut::from_rust(output),
403                                 }
404                         },
405                         nativeSpendableOutputDescriptor::DelayedPaymentOutput (mut a, ) => {
406                                 SpendableOutputDescriptor::DelayedPaymentOutput (
407                                         crate::chain::keysinterface::DelayedPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a)), is_owned: true },
408                                 )
409                         },
410                         nativeSpendableOutputDescriptor::StaticPaymentOutput (mut a, ) => {
411                                 SpendableOutputDescriptor::StaticPaymentOutput (
412                                         crate::chain::keysinterface::StaticPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a)), is_owned: true },
413                                 )
414                         },
415                 }
416         }
417 }
418 #[no_mangle]
419 pub extern "C" fn SpendableOutputDescriptor_free(this_ptr: SpendableOutputDescriptor) { }
420 #[no_mangle]
421 pub extern "C" fn SpendableOutputDescriptor_clone(orig: &SpendableOutputDescriptor) -> SpendableOutputDescriptor {
422         orig.clone()
423 }
424 #[no_mangle]
425 pub extern "C" fn SpendableOutputDescriptor_write(obj: &SpendableOutputDescriptor) -> crate::c_types::derived::CVec_u8Z {
426         crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
427 }
428 #[no_mangle]
429 pub extern "C" fn SpendableOutputDescriptor_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_SpendableOutputDescriptorDecodeErrorZ {
430         let res = crate::c_types::deserialize_obj(ser);
431         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::chain::keysinterface::SpendableOutputDescriptor::native_into(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
432         local_res
433 }
434 /// A trait to sign lightning channel transactions as described in BOLT 3.
435 ///
436 /// Signing services could be implemented on a hardware wallet. In this case,
437 /// the current Sign would be a front-end on top of a communication
438 /// channel connected to your secure device and lightning key material wouldn't
439 /// reside on a hot server. Nevertheless, a this deployment would still need
440 /// to trust the ChannelManager to avoid loss of funds as this latest component
441 /// could ask to sign commitment transaction with HTLCs paying to attacker pubkeys.
442 ///
443 /// A more secure iteration would be to use hashlock (or payment points) to pair
444 /// invoice/incoming HTLCs with outgoing HTLCs to implement a no-trust-ChannelManager
445 /// at the price of more state and computation on the hardware wallet side. In the future,
446 /// we are looking forward to design such interface.
447 ///
448 /// In any case, ChannelMonitor or fallback watchtowers are always going to be trusted
449 /// to act, as liveness and breach reply correctness are always going to be hard requirements
450 /// of LN security model, orthogonal of key management issues.
451 #[repr(C)]
452 pub struct Sign {
453         pub this_arg: *mut c_void,
454         /// Gets the per-commitment point for a specific commitment number
455         ///
456         /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
457         #[must_use]
458         pub get_per_commitment_point: extern "C" fn (this_arg: *const c_void, idx: u64) -> crate::c_types::PublicKey,
459         /// Gets the commitment secret for a specific commitment number as part of the revocation process
460         ///
461         /// An external signer implementation should error here if the commitment was already signed
462         /// and should refuse to sign it in the future.
463         ///
464         /// May be called more than once for the same index.
465         ///
466         /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
467         #[must_use]
468         pub release_commitment_secret: extern "C" fn (this_arg: *const c_void, idx: u64) -> crate::c_types::ThirtyTwoBytes,
469         /// Gets the holder's channel public keys and basepoints
470         pub pubkeys: crate::ln::chan_utils::ChannelPublicKeys,
471         /// Fill in the pubkeys field as a reference to it will be given to Rust after this returns
472         /// Note that this takes a pointer to this object, not the this_ptr like other methods do
473         /// This function pointer may be NULL if pubkeys is filled in when this object is created and never needs updating.
474         pub set_pubkeys: Option<extern "C" fn(&Sign)>,
475         /// Gets an arbitrary identifier describing the set of keys which are provided back to you in
476         /// some SpendableOutputDescriptor types. This should be sufficient to identify this
477         /// Sign object uniquely and lookup or re-derive its keys.
478         #[must_use]
479         pub channel_keys_id: extern "C" fn (this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes,
480         /// Create a signature for a counterparty's commitment transaction and associated HTLC transactions.
481         ///
482         /// Note that if signing fails or is rejected, the channel will be force-closed.
483         #[must_use]
484         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,
485         /// Create a signatures for a holder's commitment transaction and its claiming HTLC transactions.
486         /// This will only ever be called with a non-revoked commitment_tx.  This will be called with the
487         /// latest commitment_tx when we initiate a force-close.
488         /// This will be called with the previous latest, just to get claiming HTLC signatures, if we are
489         /// reacting to a ChannelMonitor replica that decided to broadcast before it had been updated to
490         /// the latest.
491         /// This may be called multiple times for the same transaction.
492         ///
493         /// An external signer implementation should check that the commitment has not been revoked.
494         ///
495         /// May return Err if key derivation fails.  Callers, such as ChannelMonitor, will panic in such a case.
496         #[must_use]
497         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,
498         /// Create a signature for the given input in a transaction spending an HTLC or commitment
499         /// transaction output when our counterparty broadcasts an old state.
500         ///
501         /// A justice transaction may claim multiples outputs at the same time if timelocks are
502         /// similar, but only a signature for the input at index `input` should be signed for here.
503         /// It may be called multiples time for same output(s) if a fee-bump is needed with regards
504         /// to an upcoming timelock expiration.
505         ///
506         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
507         ///
508         /// per_commitment_key is revocation secret which was provided by our counterparty when they
509         /// revoked the state which they eventually broadcast. It's not a _holder_ secret key and does
510         /// not allow the spending of any funds by itself (you need our holder revocation_secret to do
511         /// so).
512         ///
513         /// htlc holds HTLC elements (hash, timelock) if the output being spent is a HTLC output, thus
514         /// changing the format of the witness script (which is committed to in the BIP 143
515         /// signatures).
516         #[must_use]
517         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,
518         /// Create a signature for a claiming transaction for a HTLC output on a counterparty's commitment
519         /// transaction, either offered or received.
520         ///
521         /// Such a transaction may claim multiples offered outputs at same time if we know the
522         /// preimage for each when we create it, but only the input at index `input` should be
523         /// signed for here. It may be called multiple times for same output(s) if a fee-bump is
524         /// needed with regards to an upcoming timelock expiration.
525         ///
526         /// Witness_script is either a offered or received script as defined in BOLT3 for HTLC
527         /// outputs.
528         ///
529         /// Amount is value of the output spent by this input, committed to in the BIP 143 signature.
530         ///
531         /// Per_commitment_point is the dynamic point corresponding to the channel state
532         /// detected onchain. It has been generated by our counterparty and is used to derive
533         /// channel state keys, which are then included in the witness script and committed to in the
534         /// BIP 143 signature.
535         #[must_use]
536         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,
537         /// Create a signature for a (proposed) closing transaction.
538         ///
539         /// Note that, due to rounding, there may be one \"missing\" satoshi, and either party may have
540         /// chosen to forgo their output as dust.
541         #[must_use]
542         pub sign_closing_transaction: extern "C" fn (this_arg: *const c_void, closing_tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_SignatureNoneZ,
543         /// Signs a channel announcement message with our funding key, proving it comes from one
544         /// of the channel participants.
545         ///
546         /// Note that if this fails or is rejected, the channel will not be publicly announced and
547         /// our counterparty may (though likely will not) close the channel on us for violating the
548         /// protocol.
549         #[must_use]
550         pub sign_channel_announcement: extern "C" fn (this_arg: *const c_void, msg: &crate::ln::msgs::UnsignedChannelAnnouncement) -> crate::c_types::derived::CResult_SignatureNoneZ,
551         /// Set the counterparty static channel data, including basepoints,
552         /// counterparty_selected/holder_selected_contest_delay and funding outpoint.
553         /// This is done as soon as the funding outpoint is known.  Since these are static channel data,
554         /// they MUST NOT be allowed to change to different values once set.
555         ///
556         /// channel_parameters.is_populated() MUST be true.
557         ///
558         /// We bind holder_selected_contest_delay late here for API convenience.
559         ///
560         /// Will be called before any signatures are applied.
561         pub ready_channel: extern "C" fn (this_arg: *mut c_void, channel_parameters: &crate::ln::chan_utils::ChannelTransactionParameters),
562         pub clone: Option<extern "C" fn (this_arg: *const c_void) -> *mut c_void>,
563         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
564         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
565 }
566 unsafe impl Send for Sign {}
567 #[no_mangle]
568 pub extern "C" fn Sign_clone(orig: &Sign) -> Sign {
569         Sign {
570                 this_arg: if let Some(f) = orig.clone { (f)(orig.this_arg) } else { orig.this_arg },
571                 get_per_commitment_point: orig.get_per_commitment_point.clone(),
572                 release_commitment_secret: orig.release_commitment_secret.clone(),
573                 pubkeys: orig.pubkeys.clone(),
574                 set_pubkeys: orig.set_pubkeys.clone(),
575                 channel_keys_id: orig.channel_keys_id.clone(),
576                 sign_counterparty_commitment: orig.sign_counterparty_commitment.clone(),
577                 sign_holder_commitment_and_htlcs: orig.sign_holder_commitment_and_htlcs.clone(),
578                 sign_justice_transaction: orig.sign_justice_transaction.clone(),
579                 sign_counterparty_htlc_transaction: orig.sign_counterparty_htlc_transaction.clone(),
580                 sign_closing_transaction: orig.sign_closing_transaction.clone(),
581                 sign_channel_announcement: orig.sign_channel_announcement.clone(),
582                 ready_channel: orig.ready_channel.clone(),
583                 clone: orig.clone.clone(),
584                 write: orig.write.clone(),
585                 free: orig.free.clone(),
586         }
587 }
588 impl Clone for Sign {
589         fn clone(&self) -> Self {
590                 Sign_clone(self)
591         }
592 }
593 impl lightning::util::ser::Writeable for Sign {
594         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
595                 let vec = (self.write)(self.this_arg);
596                 w.write_all(vec.as_slice())
597         }
598 }
599
600 use lightning::chain::keysinterface::Sign as rustSign;
601 impl rustSign for Sign {
602         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 {
603                 let mut ret = (self.get_per_commitment_point)(self.this_arg, idx);
604                 ret.into_rust()
605         }
606         fn release_commitment_secret(&self, idx: u64) -> [u8; 32] {
607                 let mut ret = (self.release_commitment_secret)(self.this_arg, idx);
608                 ret.data
609         }
610         fn pubkeys(&self) -> &lightning::ln::chan_utils::ChannelPublicKeys {
611                 if let Some(f) = self.set_pubkeys {
612                         (f)(self);
613                 }
614                 unsafe { &*self.pubkeys.inner }
615         }
616         fn channel_keys_id(&self) -> [u8; 32] {
617                 let mut ret = (self.channel_keys_id)(self.this_arg);
618                 ret.data
619         }
620         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>), ()> {
621                 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 });
622                 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)) })*/ })};
623                 local_ret
624         }
625         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>), ()> {
626                 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 });
627                 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)) })*/ })};
628                 local_ret
629         }
630         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, ()> {
631                 let mut local_justice_tx = ::bitcoin::consensus::encode::serialize(justice_tx);
632                 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 };
633                 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);
634                 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)) })*/ })};
635                 local_ret
636         }
637         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, ()> {
638                 let mut local_htlc_tx = ::bitcoin::consensus::encode::serialize(htlc_tx);
639                 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 });
640                 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)) })*/ })};
641                 local_ret
642         }
643         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, ()> {
644                 let mut local_closing_tx = ::bitcoin::consensus::encode::serialize(closing_tx);
645                 let mut ret = (self.sign_closing_transaction)(self.this_arg, crate::c_types::Transaction::from_vec(local_closing_tx));
646                 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)) })*/ })};
647                 local_ret
648         }
649         fn sign_channel_announcement<T:bitcoin::secp256k1::Signing>(&self, msg: &lightning::ln::msgs::UnsignedChannelAnnouncement, _secp_ctx: &bitcoin::secp256k1::Secp256k1<T>) -> Result<bitcoin::secp256k1::Signature, ()> {
650                 let mut ret = (self.sign_channel_announcement)(self.this_arg, &crate::ln::msgs::UnsignedChannelAnnouncement { inner: unsafe { (msg as *const _) as *mut _ }, is_owned: false });
651                 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)) })*/ })};
652                 local_ret
653         }
654         fn ready_channel(&mut self, channel_parameters: &lightning::ln::chan_utils::ChannelTransactionParameters) {
655                 (self.ready_channel)(self.this_arg, &crate::ln::chan_utils::ChannelTransactionParameters { inner: unsafe { (channel_parameters as *const _) as *mut _ }, is_owned: false })
656         }
657 }
658
659 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
660 // directly as a Deref trait in higher-level structs:
661 impl std::ops::Deref for Sign {
662         type Target = Self;
663         fn deref(&self) -> &Self {
664                 self
665         }
666 }
667 /// Calls the free function if one is set
668 #[no_mangle]
669 pub extern "C" fn Sign_free(this_ptr: Sign) { }
670 impl Drop for Sign {
671         fn drop(&mut self) {
672                 if let Some(f) = self.free {
673                         f(self.this_arg);
674                 }
675         }
676 }
677 /// A trait to describe an object which can get user secrets and key material.
678 #[repr(C)]
679 pub struct KeysInterface {
680         pub this_arg: *mut c_void,
681         /// Get node secret key (aka node_id or network_key).
682         ///
683         /// This method must return the same value each time it is called.
684         #[must_use]
685         pub get_node_secret: extern "C" fn (this_arg: *const c_void) -> crate::c_types::SecretKey,
686         /// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
687         ///
688         /// This method should return a different value each time it is called, to avoid linking
689         /// on-chain funds across channels as controlled to the same user.
690         #[must_use]
691         pub get_destination_script: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
692         /// Get a public key which we will send funds to (in the form of a P2WPKH output) when closing
693         /// a channel.
694         ///
695         /// This method should return a different value each time it is called, to avoid linking
696         /// on-chain funds across channels as controlled to the same user.
697         #[must_use]
698         pub get_shutdown_pubkey: extern "C" fn (this_arg: *const c_void) -> crate::c_types::PublicKey,
699         /// Get a new set of Sign for per-channel secrets. These MUST be unique even if you
700         /// restarted with some stale data!
701         ///
702         /// This method must return a different value each time it is called.
703         #[must_use]
704         pub get_channel_signer: extern "C" fn (this_arg: *const c_void, inbound: bool, channel_value_satoshis: u64) -> crate::chain::keysinterface::Sign,
705         /// Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
706         /// onion packets and for temporary channel IDs. There is no requirement that these be
707         /// persisted anywhere, though they must be unique across restarts.
708         ///
709         /// This method must return a different value each time it is called.
710         #[must_use]
711         pub get_secure_random_bytes: extern "C" fn (this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes,
712         /// Reads a `Signer` for this `KeysInterface` from the given input stream.
713         /// This is only called during deserialization of other objects which contain
714         /// `Sign`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
715         /// The bytes are exactly those which `<Self::Signer as Writeable>::write()` writes, and
716         /// contain no versioning scheme. You may wish to include your own version prefix and ensure
717         /// you've read all of the provided bytes to ensure no corruption occurred.
718         #[must_use]
719         pub read_chan_signer: extern "C" fn (this_arg: *const c_void, reader: crate::c_types::u8slice) -> crate::c_types::derived::CResult_SignDecodeErrorZ,
720         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
721 }
722 unsafe impl Send for KeysInterface {}
723 unsafe impl Sync for KeysInterface {}
724
725 use lightning::chain::keysinterface::KeysInterface as rustKeysInterface;
726 impl rustKeysInterface for KeysInterface {
727         type Signer = crate::chain::keysinterface::Sign;
728         fn get_node_secret(&self) -> bitcoin::secp256k1::key::SecretKey {
729                 let mut ret = (self.get_node_secret)(self.this_arg);
730                 ret.into_rust()
731         }
732         fn get_destination_script(&self) -> bitcoin::blockdata::script::Script {
733                 let mut ret = (self.get_destination_script)(self.this_arg);
734                 ::bitcoin::blockdata::script::Script::from(ret.into_rust())
735         }
736         fn get_shutdown_pubkey(&self) -> bitcoin::secp256k1::key::PublicKey {
737                 let mut ret = (self.get_shutdown_pubkey)(self.this_arg);
738                 ret.into_rust()
739         }
740         fn get_channel_signer(&self, inbound: bool, channel_value_satoshis: u64) -> crate::chain::keysinterface::Sign {
741                 let mut ret = (self.get_channel_signer)(self.this_arg, inbound, channel_value_satoshis);
742                 ret
743         }
744         fn get_secure_random_bytes(&self) -> [u8; 32] {
745                 let mut ret = (self.get_secure_random_bytes)(self.this_arg);
746                 ret.data
747         }
748         fn read_chan_signer(&self, reader: &[u8]) -> Result<crate::chain::keysinterface::Sign, lightning::ln::msgs::DecodeError> {
749                 let mut local_reader = crate::c_types::u8slice::from_slice(reader);
750                 let mut ret = (self.read_chan_signer)(self.this_arg, local_reader);
751                 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()) } })};
752                 local_ret
753         }
754 }
755
756 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
757 // directly as a Deref trait in higher-level structs:
758 impl std::ops::Deref for KeysInterface {
759         type Target = Self;
760         fn deref(&self) -> &Self {
761                 self
762         }
763 }
764 /// Calls the free function if one is set
765 #[no_mangle]
766 pub extern "C" fn KeysInterface_free(this_ptr: KeysInterface) { }
767 impl Drop for KeysInterface {
768         fn drop(&mut self) {
769                 if let Some(f) = self.free {
770                         f(self.this_arg);
771                 }
772         }
773 }
774
775 use lightning::chain::keysinterface::InMemorySigner as nativeInMemorySignerImport;
776 type nativeInMemorySigner = nativeInMemorySignerImport;
777
778 /// A simple implementation of Sign that just keeps the private keys in memory.
779 ///
780 /// This implementation performs no policy checks and is insufficient by itself as
781 /// a secure external signer.
782 #[must_use]
783 #[repr(C)]
784 pub struct InMemorySigner {
785         /// Nearly everywhere, inner must be non-null, however in places where
786         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
787         pub inner: *mut nativeInMemorySigner,
788         pub is_owned: bool,
789 }
790
791 impl Drop for InMemorySigner {
792         fn drop(&mut self) {
793                 if self.is_owned && !<*mut nativeInMemorySigner>::is_null(self.inner) {
794                         let _ = unsafe { Box::from_raw(self.inner) };
795                 }
796         }
797 }
798 #[no_mangle]
799 pub extern "C" fn InMemorySigner_free(this_ptr: InMemorySigner) { }
800 #[allow(unused)]
801 /// Used only if an object of this type is returned as a trait impl by a method
802 extern "C" fn InMemorySigner_free_void(this_ptr: *mut c_void) {
803         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInMemorySigner); }
804 }
805 #[allow(unused)]
806 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
807 impl InMemorySigner {
808         pub(crate) fn take_inner(mut self) -> *mut nativeInMemorySigner {
809                 assert!(self.is_owned);
810                 let ret = self.inner;
811                 self.inner = std::ptr::null_mut();
812                 ret
813         }
814 }
815 /// Private key of anchor tx
816 #[no_mangle]
817 pub extern "C" fn InMemorySigner_get_funding_key(this_ptr: &InMemorySigner) -> *const [u8; 32] {
818         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_key;
819         (*inner_val).as_ref()
820 }
821 /// Private key of anchor tx
822 #[no_mangle]
823 pub extern "C" fn InMemorySigner_set_funding_key(this_ptr: &mut InMemorySigner, mut val: crate::c_types::SecretKey) {
824         unsafe { &mut *this_ptr.inner }.funding_key = val.into_rust();
825 }
826 /// Holder secret key for blinded revocation pubkey
827 #[no_mangle]
828 pub extern "C" fn InMemorySigner_get_revocation_base_key(this_ptr: &InMemorySigner) -> *const [u8; 32] {
829         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.revocation_base_key;
830         (*inner_val).as_ref()
831 }
832 /// Holder secret key for blinded revocation pubkey
833 #[no_mangle]
834 pub extern "C" fn InMemorySigner_set_revocation_base_key(this_ptr: &mut InMemorySigner, mut val: crate::c_types::SecretKey) {
835         unsafe { &mut *this_ptr.inner }.revocation_base_key = val.into_rust();
836 }
837 /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions
838 #[no_mangle]
839 pub extern "C" fn InMemorySigner_get_payment_key(this_ptr: &InMemorySigner) -> *const [u8; 32] {
840         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.payment_key;
841         (*inner_val).as_ref()
842 }
843 /// Holder secret key used for our balance in counterparty-broadcasted commitment transactions
844 #[no_mangle]
845 pub extern "C" fn InMemorySigner_set_payment_key(this_ptr: &mut InMemorySigner, mut val: crate::c_types::SecretKey) {
846         unsafe { &mut *this_ptr.inner }.payment_key = val.into_rust();
847 }
848 /// Holder secret key used in HTLC tx
849 #[no_mangle]
850 pub extern "C" fn InMemorySigner_get_delayed_payment_base_key(this_ptr: &InMemorySigner) -> *const [u8; 32] {
851         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.delayed_payment_base_key;
852         (*inner_val).as_ref()
853 }
854 /// Holder secret key used in HTLC tx
855 #[no_mangle]
856 pub extern "C" fn InMemorySigner_set_delayed_payment_base_key(this_ptr: &mut InMemorySigner, mut val: crate::c_types::SecretKey) {
857         unsafe { &mut *this_ptr.inner }.delayed_payment_base_key = val.into_rust();
858 }
859 /// Holder htlc secret key used in commitment tx htlc outputs
860 #[no_mangle]
861 pub extern "C" fn InMemorySigner_get_htlc_base_key(this_ptr: &InMemorySigner) -> *const [u8; 32] {
862         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_base_key;
863         (*inner_val).as_ref()
864 }
865 /// Holder htlc secret key used in commitment tx htlc outputs
866 #[no_mangle]
867 pub extern "C" fn InMemorySigner_set_htlc_base_key(this_ptr: &mut InMemorySigner, mut val: crate::c_types::SecretKey) {
868         unsafe { &mut *this_ptr.inner }.htlc_base_key = val.into_rust();
869 }
870 /// Commitment seed
871 #[no_mangle]
872 pub extern "C" fn InMemorySigner_get_commitment_seed(this_ptr: &InMemorySigner) -> *const [u8; 32] {
873         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.commitment_seed;
874         &(*inner_val)
875 }
876 /// Commitment seed
877 #[no_mangle]
878 pub extern "C" fn InMemorySigner_set_commitment_seed(this_ptr: &mut InMemorySigner, mut val: crate::c_types::ThirtyTwoBytes) {
879         unsafe { &mut *this_ptr.inner }.commitment_seed = val.data;
880 }
881 impl Clone for InMemorySigner {
882         fn clone(&self) -> Self {
883                 Self {
884                         inner: if <*mut nativeInMemorySigner>::is_null(self.inner) { std::ptr::null_mut() } else {
885                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
886                         is_owned: true,
887                 }
888         }
889 }
890 #[allow(unused)]
891 /// Used only if an object of this type is returned as a trait impl by a method
892 pub(crate) extern "C" fn InMemorySigner_clone_void(this_ptr: *const c_void) -> *mut c_void {
893         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInMemorySigner)).clone() })) as *mut c_void
894 }
895 #[no_mangle]
896 pub extern "C" fn InMemorySigner_clone(orig: &InMemorySigner) -> InMemorySigner {
897         orig.clone()
898 }
899 /// Create a new InMemorySigner
900 #[must_use]
901 #[no_mangle]
902 pub extern "C" fn InMemorySigner_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::InMemorySigner {
903         let mut ret = lightning::chain::keysinterface::InMemorySigner::new(secp256k1::SECP256K1, 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);
904         crate::chain::keysinterface::InMemorySigner { inner: Box::into_raw(Box::new(ret)), is_owned: true }
905 }
906
907 /// Counterparty pubkeys.
908 /// Will panic if ready_channel wasn't called.
909 #[must_use]
910 #[no_mangle]
911 pub extern "C" fn InMemorySigner_counterparty_pubkeys(this_arg: &InMemorySigner) -> crate::ln::chan_utils::ChannelPublicKeys {
912         let mut ret = unsafe { &*this_arg.inner }.counterparty_pubkeys();
913         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
914 }
915
916 /// The contest_delay value specified by our counterparty and applied on holder-broadcastable
917 /// transactions, ie the amount of time that we have to wait to recover our funds if we
918 /// broadcast a transaction.
919 /// Will panic if ready_channel wasn't called.
920 #[must_use]
921 #[no_mangle]
922 pub extern "C" fn InMemorySigner_counterparty_selected_contest_delay(this_arg: &InMemorySigner) -> u16 {
923         let mut ret = unsafe { &*this_arg.inner }.counterparty_selected_contest_delay();
924         ret
925 }
926
927 /// The contest_delay value specified by us and applied on transactions broadcastable
928 /// by our counterparty, ie the amount of time that they have to wait to recover their funds
929 /// if they broadcast a transaction.
930 /// Will panic if ready_channel wasn't called.
931 #[must_use]
932 #[no_mangle]
933 pub extern "C" fn InMemorySigner_holder_selected_contest_delay(this_arg: &InMemorySigner) -> u16 {
934         let mut ret = unsafe { &*this_arg.inner }.holder_selected_contest_delay();
935         ret
936 }
937
938 /// Whether the holder is the initiator
939 /// Will panic if ready_channel wasn't called.
940 #[must_use]
941 #[no_mangle]
942 pub extern "C" fn InMemorySigner_is_outbound(this_arg: &InMemorySigner) -> bool {
943         let mut ret = unsafe { &*this_arg.inner }.is_outbound();
944         ret
945 }
946
947 /// Funding outpoint
948 /// Will panic if ready_channel wasn't called.
949 #[must_use]
950 #[no_mangle]
951 pub extern "C" fn InMemorySigner_funding_outpoint(this_arg: &InMemorySigner) -> crate::chain::transaction::OutPoint {
952         let mut ret = unsafe { &*this_arg.inner }.funding_outpoint();
953         crate::chain::transaction::OutPoint { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
954 }
955
956 /// Obtain a ChannelTransactionParameters for this channel, to be used when verifying or
957 /// building transactions.
958 ///
959 /// Will panic if ready_channel wasn't called.
960 #[must_use]
961 #[no_mangle]
962 pub extern "C" fn InMemorySigner_get_channel_parameters(this_arg: &InMemorySigner) -> crate::ln::chan_utils::ChannelTransactionParameters {
963         let mut ret = unsafe { &*this_arg.inner }.get_channel_parameters();
964         crate::ln::chan_utils::ChannelTransactionParameters { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
965 }
966
967 /// Sign the single input of spend_tx at index `input_idx` which spends the output
968 /// described by descriptor, returning the witness stack for the input.
969 ///
970 /// Returns an Err if the input at input_idx does not exist, has a non-empty script_sig,
971 /// or is not spending the outpoint described by `descriptor.outpoint`.
972 #[must_use]
973 #[no_mangle]
974 pub extern "C" fn InMemorySigner_sign_counterparty_payment_input(this_arg: &InMemorySigner, mut spend_tx: crate::c_types::Transaction, mut input_idx: usize, descriptor: &crate::chain::keysinterface::StaticPaymentOutputDescriptor) -> crate::c_types::derived::CResult_CVec_CVec_u8ZZNoneZ {
975         let mut ret = unsafe { &*this_arg.inner }.sign_counterparty_payment_input(&spend_tx.into_bitcoin(), input_idx, unsafe { &*descriptor.inner }, secp256k1::SECP256K1);
976         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() };
977         local_ret
978 }
979
980 /// Sign the single input of spend_tx at index `input_idx` which spends the output
981 /// described by descriptor, returning the witness stack for the input.
982 ///
983 /// Returns an Err if the input at input_idx does not exist, has a non-empty script_sig,
984 /// is not spending the outpoint described by `descriptor.outpoint`, or does not have a
985 /// sequence set to `descriptor.to_self_delay`.
986 #[must_use]
987 #[no_mangle]
988 pub extern "C" fn InMemorySigner_sign_dynamic_p2wsh_input(this_arg: &InMemorySigner, mut spend_tx: crate::c_types::Transaction, mut input_idx: usize, descriptor: &crate::chain::keysinterface::DelayedPaymentOutputDescriptor) -> crate::c_types::derived::CResult_CVec_CVec_u8ZZNoneZ {
989         let mut ret = unsafe { &*this_arg.inner }.sign_dynamic_p2wsh_input(&spend_tx.into_bitcoin(), input_idx, unsafe { &*descriptor.inner }, secp256k1::SECP256K1);
990         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() };
991         local_ret
992 }
993
994 impl From<nativeInMemorySigner> for crate::chain::keysinterface::Sign {
995         fn from(obj: nativeInMemorySigner) -> Self {
996                 let mut rust_obj = InMemorySigner { inner: Box::into_raw(Box::new(obj)), is_owned: true };
997                 let mut ret = InMemorySigner_as_Sign(&rust_obj);
998                 // 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
999                 rust_obj.inner = std::ptr::null_mut();
1000                 ret.free = Some(InMemorySigner_free_void);
1001                 ret
1002         }
1003 }
1004 #[no_mangle]
1005 pub extern "C" fn InMemorySigner_as_Sign(this_arg: &InMemorySigner) -> crate::chain::keysinterface::Sign {
1006         crate::chain::keysinterface::Sign {
1007                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
1008                 free: None,
1009                 get_per_commitment_point: InMemorySigner_Sign_get_per_commitment_point,
1010                 release_commitment_secret: InMemorySigner_Sign_release_commitment_secret,
1011
1012                 pubkeys: crate::ln::chan_utils::ChannelPublicKeys { inner: std::ptr::null_mut(), is_owned: true },
1013                 set_pubkeys: Some(InMemorySigner_Sign_set_pubkeys),
1014                 channel_keys_id: InMemorySigner_Sign_channel_keys_id,
1015                 sign_counterparty_commitment: InMemorySigner_Sign_sign_counterparty_commitment,
1016                 sign_holder_commitment_and_htlcs: InMemorySigner_Sign_sign_holder_commitment_and_htlcs,
1017                 sign_justice_transaction: InMemorySigner_Sign_sign_justice_transaction,
1018                 sign_counterparty_htlc_transaction: InMemorySigner_Sign_sign_counterparty_htlc_transaction,
1019                 sign_closing_transaction: InMemorySigner_Sign_sign_closing_transaction,
1020                 sign_channel_announcement: InMemorySigner_Sign_sign_channel_announcement,
1021                 ready_channel: InMemorySigner_Sign_ready_channel,
1022                 clone: Some(InMemorySigner_clone_void),
1023                 write: InMemorySigner_write_void,
1024         }
1025 }
1026
1027 #[must_use]
1028 extern "C" fn InMemorySigner_Sign_get_per_commitment_point(this_arg: *const c_void, mut idx: u64) -> crate::c_types::PublicKey {
1029         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::get_per_commitment_point(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, idx, secp256k1::SECP256K1);
1030         crate::c_types::PublicKey::from_rust(&ret)
1031 }
1032 #[must_use]
1033 extern "C" fn InMemorySigner_Sign_release_commitment_secret(this_arg: *const c_void, mut idx: u64) -> crate::c_types::ThirtyTwoBytes {
1034         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::release_commitment_secret(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, idx);
1035         crate::c_types::ThirtyTwoBytes { data: ret }
1036 }
1037 #[must_use]
1038 extern "C" fn InMemorySigner_Sign_pubkeys(this_arg: *const c_void) -> crate::ln::chan_utils::ChannelPublicKeys {
1039         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::pubkeys(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, );
1040         crate::ln::chan_utils::ChannelPublicKeys { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
1041 }
1042 extern "C" fn InMemorySigner_Sign_set_pubkeys(trait_self_arg: &Sign) {
1043         // This is a bit race-y in the general case, but for our specific use-cases today, we're safe
1044         // Specifically, we must ensure that the first time we're called it can never be in parallel
1045         if trait_self_arg.pubkeys.inner.is_null() {
1046                 unsafe { &mut *(trait_self_arg as *const Sign  as *mut Sign) }.pubkeys = InMemorySigner_Sign_pubkeys(trait_self_arg.this_arg);
1047         }
1048 }
1049 #[must_use]
1050 extern "C" fn InMemorySigner_Sign_channel_keys_id(this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes {
1051         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::channel_keys_id(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, );
1052         crate::c_types::ThirtyTwoBytes { data: ret }
1053 }
1054 #[must_use]
1055 extern "C" fn InMemorySigner_Sign_sign_counterparty_commitment(this_arg: *const c_void, commitment_tx: &crate::ln::chan_utils::CommitmentTransaction) -> crate::c_types::derived::CResult_C2Tuple_SignatureCVec_SignatureZZNoneZ {
1056         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::sign_counterparty_commitment(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, unsafe { &*commitment_tx.inner }, secp256k1::SECP256K1);
1057         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() };
1058         local_ret
1059 }
1060 #[must_use]
1061 extern "C" fn InMemorySigner_Sign_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 {
1062         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::sign_holder_commitment_and_htlcs(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, unsafe { &*commitment_tx.inner }, secp256k1::SECP256K1);
1063         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() };
1064         local_ret
1065 }
1066 #[must_use]
1067 extern "C" fn InMemorySigner_Sign_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 {
1068         let mut local_htlc = if htlc.inner.is_null() { None } else { Some((* { unsafe { &*htlc.inner } }).clone()) };
1069         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::sign_justice_transaction(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, &justice_tx.into_bitcoin(), input, amount, &::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *per_commitment_key}[..]).unwrap(), &local_htlc, secp256k1::SECP256K1);
1070         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() };
1071         local_ret
1072 }
1073 #[must_use]
1074 extern "C" fn InMemorySigner_Sign_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 {
1075         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::sign_counterparty_htlc_transaction(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, &htlc_tx.into_bitcoin(), input, amount, &per_commitment_point.into_rust(), unsafe { &*htlc.inner }, secp256k1::SECP256K1);
1076         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() };
1077         local_ret
1078 }
1079 #[must_use]
1080 extern "C" fn InMemorySigner_Sign_sign_closing_transaction(this_arg: *const c_void, mut closing_tx: crate::c_types::Transaction) -> crate::c_types::derived::CResult_SignatureNoneZ {
1081         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::sign_closing_transaction(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, &closing_tx.into_bitcoin(), secp256k1::SECP256K1);
1082         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() };
1083         local_ret
1084 }
1085 #[must_use]
1086 extern "C" fn InMemorySigner_Sign_sign_channel_announcement(this_arg: *const c_void, msg: &crate::ln::msgs::UnsignedChannelAnnouncement) -> crate::c_types::derived::CResult_SignatureNoneZ {
1087         let mut ret = <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::sign_channel_announcement(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, unsafe { &*msg.inner }, secp256k1::SECP256K1);
1088         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() };
1089         local_ret
1090 }
1091 extern "C" fn InMemorySigner_Sign_ready_channel(this_arg: *mut c_void, channel_parameters: &crate::ln::chan_utils::ChannelTransactionParameters) {
1092         <nativeInMemorySigner as lightning::chain::keysinterface::Sign<>>::ready_channel(unsafe { &mut *(this_arg as *mut nativeInMemorySigner) }, unsafe { &*channel_parameters.inner })
1093 }
1094
1095 #[no_mangle]
1096 pub extern "C" fn InMemorySigner_write(obj: &InMemorySigner) -> crate::c_types::derived::CVec_u8Z {
1097         crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
1098 }
1099 #[no_mangle]
1100 pub(crate) extern "C" fn InMemorySigner_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1101         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInMemorySigner) })
1102 }
1103 #[no_mangle]
1104 pub extern "C" fn InMemorySigner_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InMemorySignerDecodeErrorZ {
1105         let res = crate::c_types::deserialize_obj(ser);
1106         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::chain::keysinterface::InMemorySigner { 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() };
1107         local_res
1108 }
1109
1110 use lightning::chain::keysinterface::KeysManager as nativeKeysManagerImport;
1111 type nativeKeysManager = nativeKeysManagerImport;
1112
1113 /// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
1114 /// and derives keys from that.
1115 ///
1116 /// Your node_id is seed/0'
1117 /// ChannelMonitor closes may use seed/1'
1118 /// Cooperative closes may use seed/2'
1119 /// The two close keys may be needed to claim on-chain funds!
1120 #[must_use]
1121 #[repr(C)]
1122 pub struct KeysManager {
1123         /// Nearly everywhere, inner must be non-null, however in places where
1124         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1125         pub inner: *mut nativeKeysManager,
1126         pub is_owned: bool,
1127 }
1128
1129 impl Drop for KeysManager {
1130         fn drop(&mut self) {
1131                 if self.is_owned && !<*mut nativeKeysManager>::is_null(self.inner) {
1132                         let _ = unsafe { Box::from_raw(self.inner) };
1133                 }
1134         }
1135 }
1136 #[no_mangle]
1137 pub extern "C" fn KeysManager_free(this_ptr: KeysManager) { }
1138 #[allow(unused)]
1139 /// Used only if an object of this type is returned as a trait impl by a method
1140 extern "C" fn KeysManager_free_void(this_ptr: *mut c_void) {
1141         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeKeysManager); }
1142 }
1143 #[allow(unused)]
1144 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1145 impl KeysManager {
1146         pub(crate) fn take_inner(mut self) -> *mut nativeKeysManager {
1147                 assert!(self.is_owned);
1148                 let ret = self.inner;
1149                 self.inner = std::ptr::null_mut();
1150                 ret
1151         }
1152 }
1153 /// Constructs a KeysManager from a 32-byte seed. If the seed is in some way biased (eg your
1154 /// CSRNG is busted) this may panic (but more importantly, you will possibly lose funds).
1155 /// starting_time isn't strictly required to actually be a time, but it must absolutely,
1156 /// without a doubt, be unique to this instance. ie if you start multiple times with the same
1157 /// seed, starting_time must be unique to each run. Thus, the easiest way to achieve this is to
1158 /// simply use the current time (with very high precision).
1159 ///
1160 /// The seed MUST be backed up safely prior to use so that the keys can be re-created, however,
1161 /// obviously, starting_time should be unique every time you reload the library - it is only
1162 /// used to generate new ephemeral key data (which will be stored by the individual channel if
1163 /// necessary).
1164 ///
1165 /// Note that the seed is required to recover certain on-chain funds independent of
1166 /// ChannelMonitor data, though a current copy of ChannelMonitor data is also required for any
1167 /// channel, and some on-chain during-closing funds.
1168 ///
1169 /// Note that until the 0.1 release there is no guarantee of backward compatibility between
1170 /// versions. Once the library is more fully supported, the docs will be updated to include a
1171 /// detailed description of the guarantee.
1172 #[must_use]
1173 #[no_mangle]
1174 pub extern "C" fn KeysManager_new(seed: *const [u8; 32], mut starting_time_secs: u64, mut starting_time_nanos: u32) -> KeysManager {
1175         let mut ret = lightning::chain::keysinterface::KeysManager::new(unsafe { &*seed}, starting_time_secs, starting_time_nanos);
1176         KeysManager { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1177 }
1178
1179 /// Derive an old Sign containing per-channel secrets based on a key derivation parameters.
1180 ///
1181 /// Key derivation parameters are accessible through a per-channel secrets
1182 /// Sign::channel_keys_id and is provided inside DynamicOuputP2WSH in case of
1183 /// onchain output detection for which a corresponding delayed_payment_key must be derived.
1184 #[must_use]
1185 #[no_mangle]
1186 pub extern "C" fn KeysManager_derive_channel_keys(this_arg: &KeysManager, mut channel_value_satoshis: u64, params: *const [u8; 32]) -> crate::chain::keysinterface::InMemorySigner {
1187         let mut ret = unsafe { &*this_arg.inner }.derive_channel_keys(channel_value_satoshis, unsafe { &*params});
1188         crate::chain::keysinterface::InMemorySigner { inner: Box::into_raw(Box::new(ret)), is_owned: true }
1189 }
1190
1191 /// Creates a Transaction which spends the given descriptors to the given outputs, plus an
1192 /// output to the given change destination (if sufficient change value remains). The
1193 /// transaction will have a feerate, at least, of the given value.
1194 ///
1195 /// Returns `Err(())` if the output value is greater than the input value minus required fee or
1196 /// if a descriptor was duplicated.
1197 ///
1198 /// We do not enforce that outputs meet the dust limit or that any output scripts are standard.
1199 ///
1200 /// May panic if the `SpendableOutputDescriptor`s were not generated by Channels which used
1201 /// this KeysManager or one of the `InMemorySigner` created by this KeysManager.
1202 #[must_use]
1203 #[no_mangle]
1204 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 {
1205         let mut local_descriptors = Vec::new(); for mut item in descriptors.into_rust().drain(..) { local_descriptors.push( { item.into_native() }); };
1206         let mut local_outputs = Vec::new(); for mut item in outputs.into_rust().drain(..) { local_outputs.push( { item.into_rust() }); };
1207         let mut ret = unsafe { &*this_arg.inner }.spend_spendable_outputs(&local_descriptors.iter().collect::<Vec<_>>()[..], local_outputs, ::bitcoin::blockdata::script::Script::from(change_destination_script.into_rust()), feerate_sat_per_1000_weight, secp256k1::SECP256K1);
1208         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() };
1209         local_ret
1210 }
1211
1212 impl From<nativeKeysManager> for crate::chain::keysinterface::KeysInterface {
1213         fn from(obj: nativeKeysManager) -> Self {
1214                 let mut rust_obj = KeysManager { inner: Box::into_raw(Box::new(obj)), is_owned: true };
1215                 let mut ret = KeysManager_as_KeysInterface(&rust_obj);
1216                 // 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
1217                 rust_obj.inner = std::ptr::null_mut();
1218                 ret.free = Some(KeysManager_free_void);
1219                 ret
1220         }
1221 }
1222 #[no_mangle]
1223 pub extern "C" fn KeysManager_as_KeysInterface(this_arg: &KeysManager) -> crate::chain::keysinterface::KeysInterface {
1224         crate::chain::keysinterface::KeysInterface {
1225                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
1226                 free: None,
1227                 get_node_secret: KeysManager_KeysInterface_get_node_secret,
1228                 get_destination_script: KeysManager_KeysInterface_get_destination_script,
1229                 get_shutdown_pubkey: KeysManager_KeysInterface_get_shutdown_pubkey,
1230                 get_channel_signer: KeysManager_KeysInterface_get_channel_signer,
1231                 get_secure_random_bytes: KeysManager_KeysInterface_get_secure_random_bytes,
1232                 read_chan_signer: KeysManager_KeysInterface_read_chan_signer,
1233         }
1234 }
1235
1236 #[must_use]
1237 extern "C" fn KeysManager_KeysInterface_get_node_secret(this_arg: *const c_void) -> crate::c_types::SecretKey {
1238         let mut ret = <nativeKeysManager as lightning::chain::keysinterface::KeysInterface<>>::get_node_secret(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, );
1239         crate::c_types::SecretKey::from_rust(ret)
1240 }
1241 #[must_use]
1242 extern "C" fn KeysManager_KeysInterface_get_destination_script(this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1243         let mut ret = <nativeKeysManager as lightning::chain::keysinterface::KeysInterface<>>::get_destination_script(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, );
1244         ret.into_bytes().into()
1245 }
1246 #[must_use]
1247 extern "C" fn KeysManager_KeysInterface_get_shutdown_pubkey(this_arg: *const c_void) -> crate::c_types::PublicKey {
1248         let mut ret = <nativeKeysManager as lightning::chain::keysinterface::KeysInterface<>>::get_shutdown_pubkey(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, );
1249         crate::c_types::PublicKey::from_rust(&ret)
1250 }
1251 #[must_use]
1252 extern "C" fn KeysManager_KeysInterface_get_channel_signer(this_arg: *const c_void, mut _inbound: bool, mut channel_value_satoshis: u64) -> crate::chain::keysinterface::Sign {
1253         let mut ret = <nativeKeysManager as lightning::chain::keysinterface::KeysInterface<>>::get_channel_signer(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, _inbound, channel_value_satoshis);
1254         ret.into()
1255 }
1256 #[must_use]
1257 extern "C" fn KeysManager_KeysInterface_get_secure_random_bytes(this_arg: *const c_void) -> crate::c_types::ThirtyTwoBytes {
1258         let mut ret = <nativeKeysManager as lightning::chain::keysinterface::KeysInterface<>>::get_secure_random_bytes(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, );
1259         crate::c_types::ThirtyTwoBytes { data: ret }
1260 }
1261 #[must_use]
1262 extern "C" fn KeysManager_KeysInterface_read_chan_signer(this_arg: *const c_void, mut reader: crate::c_types::u8slice) -> crate::c_types::derived::CResult_SignDecodeErrorZ {
1263         let mut ret = <nativeKeysManager as lightning::chain::keysinterface::KeysInterface<>>::read_chan_signer(unsafe { &mut *(this_arg as *mut nativeKeysManager) }, reader.to_slice());
1264         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() };
1265         local_ret
1266 }
1267