|
| 1 | +// This file is Copyright its original authors, visible in version control history. |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 | +// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in |
| 6 | +// accordance with one or both of these licenses. |
| 7 | + |
| 8 | +use lightning::impl_writeable_tlv_based; |
| 9 | +use lightning::impl_writeable_tlv_based_enum; |
| 10 | +use lightning::ln::channelmanager::PaymentId; |
| 11 | +use lightning::offers::offer::Offer as LdkOffer; |
| 12 | +use lightning::offers::refund::Refund as LdkRefund; |
| 13 | +use lightning_invoice::Bolt11Invoice as LdkBolt11Invoice; |
| 14 | + |
| 15 | +use crate::data_store::{StorableObject, StorableObjectId, StorableObjectUpdate}; |
| 16 | +use crate::hex_utils; |
| 17 | +use crate::payment::store::LSPFeeLimits; |
| 18 | + |
| 19 | +/// An opaque identifier for a payment metadata entry. |
| 20 | +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] |
| 21 | +pub(crate) struct MetadataId { |
| 22 | + pub id: [u8; 32], |
| 23 | +} |
| 24 | + |
| 25 | +impl StorableObjectId for MetadataId { |
| 26 | + fn encode_to_hex_str(&self) -> String { |
| 27 | + hex_utils::to_string(&self.id) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl_writeable_tlv_based!(MetadataId, { (0, id, required) }); |
| 32 | + |
| 33 | +/// The kind of metadata stored in a [`PaymentMetadataEntry`]. |
| 34 | +#[derive(Clone, Debug)] |
| 35 | +pub(crate) enum PaymentMetadataKind { |
| 36 | + /// A BOLT 11 invoice. |
| 37 | + Bolt11Invoice { invoice: LdkBolt11Invoice }, |
| 38 | + /// A BOLT 12 offer. |
| 39 | + Bolt12Offer { offer: LdkOffer }, |
| 40 | + /// A BOLT 12 refund. |
| 41 | + Bolt12Refund { refund: LdkRefund }, |
| 42 | + /// LSP fee limits for a JIT channel payment. |
| 43 | + LSPFeeLimits { limits: LSPFeeLimits }, |
| 44 | +} |
| 45 | + |
| 46 | +impl_writeable_tlv_based_enum!(PaymentMetadataKind, |
| 47 | + (0, Bolt11Invoice) => { |
| 48 | + (0, invoice, required), |
| 49 | + }, |
| 50 | + (2, Bolt12Offer) => { |
| 51 | + (0, offer, required), |
| 52 | + }, |
| 53 | + (4, Bolt12Refund) => { |
| 54 | + (0, refund, required), |
| 55 | + }, |
| 56 | + (6, LSPFeeLimits) => { |
| 57 | + (0, limits, required), |
| 58 | + } |
| 59 | +); |
| 60 | + |
| 61 | +/// A metadata entry associating a [`PaymentMetadataKind`] with one or more payments. |
| 62 | +#[derive(Clone, Debug)] |
| 63 | +pub(crate) struct PaymentMetadataEntry { |
| 64 | + /// The unique identifier for this metadata entry. |
| 65 | + pub id: MetadataId, |
| 66 | + /// The kind of metadata. |
| 67 | + pub kind: PaymentMetadataKind, |
| 68 | + /// The payment IDs associated with this metadata. |
| 69 | + pub payment_ids: Vec<PaymentId>, |
| 70 | +} |
| 71 | + |
| 72 | +impl_writeable_tlv_based!(PaymentMetadataEntry, { |
| 73 | + (0, id, required), |
| 74 | + (2, kind, required), |
| 75 | + (4, payment_ids, optional_vec), |
| 76 | +}); |
| 77 | + |
| 78 | +/// An update to a [`PaymentMetadataEntry`]. |
| 79 | +#[derive(Clone, Debug)] |
| 80 | +pub(crate) struct PaymentMetadataEntryUpdate { |
| 81 | + pub id: MetadataId, |
| 82 | + pub payment_ids: Option<Vec<PaymentId>>, |
| 83 | +} |
| 84 | + |
| 85 | +impl StorableObject for PaymentMetadataEntry { |
| 86 | + type Id = MetadataId; |
| 87 | + type Update = PaymentMetadataEntryUpdate; |
| 88 | + |
| 89 | + fn id(&self) -> Self::Id { |
| 90 | + self.id |
| 91 | + } |
| 92 | + |
| 93 | + fn update(&mut self, update: Self::Update) -> bool { |
| 94 | + let mut updated = false; |
| 95 | + |
| 96 | + if let Some(new_payment_ids) = update.payment_ids { |
| 97 | + if self.payment_ids != new_payment_ids { |
| 98 | + self.payment_ids = new_payment_ids; |
| 99 | + updated = true; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + updated |
| 104 | + } |
| 105 | + |
| 106 | + fn to_update(&self) -> Self::Update { |
| 107 | + PaymentMetadataEntryUpdate { id: self.id, payment_ids: Some(self.payment_ids.clone()) } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl StorableObjectUpdate<PaymentMetadataEntry> for PaymentMetadataEntryUpdate { |
| 112 | + fn id(&self) -> <PaymentMetadataEntry as StorableObject>::Id { |
| 113 | + self.id |
| 114 | + } |
| 115 | +} |
0 commit comments