@@ -8095,7 +8095,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
8095
8095
8096
8096
let per_peer_state = self.per_peer_state.read().unwrap();
8097
8097
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
8098
- .ok_or_else(|| {
8098
+ .ok_or_else(|| {
8099
8099
debug_assert!(false);
8100
8100
MsgHandleErrInternal::send_err_msg_no_close(
8101
8101
format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id),
@@ -10172,6 +10172,28 @@ impl Default for Bolt11InvoiceParameters {
10172
10172
}
10173
10173
10174
10174
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
+
10175
10197
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
10176
10198
/// [`ChannelManager`] when handling [`InvoiceRequest`] messages for the offer. The offer's
10177
10199
/// expiration will be `absolute_expiry` if `Some`, otherwise it will not expire.
@@ -10196,21 +10218,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
10196
10218
/// [`Offer`]: crate::offers::offer::Offer
10197
10219
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
10198
10220
pub fn create_offer_builder(&$self) -> Result<$builder, Bolt12SemanticError> {
10199
- let node_id = $self.get_our_node_id();
10200
- let expanded_key = &$self.inbound_payment_key;
10201
- let entropy = &*$self.entropy_source;
10202
- let secp_ctx = &$self.secp_ctx;
10203
-
10204
- let nonce = Nonce::from_entropy_source(entropy);
10205
- let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10206
- let path = $self.create_blinded_paths(context)
10207
- .and_then(|paths| paths.into_iter().next().ok_or(()))
10208
- .map_err(|_| Bolt12SemanticError::MissingPaths)?;
10209
- let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10210
- .chain_hash($self.chain_hash)
10211
- .path(path);
10212
-
10213
- Ok(builder.into())
10221
+ $self.create_offer_builder_intern(|_, context, _| {
10222
+ $self.create_blinded_paths(context)
10223
+ .and_then(|paths| paths.into_iter().next().ok_or(()))
10224
+ .map(Some)
10225
+ .map_err(|_| Bolt12SemanticError::MissingPaths)
10226
+ })
10214
10227
}
10215
10228
10216
10229
/// Creates an [`OfferBuilder`] such that the [`Offer`] it builds is recognized by the
@@ -10239,32 +10252,63 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
10239
10252
&$self,
10240
10253
router: ME,
10241
10254
) -> Result<$builder, Bolt12SemanticError> {
10255
+ $self.create_offer_builder_intern(|node_id, context, secp_ctx| {
10256
+ let peers = $self.get_peers_for_blinded_path();
10257
+ router.create_blinded_paths(node_id, context, peers, secp_ctx)
10258
+ .map(|paths| paths.into_iter().next())
10259
+ .map_err(|_| Bolt12SemanticError::MissingPaths)
10260
+ })
10261
+ }
10262
+ } }
10263
+
10264
+ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10265
+ fn create_refund_builder_intern<PF>(
10266
+ &$self,
10267
+ make_path: PF,
10268
+ amount_msats: u64,
10269
+ absolute_expiry: Duration,
10270
+ payment_id: PaymentId,
10271
+ retry_strategy: Retry,
10272
+ route_params_config: RouteParametersConfig
10273
+ ) -> Result<$builder, Bolt12SemanticError>
10274
+ where
10275
+ PF: FnOnce(PublicKey, MessageContext, &secp256k1::Secp256k1<secp256k1::All>) -> Result<Option<BlindedMessagePath>, Bolt12SemanticError>,
10276
+ {
10242
10277
let node_id = $self.get_our_node_id();
10243
10278
let expanded_key = &$self.inbound_payment_key;
10244
10279
let entropy = &*$self.entropy_source;
10245
10280
let secp_ctx = &$self.secp_ctx;
10246
10281
10247
10282
let nonce = Nonce::from_entropy_source(entropy);
10248
- let context = MessageContext::Offers(OffersContext::InvoiceRequest { nonce });
10283
+ let context = MessageContext::Offers(
10284
+ OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
10285
+ );
10249
10286
10250
- let peers = $self.get_peers_for_blinded_path();
10287
+ // Create the base builder with common properties
10288
+ let mut builder = RefundBuilder::deriving_signing_pubkey(
10289
+ node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10290
+ )?
10291
+ .chain_hash($self.chain_hash)
10292
+ .absolute_expiry(absolute_expiry);
10251
10293
10252
- let path = router.create_blinded_paths(node_id, context, peers, secp_ctx)
10253
- .map_err(|_| Bolt12SemanticError::MissingPaths)?
10254
- .into_iter().next();
10294
+ // Add path if one is provided by the path creator
10295
+ if let Some(path) = make_path(node_id, context, secp_ctx)? {
10296
+ builder = builder.path(path);
10297
+ }
10255
10298
10256
- let mut builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
10257
- .chain_hash ($self.chain_hash );
10299
+ // Handle persistence and payment tracking
10300
+ let _persistence_guard = PersistenceNotifierGuard::notify_on_drop ($self);
10258
10301
10259
- if let Some(path) = path {
10260
- builder = builder.path(path)
10261
- }
10302
+ let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10303
+ $self.pending_outbound_payments
10304
+ .add_new_awaiting_invoice(
10305
+ payment_id, expiration, retry_strategy, route_params_config, None,
10306
+ )
10307
+ .map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10262
10308
10263
10309
Ok(builder.into())
10264
10310
}
10265
- } }
10266
10311
10267
- macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10268
10312
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
10269
10313
/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
10270
10314
///
@@ -10287,8 +10331,7 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10287
10331
///
10288
10332
/// # Privacy
10289
10333
///
10290
- /// Uses [`MessageRouter`] to construct a [`BlindedMessagePath`] for the refund based on the given
10291
- /// `absolute_expiry` according to [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`]. See those docs for
10334
+ /// Uses [`MessageRouter`] to construct a [`BlindedMessagePath`] for the refund. See those docs for
10292
10335
/// privacy implications as well as those of the parameterized [`Router`], which implements
10293
10336
/// [`MessageRouter`].
10294
10337
///
@@ -10314,34 +10357,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10314
10357
&$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
10315
10358
retry_strategy: Retry, route_params_config: RouteParametersConfig
10316
10359
) -> Result<$builder, Bolt12SemanticError> {
10317
- let node_id = $self.get_our_node_id();
10318
- let expanded_key = &$self.inbound_payment_key;
10319
- let entropy = &*$self.entropy_source;
10320
- let secp_ctx = &$self.secp_ctx;
10321
-
10322
- let nonce = Nonce::from_entropy_source(entropy);
10323
- let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: None };
10324
- let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry))
10325
- .and_then(|paths| paths.into_iter().next().ok_or(()))
10326
- .map_err(|_| Bolt12SemanticError::MissingPaths)?;
10327
-
10328
- let builder = RefundBuilder::deriving_signing_pubkey(
10329
- node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10330
- )?
10331
- .chain_hash($self.chain_hash)
10332
- .absolute_expiry(absolute_expiry)
10333
- .path(path);
10334
-
10335
- let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10336
-
10337
- let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10338
- $self.pending_outbound_payments
10339
- .add_new_awaiting_invoice(
10340
- payment_id, expiration, retry_strategy, route_params_config, None,
10341
- )
10342
- .map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10343
-
10344
- Ok(builder.into())
10360
+ $self.create_refund_builder_intern(
10361
+ |_, context, _| {
10362
+ $self.create_blinded_paths(context)
10363
+ .and_then(|paths| paths.into_iter().next().ok_or(()))
10364
+ .map(Some)
10365
+ .map_err(|_| Bolt12SemanticError::MissingPaths)
10366
+ },
10367
+ amount_msats,
10368
+ absolute_expiry,
10369
+ payment_id,
10370
+ retry_strategy,
10371
+ route_params_config
10372
+ )
10345
10373
}
10346
10374
10347
10375
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
@@ -10392,41 +10420,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10392
10420
&$self, router: ME, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
10393
10421
retry_strategy: Retry, route_params_config: RouteParametersConfig
10394
10422
) -> Result<$builder, Bolt12SemanticError> {
10395
- let node_id = $self.get_our_node_id();
10396
- let expanded_key = &$self.inbound_payment_key;
10397
- let entropy = &*$self.entropy_source;
10398
- let secp_ctx = &$self.secp_ctx;
10399
-
10400
- let nonce = Nonce::from_entropy_source(entropy);
10401
- let context = MessageContext::Offers(
10402
- OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
10403
- );
10404
-
10405
- let peers = $self.get_peers_for_blinded_path();
10406
- let path = router.create_blinded_paths(node_id, context, peers, secp_ctx)
10407
- .map_err(|_| Bolt12SemanticError::MissingPaths)?
10408
- .into_iter().next();
10409
-
10410
- let mut builder = RefundBuilder::deriving_signing_pubkey(
10411
- node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10412
- )?
10413
- .chain_hash($self.chain_hash)
10414
- .absolute_expiry(absolute_expiry);
10415
-
10416
- if let Some(path) = path {
10417
- builder = builder.path(path)
10418
- }
10419
-
10420
- let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10421
-
10422
- let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10423
- $self.pending_outbound_payments
10424
- .add_new_awaiting_invoice(
10425
- payment_id, expiration, retry_strategy, route_params_config, None,
10426
- )
10427
- .map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10428
-
10429
- Ok(builder.into())
10423
+ $self.create_refund_builder_intern(
10424
+ |node_id, context, secp_ctx| {
10425
+ let peers = $self.get_peers_for_blinded_path();
10426
+ router.create_blinded_paths(node_id, context, peers, secp_ctx)
10427
+ .map(|paths| paths.into_iter().next())
10428
+ .map_err(|_| Bolt12SemanticError::MissingPaths)
10429
+ },
10430
+ amount_msats,
10431
+ absolute_expiry,
10432
+ payment_id,
10433
+ retry_strategy,
10434
+ route_params_config
10435
+ )
10430
10436
}
10431
10437
} }
10432
10438
0 commit comments