Skip to content

Commit

Permalink
Add WebLN::send_multi_payment
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jan 23, 2024
1 parent 2b1a0f6 commit 5f4b0e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions webln-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

extern crate alloc;

use alloc::string::String;
use alloc::vec::Vec;
use core::ops::Deref;

use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -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<String>) -> 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.
Expand Down
23 changes: 23 additions & 0 deletions webln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -111,6 +112,7 @@ pub enum GetInfoMethod {
Keysend,
MakeInvoice,
SendPayment,
SendMultiPayment,
SendPaymentAsync,
SignMessage,
VerifyMessage,
Expand All @@ -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,
Expand All @@ -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}"),
Expand Down Expand Up @@ -483,6 +487,25 @@ impl WebLN {
})
}

/// Request that the user sends multiple payments.
pub async fn send_multi_payment<I, S>(&self, invoices: I) -> Result<(), Error>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
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.
Expand Down

0 comments on commit 5f4b0e5

Please sign in to comment.