Skip to content

Commit 6a54098

Browse files
Add utils for creating blinded PaymentParameters
1 parent 3184393 commit 6a54098

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

lightning/src/routing/router.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::ln::PaymentHash;
1818
use crate::ln::channelmanager::{ChannelDetails, PaymentId};
1919
use crate::ln::features::{Bolt12InvoiceFeatures, ChannelFeatures, InvoiceFeatures, NodeFeatures};
2020
use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
21-
use crate::offers::invoice::BlindedPayInfo;
21+
use crate::offers::invoice::{BlindedPayInfo, Invoice as Bolt12Invoice};
2222
use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
2323
use crate::routing::scoring::{ChannelUsage, LockableScore, Score};
2424
use crate::util::ser::{Writeable, Readable, ReadableArgs, Writer};
@@ -634,8 +634,40 @@ impl PaymentParameters {
634634
.expect("PaymentParameters::from_node_id should always initialize the payee as unblinded")
635635
}
636636

637-
/// Includes the payee's features. Errors if the parameters were initialized with blinded payment
638-
/// paths.
637+
/// Creates parameters for paying to a blinded payee from the provided invoice. Sets
638+
/// [`Payee::Blinded::route_hints`], [`Payee::Blinded::features`], and
639+
/// [`PaymentParameters::expiry_time`].
640+
pub fn from_bolt12_invoice(invoice: &Bolt12Invoice) -> Self {
641+
Self::blinded(invoice.payment_paths().to_vec())
642+
.with_bolt12_features(invoice.features().clone()).unwrap()
643+
.with_expiry_time(invoice.created_at().as_secs().saturating_add(invoice.relative_expiry().as_secs()))
644+
}
645+
646+
fn blinded(blinded_route_hints: Vec<(BlindedPayInfo, BlindedPath)>) -> Self {
647+
Self {
648+
payee: Payee::Blinded { route_hints: blinded_route_hints, features: None },
649+
expiry_time: None,
650+
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
651+
max_path_count: DEFAULT_MAX_PATH_COUNT,
652+
max_channel_saturation_power_of_half: 2,
653+
previously_failed_channels: Vec::new(),
654+
}
655+
}
656+
657+
/// Includes the payee's features. Errors if the parameters were not initialized with
658+
/// [`PaymentParameters::from_bolt12_invoice`].
659+
///
660+
/// This is not exported to bindings users since bindings don't support move semantics
661+
pub fn with_bolt12_features(self, features: Bolt12InvoiceFeatures) -> Result<Self, ()> {
662+
match self.payee {
663+
Payee::Clear { .. } => Err(()),
664+
Payee::Blinded { route_hints, .. } =>
665+
Ok(Self { payee: Payee::Blinded { route_hints, features: Some(features) }, ..self })
666+
}
667+
}
668+
669+
/// Includes the payee's features. Errors if the parameters were initialized with
670+
/// [`PaymentParameters::from_bolt12_invoice`].
639671
///
640672
/// This is not exported to bindings users since bindings don't support move semantics
641673
pub fn with_bolt11_features(self, features: InvoiceFeatures) -> Result<Self, ()> {
@@ -651,7 +683,7 @@ impl PaymentParameters {
651683
}
652684

653685
/// Includes hints for routing to the payee. Errors if the parameters were initialized with
654-
/// blinded payment paths.
686+
/// [`PaymentParameters::from_bolt12_invoice`].
655687
///
656688
/// This is not exported to bindings users since bindings don't support move semantics
657689
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {

0 commit comments

Comments
 (0)