From 6f12a4a2b041dd0f4dc7b6c6a44a0e0ce26229a1 Mon Sep 17 00:00:00 2001 From: Restioson Date: Mon, 28 Aug 2023 16:48:56 +0200 Subject: [PATCH] feat: model invoice expiry time --- .../features/wallet/domain/wallet_history.dart | 7 +++++++ .../features/wallet/wallet_history_item.dart | 4 ++++ mobile/native/src/api.rs | 1 + mobile/native/src/ln_dlc/mod.rs | 18 ++++++++++++++---- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/mobile/lib/features/wallet/domain/wallet_history.dart b/mobile/lib/features/wallet/domain/wallet_history.dart index de9ce456a..49edb041e 100644 --- a/mobile/lib/features/wallet/domain/wallet_history.dart +++ b/mobile/lib/features/wallet/domain/wallet_history.dart @@ -80,6 +80,10 @@ abstract class WalletHistoryItemData { rust.WalletHistoryItemType_Lightning type = item.walletType as rust.WalletHistoryItemType_Lightning; + DateTime? expiry = type.expiryTimestamp != null + ? DateTime.fromMillisecondsSinceEpoch(type.expiryTimestamp! * 1000) + : null; + return LightningPaymentData( flow: flow, amount: amount, @@ -88,6 +92,7 @@ abstract class WalletHistoryItemData { preimage: type.paymentPreimage, description: type.description, paymentHash: type.paymentHash, + expiry: expiry, invoice: type.invoice); } } @@ -97,6 +102,7 @@ class LightningPaymentData extends WalletHistoryItemData { final String? preimage; final String description; final String? invoice; + final DateTime? expiry; LightningPaymentData( {required super.flow, @@ -106,6 +112,7 @@ class LightningPaymentData extends WalletHistoryItemData { required this.preimage, required this.description, required this.invoice, + required this.expiry, required this.paymentHash}); @override diff --git a/mobile/lib/features/wallet/wallet_history_item.dart b/mobile/lib/features/wallet/wallet_history_item.dart index 306ab457a..03f3387cb 100644 --- a/mobile/lib/features/wallet/wallet_history_item.dart +++ b/mobile/lib/features/wallet/wallet_history_item.dart @@ -167,6 +167,10 @@ class LightningPaymentHistoryItem extends WalletHistoryItem { @override List getDetails() { return [ + Visibility( + visible: data.expiry != null, + child: HistoryDetail(label: "Expiry time", value: WalletHistoryItem.dateFormat.format(data.expiry ?? DateTime.utc(0))), + ), HistoryDetail(label: "Invoice description", value: data.description), Visibility( visible: data.invoice != null, diff --git a/mobile/native/src/api.rs b/mobile/native/src/api.rs index 65be218b1..788d38151 100644 --- a/mobile/native/src/api.rs +++ b/mobile/native/src/api.rs @@ -85,6 +85,7 @@ pub enum WalletHistoryItemType { description: String, payment_preimage: Option, invoice: Option, + expiry_timestamp: Option, }, Trade { order_id: String, diff --git a/mobile/native/src/ln_dlc/mod.rs b/mobile/native/src/ln_dlc/mod.rs index cf21f3c39..4435a4598 100644 --- a/mobile/native/src/ln_dlc/mod.rs +++ b/mobile/native/src/ln_dlc/mod.rs @@ -347,18 +347,23 @@ fn keep_wallet_balance_and_history_up_to_date(node: &Node) -> Result<()> { None => return None, }; - let expired = match details.invoice.as_deref().map(Invoice::from_str) { + let decoded_invoice = match details.invoice.as_deref().map(Invoice::from_str) { Some(Ok(inv)) => { tracing::info!(?inv, "Decoded invoice"); - inv.is_expired() + Some(inv) } Some(Err(err)) => { tracing::warn!(%err, "Failed to deserialize invoice"); - false + None } - None => false, + None => None, }; + let expired = decoded_invoice + .as_ref() + .map(|inv| inv.is_expired()) + .unwrap_or(false); + let status = match details.status { HTLCStatus::Pending if expired => api::Status::Expired, HTLCStatus::Pending => api::Status::Pending, @@ -391,11 +396,16 @@ fn keep_wallet_balance_and_history_up_to_date(node: &Node) -> Result<()> { payment_hash, } } else { + let expiry_timestamp = decoded_invoice + .and_then(|inv| inv.timestamp().checked_add(inv.expiry_time())) + .map(|time| OffsetDateTime::from(time).unix_timestamp() as u64); + api::WalletHistoryItemType::Lightning { payment_hash, description: details.description.clone(), payment_preimage: details.preimage.clone(), invoice: details.invoice.clone(), + expiry_timestamp, } };