Skip to content

Commit f159cd6

Browse files
committed
f: DRY up create_offer_builder
1 parent 42ad956 commit f159cd6

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10172,6 +10172,28 @@ impl Default for Bolt11InvoiceParameters {
1017210172
}
1017310173

1017410174
macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
10175+
fn create_offer_builder_intern<PF>(&$self, make_path: PF) -> Result<$builder, Bolt12SemanticError>
10176+
where
10177+
PF: FnOnce(PublicKey, MessageContext, &secp256k1::Secp256k1<secp256k1::All>) -> Result<Option<BlindedMessagePath>, Bolt12SemanticError>,
10178+
{
10179+
let node_id = $self.get_our_node_id();
10180+
let expanded_key = &$self.inbound_payment_key;
10181+
let entropy = &*$self.entropy_source;
10182+
let secp_ctx = &$self.secp_ctx;
10183+
10184+
let nonce = Nonce::from_entropy_source(entropy);
10185+
let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10186+
10187+
let mut builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10188+
.chain_hash($self.chain_hash);
10189+
10190+
if let Some(path) = make_path(node_id, context, secp_ctx)? {
10191+
builder = builder.path(path)
10192+
}
10193+
10194+
Ok(builder.into())
10195+
}
10196+
1017510197
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
1017610198
/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer. The offer's
1017710199
/// expiration will be `absolute_expiry` if `Some`, otherwise it will not expire.
@@ -10197,21 +10219,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
1019710219
/// [`Offer`]: crate::offers::offer::Offer
1019810220
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1019910221
pub fn create_offer_builder(&$self) -> Result<$builder, Bolt12SemanticError> {
10200-
let node_id = $self.get_our_node_id();
10201-
let expanded_key = &$self.inbound_payment_key;
10202-
let entropy = &*$self.entropy_source;
10203-
let secp_ctx = &$self.secp_ctx;
10204-
10205-
let nonce = Nonce::from_entropy_source(entropy);
10206-
let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10207-
let path = $self.create_blinded_paths(context)
10208-
.and_then(|paths| paths.into_iter().next().ok_or(()))
10209-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
10210-
let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10211-
.chain_hash($self.chain_hash)
10212-
.path(path);
10213-
10214-
Ok(builder.into())
10222+
$self.create_offer_builder_intern(|_, context, _| {
10223+
$self.create_blinded_paths(context)
10224+
.and_then(|paths| paths.into_iter().next().ok_or(()))
10225+
.map(Some)
10226+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10227+
})
1021510228
}
1021610229

1021710230
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
@@ -10240,28 +10253,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
1024010253
&$self,
1024110254
router: ME,
1024210255
) -> Result<$builder, Bolt12SemanticError> {
10243-
let node_id = $self.get_our_node_id();
10244-
let expanded_key = &$self.inbound_payment_key;
10245-
let entropy = &*$self.entropy_source;
10246-
let secp_ctx = &$self.secp_ctx;
10247-
10248-
let nonce = Nonce::from_entropy_source(entropy);
10249-
let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10250-
10251-
let peers = $self.get_peers_for_blinded_path();
10252-
10253-
let path = router.create_blinded_paths(node_id, context, peers, secp_ctx)
10254-
.map_err(|_| Bolt12SemanticError::MissingPaths)?
10255-
.into_iter().next();
10256-
10257-
let mut builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10258-
.chain_hash($self.chain_hash);
10259-
10260-
if let Some(path) = path {
10261-
builder = builder.path(path)
10262-
}
10263-
10264-
Ok(builder.into())
10256+
$self.create_offer_builder_intern(|node_id, context, secp_ctx| {
10257+
let peers = $self.get_peers_for_blinded_path();
10258+
router.create_blinded_paths(node_id, context, peers, secp_ctx)
10259+
.map(|paths| paths.into_iter().next())
10260+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10261+
})
1026510262
}
1026610263
} }
1026710264

0 commit comments

Comments
 (0)