- bindings updates
[rust-lightning] / lightning-c-bindings / src / chain / keysinterface.rs
index 576a620504cd5c85f9541fc99c1be28847514046..7642138ed684e772ce5b3e361d2a86b43ada1d59 100644 (file)
@@ -264,78 +264,171 @@ pub(crate) extern "C" fn StaticPaymentOutputDescriptor_clone_void(this_ptr: *con
 pub extern "C" fn StaticPaymentOutputDescriptor_clone(orig: &StaticPaymentOutputDescriptor) -> StaticPaymentOutputDescriptor {
        orig.clone()
 }
-
-use lightning::chain::keysinterface::SpendableOutputDescriptor as nativeSpendableOutputDescriptorImport;
-type nativeSpendableOutputDescriptor = nativeSpendableOutputDescriptorImport;
-
 /// When on-chain outputs are created by rust-lightning (which our counterparty is not able to
 /// claim at any point in the future) an event is generated which you must track and be able to
 /// spend on-chain. The information needed to do this is provided in this enum, including the
 /// outpoint describing which txid and output index is available, the full output which exists at
 /// that txid/index, and any keys or other information required to sign.
 #[must_use]
+#[derive(Clone)]
 #[repr(C)]
-pub struct SpendableOutputDescriptor {
-       /// Nearly everywhere, inner must be non-null, however in places where
-       /// the Rust equivalent takes an Option, it may be set to null to indicate None.
-       pub inner: *mut nativeSpendableOutputDescriptor,
-       pub is_owned: bool,
+pub enum SpendableOutputDescriptor {
+       /// An output to a script which was provided via KeysInterface directly, either from
+       /// `get_destination_script()` or `get_shutdown_pubkey()`, thus you should already know how to
+       /// spend it. No secret keys are provided as rust-lightning was never given any key.
+       /// These may include outputs from a transaction punishing our counterparty or claiming an HTLC
+       /// on-chain using the payment preimage or after it has timed out.
+       StaticOutput {
+               outpoint: crate::chain::transaction::OutPoint,
+               output: crate::c_types::TxOut,
+       },
+       /// An output to a P2WSH script which can be spent with a single signature after a CSV delay.
+       ///
+       /// The witness in the spending input should be:
+       /// <BIP 143 signature> <empty vector> (MINIMALIF standard rule) <provided witnessScript>
+       ///
+       /// Note that the nSequence field in the spending input must be set to to_self_delay
+       /// (which means the transaction is not broadcastable until at least to_self_delay
+       /// blocks after the outpoint confirms).
+       ///
+       /// These are generally the result of a \"revocable\" output to us, spendable only by us unless
+       /// it is an output from an old state which we broadcast (which should never happen).
+       ///
+       /// To derive the delayed_payment key which is used to sign for this input, you must pass the
+       /// holder delayed_payment_base_key (ie the private key which corresponds to the pubkey in
+       /// ChannelKeys::pubkeys().delayed_payment_basepoint) and the provided per_commitment_point to
+       /// chan_utils::derive_private_key. The public key can be generated without the secret key
+       /// using chan_utils::derive_public_key and only the delayed_payment_basepoint which appears in
+       /// ChannelKeys::pubkeys().
+       ///
+       /// To derive the revocation_pubkey provided here (which is used in the witness
+       /// script generation), you must pass the counterparty revocation_basepoint (which appears in the
+       /// call to ChannelKeys::ready_channel) and the provided per_commitment point
+       /// to chan_utils::derive_public_revocation_key.
+       ///
+       /// The witness script which is hashed and included in the output script_pubkey may be
+       /// regenerated by passing the revocation_pubkey (derived as above), our delayed_payment pubkey
+       /// (derived as above), and the to_self_delay contained here to
+       /// chan_utils::get_revokeable_redeemscript.
+       DelayedPaymentOutput(crate::chain::keysinterface::DelayedPaymentOutputDescriptor),
+       /// An output to a P2WPKH, spendable exclusively by our payment key (ie the private key which
+       /// corresponds to the public key in ChannelKeys::pubkeys().payment_point).
+       /// The witness in the spending input, is, thus, simply:
+       /// <BIP 143 signature> <payment key>
+       ///
+       /// These are generally the result of our counterparty having broadcast the current state,
+       /// allowing us to claim the non-HTLC-encumbered outputs immediately.
+       StaticPaymentOutput(crate::chain::keysinterface::StaticPaymentOutputDescriptor),
 }
-
-impl Drop for SpendableOutputDescriptor {
-       fn drop(&mut self) {
-               if self.is_owned && !self.inner.is_null() {
-                       let _ = unsafe { Box::from_raw(self.inner) };
+use lightning::chain::keysinterface::SpendableOutputDescriptor as nativeSpendableOutputDescriptor;
+impl SpendableOutputDescriptor {
+       #[allow(unused)]
+       pub(crate) fn to_native(&self) -> nativeSpendableOutputDescriptor {
+               match self {
+                       SpendableOutputDescriptor::StaticOutput {ref outpoint, ref output, } => {
+                               let mut outpoint_nonref = (*outpoint).clone();
+                               let mut output_nonref = (*output).clone();
+                               nativeSpendableOutputDescriptor::StaticOutput {
+                                       outpoint: *unsafe { Box::from_raw(outpoint_nonref.take_inner()) },
+                                       output: output_nonref.into_rust(),
+                               }
+                       },
+                       SpendableOutputDescriptor::DelayedPaymentOutput (ref a, ) => {
+                               let mut a_nonref = (*a).clone();
+                               nativeSpendableOutputDescriptor::DelayedPaymentOutput (
+                                       *unsafe { Box::from_raw(a_nonref.take_inner()) },
+                               )
+                       },
+                       SpendableOutputDescriptor::StaticPaymentOutput (ref a, ) => {
+                               let mut a_nonref = (*a).clone();
+                               nativeSpendableOutputDescriptor::StaticPaymentOutput (
+                                       *unsafe { Box::from_raw(a_nonref.take_inner()) },
+                               )
+                       },
                }
        }
-}
-#[no_mangle]
-pub extern "C" fn SpendableOutputDescriptor_free(this_ptr: SpendableOutputDescriptor) { }
-#[allow(unused)]
-/// Used only if an object of this type is returned as a trait impl by a method
-extern "C" fn SpendableOutputDescriptor_free_void(this_ptr: *mut c_void) {
-       unsafe { let _ = Box::from_raw(this_ptr as *mut nativeSpendableOutputDescriptor); }
-}
-#[allow(unused)]
-/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
-impl SpendableOutputDescriptor {
-       pub(crate) fn take_inner(mut self) -> *mut nativeSpendableOutputDescriptor {
-               assert!(self.is_owned);
-               let ret = self.inner;
-               self.inner = std::ptr::null_mut();
-               ret
+       #[allow(unused)]
+       pub(crate) fn into_native(self) -> nativeSpendableOutputDescriptor {
+               match self {
+                       SpendableOutputDescriptor::StaticOutput {mut outpoint, mut output, } => {
+                               nativeSpendableOutputDescriptor::StaticOutput {
+                                       outpoint: *unsafe { Box::from_raw(outpoint.take_inner()) },
+                                       output: output.into_rust(),
+                               }
+                       },
+                       SpendableOutputDescriptor::DelayedPaymentOutput (mut a, ) => {
+                               nativeSpendableOutputDescriptor::DelayedPaymentOutput (
+                                       *unsafe { Box::from_raw(a.take_inner()) },
+                               )
+                       },
+                       SpendableOutputDescriptor::StaticPaymentOutput (mut a, ) => {
+                               nativeSpendableOutputDescriptor::StaticPaymentOutput (
+                                       *unsafe { Box::from_raw(a.take_inner()) },
+                               )
+                       },
+               }
        }
-}
-impl Clone for SpendableOutputDescriptor {
-       fn clone(&self) -> Self {
-               Self {
-                       inner: if self.inner.is_null() { std::ptr::null_mut() } else {
-                               Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
-                       is_owned: true,
+       #[allow(unused)]
+       pub(crate) fn from_native(native: &nativeSpendableOutputDescriptor) -> Self {
+               match native {
+                       nativeSpendableOutputDescriptor::StaticOutput {ref outpoint, ref output, } => {
+                               let mut outpoint_nonref = (*outpoint).clone();
+                               let mut output_nonref = (*output).clone();
+                               SpendableOutputDescriptor::StaticOutput {
+                                       outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint_nonref)), is_owned: true },
+                                       output: crate::c_types::TxOut::from_rust(output_nonref),
+                               }
+                       },
+                       nativeSpendableOutputDescriptor::DelayedPaymentOutput (ref a, ) => {
+                               let mut a_nonref = (*a).clone();
+                               SpendableOutputDescriptor::DelayedPaymentOutput (
+                                       crate::chain::keysinterface::DelayedPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a_nonref)), is_owned: true },
+                               )
+                       },
+                       nativeSpendableOutputDescriptor::StaticPaymentOutput (ref a, ) => {
+                               let mut a_nonref = (*a).clone();
+                               SpendableOutputDescriptor::StaticPaymentOutput (
+                                       crate::chain::keysinterface::StaticPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a_nonref)), is_owned: true },
+                               )
+                       },
+               }
+       }
+       #[allow(unused)]
+       pub(crate) fn native_into(native: nativeSpendableOutputDescriptor) -> Self {
+               match native {
+                       nativeSpendableOutputDescriptor::StaticOutput {mut outpoint, mut output, } => {
+                               SpendableOutputDescriptor::StaticOutput {
+                                       outpoint: crate::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(outpoint)), is_owned: true },
+                                       output: crate::c_types::TxOut::from_rust(output),
+                               }
+                       },
+                       nativeSpendableOutputDescriptor::DelayedPaymentOutput (mut a, ) => {
+                               SpendableOutputDescriptor::DelayedPaymentOutput (
+                                       crate::chain::keysinterface::DelayedPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a)), is_owned: true },
+                               )
+                       },
+                       nativeSpendableOutputDescriptor::StaticPaymentOutput (mut a, ) => {
+                               SpendableOutputDescriptor::StaticPaymentOutput (
+                                       crate::chain::keysinterface::StaticPaymentOutputDescriptor { inner: Box::into_raw(Box::new(a)), is_owned: true },
+                               )
+                       },
                }
        }
