@@ -37,11 +37,10 @@ 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 , VerifiedInvoiceRequestEnum ,
43
+ InvoiceRequest , InvoiceRequestBuilder , VerifiedInvoiceRequest , VerifiedInvoiceRequestEnum ,
45
44
} ;
46
45
use crate :: offers:: nonce:: Nonce ;
47
46
use crate :: offers:: offer:: { DerivedMetadata , Offer , OfferBuilder } ;
@@ -52,7 +51,7 @@ use crate::onion_message::messenger::{Destination, MessageRouter, MessageSendIns
52
51
use crate :: onion_message:: offers:: OffersMessage ;
53
52
use crate :: onion_message:: packet:: OnionMessageContents ;
54
53
use crate :: routing:: router:: Router ;
55
- use crate :: sign:: { EntropySource , NodeSigner , ReceiveAuthKey } ;
54
+ use crate :: sign:: { EntropySource , ReceiveAuthKey } ;
56
55
use crate :: sync:: { Mutex , RwLock } ;
57
56
use crate :: types:: payment:: { PaymentHash , PaymentSecret } ;
58
57
use crate :: util:: ser:: Writeable ;
@@ -920,94 +919,126 @@ where
920
919
Ok ( builder. into ( ) )
921
920
}
922
921
923
- /// Creates a response for the provided [`VerifiedInvoiceRequestEnum`].
922
+ /// Creates an [`InvoiceBuilder`] with [`DerivedSigningPubkey`] for the
923
+ /// provided [`VerifiedInvoiceRequest<DerivedSigningPubkey>`].
924
924
///
925
- /// A response can be either an [`OffersMessage::Invoice`] with additional [`MessageContext`],
926
- /// or an [`OffersMessage::InvoiceError`], depending on the [`InvoiceRequest`] .
925
+ /// Returns the invoice builder along with a [`MessageContext`] that can
926
+ /// later be used to respond to the counterparty .
927
927
///
928
- /// An [`OffersMessage::InvoiceError`] will be generated if:
929
- /// - We fail to generate valid payment paths to include in the [`Bolt12Invoice`].
930
- /// - We fail to generate a valid signed [`Bolt12Invoice`] for the [`InvoiceRequest`].
931
- pub fn create_response_for_invoice_request < ES : Deref , NS : Deref , R : Deref > (
932
- & self , signer : & NS , router : & R , entropy_source : ES ,
933
- invoice_request : VerifiedInvoiceRequestEnum , amount_msats : u64 , payment_hash : PaymentHash ,
934
- payment_secret : PaymentSecret , usable_channels : Vec < ChannelDetails > ,
935
- ) -> ( OffersMessage , Option < MessageContext > )
928
+ /// Use this method when you want to inspect or modify the [`InvoiceBuilder`]
929
+ /// before signing and generating the final [`Bolt12Invoice`].
930
+ ///
931
+ /// # Errors
932
+ ///
933
+ /// Returns a [`Bolt12SemanticError`] if:
934
+ /// - User call the function with [`VerifiedInvoiceRequest<ExplicitSigningPubkey>`].
935
+ /// - Valid blinded payment paths could not be generated for the [`Bolt12Invoice`].
936
+ /// - The [`InvoiceBuilder`] could not be created from the [`InvoiceRequest`].
937
+ pub fn create_invoice_builder_from_invoice_request_with_keys < ' a , ES : Deref , R : Deref > (
938
+ & ' a self , router : & R , entropy_source : ES ,
939
+ invoice_request : & ' a VerifiedInvoiceRequest < DerivedSigningPubkey > , amount_msats : u64 ,
940
+ payment_hash : PaymentHash , payment_secret : PaymentSecret ,
941
+ usable_channels : Vec < ChannelDetails > ,
942
+ ) -> Result < ( InvoiceBuilder < ' a , DerivedSigningPubkey > , MessageContext ) , Bolt12SemanticError >
936
943
where
937
944
ES :: Target : EntropySource ,
938
- NS :: Target : NodeSigner ,
945
+
939
946
R :: Target : Router ,
940
947
{
941
948
let entropy = & * entropy_source;
942
- let secp_ctx = & self . secp_ctx ;
949
+ let relative_expiry = DEFAULT_RELATIVE_EXPIRY . as_secs ( ) as u32 ;
950
+
951
+ let context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext {
952
+ offer_id : invoice_request. offer_id ,
953
+ invoice_request : invoice_request. fields ( ) ,
954
+ } ) ;
955
+
956
+ let payment_paths = self
957
+ . create_blinded_payment_paths (
958
+ router,
959
+ entropy,
960
+ usable_channels,
961
+ Some ( amount_msats) ,
962
+ payment_secret,
963
+ context,
964
+ relative_expiry,
965
+ )
966
+ . map_err ( |_| Bolt12SemanticError :: MissingPaths ) ?;
967
+
968
+ #[ cfg( feature = "std" ) ]
969
+ let builder = invoice_request. respond_using_derived_keys ( payment_paths, payment_hash) ;
970
+ #[ cfg( not( feature = "std" ) ) ]
971
+ let builder = invoice_request. respond_using_derived_keys_no_std (
972
+ payment_paths,
973
+ payment_hash,
974
+ Duration :: from_secs ( self . highest_seen_timestamp . load ( Ordering :: Acquire ) as u64 ) ,
975
+ ) ;
976
+ let builder = builder. map ( |b| InvoiceBuilder :: from ( b) . allow_mpp ( ) ) ?;
977
+
978
+ let context = MessageContext :: Offers ( OffersContext :: InboundPayment { payment_hash } ) ;
979
+
980
+ Ok ( ( builder, context) )
981
+ }
943
982
983
+ /// Creates an [`InvoiceBuilder`] with [`ExplicitSigningPubkey`] for the
984
+ /// provided [`VerifiedInvoiceRequest<ExplicitSigningPubkey>`].
985
+ ///
986
+ /// Returns the invoice builder along with a [`MessageContext`] that can
987
+ /// later be used to respond to the counterparty.
988
+ ///
989
+ /// Use this method when you want to inspect or modify the [`InvoiceBuilder`]
990
+ /// before signing and generating the final [`Bolt12Invoice`].
991
+ ///
992
+ /// # Errors
993
+ ///
994
+ /// Returns a [`Bolt12SemanticError`] if:
995
+ /// - User call the function with [`VerifiedInvoiceRequest<DerivedSigningPubkey>`].
996
+ /// - Valid blinded payment paths could not be generated for the [`Bolt12Invoice`].
997
+ /// - The [`InvoiceBuilder`] could not be created from the [`InvoiceRequest`].
998
+ pub fn create_invoice_builder_from_invoice_request_without_keys < ' a , ES : Deref , R : Deref > (
999
+ & ' a self , router : & R , entropy_source : ES ,
1000
+ invoice_request : & ' a VerifiedInvoiceRequest < ExplicitSigningPubkey > , amount_msats : u64 ,
1001
+ payment_hash : PaymentHash , payment_secret : PaymentSecret ,
1002
+ usable_channels : Vec < ChannelDetails > ,
1003
+ ) -> Result < ( InvoiceBuilder < ' a , ExplicitSigningPubkey > , MessageContext ) , Bolt12SemanticError >
1004
+ where
1005
+ ES :: Target : EntropySource ,
1006
+ R :: Target : Router ,
1007
+ {
1008
+ let entropy = & * entropy_source;
944
1009
let relative_expiry = DEFAULT_RELATIVE_EXPIRY . as_secs ( ) as u32 ;
945
1010
946
1011
let context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext {
947
- offer_id : invoice_request. offer_id ( ) ,
1012
+ offer_id : invoice_request. offer_id ,
948
1013
invoice_request : invoice_request. fields ( ) ,
949
1014
} ) ;
950
1015
951
- let payment_paths = match self . create_blinded_payment_paths (
952
- router,
953
- entropy,
954
- usable_channels,
955
- Some ( amount_msats) ,
956
- payment_secret,
957
- context,
958
- relative_expiry,
959
- ) {
960
- Ok ( paths) => paths,
961
- Err ( _) => {
962
- let error = InvoiceError :: from ( Bolt12SemanticError :: MissingPaths ) ;
963
- return ( OffersMessage :: InvoiceError ( error. into ( ) ) , None ) ;
964
- } ,
965
- } ;
1016
+ let payment_paths = self
1017
+ . create_blinded_payment_paths (
1018
+ router,
1019
+ entropy,
1020
+ usable_channels,
1021
+ Some ( amount_msats) ,
1022
+ payment_secret,
1023
+ context,
1024
+ relative_expiry,
1025
+ )
1026
+ . map_err ( |_| Bolt12SemanticError :: MissingPaths ) ?;
966
1027
1028
+ #[ cfg( feature = "std" ) ]
1029
+ let builder = invoice_request. respond_with ( payment_paths, payment_hash) ;
967
1030
#[ cfg( not( feature = "std" ) ) ]
968
- let created_at = Duration :: from_secs ( self . highest_seen_timestamp . load ( Ordering :: Acquire ) as u64 ) ;
1031
+ let builder = invoice_request. respond_with_no_std (
1032
+ payment_paths,
1033
+ payment_hash,
1034
+ Duration :: from_secs ( self . highest_seen_timestamp . load ( Ordering :: Acquire ) as u64 ) ,
1035
+ ) ;
969
1036
970
- let response = match invoice_request {
971
- VerifiedInvoiceRequestEnum :: WithKeys ( request) => {
972
- #[ cfg( feature = "std" ) ]
973
- let builder = request. respond_using_derived_keys ( payment_paths, payment_hash) ;
974
- #[ cfg( not( feature = "std" ) ) ]
975
- let builder = request. respond_using_derived_keys_no_std ( payment_paths, payment_hash, created_at) ;
976
- builder
977
- . map ( InvoiceBuilder :: < DerivedSigningPubkey > :: from)
978
- . and_then ( |builder| builder. allow_mpp ( ) . build_and_sign ( secp_ctx) )
979
- . map_err ( InvoiceError :: from)
980
- } ,
981
- VerifiedInvoiceRequestEnum :: WithoutKeys ( request) => {
982
- #[ cfg( feature = "std" ) ]
983
- let builder = request. respond_with ( payment_paths, payment_hash) ;
984
- #[ cfg( not( feature = "std" ) ) ]
985
- let builder = request. respond_with_no_std ( payment_paths, payment_hash, created_at) ;
986
- builder
987
- . map ( InvoiceBuilder :: < ExplicitSigningPubkey > :: from)
988
- . and_then ( |builder| builder. allow_mpp ( ) . build ( ) )
989
- . map_err ( InvoiceError :: from)
990
- . and_then ( |invoice| {
991
- #[ cfg( c_bindings) ]
992
- let mut invoice = invoice;
993
- invoice
994
- . sign ( |invoice : & UnsignedBolt12Invoice | {
995
- signer. sign_bolt12_invoice ( invoice)
996
- } )
997
- . map_err ( InvoiceError :: from)
998
- } )
999
- } ,
1000
- } ;
1037
+ let builder = builder. map ( |b| InvoiceBuilder :: from ( b) . allow_mpp ( ) ) ?;
1001
1038
1002
- match response {
1003
- Ok ( invoice) => {
1004
- let context =
1005
- MessageContext :: Offers ( OffersContext :: InboundPayment { payment_hash } ) ;
1039
+ let context = MessageContext :: Offers ( OffersContext :: InboundPayment { payment_hash } ) ;
1006
1040
1007
- ( OffersMessage :: Invoice ( invoice) , Some ( context) )
1008
- } ,
1009
- Err ( error) => ( OffersMessage :: InvoiceError ( error. into ( ) ) , None ) ,
1010
- }
1041
+ Ok ( ( builder, context) )
1011
1042
}
1012
1043
1013
1044
/// Enqueues the created [`InvoiceRequest`] to be sent to the counterparty.
@@ -1034,6 +1065,7 @@ where
1034
1065
/// valid reply paths for the counterparty to send back the corresponding [`Bolt12Invoice`]
1035
1066
/// or [`InvoiceError`].
1036
1067
///
1068
+ /// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
1037
1069
/// [`supports_onion_messages`]: crate::types::features::Features::supports_onion_messages
1038
1070
pub fn enqueue_invoice_request (
1039
1071
& self , invoice_request : InvoiceRequest , payment_id : PaymentId , nonce : Nonce ,
@@ -1079,6 +1111,7 @@ where
1079
1111
/// reply paths for the counterparty to send back the corresponding [`InvoiceError`] if we fail
1080
1112
/// to create blinded reply paths
1081
1113
///
1114
+ /// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
1082
1115
/// [`supports_onion_messages`]: crate::types::features::Features::supports_onion_messages
1083
1116
pub fn enqueue_invoice (
1084
1117
& self , invoice : Bolt12Invoice , refund : & Refund , peers : Vec < MessageForwardNode > ,
0 commit comments