X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Fscript.rs;h=fe5c68110db96e0d945e71b0595b3eb811e56e6a;hp=7dc4b1d515bc387dc23c69ed163efedf12b4aeaf;hb=1eaf50a3d9f777b462ff1817678473567a00eb75;hpb=68c220f7ee4fa0b6b4a300f56cd30b9b02ebfbdf diff --git a/lightning-c-bindings/src/lightning/ln/script.rs b/lightning-c-bindings/src/lightning/ln/script.rs index 7dc4b1d..fe5c681 100644 --- a/lightning-c-bindings/src/lightning/ln/script.rs +++ b/lightning-c-bindings/src/lightning/ln/script.rs @@ -8,14 +8,17 @@ //! Abstractions for scripts used in the Lightning Network. -use std::str::FromStr; -use std::ffi::c_void; +use alloc::str::FromStr; +use core::ffi::c_void; +use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; use lightning::ln::script::ShutdownScript as nativeShutdownScriptImport; -type nativeShutdownScript = nativeShutdownScriptImport; +pub(crate) type nativeShutdownScript = nativeShutdownScriptImport; /// A script pubkey for shutting down a channel as defined by [BOLT #2]. /// @@ -38,7 +41,7 @@ pub struct ShutdownScript { impl Drop for ShutdownScript { fn drop(&mut self) { if self.is_owned && !<*mut nativeShutdownScript>::is_null(self.inner) { - let _ = unsafe { Box::from_raw(self.inner) }; + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; } } } @@ -47,24 +50,30 @@ impl Drop for ShutdownScript { pub extern "C" fn ShutdownScript_free(this_obj: ShutdownScript) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn ShutdownScript_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn ShutdownScript_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeShutdownScript); } } #[allow(unused)] -/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl ShutdownScript { + pub(crate) fn get_native_ref(&self) -> &'static nativeShutdownScript { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeShutdownScript { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy pub(crate) fn take_inner(mut self) -> *mut nativeShutdownScript { assert!(self.is_owned); - let ret = self.inner; - self.inner = std::ptr::null_mut(); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = core::ptr::null_mut(); ret } } impl Clone for ShutdownScript { fn clone(&self) -> Self { Self { - inner: if <*mut nativeShutdownScript>::is_null(self.inner) { std::ptr::null_mut() } else { - Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) }, + inner: if <*mut nativeShutdownScript>::is_null(self.inner) { core::ptr::null_mut() } else { + ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, is_owned: true, } } @@ -81,7 +90,7 @@ pub extern "C" fn ShutdownScript_clone(orig: &ShutdownScript) -> ShutdownScript } use lightning::ln::script::InvalidShutdownScript as nativeInvalidShutdownScriptImport; -type nativeInvalidShutdownScript = nativeInvalidShutdownScriptImport; +pub(crate) type nativeInvalidShutdownScript = nativeInvalidShutdownScriptImport; /// An error occurring when converting from [`Script`] to [`ShutdownScript`]. #[must_use] @@ -102,7 +111,7 @@ pub struct InvalidShutdownScript { impl Drop for InvalidShutdownScript { fn drop(&mut self) { if self.is_owned && !<*mut nativeInvalidShutdownScript>::is_null(self.inner) { - let _ = unsafe { Box::from_raw(self.inner) }; + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; } } } @@ -111,16 +120,22 @@ impl Drop for InvalidShutdownScript { pub extern "C" fn InvalidShutdownScript_free(this_obj: InvalidShutdownScript) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn InvalidShutdownScript_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn InvalidShutdownScript_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeInvalidShutdownScript); } } #[allow(unused)] -/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy impl InvalidShutdownScript { + pub(crate) fn get_native_ref(&self) -> &'static nativeInvalidShutdownScript { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInvalidShutdownScript { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy pub(crate) fn take_inner(mut self) -> *mut nativeInvalidShutdownScript { assert!(self.is_owned); - let ret = self.inner; - self.inner = std::ptr::null_mut(); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = core::ptr::null_mut(); ret } } @@ -129,7 +144,7 @@ impl InvalidShutdownScript { /// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md #[no_mangle] pub extern "C" fn InvalidShutdownScript_get_script(this_ptr: &InvalidShutdownScript) -> crate::c_types::u8slice { - let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.script; + let mut inner_val = &mut this_ptr.get_native_mut_ref().script; crate::c_types::u8slice::from_slice(&inner_val[..]) } /// The script that did not meet the requirements from [BOLT #2]. @@ -137,20 +152,39 @@ pub extern "C" fn InvalidShutdownScript_get_script(this_ptr: &InvalidShutdownScr /// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md #[no_mangle] pub extern "C" fn InvalidShutdownScript_set_script(this_ptr: &mut InvalidShutdownScript, mut val: crate::c_types::derived::CVec_u8Z) { - unsafe { &mut *this_ptr.inner }.script = ::bitcoin::blockdata::script::Script::from(val.into_rust()); + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.script = ::bitcoin::blockdata::script::Script::from(val.into_rust()); } /// Constructs a new InvalidShutdownScript given each field #[must_use] #[no_mangle] pub extern "C" fn InvalidShutdownScript_new(mut script_arg: crate::c_types::derived::CVec_u8Z) -> InvalidShutdownScript { - InvalidShutdownScript { inner: Box::into_raw(Box::new(nativeInvalidShutdownScript { + InvalidShutdownScript { inner: ObjOps::heap_alloc(nativeInvalidShutdownScript { script: ::bitcoin::blockdata::script::Script::from(script_arg.into_rust()), - })), is_owned: true } + }), is_owned: true } +} +impl Clone for InvalidShutdownScript { + fn clone(&self) -> Self { + Self { + inner: if <*mut nativeInvalidShutdownScript>::is_null(self.inner) { core::ptr::null_mut() } else { + ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, + 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 InvalidShutdownScript_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInvalidShutdownScript)).clone() })) as *mut c_void +} +#[no_mangle] +/// Creates a copy of the InvalidShutdownScript +pub extern "C" fn InvalidShutdownScript_clone(orig: &InvalidShutdownScript) -> InvalidShutdownScript { + orig.clone() } #[no_mangle] /// Serialize the ShutdownScript object into a byte array which can be read by ShutdownScript_read pub extern "C" fn ShutdownScript_write(obj: &ShutdownScript) -> crate::c_types::derived::CVec_u8Z { - crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner }) + crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref()) } #[no_mangle] pub(crate) extern "C" fn ShutdownScript_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z { @@ -159,32 +193,16 @@ pub(crate) extern "C" fn ShutdownScript_write_void(obj: *const c_void) -> crate: #[no_mangle] /// Read a ShutdownScript from a byte array, created by ShutdownScript_write pub extern "C" fn ShutdownScript_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ShutdownScriptDecodeErrorZ { - let res = crate::c_types::deserialize_obj(ser); - let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + let res: Result = crate::c_types::deserialize_obj(ser); + let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() }; local_res } -/// Generates a P2PKH script pubkey from the given [`PubkeyHash`]. -#[must_use] -#[no_mangle] -pub extern "C" fn ShutdownScript_new_p2pkh(pubkey_hash: *const [u8; 20]) -> ShutdownScript { - let mut ret = lightning::ln::script::ShutdownScript::new_p2pkh(&bitcoin::hash_types::PubkeyHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *pubkey_hash }.clone()))); - ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } -} - -/// Generates a P2SH script pubkey from the given [`ScriptHash`]. -#[must_use] -#[no_mangle] -pub extern "C" fn ShutdownScript_new_p2sh(script_hash: *const [u8; 20]) -> ShutdownScript { - let mut ret = lightning::ln::script::ShutdownScript::new_p2sh(&bitcoin::hash_types::ScriptHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *script_hash }.clone()))); - ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } -} - /// Generates a P2WPKH script pubkey from the given [`WPubkeyHash`]. #[must_use] #[no_mangle] pub extern "C" fn ShutdownScript_new_p2wpkh(pubkey_hash: *const [u8; 20]) -> ShutdownScript { let mut ret = lightning::ln::script::ShutdownScript::new_p2wpkh(&bitcoin::hash_types::WPubkeyHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *pubkey_hash }.clone()))); - ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } + ShutdownScript { inner: ObjOps::heap_alloc(ret), is_owned: true } } /// Generates a P2WSH script pubkey from the given [`WScriptHash`]. @@ -192,10 +210,13 @@ pub extern "C" fn ShutdownScript_new_p2wpkh(pubkey_hash: *const [u8; 20]) -> Shu #[no_mangle] pub extern "C" fn ShutdownScript_new_p2wsh(script_hash: *const [u8; 32]) -> ShutdownScript { let mut ret = lightning::ln::script::ShutdownScript::new_p2wsh(&bitcoin::hash_types::WScriptHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *script_hash }.clone()))); - ShutdownScript { inner: Box::into_raw(Box::new(ret)), is_owned: true } + ShutdownScript { inner: ObjOps::heap_alloc(ret), is_owned: true } } -/// Generates a P2WSH script pubkey from the given segwit version and program. +/// Generates a witness script pubkey from the given segwit version and program. +/// +/// Note for version-zero witness scripts you must use [`ShutdownScript::new_p2wpkh`] or +/// [`ShutdownScript::new_p2wsh`] instead. /// /// # Errors /// @@ -204,7 +225,7 @@ pub extern "C" fn ShutdownScript_new_p2wsh(script_hash: *const [u8; 32]) -> Shut #[no_mangle] pub extern "C" fn ShutdownScript_new_witness_program(mut version: u8, mut program: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ShutdownScriptInvalidShutdownScriptZ { let mut ret = lightning::ln::script::ShutdownScript::new_witness_program(core::num::NonZeroU8::new(version).expect("Value must be non-zero"), program.to_slice()); - let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::script::InvalidShutdownScript { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() }; + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::script::InvalidShutdownScript { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() }; local_ret } @@ -222,7 +243,7 @@ pub extern "C" fn ShutdownScript_into_inner(mut this_arg: ShutdownScript) -> cra #[must_use] #[no_mangle] pub extern "C" fn ShutdownScript_as_legacy_pubkey(this_arg: &ShutdownScript) -> crate::c_types::PublicKey { - let mut ret = unsafe { &*this_arg.inner }.as_legacy_pubkey(); + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.as_legacy_pubkey(); let mut local_ret = if ret.is_none() { crate::c_types::PublicKey::null() } else { { crate::c_types::PublicKey::from_rust(&(ret.unwrap())) } }; local_ret } @@ -233,7 +254,7 @@ pub extern "C" fn ShutdownScript_as_legacy_pubkey(this_arg: &ShutdownScript) -> #[must_use] #[no_mangle] pub extern "C" fn ShutdownScript_is_compatible(this_arg: &ShutdownScript, features: &crate::lightning::ln::features::InitFeatures) -> bool { - let mut ret = unsafe { &*this_arg.inner }.is_compatible(unsafe { &*features.inner }); + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_compatible(features.get_native_ref()); ret }