Skip to content

Commit 88080d5

Browse files
committed
Define an OfferId when built with DerivedMetadata
TODO: Determine if there is a better way of defining the id
1 parent 5c767f0 commit 88080d5

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

lightning/src/offers/offer.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ use crate::blinded_path::BlindedPath;
9191
use crate::ln::channelmanager::PaymentId;
9292
use crate::ln::features::OfferFeatures;
9393
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
94-
use crate::ln::msgs::MAX_VALUE_MSAT;
94+
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
9595
use crate::offers::merkle::TlvStream;
9696
use crate::offers::parse::{Bech32Encode, Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
9797
use crate::offers::signer::{Metadata, MetadataMaterial, self};
98-
use crate::util::ser::{HighZeroBytesDroppedBigSize, WithoutLength, Writeable, Writer};
98+
use crate::util::ser::{HighZeroBytesDroppedBigSize, Readable, WithoutLength, Writeable, Writer};
9999
use crate::util::string::PrintableString;
100100

101101
#[cfg(not(c_bindings))]
@@ -114,6 +114,23 @@ use std::time::SystemTime;
114114

115115
pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Offer ~~~~~~";
116116

117+
/// An identifier for an [`Offer`] built using [`DerivedMetadata`].
118+
#[derive(Clone, Copy, Debug, PartialEq)]
119+
pub struct OfferId(Nonce);
120+
121+
impl Writeable for OfferId {
122+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
123+
self.0.0.write(w)
124+
}
125+
}
126+
127+
impl Readable for OfferId {
128+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
129+
let buf: [u8; Nonce::LENGTH] = Readable::read(r)?;
130+
Ok(OfferId(Nonce(buf)))
131+
}
132+
}
133+
117134
/// Builds an [`Offer`] for the "offer to be paid" flow.
118135
///
119136
/// See [module-level documentation] for usage.
@@ -123,7 +140,7 @@ pub(super) const IV_BYTES: &[u8; IV_LEN] = b"LDK Offer ~~~~~~";
123140
/// [module-level documentation]: self
124141
pub struct OfferBuilder<'a, M: MetadataStrategy, T: secp256k1::Signing> {
125142
offer: OfferContents,
126-
metadata_strategy: core::marker::PhantomData<M>,
143+
metadata_strategy: M,
127144
secp_ctx: Option<&'a Secp256k1<T>>,
128145
}
129146

@@ -151,7 +168,7 @@ pub struct OfferWithExplicitMetadataBuilder<'a> {
151168
#[cfg(c_bindings)]
152169
pub struct OfferWithDerivedMetadataBuilder<'a> {
153170
offer: OfferContents,
154-
metadata_strategy: core::marker::PhantomData<DerivedMetadata>,
171+
metadata_strategy: DerivedMetadata,
155172
secp_ctx: Option<&'a Secp256k1<secp256k1::All>>,
156173
}
157174

@@ -163,12 +180,13 @@ pub trait MetadataStrategy {}
163180
/// [`Offer::metadata`] may be explicitly set or left empty.
164181
///
165182
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
183+
#[derive(Default)]
166184
pub struct ExplicitMetadata {}
167185

168186
/// [`Offer::metadata`] will be derived.
169187
///
170188
/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
171-
pub struct DerivedMetadata {}
189+
pub struct DerivedMetadata(OfferId);
172190

173191
impl MetadataStrategy for ExplicitMetadata {}
174192

@@ -197,7 +215,7 @@ macro_rules! offer_explicit_metadata_builder_methods { (
197215
features: OfferFeatures::empty(), absolute_expiry: None, issuer: None, paths: None,
198216
supported_quantity: Quantity::One, signing_pubkey,
199217
},
200-
metadata_strategy: core::marker::PhantomData,
218+
metadata_strategy: Default::default(),
201219
secp_ctx: None,
202220
}
203221
}
@@ -236,10 +254,15 @@ macro_rules! offer_derived_metadata_builder_methods { ($secp_context: ty) => {
236254
features: OfferFeatures::empty(), absolute_expiry: None, issuer: None, paths: None,
237255
supported_quantity: Quantity::One, signing_pubkey: node_id,
238256
},
239-
metadata_strategy: core::marker::PhantomData,
257+
metadata_strategy: DerivedMetadata(OfferId(nonce)),
240258
secp_ctx: Some(secp_ctx),
241259
}
242260
}
261+
262+
/// Returns an identifier for the [`Offer`] constructed by the builder.
263+
pub fn offer_id(&self) -> OfferId {
264+
self.metadata_strategy.0
265+
}
243266
} }
244267

245268
macro_rules! offer_builder_methods { (

0 commit comments

Comments
 (0)