Skip to content

Commit

Permalink
feat: model invoice expiry time
Browse files Browse the repository at this point in the history
  • Loading branch information
Restioson committed Sep 4, 2023
1 parent b9b26f9 commit ac259e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions mobile/lib/features/wallet/domain/wallet_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -88,6 +92,7 @@ abstract class WalletHistoryItemData {
preimage: type.paymentPreimage,
description: type.description,
paymentHash: type.paymentHash,
expiry: expiry,
invoice: type.invoice);
}
}
Expand All @@ -97,6 +102,7 @@ class LightningPaymentData extends WalletHistoryItemData {
final String? preimage;
final String description;
final String? invoice;
final DateTime? expiry;

LightningPaymentData(
{required super.flow,
Expand All @@ -106,6 +112,7 @@ class LightningPaymentData extends WalletHistoryItemData {
required this.preimage,
required this.description,
required this.invoice,
required this.expiry,
required this.paymentHash});

@override
Expand Down
6 changes: 6 additions & 0 deletions mobile/lib/features/wallet/wallet_history_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ class LightningPaymentHistoryItem extends WalletHistoryItem {
@override
List<Widget> 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,
Expand Down
1 change: 1 addition & 0 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub enum WalletHistoryItemType {
description: String,
payment_preimage: Option<String>,
invoice: Option<String>,
expiry_timestamp: Option<u64>,
},
Trade {
order_id: String,
Expand Down
18 changes: 14 additions & 4 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
};

Expand Down

0 comments on commit ac259e9

Please sign in to comment.