Skip to content

Commit 5f8ad79

Browse files
committed
DRY up InvoiceFields construction
1 parent 5420d21 commit 5f8ad79

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

lightning/src/offers/invoice.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,12 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
161161
created_at: Duration, payment_hash: PaymentHash
162162
) -> Result<Self, SemanticError> {
163163
let amount_msats = Self::check_amount_msats(invoice_request)?;
164+
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
164165
let contents = InvoiceContents::ForOffer {
165166
invoice_request: invoice_request.contents.clone(),
166-
fields: InvoiceFields {
167-
payment_paths, created_at, relative_expiry: None, payment_hash, amount_msats,
168-
fallbacks: None, features: Bolt12InvoiceFeatures::empty(),
169-
signing_pubkey: invoice_request.contents.inner.offer.signing_pubkey(),
170-
},
167+
fields: Self::fields(
168+
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
169+
),
171170
};
172171

173172
Self::new(&invoice_request.bytes, contents, None)
@@ -177,13 +176,12 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
177176
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
178177
payment_hash: PaymentHash, signing_pubkey: PublicKey
179178
) -> Result<Self, SemanticError> {
179+
let amount_msats = refund.amount_msats();
180180
let contents = InvoiceContents::ForRefund {
181181
refund: refund.contents.clone(),
182-
fields: InvoiceFields {
183-
payment_paths, created_at, relative_expiry: None, payment_hash,
184-
amount_msats: refund.amount_msats(), fallbacks: None,
185-
features: Bolt12InvoiceFeatures::empty(), signing_pubkey,
186-
},
182+
fields: Self::fields(
183+
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
184+
),
187185
};
188186

189187
Self::new(&refund.bytes, contents, None)
@@ -196,13 +194,12 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
196194
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
197195
) -> Result<Self, SemanticError> {
198196
let amount_msats = Self::check_amount_msats(invoice_request)?;
197+
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
199198
let contents = InvoiceContents::ForOffer {
200199
invoice_request: invoice_request.contents.clone(),
201-
fields: InvoiceFields {
202-
payment_paths, created_at, relative_expiry: None, payment_hash, amount_msats,
203-
fallbacks: None, features: Bolt12InvoiceFeatures::empty(),
204-
signing_pubkey: invoice_request.contents.inner.offer.signing_pubkey(),
205-
},
200+
fields: Self::fields(
201+
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
202+
),
206203
};
207204

208205
Self::new(&invoice_request.bytes, contents, Some(keys))
@@ -212,13 +209,13 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
212209
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
213210
payment_hash: PaymentHash, keys: KeyPair,
214211
) -> Result<Self, SemanticError> {
212+
let amount_msats = refund.amount_msats();
213+
let signing_pubkey = keys.public_key();
215214
let contents = InvoiceContents::ForRefund {
216215
refund: refund.contents.clone(),
217-
fields: InvoiceFields {
218-
payment_paths, created_at, relative_expiry: None, payment_hash,
219-
amount_msats: refund.amount_msats(), fallbacks: None,
220-
features: Bolt12InvoiceFeatures::empty(), signing_pubkey: keys.public_key(),
221-
},
216+
fields: Self::fields(
217+
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
218+
),
222219
};
223220

224221
Self::new(&refund.bytes, contents, Some(keys))
@@ -240,6 +237,16 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
240237
}
241238
}
242239

240+
fn fields(
241+
payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
242+
payment_hash: PaymentHash, amount_msats: u64, signing_pubkey: PublicKey
243+
) -> InvoiceFields {
244+
InvoiceFields {
245+
payment_paths, created_at, relative_expiry: None, payment_hash, amount_msats,
246+
fallbacks: None, features: Bolt12InvoiceFeatures::empty(), signing_pubkey,
247+
}
248+
}
249+
243250
fn new(
244251
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents, keys: Option<KeyPair>
245252
) -> Result<Self, SemanticError> {

0 commit comments

Comments
 (0)