From 5f4b0e55ca931062b0884a1cd79a82d8e9bb4c43 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Tue, 23 Jan 2024 18:14:57 +0100 Subject: [PATCH] Add `WebLN::send_multi_payment` --- webln-js/src/lib.rs | 11 +++++++++++ webln/src/lib.rs | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/webln-js/src/lib.rs b/webln-js/src/lib.rs index 87c7560..8e1e7e5 100644 --- a/webln-js/src/lib.rs +++ b/webln-js/src/lib.rs @@ -8,6 +8,8 @@ extern crate alloc; +use alloc::string::String; +use alloc::vec::Vec; use core::ops::Deref; use wasm_bindgen::prelude::*; @@ -115,6 +117,15 @@ impl JsWebLN { .into()) } + /// Request that the user sends multiple payments. + #[wasm_bindgen(js_name = sendMultiPayment)] + pub async fn send_multi_payment(&self, invoices: Vec) -> Result<()> { + self.inner + .send_multi_payment(invoices) + .await + .map_err(into_err) + } + /// Request that the user sends a payment for an invoice. /// The payment will only be initiated and will not wait for a preimage to be returned. /// This is useful when paying HOLD Invoices. There is no guarantee that the payment will be successfully sent to the receiver. diff --git a/webln/src/lib.rs b/webln/src/lib.rs index bc1fef5..d17ba08 100644 --- a/webln/src/lib.rs +++ b/webln/src/lib.rs @@ -31,6 +31,7 @@ const GET_INFO: &str = "getInfo"; const KEYSEND: &str = "keysend"; const MAKE_INVOICE: &str = "makeInvoice"; const SEND_PAYMENT: &str = "sendPayment"; +const SEND_MULTI_PAYMENT: &str = "sendMultiPayment"; const SEND_PAYMENT_ASYNC: &str = "sendPaymentAsync"; const SIGN_MESSAGE: &str = "signMessage"; const VERIFY_MESSAGE: &str = "verifyMessage"; @@ -111,6 +112,7 @@ pub enum GetInfoMethod { Keysend, MakeInvoice, SendPayment, + SendMultiPayment, SendPaymentAsync, SignMessage, VerifyMessage, @@ -131,6 +133,7 @@ impl From<&str> for GetInfoMethod { KEYSEND => Self::Keysend, MAKE_INVOICE => Self::MakeInvoice, SEND_PAYMENT => Self::SendPayment, + SEND_MULTI_PAYMENT => Self::SendMultiPayment, SEND_PAYMENT_ASYNC => Self::SendPaymentAsync, SIGN_MESSAGE => Self::SignMessage, VERIFY_MESSAGE => Self::VerifyMessage, @@ -153,6 +156,7 @@ impl fmt::Display for GetInfoMethod { Self::Keysend => write!(f, "{KEYSEND}"), Self::MakeInvoice => write!(f, "{MAKE_INVOICE}"), Self::SendPayment => write!(f, "{SEND_PAYMENT}"), + Self::SendMultiPayment => write!(f, "{SEND_MULTI_PAYMENT}"), Self::SendPaymentAsync => write!(f, "{SEND_PAYMENT_ASYNC}"), Self::SignMessage => write!(f, "{SIGN_MESSAGE}"), Self::VerifyMessage => write!(f, "{VERIFY_MESSAGE}"), @@ -483,6 +487,25 @@ impl WebLN { }) } + /// Request that the user sends multiple payments. + pub async fn send_multi_payment(&self, invoices: I) -> Result<(), Error> + where + I: IntoIterator, + S: AsRef, + { + let invoices: Array = invoices + .into_iter() + .map(|i| JsValue::from_str(i.as_ref())) + .collect(); + let func: Function = self.get_func(&self.webln_obj, SEND_MULTI_PAYMENT)?; + let promise: Promise = Promise::resolve(&func.call1(&self.webln_obj, &invoices.into())?); + let result: JsValue = JsFuture::from(promise).await?; + let _send_multi_payment_obj: Object = + result.dyn_into().map_err(|_| Error::SomethingGoneWrong)?; + // TODO: deserialize response + Ok(()) + } + /// Request that the user sends a payment for an invoice. /// The payment will only be initiated and will not wait for a preimage to be returned. /// This is useful when paying HOLD Invoices. There is no guarantee that the payment will be successfully sent to the receiver.