1use std::time::{SystemTime, UNIX_EPOCH};
4
5use chrono::{DateTime, Utc};
6use lightning_invoice::Bolt11Invoice;
7
8use crate::error::Error;
9
10#[derive(Debug, Clone)]
11pub struct ParsedInvoice {
12 pub payment_hash: String,
13 pub amount_msat: u64,
14 pub expiry_at: Option<DateTime<Utc>>,
15}
16
17pub fn parse_invoice(input: &str) -> Result<ParsedInvoice, Error> {
18 let inv: Bolt11Invoice = input.parse().map_err(|e| Error::Bolt11(format!("{e:?}")))?;
19 let payment_hash = hex::encode(inv.payment_hash());
20 let amount_msat = inv.amount_milli_satoshis().unwrap_or(0);
21 let expiry_at = inv.expires_at().and_then(|d| {
22 let secs = d.as_secs();
23 SystemTime::UNIX_EPOCH
24 .checked_add(std::time::Duration::from_secs(secs))
25 .and_then(|st| {
26 let dur = st.duration_since(UNIX_EPOCH).unwrap_or_default();
27 DateTime::<Utc>::from_timestamp(dur.as_secs() as i64, 0)
28 })
29 });
30 Ok(ParsedInvoice {
31 payment_hash,
32 amount_msat,
33 expiry_at,
34 })
35}