From 90eb956052cf58bf46b609c705c85d1ef585f78f Mon Sep 17 00:00:00 2001 From: Erdem Yerebasmaz Date: Mon, 27 May 2024 18:19:13 +0300 Subject: [PATCH] Stop using singleton instance on Dart bindings #208 --- libs/sdk-core/src/binding.rs | 816 ++++++++++++++++++----------------- 1 file changed, 414 insertions(+), 402 deletions(-) diff --git a/libs/sdk-core/src/binding.rs b/libs/sdk-core/src/binding.rs index 3e5e8cde7..db0e2e109 100644 --- a/libs/sdk-core/src/binding.rs +++ b/libs/sdk-core/src/binding.rs @@ -57,491 +57,464 @@ static NOTIFICATION_STREAM: OnceCell> = OnceCell::new(); static RT: Lazy = Lazy::new(|| tokio::runtime::Runtime::new().unwrap()); static LOG_INIT: OnceCell = OnceCell::new(); -/* Breez Services API's */ - /// Wrapper around [BreezServices::connect] which also initializes SDK logging -pub fn connect(req: ConnectRequest) -> Result<()> { - block_on(async move { - let mut locked = BREEZ_SERVICES_INSTANCE.lock().await; - match *locked { - None => { - let breez_services = - BreezServices::connect(req, Box::new(BindingEventListener {})).await?; - - *locked = Some(breez_services); - Ok(()) - } - Some(_) => Err(ConnectError::Generic { - err: "Static node services already set, please call disconnect() first".into(), - }), - } - }) - .map_err(anyhow::Error::new::) +pub fn connect(req: ConnectRequest) -> Result { + let breez_services = BreezServices::connect(req, Box::new(BindingEventListener {})) + .map_err(anyhow::Error::new::) + .await?; + Ok(BindingBreezServices { breez_services }) } -/// Check whether node service is initialized or not -pub fn is_initialized() -> bool { - block_on(async { get_breez_services().await.is_ok() }) +pub struct BindingBreezServices { + breez_services: Arc, } -/// See [BreezServices::sync] -pub fn sync() -> Result<()> { - block_on(async { get_breez_services().await?.sync().await }) - .map_err(anyhow::Error::new::) -} +/* Breez Services API's */ + +impl BindingBreezServices { + /// See [BreezServices::sync] + pub fn sync(&self) -> Result<()> { + self.breez_services + .sync() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::node_credentials] -pub fn node_credentials() -> Result> { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::node_credentials] + pub fn node_credentials(&self) -> Result> { + self.breez_services .node_credentials() .map_err(anyhow::Error::new::) - }) -} + } -/// See [BreezServices::node_info] -pub fn node_info() -> Result { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::node_info] + pub fn node_info(&self) -> Result { + self.breez_services .node_info() .map_err(anyhow::Error::new::) - }) -} - -/// See [BreezServices::configure_node] -pub fn configure_node(req: ConfigureNodeRequest) -> Result<()> { - block_on(async { get_breez_services().await?.configure_node(req).await }) - .map_err(anyhow::Error::new::) -} - -/// Cleanup node resources and stop the signer. -pub fn disconnect() -> Result<()> { - block_on(async { - // To avoid deadlock: first disconnect SDK, then acquire lock and unset global instance - get_breez_services().await?.disconnect().await?; - let mut locked_sdk_instance = BREEZ_SERVICES_INSTANCE.lock().await; - *locked_sdk_instance = None; - - Ok(()) - }) - .map_err(anyhow::Error::new::) -} - -/// See [BreezServices::sign_message] -pub fn sign_message(req: SignMessageRequest) -> Result { - block_on(async { get_breez_services().await?.sign_message(req).await }) - .map_err(anyhow::Error::new::) -} - -/// See [BreezServices::check_message] -pub fn check_message(req: CheckMessageRequest) -> Result { - block_on(async { get_breez_services().await?.check_message(req).await }) - .map_err(anyhow::Error::new::) -} - -/* Breez Services Helper API's */ - -/// See [breez_services::mnemonic_to_seed] -pub fn mnemonic_to_seed(phrase: String) -> Result> { - breez_services::mnemonic_to_seed(phrase) -} - -/// See [BreezServices::default_config] -pub fn default_config( - env_type: EnvironmentType, - api_key: String, - node_config: NodeConfig, -) -> Config { - BreezServices::default_config(env_type, api_key, node_config) -} - -/// See [BreezServices::static_backup] -pub fn static_backup(req: StaticBackupRequest) -> Result { - BreezServices::static_backup(req).map_err(anyhow::Error::new::) -} + } -/// See [BreezServices::service_health_check] -pub fn service_health_check(api_key: String) -> Result { - block_on(async { BreezServices::service_health_check(api_key).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::configure_node] + pub fn configure_node(&self, req: ConfigureNodeRequest) -> Result<()> { + self.breez_services + .configure_node(req) + .await + .map_err(anyhow::Error::new::) + } -/* Stream API's */ + /// Cleanup node resources and stop the signer. + pub fn disconnect(&self) -> Result<()> { + self.breez_services + .disconnect() + .await + .map_err(anyhow::Error::new::) + } -/// If used, this must be called before `connect`. It can only be called once. -pub fn breez_events_stream(s: StreamSink) -> Result<()> { - NOTIFICATION_STREAM - .set(s) - .map_err(|_| anyhow!("Events stream already created"))?; - Ok(()) -} + /// See [BreezServices::sign_message] + pub fn sign_message(&self, req: SignMessageRequest) -> Result { + self.breez_services + .sign_message(req) + .await + .map_err(anyhow::Error::new::) + } -/// If used, this must be called before `connect`. It can only be called once. -pub fn breez_log_stream(s: StreamSink) -> Result<()> { - LOG_INIT - .set(true) - .map_err(|_| anyhow!("Log stream already created"))?; - BindingLogger::init(s); - Ok(()) -} + /// See [BreezServices::check_message] + pub fn check_message(&self, req: CheckMessageRequest) -> Result { + self.breez_services + .check_message(req) + .await + .map_err(anyhow::Error::new::) + } -/* LSP API's */ + /* LSP API's */ -/// See [BreezServices::list_lsps] -pub fn list_lsps() -> Result> { - block_on(async { get_breez_services().await?.list_lsps().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::list_lsps] + pub fn list_lsps(&self) -> Result> { + self.breez_services + .list_lsps() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::connect_lsp] -pub fn connect_lsp(lsp_id: String) -> Result<()> { - block_on(async { get_breez_services().await?.connect_lsp(lsp_id).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::connect_lsp] + pub fn connect_lsp(&self, lsp_id: String) -> Result<()> { + self.breez_services + .connect_lsp(lsp_id) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::lsp_id] -pub fn lsp_id() -> Result> { - block_on(async { get_breez_services().await?.lsp_id().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::lsp_id] + pub fn lsp_id(&self) -> Result> { + self.breez_services + .lsp_id() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::fetch_lsp_info] -pub fn fetch_lsp_info(id: String) -> Result> { - block_on(async { get_breez_services().await?.fetch_lsp_info(id).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::fetch_lsp_info] + pub fn fetch_lsp_info(&self, id: String) -> Result> { + self.breez_services + .fetch_lsp_info(id) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::lsp_info] -pub fn lsp_info() -> Result { - block_on(async { get_breez_services().await?.lsp_info().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::lsp_info] + pub fn lsp_info(&self) -> Result { + self.breez_services + .lsp_info() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::close_lsp_channels] -pub fn close_lsp_channels() -> Result<()> { - block_on(async { - _ = get_breez_services().await?.close_lsp_channels().await?; + /// See [BreezServices::close_lsp_channels] + pub fn close_lsp_channels(&self) -> Result<()> { + self.breez_services.close_lsp_channels().await?; Ok(()) - }) -} + } -pub fn register_webhook(webhook_url: String) -> Result<()> { - block_on(async { - get_breez_services() - .await? + pub fn register_webhook(&self, webhook_url: String) -> Result<()> { + self.breez_services .register_webhook(webhook_url) .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -pub fn unregister_webhook(webhook_url: String) -> Result<()> { - block_on(async { - get_breez_services() - .await? + pub fn unregister_webhook(&self, webhook_url: String) -> Result<()> { + self.breez_services .unregister_webhook(webhook_url) .await - }) - .map_err(anyhow::Error::new::) -} - -/* Backup API's */ - -/// See [BreezServices::backup] -pub fn backup() -> Result<()> { - block_on(async { get_breez_services().await?.backup().await }) - .map_err(anyhow::Error::new::) -} - -/// See [BreezServices::backup_status] -pub fn backup_status() -> Result { - block_on(async { get_breez_services().await?.backup_status() }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/* Parse API's */ + /* Backup API's */ -pub fn parse_invoice(invoice: String) -> Result { - invoice::parse_invoice(&invoice).map_err(|e| anyhow::Error::new::(e.into())) -} + /// See [BreezServices::backup] + pub fn backup(&self) -> Result<()> { + self.breez_services + .backup() + .await + .map_err(anyhow::Error::new::) + } -pub fn parse_input(input: String) -> Result { - block_on(async { input_parser::parse(&input).await }) -} + /// See [BreezServices::backup_status] + pub fn backup_status(&self) -> Result { + self.breez_services + .backup_status() + .map_err(anyhow::Error::new::) + } -/* Payment API's */ + /* Payment API's */ -/// See [BreezServices::list_payments] -pub fn list_payments(req: ListPaymentsRequest) -> Result> { - block_on(async { get_breez_services().await?.list_payments(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::list_payments] + pub fn list_payments(&self, req: ListPaymentsRequest) -> Result> { + self.breez_services + .list_payments(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::list_payments] -pub fn payment_by_hash(hash: String) -> Result> { - block_on(async { get_breez_services().await?.payment_by_hash(hash).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::list_payments] + pub fn payment_by_hash(&self, hash: String) -> Result> { + self.breez_services + .payment_by_hash(hash) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::set_payment_metadata] -pub fn set_payment_metadata(hash: String, metadata: String) -> Result<()> { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::set_payment_metadata] + pub fn set_payment_metadata(&self, hash: String, metadata: String) -> Result<()> { + self.breez_services .set_payment_metadata(hash, metadata) .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/* Lightning Payment API's */ + /* Lightning Payment API's */ -/// See [BreezServices::send_payment] -pub fn send_payment(req: SendPaymentRequest) -> Result { - block_on(async { get_breez_services().await?.send_payment(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::send_payment] + pub fn send_payment(&self, req: SendPaymentRequest) -> Result { + self.breez_services + .send_payment(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::send_spontaneous_payment] -pub fn send_spontaneous_payment(req: SendSpontaneousPaymentRequest) -> Result { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::send_spontaneous_payment] + pub fn send_spontaneous_payment( + &self, + req: SendSpontaneousPaymentRequest, + ) -> Result { + self.breez_services .send_spontaneous_payment(req) .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::receive_payment] -pub fn receive_payment(req: ReceivePaymentRequest) -> Result { - block_on(async { get_breez_services().await?.receive_payment(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::receive_payment] + pub fn receive_payment(&self, req: ReceivePaymentRequest) -> Result { + self.breez_services + .receive_payment(req) + .await + .map_err(anyhow::Error::new::) + } -/* LNURL API's */ + /* LNURL API's */ -/// See [BreezServices::lnurl_pay] -pub fn lnurl_pay(req: LnUrlPayRequest) -> Result { - block_on(async { get_breez_services().await?.lnurl_pay(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::lnurl_pay] + pub fn lnurl_pay(&self, req: LnUrlPayRequest) -> Result { + self.breez_services + .lnurl_pay(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::lnurl_withdraw] -pub fn lnurl_withdraw(req: LnUrlWithdrawRequest) -> Result { - block_on(async { get_breez_services().await?.lnurl_withdraw(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::lnurl_withdraw] + pub fn lnurl_withdraw(&self, req: LnUrlWithdrawRequest) -> Result { + self.breez_services + .lnurl_withdraw(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::lnurl_auth] -pub fn lnurl_auth(req_data: LnUrlAuthRequestData) -> Result { - block_on(async { get_breez_services().await?.lnurl_auth(req_data).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::lnurl_auth] + pub fn lnurl_auth(&self, req_data: LnUrlAuthRequestData) -> Result { + self.breez_services + .lnurl_auth(req_data) + .await + .map_err(anyhow::Error::new::) + } -/* Support API */ + /* Support API */ -/// See [BreezServices::report_issue] -pub fn report_issue(req: ReportIssueRequest) -> Result<()> { - block_on(async { get_breez_services().await?.report_issue(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::report_issue] + pub fn report_issue(&self, req: ReportIssueRequest) -> Result<()> { + self.breez_services + .report_issue(req) + .await + .map_err(anyhow::Error::new::) + } -/* Fiat Currency API's */ + /* Fiat Currency API's */ -/// See [BreezServices::fetch_fiat_rates] -pub fn fetch_fiat_rates() -> Result> { - block_on(async { get_breez_services().await?.fetch_fiat_rates().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::fetch_fiat_rates] + pub fn fetch_fiat_rates(&self) -> Result> { + self.breez_services + .fetch_fiat_rates() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::list_fiat_currencies] -pub fn list_fiat_currencies() -> Result> { - block_on(async { get_breez_services().await?.list_fiat_currencies().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::list_fiat_currencies] + pub fn list_fiat_currencies(&self) -> Result> { + self.breez_services + .list_fiat_currencies() + .await + .map_err(anyhow::Error::new::) + } -/* On-Chain Swap API's */ + /* On-Chain Swap API's */ -/// See [BreezServices::max_reverse_swap_amount] -pub fn max_reverse_swap_amount() -> Result { - #[allow(deprecated)] - block_on(async { get_breez_services().await?.max_reverse_swap_amount().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::max_reverse_swap_amount] + pub fn max_reverse_swap_amount(&self) -> Result { + #[allow(deprecated)] + self.breez_services + .max_reverse_swap_amount() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::send_onchain] -pub fn send_onchain(req: SendOnchainRequest) -> Result { - #[allow(deprecated)] - block_on(async { get_breez_services().await?.send_onchain(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::send_onchain] + pub fn send_onchain(&self, req: SendOnchainRequest) -> Result { + #[allow(deprecated)] + self.breez_services + .send_onchain(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::pay_onchain] -pub fn pay_onchain(req: PayOnchainRequest) -> Result { - block_on(async { get_breez_services().await?.pay_onchain(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::pay_onchain] + pub fn pay_onchain(&self, req: PayOnchainRequest) -> Result { + self.breez_services + .pay_onchain(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::receive_onchain] -pub fn receive_onchain(req: ReceiveOnchainRequest) -> Result { - block_on(async { get_breez_services().await?.receive_onchain(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::receive_onchain] + pub fn receive_onchain(&self, req: ReceiveOnchainRequest) -> Result { + self.breez_services + .receive_onchain(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::buy_bitcoin] -pub fn buy_bitcoin(req: BuyBitcoinRequest) -> Result { - block_on(async { get_breez_services().await?.buy_bitcoin(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::buy_bitcoin] + pub fn buy_bitcoin(&self, req: BuyBitcoinRequest) -> Result { + self.breez_services + .buy_bitcoin(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::redeem_onchain_funds] -pub fn redeem_onchain_funds(req: RedeemOnchainFundsRequest) -> Result { - block_on(async { get_breez_services().await?.redeem_onchain_funds(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::redeem_onchain_funds] + pub fn redeem_onchain_funds( + &self, + req: RedeemOnchainFundsRequest, + ) -> Result { + self.breez_services + .redeem_onchain_funds(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::prepare_redeem_onchain_funds] -pub fn prepare_redeem_onchain_funds( - req: PrepareRedeemOnchainFundsRequest, -) -> Result { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::prepare_redeem_onchain_funds] + pub fn prepare_redeem_onchain_funds( + &self, + req: PrepareRedeemOnchainFundsRequest, + ) -> Result { + self.breez_services .prepare_redeem_onchain_funds(req) .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/* Refundables API's */ + /* Refundables API's */ -/// See [BreezServices::list_refundables] -pub fn list_refundables() -> Result> { - block_on(async { get_breez_services().await?.list_refundables().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::list_refundables] + pub fn list_refundables(&self) -> Result> { + self.breez_services + .list_refundables() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::prepare_refund] -pub fn prepare_refund(req: PrepareRefundRequest) -> Result { - block_on(async { get_breez_services().await?.prepare_refund(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::prepare_refund] + pub fn prepare_refund(&self, req: PrepareRefundRequest) -> Result { + self.breez_services + .prepare_refund(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::refund] -pub fn refund(req: RefundRequest) -> Result { - block_on(async { get_breez_services().await?.refund(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::refund] + pub fn refund(&self, req: RefundRequest) -> Result { + self.breez_services + .refund(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::rescan_swaps] -pub fn rescan_swaps() -> Result<()> { - block_on(async { get_breez_services().await?.rescan_swaps().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::rescan_swaps] + pub fn rescan_swaps(&self) -> Result<()> { + self.breez_services + .rescan_swaps() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::redeem_swap] -pub fn redeem_swap(swap_address: String) -> Result<()> { - block_on(async { get_breez_services().await?.redeem_swap(swap_address).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::redeem_swap] + pub fn redeem_swap(&self, swap_address: String) -> Result<()> { + self.breez_services + .redeem_swap(swap_address) + .await + .map_err(anyhow::Error::new::) + } -/* In Progress Swap API's */ + /* In Progress Swap API's */ -/// See [BreezServices::in_progress_swap] -pub fn in_progress_swap() -> Result> { - block_on(async { get_breez_services().await?.in_progress_swap().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::in_progress_swap] + pub fn in_progress_swap(&self) -> Result> { + self.breez_services + .in_progress_swap() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::in_progress_reverse_swaps] -pub fn in_progress_reverse_swaps() -> Result> { - #[allow(deprecated)] - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::in_progress_reverse_swaps] + pub fn in_progress_reverse_swaps(&self) -> Result> { + #[allow(deprecated)] + self.breez_services .in_progress_reverse_swaps() .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/* Swap Fee API's */ + /* Swap Fee API's */ -/// See [BreezServices::open_channel_fee] -pub fn open_channel_fee(req: OpenChannelFeeRequest) -> Result { - block_on(async { get_breez_services().await?.open_channel_fee(req).await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::open_channel_fee] + pub fn open_channel_fee(&self, req: OpenChannelFeeRequest) -> Result { + self.breez_services + .open_channel_fee(req) + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::fetch_reverse_swap_fees] -pub fn fetch_reverse_swap_fees(req: ReverseSwapFeesRequest) -> Result { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::fetch_reverse_swap_fees] + pub fn fetch_reverse_swap_fees( + &self, + req: ReverseSwapFeesRequest, + ) -> Result { + self.breez_services .fetch_reverse_swap_fees(req) .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::onchain_payment_limits] -pub fn onchain_payment_limits() -> Result { - block_on(async { get_breez_services().await?.onchain_payment_limits().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::onchain_payment_limits] + pub fn onchain_payment_limits(&self) -> Result { + self.breez_services + .onchain_payment_limits() + .await + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::prepare_onchain_payment] -pub fn prepare_onchain_payment( - req: PrepareOnchainPaymentRequest, -) -> Result { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::prepare_onchain_payment] + pub fn prepare_onchain_payment( + &self, + req: PrepareOnchainPaymentRequest, + ) -> Result { + self.breez_services .prepare_onchain_payment(req) .await .map_err(anyhow::Error::new::) - }) -} + } -/// See [BreezServices::in_progress_onchain_payments] -pub fn in_progress_onchain_payments() -> Result> { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::in_progress_onchain_payments] + pub fn in_progress_onchain_payments(&self) -> Result> { + self.breez_services .in_progress_onchain_payments() .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::recommended_fees] -pub fn recommended_fees() -> Result { - block_on(async { get_breez_services().await?.recommended_fees().await }) - .map_err(anyhow::Error::new::) -} + /// See [BreezServices::recommended_fees] + pub fn recommended_fees(&self) -> Result { + self.breez_services + .recommended_fees() + .await + .map_err(anyhow::Error::new::) + } -/* CLI API's */ + /* CLI API's */ -/// See [BreezServices::execute_dev_command] -pub fn execute_command(command: String) -> Result { - block_on(async { - get_breez_services() - .await? + /// See [BreezServices::execute_dev_command] + pub fn execute_command(&self, command: String) -> Result { + self.breez_services .execute_dev_command(command) .await - }) - .map_err(anyhow::Error::new::) -} + .map_err(anyhow::Error::new::) + } -/// See [BreezServices::generate_diagnostic_data] -pub fn generate_diagnostic_data() -> Result { - block_on(async { get_breez_services().await?.generate_diagnostic_data().await }) - .map_err(anyhow::Error::new::) + /// See [BreezServices::generate_diagnostic_data] + pub fn generate_diagnostic_data(&self) -> Result { + self.breez_services + .generate_diagnostic_data() + .await + .map_err(anyhow::Error::new::) + } } - /* Binding Related Logic */ struct BindingEventListener; @@ -582,19 +555,58 @@ impl log::Log for BindingLogger { fn flush(&self) {} } -async fn get_breez_services() -> Result, SdkError> { - match BREEZ_SERVICES_INSTANCE.lock().await.as_ref() { - None => Err(SdkError::Generic { - err: "Node service was not initialized".into(), - }), - Some(sdk) => Ok(sdk.clone()), - } +/* Parse API's */ + +pub fn parse_invoice(invoice: String) -> Result { + invoice::parse_invoice(&invoice).map_err(|e| anyhow::Error::new::(e.into())) +} + +pub fn parse_input(input: String) -> Result { + input_parser::parse(&input) +} + +/* Breez Services Helper API's */ + +/// See [breez_services::mnemonic_to_seed] +pub fn mnemonic_to_seed(&self, phrase: String) -> Result> { + breez_services::mnemonic_to_seed(phrase) +} + +/// See [BreezServices::default_config] +pub fn default_config( + &self, + env_type: EnvironmentType, + api_key: String, + node_config: NodeConfig, +) -> Config { + BreezServices::default_config(env_type, api_key, node_config) } -fn block_on(future: F) -> F::Output { - rt().block_on(future) +/// See [BreezServices::static_backup] +pub fn static_backup(req: StaticBackupRequest) -> Result { + BreezServices::static_backup(req).map_err(anyhow::Error::new::) +} + +/// See [BreezServices::service_health_check] +pub fn service_health_check(&self, api_key: String) -> Result { + BreezServices::service_health_check(api_key).map_err(anyhow::Error::new::) } -pub(crate) fn rt() -> &'static tokio::runtime::Runtime { - &RT +/* Stream API's */ + +/// If used, this must be called before `connect`. It can only be called once. +pub fn breez_events_stream(s: StreamSink) -> Result<()> { + NOTIFICATION_STREAM + .set(s) + .map_err(|_| anyhow!("Events stream already created"))?; + Ok(()) +} + +/// If used, this must be called before `connect`. It can only be called once. +pub fn breez_log_stream(s: StreamSink) -> Result<()> { + LOG_INIT + .set(true) + .map_err(|_| anyhow!("Log stream already created"))?; + BindingLogger::init(s); + Ok(()) }