From cccae59db61d9d8131e369523521b2a411806b83 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 19 Jan 2024 13:01:05 +0100 Subject: [PATCH] Add `WebLN::send_payment` --- webln-js/src/lib.rs | 13 +++++++++++++ webln/src/lib.rs | 22 ++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/webln-js/src/lib.rs b/webln-js/src/lib.rs index db85bc1..0e8d46e 100644 --- a/webln-js/src/lib.rs +++ b/webln-js/src/lib.rs @@ -71,4 +71,17 @@ impl JsWebLN { .map_err(into_err)? .into()) } + + // TODO: add `make_invoice` + + // Request that the user sends a payment for an invoice. + #[wasm_bindgen(js_name = sendPayment)] + pub async fn send_payment(&self, invoice: String) -> Result { + Ok(self + .inner + .send_payment(invoice) + .await + .map_err(into_err)? + .into()) + } } diff --git a/webln/src/lib.rs b/webln/src/lib.rs index bb797f0..094fb0d 100644 --- a/webln/src/lib.rs +++ b/webln/src/lib.rs @@ -17,6 +17,8 @@ pub const IS_ENABLED: &str = "isEnabled"; pub const ENABLE: &str = "enable"; pub const GET_INFO: &str = "getInfo"; pub const KEYSEND: &str = "keysend"; +// pub const MAKE_INVOICE: &str = "makeInvoice"; +pub const SEND_PAYMENT: &str = "sendPayment"; /// WebLN error #[derive(Debug)] @@ -90,7 +92,7 @@ where GET_INFO => Self::GetInfo, KEYSEND => Self::Keysend, "makeInvoice" => Self::MakeInvoice, - "sendPayment" => Self::SendPayment, + SEND_PAYMENT => Self::SendPayment, "sendPaymentAsync" => Self::SendPaymentAsync, "signMessage" => Self::SignMessage, "verifyMessage" => Self::VerifyMessage, @@ -112,7 +114,7 @@ impl fmt::Display for GetInfoMethod { Self::GetInfo => write!(f, "{GET_INFO}"), Self::Keysend => write!(f, "{KEYSEND}"), Self::MakeInvoice => write!(f, "makeInvoice"), - Self::SendPayment => write!(f, "sendPayment"), + Self::SendPayment => write!(f, "{SEND_PAYMENT}"), Self::SendPaymentAsync => write!(f, "sendPaymentAsync"), Self::SignMessage => write!(f, "signMessage"), Self::VerifyMessage => write!(f, "verifyMessage"), @@ -264,4 +266,20 @@ impl WebLN { .ok_or_else(|| Error::TypeMismatch(String::from("expected a string [preimage]")))?, }) } + + // TODO: add `make_invoice` + + // Request that the user sends a payment for an invoice. + pub async fn send_payment(&self, invoice: String) -> Result { + let func: Function = self.get_func(&self.webln_obj, SEND_PAYMENT)?; + let promise: Promise = Promise::resolve(&func.call1(&self.webln_obj, &invoice.into())?); + let result: JsValue = JsFuture::from(promise).await?; + let send_payment_obj: Object = result.dyn_into()?; + Ok(SendPaymentResponse { + preimage: self + .get_value_by_key(&send_payment_obj, "preimage")? + .as_string() + .ok_or_else(|| Error::TypeMismatch(String::from("expected a string [preimage]")))?, + }) + } }