Skip to content

Commit 0bcf220

Browse files
committed
f - refactor invoice_request contents to its own struct
1 parent 469e6fa commit 0bcf220

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ use bitcoin::secp256k1::schnorr::Signature;
1515
use core::convert::TryFrom;
1616
use core::str::FromStr;
1717
use ln::features::OfferFeatures;
18-
use offers::{Offer, PayerTlvStream};
18+
use offers::PayerTlvStream;
1919
use offers::merkle::SignatureTlvStream;
20-
use offers::offer::{OfferTlvStream, ParsedOffer};
20+
use offers::offer::{OfferContents, OfferTlvStream};
2121
use offers::parse::{Bech32Encode, ParseError, SemanticError};
2222

2323
///
2424
pub struct InvoiceRequest {
2525
bytes: Vec<u8>,
26-
offer: Offer,
26+
contents: InvoiceRequestContents,
27+
}
28+
29+
///
30+
pub(crate) struct InvoiceRequestContents {
31+
offer: OfferContents,
2732
chain: Option<BlockHash>,
2833
amount_msat: Option<u64>,
2934
features: Option<OfferFeatures>,
@@ -63,24 +68,23 @@ impl FromStr for InvoiceRequest {
6368

6469
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
6570
let (tlv_stream, bytes) = InvoiceRequest::from_bech32_str(s)?;
66-
Ok(InvoiceRequest::try_from((tlv_stream, bytes))?)
71+
let contents = InvoiceRequestContents::try_from(tlv_stream)?;
72+
Ok(InvoiceRequest { bytes, contents })
6773
}
6874
}
6975

70-
type ParsedInvoiceRequest = (FullInvoiceRequestTlvStream, Vec<u8>);
71-
72-
impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest {
76+
impl TryFrom<FullInvoiceRequestTlvStream> for InvoiceRequestContents {
7377
type Error = SemanticError;
7478

75-
fn try_from(invoice_request: ParsedInvoiceRequest) -> Result<Self, Self::Error> {
76-
let ((
77-
PayerTlvStream { payer_info },
78-
offer_tlv_stream,
79-
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
80-
SignatureTlvStream { signature },
81-
), bytes) = invoice_request;
79+
fn try_from(tlv_stream: FullInvoiceRequestTlvStream) -> Result<Self, Self::Error> {
80+
let (
81+
PayerTlvStream { payer_info },
82+
offer_tlv_stream,
83+
InvoiceRequestTlvStream { chain, amount, features, quantity, payer_id, payer_note },
84+
SignatureTlvStream { signature },
85+
) = tlv_stream;
8286

83-
let offer = Offer::try_from(ParsedOffer(offer_tlv_stream, vec![]))?;
87+
let offer = OfferContents::try_from(offer_tlv_stream)?;
8488

8589
let chain = match chain {
8690
None => None,
@@ -97,8 +101,8 @@ impl TryFrom<ParsedInvoiceRequest> for InvoiceRequest {
97101

98102
let payer_info = payer_info.map(Into::into);
99103

100-
Ok(InvoiceRequest {
101-
bytes, offer, chain, amount_msat, features, quantity, payer_id, payer_note, payer_info,
104+
Ok(InvoiceRequestContents {
105+
offer, chain, amount_msat, features, quantity, payer_id, payer_note, payer_info,
102106
signature,
103107
})
104108
}

lightning/src/offers/offer.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,7 @@ pub(crate) struct OfferContents {
192192
impl Offer {
193193
///
194194
pub fn chain(&self) -> BlockHash {
195-
// TODO: Update once spec is finalized
196-
self.contents.chains
197-
.as_ref()
198-
.and_then(|chains| chains.first().copied())
199-
.unwrap_or_else(|| genesis_block(Network::Bitcoin).block_hash())
195+
self.contents.chain()
200196
}
201197

202198
///
@@ -298,6 +294,14 @@ impl AsRef<[u8]> for Offer {
298294
}
299295

300296
impl OfferContents {
297+
pub fn chain(&self) -> BlockHash {
298+
// TODO: Update once spec is finalized
299+
self.chains
300+
.as_ref()
301+
.and_then(|chains| chains.first().copied())
302+
.unwrap_or_else(|| genesis_block(Network::Bitcoin).block_hash())
303+
}
304+
301305
pub fn quantity_min(&self) -> u64 {
302306
self.quantity_min.unwrap_or(1)
303307
}

0 commit comments

Comments
 (0)