From df4de8c11f8fbc5856dc537c97ac4b730261fcdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loun=C3=A8s=20Ksouri?= Date: Tue, 21 Jun 2022 18:47:35 +0200 Subject: [PATCH] feat: add support for custom expiry --- proto/sensei.proto | 1 + senseicore/src/node.rs | 19 +++++++++++++++---- senseicore/src/services/node.rs | 1 + src/cli.rs | 1 + src/grpc/adaptor.rs | 1 + src/http/node.rs | 2 ++ 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/proto/sensei.proto b/proto/sensei.proto index dc70234..207b61d 100644 --- a/proto/sensei.proto +++ b/proto/sensei.proto @@ -292,6 +292,7 @@ message KeysendResponse {} message CreateInvoiceRequest { uint64 amt_msat = 1; string description = 2; + optional uint32 expiry = 3; } message CreateInvoiceResponse { string invoice = 1; diff --git a/senseicore/src/node.rs b/senseicore/src/node.rs index 403ed3b..99b4dd3 100644 --- a/senseicore/src/node.rs +++ b/senseicore/src/node.rs @@ -66,7 +66,9 @@ use lightning::util::config::UserConfig; use lightning::util::ser::ReadableArgs; use lightning_background_processor::BackgroundProcessor; use lightning_invoice::utils::DefaultRouter; -use lightning_invoice::{payment, utils, Currency, Invoice, InvoiceDescription}; +use lightning_invoice::{ + payment, utils, Currency, Invoice, InvoiceDescription, DEFAULT_EXPIRY_TIME, +}; use lightning_net_tokio::SocketDescriptor; use lightning_rapid_gossip_sync::RapidGossipSync; use macaroon::Macaroon; @@ -1185,7 +1187,12 @@ impl LightningNode { Ok(()) } - pub async fn get_invoice(&self, amt_msat: u64, description: String) -> Result { + pub async fn get_invoice( + &self, + amt_msat: u64, + description: String, + expiry: Option, + ) -> Result { let currency = match self.config.network { Network::Bitcoin => Currency::Bitcoin, Network::Testnet => Currency::BitcoinTestnet, @@ -1193,13 +1200,16 @@ impl LightningNode { Network::Signet => Currency::Signet, }; + let invoice_expiry_delta_secs: u32 = + expiry.unwrap_or_else(|| DEFAULT_EXPIRY_TIME.try_into().unwrap()); + let invoice = utils::create_invoice_from_channelmanager( &self.channel_manager, self.keys_manager.clone(), currency, Some(amt_msat), description.clone(), - 3600, // FIXME invoice_expiry_delta_secs + invoice_expiry_delta_secs, )?; let payment_hash = hex_utils::hex_str(&(*invoice.payment_hash()).into_inner()); @@ -1563,8 +1573,9 @@ impl LightningNode { NodeRequest::GetInvoice { amt_msat, description, + expiry, } => { - let invoice = self.get_invoice(amt_msat, description).await?; + let invoice = self.get_invoice(amt_msat, description, expiry).await?; let invoice_str = format!("{}", invoice); Ok(NodeResponse::GetInvoice { invoice: invoice_str, diff --git a/senseicore/src/services/node.rs b/senseicore/src/services/node.rs index dc46f63..60d473a 100644 --- a/senseicore/src/services/node.rs +++ b/senseicore/src/services/node.rs @@ -203,6 +203,7 @@ pub enum NodeRequest { GetInvoice { amt_msat: u64, description: String, + expiry: Option, }, LabelPayment { label: String, diff --git a/src/cli.rs b/src/cli.rs index 40f9cb6..0087a30 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -311,6 +311,7 @@ async fn main() -> Result<(), Box> { let request = tonic::Request::new(CreateInvoiceRequest { amt_msat, description: String::from(""), + expiry: Some(3600), }); let response = client.create_invoice(request).await?; println!("{:?}", response.into_inner()); diff --git a/src/grpc/adaptor.rs b/src/grpc/adaptor.rs index d9e81ea..07f2203 100644 --- a/src/grpc/adaptor.rs +++ b/src/grpc/adaptor.rs @@ -352,6 +352,7 @@ impl From for NodeRequest { NodeRequest::GetInvoice { amt_msat: req.amt_msat, description: req.description, + expiry: req.expiry, } } } diff --git a/src/http/node.rs b/src/http/node.rs index ede1860..0f72f4c 100644 --- a/src/http/node.rs +++ b/src/http/node.rs @@ -31,6 +31,7 @@ use super::utils::get_macaroon_hex_str_from_cookies_or_header; pub struct GetInvoiceParams { pub amt_msat: u64, pub description: String, + pub expiry: Option, } impl From for NodeRequest { @@ -38,6 +39,7 @@ impl From for NodeRequest { Self::GetInvoice { amt_msat: params.amt_msat, description: params.description, + expiry: params.expiry, } } }