-}
-#[allow(unused)]
-/// Used only if an object of this type is returned as a trait impl by a method
-pub(crate) extern "C" fn SpendableOutputDescriptor_clone_void(this_ptr: *const c_void) -> *mut c_void {
-       Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeSpendableOutputDescriptor)).clone() })) as *mut c_void
 }
 #[no_mangle]
+pub extern "C" fn SpendableOutputDescriptor_free(this_ptr: SpendableOutputDescriptor) { }
+#[no_mangle]
 pub extern "C" fn SpendableOutputDescriptor_clone(orig: &SpendableOutputDescriptor) -> SpendableOutputDescriptor {
        orig.clone()
 }
 #[no_mangle]
 pub extern "C" fn SpendableOutputDescriptor_write(obj: &SpendableOutputDescriptor) -> crate::c_types::derived::CVec_u8Z {
-       crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
-}
-#[no_mangle]
-pub(crate) extern "C" fn SpendableOutputDescriptor_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
-       crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeSpendableOutputDescriptor) })
+       crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
 }
 #[no_mangle]
 pub extern "C" fn SpendableOutputDescriptor_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_SpendableOutputDescriptorDecodeErrorZ {
        let res = crate::c_types::deserialize_obj(ser);
-       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() };
+       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() };
        local_res
 }
 /// Set of lightning keys needed to operate a channel as described in BOLT 3.
@@ -1109,9 +1202,9 @@ pub extern "C" fn KeysManager_derive_channel_keys(this_arg: &KeysManager, mut ch
 #[must_use]
 #[no_mangle]
 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 {
-       let mut local_descriptors = Vec::new(); for mut item in descriptors.as_slice().iter() { local_descriptors.push( { unsafe { &*item.inner } }); };
+       let mut local_descriptors = Vec::new(); for mut item in descriptors.into_rust().drain(..) { local_descriptors.push( { item.into_native() }); };
        let mut local_outputs = Vec::new(); for mut item in outputs.into_rust().drain(..) { local_outputs.push( { item.into_rust() }); };
-       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());
+       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, &bitcoin::secp256k1::Secp256k1::new());
        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() };
        local_ret
 }