Skip to content

Commit 2f653f4

Browse files
f add from_bolt12_invoice util
1 parent d546ffb commit 2f653f4

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lightning/src/routing/router.rs

Lines changed: 17 additions & 6 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,19 @@ impl PaymentParameters {
634634
.expect("PaymentParameters::from_node_id should always initialize the payee as unblinded")
635635
}
636636

637-
/// Creates parameters for paying to a blinded payee.
638-
pub fn blinded(blinded_route_hints: Vec<(BlindedPayInfo, BlindedPath)>) -> Self {
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+
let route_hints = invoice.payment_paths().iter()
642+
.map(|(path, info)| (info.clone(), path.clone())).collect();
643+
Self::blinded(route_hints)
644+
.with_bolt12_features(invoice.features().clone()).unwrap()
645+
.with_expiry_time(invoice.created_at().as_secs().saturating_add(invoice.relative_expiry().as_secs()))
646+
}
647+
648+
// TODO: fix tuple order to be consistent with [`crate::offers::invoice::Invoice::payment_paths`]
649+
fn blinded(blinded_route_hints: Vec<(BlindedPayInfo, BlindedPath)>) -> Self {
639650
Self {
640651
payee: Payee::Blinded { route_hints: blinded_route_hints, features: None },
641652
expiry_time: None,
@@ -647,7 +658,7 @@ impl PaymentParameters {
647658
}
648659

649660
/// Includes the payee's features. Errors if the parameters were not initialized with
650-
/// [`PaymentParameters::blinded`].
661+
/// [`PaymentParameters::from_bolt12_invoice`].
651662
///
652663
/// This is not exported to bindings users since bindings don't support move semantics
653664
pub fn with_bolt12_features(self, features: Bolt12InvoiceFeatures) -> Result<Self, ()> {
@@ -659,7 +670,7 @@ impl PaymentParameters {
659670
}
660671

661672
/// Includes the payee's features. Errors if the parameters were initialized with
662-
/// [`PaymentParameters::blinded`].
673+
/// [`PaymentParameters::from_bolt12_invoice`].
663674
///
664675
/// This is not exported to bindings users since bindings don't support move semantics
665676
pub fn with_bolt11_features(self, features: InvoiceFeatures) -> Result<Self, ()> {
@@ -675,7 +686,7 @@ impl PaymentParameters {
675686
}
676687

677688
/// Includes hints for routing to the payee. Errors if the parameters were initialized with
678-
/// [`PaymentParameters::blinded`].
689+
/// [`PaymentParameters::from_bolt12_invoice`].
679690
///
680691
/// This is not exported to bindings users since bindings don't support move semantics
681692
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {

0 commit comments

Comments
 (0)