Skip to content

Commit

Permalink
DRY up InvoiceFields construction
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Apr 18, 2023
1 parent 07cc40a commit 0d7f06b
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
created_at: Duration, payment_hash: PaymentHash
) -> Result<Self, SemanticError> {
let amount_msats = Self::check_amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: InvoiceFields {
payment_paths, created_at, relative_expiry: None, payment_hash, amount_msats,
fallbacks: None, features: Bolt12InvoiceFeatures::empty(),
signing_pubkey: invoice_request.contents.inner.offer.signing_pubkey(),
},
fields: Self::fields(
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
),
};

Self::new(&invoice_request.bytes, contents, None)
Expand All @@ -177,13 +176,12 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
payment_hash: PaymentHash, signing_pubkey: PublicKey
) -> Result<Self, SemanticError> {
let amount_msats = refund.amount_msats();
let contents = InvoiceContents::ForRefund {
refund: refund.contents.clone(),
fields: InvoiceFields {
payment_paths, created_at, relative_expiry: None, payment_hash,
amount_msats: refund.amount_msats(), fallbacks: None,
features: Bolt12InvoiceFeatures::empty(), signing_pubkey,
},
fields: Self::fields(
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
),
};

Self::new(&refund.bytes, contents, None)
Expand All @@ -196,13 +194,12 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
) -> Result<Self, SemanticError> {
let amount_msats = Self::check_amount_msats(invoice_request)?;
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
let contents = InvoiceContents::ForOffer {
invoice_request: invoice_request.contents.clone(),
fields: InvoiceFields {
payment_paths, created_at, relative_expiry: None, payment_hash, amount_msats,
fallbacks: None, features: Bolt12InvoiceFeatures::empty(),
signing_pubkey: invoice_request.contents.inner.offer.signing_pubkey(),
},
fields: Self::fields(
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
),
};

Self::new(&invoice_request.bytes, contents, Some(keys))
Expand All @@ -212,13 +209,13 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
refund: &'a Refund, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
payment_hash: PaymentHash, keys: KeyPair,
) -> Result<Self, SemanticError> {
let amount_msats = refund.amount_msats();
let signing_pubkey = keys.public_key();
let contents = InvoiceContents::ForRefund {
refund: refund.contents.clone(),
fields: InvoiceFields {
payment_paths, created_at, relative_expiry: None, payment_hash,
amount_msats: refund.amount_msats(), fallbacks: None,
features: Bolt12InvoiceFeatures::empty(), signing_pubkey: keys.public_key(),
},
fields: Self::fields(
payment_paths, created_at, payment_hash, amount_msats, signing_pubkey
),
};

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

fn fields(
payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
payment_hash: PaymentHash, amount_msats: u64, signing_pubkey: PublicKey
) -> InvoiceFields {
InvoiceFields {
payment_paths, created_at, relative_expiry: None, payment_hash, amount_msats,
fallbacks: None, features: Bolt12InvoiceFeatures::empty(), signing_pubkey,
}
}

fn new(
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents, keys: Option<KeyPair>
) -> Result<Self, SemanticError> {
Expand Down

0 comments on commit 0d7f06b

Please sign in to comment.