Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom expiry #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions proto/sensei.proto
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ message KeysendResponse {}
message CreateInvoiceRequest {
uint64 amt_msat = 1;
string description = 2;
optional uint32 expiry = 3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if we do this other places with time but I personally prefer to know the units similar to amt_msat. When I look at expiry the first thing I'll ask is if it's in milliseconds or seconds. how do you feel about naming it expiry_secs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great to me 👌

}
message CreateInvoiceResponse {
string invoice = 1;
Expand Down
19 changes: 15 additions & 4 deletions senseicore/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1185,21 +1187,29 @@ impl LightningNode {
Ok(())
}

pub async fn get_invoice(&self, amt_msat: u64, description: String) -> Result<Invoice, Error> {
pub async fn get_invoice(
&self,
amt_msat: u64,
description: String,
expiry: Option<u32>,
) -> Result<Invoice, Error> {
let currency = match self.config.network {
Network::Bitcoin => Currency::Bitcoin,
Network::Testnet => Currency::BitcoinTestnet,
Network::Regtest => Currency::Regtest,
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());
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions senseicore/src/services/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub enum NodeRequest {
GetInvoice {
amt_msat: u64,
description: String,
expiry: Option<u32>,
},
LabelPayment {
label: String,
Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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());
Expand Down
1 change: 1 addition & 0 deletions src/grpc/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ impl From<CreateInvoiceRequest> for NodeRequest {
NodeRequest::GetInvoice {
amt_msat: req.amt_msat,
description: req.description,
expiry: req.expiry,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/http/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ use super::utils::get_macaroon_hex_str_from_cookies_or_header;
pub struct GetInvoiceParams {
pub amt_msat: u64,
pub description: String,
pub expiry: Option<u32>,
}

impl From<GetInvoiceParams> for NodeRequest {
fn from(params: GetInvoiceParams) -> Self {
Self::GetInvoice {
amt_msat: params.amt_msat,
description: params.description,
expiry: params.expiry,
}
}
}
Expand Down