diff --git a/lib/ls-sdk-bindings/src/lib.rs b/lib/ls-sdk-bindings/src/lib.rs index 3330519..36d53d6 100644 --- a/lib/ls-sdk-bindings/src/lib.rs +++ b/lib/ls-sdk-bindings/src/lib.rs @@ -1 +1,81 @@ -pub mod uniffi_bindings; +use std::sync::Arc; + +use anyhow::{Error, Result}; +use ls_sdk::{ + error::PaymentError, + model::{ + Network, PrepareReceiveRequest, PrepareReceiveResponse, PrepareSendResponse, + ReceivePaymentResponse, SendPaymentResponse, WalletInfo, + }, + wallet::Wallet, +}; + +// TODO Unify error enum +#[derive(Debug, thiserror::Error)] +pub enum LsSdkError { + #[error("Error: {err}")] + Generic { err: String }, +} + +impl From for LsSdkError { + fn from(e: Error) -> Self { + LsSdkError::Generic { err: e.to_string() } + } +} + +pub fn init( + mnemonic: String, + data_dir: Option, + network: Network, +) -> Result, LsSdkError> { + let ln_sdk = Wallet::init(&mnemonic, data_dir, network)?; + Ok(Arc::from(BindingWallet { ln_sdk })) +} + +pub struct BindingWallet { + ln_sdk: Arc, +} + +impl BindingWallet { + pub fn get_info(&self, with_scan: bool) -> Result { + self.ln_sdk.get_info(with_scan).map_err(Into::into) + } + + pub fn prepare_send_payment( + &self, + invoice: String, + ) -> Result { + self.ln_sdk.prepare_send_payment(&invoice) + } + + pub fn send_payment( + &self, + req: PrepareSendResponse, + ) -> Result { + self.ln_sdk.send_payment(&req) + } + + pub fn prepare_receive_payment( + &self, + req: PrepareReceiveRequest, + ) -> Result { + self.ln_sdk.prepare_receive_payment(&req) + } + + pub fn receive_payment( + &self, + req: PrepareReceiveResponse, + ) -> Result { + self.ln_sdk.receive_payment(&req) + } + + pub fn backup(&self) -> Result<(), LsSdkError> { + self.ln_sdk.backup().map_err(Into::into) + } + + pub fn restore(&self, backup_path: Option) -> Result<(), LsSdkError> { + self.ln_sdk.restore(backup_path).map_err(Into::into) + } +} + +uniffi::include_scaffolding!("ls_sdk"); diff --git a/lib/ls-sdk-bindings/src/uniffi_bindings.rs b/lib/ls-sdk-bindings/src/uniffi_bindings.rs deleted file mode 100644 index 313d280..0000000 --- a/lib/ls-sdk-bindings/src/uniffi_bindings.rs +++ /dev/null @@ -1,81 +0,0 @@ -use std::sync::Arc; - -use anyhow::{Error, Result}; -use ls_sdk::{ - error::PaymentError, - model::{ - Network, PrepareReceiveRequest, PrepareReceiveResponse, PrepareSendResponse, - ReceivePaymentResponse, SendPaymentResponse, WalletInfo, - }, - wallet::Wallet, -}; - -// TODO Unify error enum -#[derive(Debug, thiserror::Error)] -pub enum LsSdkError { - #[error("Error: {err}")] - Generic { err: String }, -} - -impl From for LsSdkError { - fn from(e: Error) -> Self { - LsSdkError::Generic { err: e.to_string() } - } -} - -pub fn connect( - mnemonic: String, - data_dir: Option, - network: Network, -) -> Result, LsSdkError> { - let ln_sdk = Wallet::connect(&mnemonic, data_dir, network)?; - Ok(Arc::from(BindingWallet { ln_sdk })) -} - -pub struct BindingWallet { - ln_sdk: Arc, -} - -impl BindingWallet { - pub fn get_info(&self, with_scan: bool) -> Result { - self.ln_sdk.get_info(with_scan).map_err(Into::into) - } - - pub fn prepare_send_payment( - &self, - invoice: String, - ) -> Result { - self.ln_sdk.prepare_send_payment(&invoice) - } - - pub fn send_payment( - &self, - req: PrepareSendResponse, - ) -> Result { - self.ln_sdk.send_payment(&req) - } - - pub fn prepare_receive_payment( - &self, - req: PrepareReceiveRequest, - ) -> Result { - self.ln_sdk.prepare_receive_payment(&req) - } - - pub fn receive_payment( - &self, - req: PrepareReceiveResponse, - ) -> Result { - self.ln_sdk.receive_payment(&req) - } - - pub fn backup(&self) -> Result<(), LsSdkError> { - self.ln_sdk.backup().map_err(Into::into) - } - - pub fn restore(&self, backup_path: Option) -> Result<(), LsSdkError> { - self.ln_sdk.restore(backup_path).map_err(Into::into) - } -} - -uniffi::include_scaffolding!("ls_sdk");