@@ -37,16 +37,16 @@ use crate::ln::inbound_payment;
37
37
use crate :: offers:: async_receive_offer_cache:: AsyncReceiveOfferCache ;
38
38
use crate :: offers:: invoice:: {
39
39
Bolt12Invoice , DerivedSigningPubkey , ExplicitSigningPubkey , InvoiceBuilder ,
40
- UnsignedBolt12Invoice , DEFAULT_RELATIVE_EXPIRY ,
40
+ DEFAULT_RELATIVE_EXPIRY ,
41
41
} ;
42
- use crate :: offers:: invoice_error:: InvoiceError ;
43
42
use crate :: offers:: invoice_request:: {
44
- InvoiceRequest , InvoiceRequestBuilder , InvoiceRequestVerifiedFromOffer ,
43
+ InvoiceRequest , InvoiceRequestBuilder , InvoiceRequestVerifiedFromOffer , VerifiedInvoiceRequest ,
45
44
} ;
46
45
use crate :: offers:: nonce:: Nonce ;
47
46
use crate :: offers:: offer:: { Amount , DerivedMetadata , Offer , OfferBuilder } ;
48
47
use crate :: offers:: parse:: Bolt12SemanticError ;
49
48
use crate :: offers:: refund:: { Refund , RefundBuilder } ;
49
+ use crate :: offers:: static_invoice:: { StaticInvoice , StaticInvoiceBuilder } ;
50
50
use crate :: onion_message:: async_payments:: {
51
51
AsyncPaymentsMessage , HeldHtlcAvailable , OfferPaths , OfferPathsRequest , ServeStaticInvoice ,
52
52
StaticInvoicePersisted ,
@@ -57,9 +57,7 @@ use crate::onion_message::messenger::{
57
57
use crate :: onion_message:: offers:: OffersMessage ;
58
58
use crate :: onion_message:: packet:: OnionMessageContents ;
59
59
use crate :: routing:: router:: Router ;
60
- use crate :: sign:: { EntropySource , NodeSigner , ReceiveAuthKey } ;
61
-
62
- use crate :: offers:: static_invoice:: { StaticInvoice , StaticInvoiceBuilder } ;
60
+ use crate :: sign:: { EntropySource , ReceiveAuthKey } ;
63
61
use crate :: sync:: { Mutex , RwLock } ;
64
62
use crate :: types:: payment:: { PaymentHash , PaymentSecret } ;
65
63
use crate :: util:: logger:: Logger ;
@@ -939,95 +937,124 @@ where
939
937
Ok ( builder. into ( ) )
940
938
}
941
939
942
- /// Creates a response for the provided [`InvoiceRequestVerifiedFromOffer`].
940
+ /// Creates an [`InvoiceBuilder<DerivedSigningPubkey>`] for the
941
+ /// provided [`VerifiedInvoiceRequest<DerivedSigningPubkey>`].
942
+ ///
943
+ /// Returns the invoice builder along with a [`MessageContext`] that can
944
+ /// later be used to respond to the counterparty.
945
+ ///
946
+ /// Use this method when you want to inspect or modify the [`InvoiceBuilder`]
947
+ /// before signing and generating the final [`Bolt12Invoice`].
943
948
///
944
- /// A response can be either an [`OffersMessage::Invoice`] with additional [`MessageContext`],
945
- /// or an [`OffersMessage::InvoiceError`], depending on the [`InvoiceRequest`].
949
+ /// # Errors
946
950
///
947
- /// An [`OffersMessage::InvoiceError`] will be generated if:
948
- /// - We fail to generate valid payment paths to include in the [`Bolt12Invoice`].
949
- /// - We fail to generate a valid signed [`Bolt12Invoice `] for the [`InvoiceRequest`].
950
- pub fn create_response_for_invoice_request < ES : Deref , NS : Deref , R : Deref > (
951
- & self , signer : & NS , router : & R , entropy_source : ES ,
952
- invoice_request : InvoiceRequestVerifiedFromOffer , amount_msats : u64 ,
951
+ /// Returns a [`Bolt12SemanticError`] if:
952
+ /// - Valid blinded payment paths could not be generated for the [`Bolt12Invoice`].
953
+ /// - The [`InvoiceBuilder `] could not be created from the [`InvoiceRequest`].
954
+ pub fn create_invoice_builder_from_invoice_request_with_keys < ' a , ES : Deref , R : Deref > (
955
+ & self , router : & R , entropy_source : ES ,
956
+ invoice_request : & ' a VerifiedInvoiceRequest < DerivedSigningPubkey > , amount_msats : u64 ,
953
957
payment_hash : PaymentHash , payment_secret : PaymentSecret ,
954
958
usable_channels : Vec < ChannelDetails > ,
955
- ) -> ( OffersMessage , Option < MessageContext > )
959
+ ) -> Result < ( InvoiceBuilder < ' a , DerivedSigningPubkey > , MessageContext ) , Bolt12SemanticError >
956
960
where
957
961
ES :: Target : EntropySource ,
958
- NS :: Target : NodeSigner ,
962
+
959
963
R :: Target : Router ,
960
964
{
961
965
let entropy = & * entropy_source;
962
- let secp_ctx = & self . secp_ctx ;
966
+ let relative_expiry = DEFAULT_RELATIVE_EXPIRY . as_secs ( ) as u32 ;
967
+
968
+ let context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext {
969
+ offer_id : invoice_request. offer_id ,
970
+ invoice_request : invoice_request. fields ( ) ,
971
+ } ) ;
972
+
973
+ let payment_paths = self
974
+ . create_blinded_payment_paths (
975
+ router,
976
+ entropy,
977
+ usable_channels,
978
+ Some ( amount_msats) ,
979
+ payment_secret,
980
+ context,
981
+ relative_expiry,
982
+ )
983
+ . map_err ( |_| Bolt12SemanticError :: MissingPaths ) ?;
984
+
985
+ #[ cfg( feature = "std" ) ]
986
+ let builder = invoice_request. respond_using_derived_keys ( payment_paths, payment_hash) ;
987
+ #[ cfg( not( feature = "std" ) ) ]
988
+ let builder = invoice_request. respond_using_derived_keys_no_std (
989
+ payment_paths,
990
+ payment_hash,
991
+ Duration :: from_secs ( self . highest_seen_timestamp . load ( Ordering :: Acquire ) as u64 ) ,
992
+ ) ;
993
+ let builder = builder. map ( |b| InvoiceBuilder :: from ( b) . allow_mpp ( ) ) ?;
963
994
995
+ let context = MessageContext :: Offers ( OffersContext :: InboundPayment { payment_hash } ) ;
996
+
997
+ Ok ( ( builder, context) )
998
+ }
999
+
1000
+ /// Creates an [`InvoiceBuilder<ExplicitSigningPubkey>`] for the
1001
+ /// provided [`VerifiedInvoiceRequest<ExplicitSigningPubkey>`].
1002
+ ///
1003
+ /// Returns the invoice builder along with a [`MessageContext`] that can
1004
+ /// later be used to respond to the counterparty.
1005
+ ///
1006
+ /// Use this method when you want to inspect or modify the [`InvoiceBuilder`]
1007
+ /// before signing and generating the final [`Bolt12Invoice`].
1008
+ ///
1009
+ /// # Errors
1010
+ ///
1011
+ /// Returns a [`Bolt12SemanticError`] if:
1012
+ /// - Valid blinded payment paths could not be generated for the [`Bolt12Invoice`].
1013
+ /// - The [`InvoiceBuilder`] could not be created from the [`InvoiceRequest`].
1014
+ pub fn create_invoice_builder_from_invoice_request_without_keys < ' a , ES : Deref , R : Deref > (
1015
+ & self , router : & R , entropy_source : ES ,
1016
+ invoice_request : & ' a VerifiedInvoiceRequest < ExplicitSigningPubkey > , amount_msats : u64 ,
1017
+ payment_hash : PaymentHash , payment_secret : PaymentSecret ,
1018
+ usable_channels : Vec < ChannelDetails > ,
1019
+ ) -> Result < ( InvoiceBuilder < ' a , ExplicitSigningPubkey > , MessageContext ) , Bolt12SemanticError >
1020
+ where
1021
+ ES :: Target : EntropySource ,
1022
+ R :: Target : Router ,
1023
+ {
1024
+ let entropy = & * entropy_source;
964
1025
let relative_expiry = DEFAULT_RELATIVE_EXPIRY . as_secs ( ) as u32 ;
965
1026
966
1027
let context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext {
967
- offer_id : invoice_request. offer_id ( ) ,
1028
+ offer_id : invoice_request. offer_id ,
968
1029
invoice_request : invoice_request. fields ( ) ,
969
1030
} ) ;
970
1031
971
- let payment_paths = match self . create_blinded_payment_paths (
972
- router,
973
- entropy,
974
- usable_channels,
975
- Some ( amount_msats) ,
976
- payment_secret,
977
- context,
978
- relative_expiry,
979
- ) {
980
- Ok ( paths) => paths,
981
- Err ( _) => {
982
- let error = InvoiceError :: from ( Bolt12SemanticError :: MissingPaths ) ;
983
- return ( OffersMessage :: InvoiceError ( error. into ( ) ) , None ) ;
984
- } ,
985
- } ;
1032
+ let payment_paths = self
1033
+ . create_blinded_payment_paths (
1034
+ router,
1035
+ entropy,
1036
+ usable_channels,
1037
+ Some ( amount_msats) ,
1038
+ payment_secret,
1039
+ context,
1040
+ relative_expiry,
1041
+ )
1042
+ . map_err ( |_| Bolt12SemanticError :: MissingPaths ) ?;
986
1043
1044
+ #[ cfg( feature = "std" ) ]
1045
+ let builder = invoice_request. respond_with ( payment_paths, payment_hash) ;
987
1046
#[ cfg( not( feature = "std" ) ) ]
988
- let created_at = Duration :: from_secs ( self . highest_seen_timestamp . load ( Ordering :: Acquire ) as u64 ) ;
1047
+ let builder = invoice_request. respond_with_no_std (
1048
+ payment_paths,
1049
+ payment_hash,
1050
+ Duration :: from_secs ( self . highest_seen_timestamp . load ( Ordering :: Acquire ) as u64 ) ,
1051
+ ) ;
989
1052
990
- let response = match invoice_request {
991
- InvoiceRequestVerifiedFromOffer :: DerivedKeys ( request) => {
992
- #[ cfg( feature = "std" ) ]
993
- let builder = request. respond_using_derived_keys ( payment_paths, payment_hash) ;
994
- #[ cfg( not( feature = "std" ) ) ]
995
- let builder = request. respond_using_derived_keys_no_std ( payment_paths, payment_hash, created_at) ;
996
- builder
997
- . map ( InvoiceBuilder :: < DerivedSigningPubkey > :: from)
998
- . and_then ( |builder| builder. allow_mpp ( ) . build_and_sign ( secp_ctx) )
999
- . map_err ( InvoiceError :: from)
1000
- } ,
1001
- InvoiceRequestVerifiedFromOffer :: ExplicitKeys ( request) => {
1002
- #[ cfg( feature = "std" ) ]
1003
- let builder = request. respond_with ( payment_paths, payment_hash) ;
1004
- #[ cfg( not( feature = "std" ) ) ]
1005
- let builder = request. respond_with_no_std ( payment_paths, payment_hash, created_at) ;
1006
- builder
1007
- . map ( InvoiceBuilder :: < ExplicitSigningPubkey > :: from)
1008
- . and_then ( |builder| builder. allow_mpp ( ) . build ( ) )
1009
- . map_err ( InvoiceError :: from)
1010
- . and_then ( |invoice| {
1011
- #[ cfg( c_bindings) ]
1012
- let mut invoice = invoice;
1013
- invoice
1014
- . sign ( |invoice : & UnsignedBolt12Invoice | {
1015
- signer. sign_bolt12_invoice ( invoice)
1016
- } )
1017
- . map_err ( InvoiceError :: from)
1018
- } )
1019
- } ,
1020
- } ;
1053
+ let builder = builder. map ( |b| InvoiceBuilder :: from ( b) . allow_mpp ( ) ) ?;
1021
1054
1022
- match response {
1023
- Ok ( invoice) => {
1024
- let context =
1025
- MessageContext :: Offers ( OffersContext :: InboundPayment { payment_hash } ) ;
1055
+ let context = MessageContext :: Offers ( OffersContext :: InboundPayment { payment_hash } ) ;
1026
1056
1027
- ( OffersMessage :: Invoice ( invoice) , Some ( context) )
1028
- } ,
1029
- Err ( error) => ( OffersMessage :: InvoiceError ( error. into ( ) ) , None ) ,
1030
- }
1057
+ Ok ( ( builder, context) )
1031
1058
}
1032
1059
1033
1060
/// Enqueues the created [`InvoiceRequest`] to be sent to the counterparty.
@@ -1054,6 +1081,7 @@ where
1054
1081
/// valid reply paths for the counterparty to send back the corresponding [`Bolt12Invoice`]
1055
1082
/// or [`InvoiceError`].
1056
1083
///
1084
+ /// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
1057
1085
/// [`supports_onion_messages`]: crate::types::features::Features::supports_onion_messages
1058
1086
pub fn enqueue_invoice_request (
1059
1087
& self , invoice_request : InvoiceRequest , payment_id : PaymentId , nonce : Nonce ,
@@ -1099,6 +1127,7 @@ where
1099
1127
/// reply paths for the counterparty to send back the corresponding [`InvoiceError`] if we fail
1100
1128
/// to create blinded reply paths
1101
1129
///
1130
+ /// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
1102
1131
/// [`supports_onion_messages`]: crate::types::features::Features::supports_onion_messages
1103
1132
pub fn enqueue_invoice (
1104
1133
& self , invoice : Bolt12Invoice , refund : & Refund , peers : Vec < MessageForwardNode > ,
0 commit comments