@@ -64,6 +64,7 @@ use crate::sign::EntropySource;
6464use crate :: io;
6565use crate :: blinded_path:: BlindedPath ;
6666use crate :: ln:: PaymentHash ;
67+ use crate :: ln:: channelmanager:: PaymentId ;
6768use crate :: ln:: features:: InvoiceRequestFeatures ;
6869use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN , Nonce } ;
6970use crate :: ln:: msgs:: DecodeError ;
@@ -128,10 +129,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
128129 }
129130
130131 pub ( super ) fn deriving_metadata < ES : Deref > (
131- offer : & ' a Offer , payer_id : PublicKey , expanded_key : & ExpandedKey , entropy_source : ES
132+ offer : & ' a Offer , payer_id : PublicKey , expanded_key : & ExpandedKey , entropy_source : ES ,
133+ payment_id : PaymentId ,
132134 ) -> Self where ES :: Target : EntropySource {
133135 let nonce = Nonce :: from_entropy_source ( entropy_source) ;
134- let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES ) ;
136+ let payment_id = Some ( payment_id) ;
137+ let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES , payment_id) ;
135138 let metadata = Metadata :: Derived ( derivation_material) ;
136139 Self {
137140 offer,
@@ -145,10 +148,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
145148
146149impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
147150 pub ( super ) fn deriving_payer_id < ES : Deref > (
148- offer : & ' a Offer , expanded_key : & ExpandedKey , entropy_source : ES , secp_ctx : & ' b Secp256k1 < T >
151+ offer : & ' a Offer , expanded_key : & ExpandedKey , entropy_source : ES ,
152+ secp_ctx : & ' b Secp256k1 < T > , payment_id : PaymentId
149153 ) -> Self where ES :: Target : EntropySource {
150154 let nonce = Nonce :: from_entropy_source ( entropy_source) ;
151- let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES ) ;
155+ let payment_id = Some ( payment_id) ;
156+ let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES , payment_id) ;
152157 let metadata = Metadata :: DerivedSigningPubkey ( derivation_material) ;
153158 Self {
154159 offer,
@@ -259,7 +264,7 @@ impl<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> InvoiceRequestBuilder<'a
259264 let mut tlv_stream = self . invoice_request . as_tlv_stream ( ) ;
260265 debug_assert ! ( tlv_stream. 2 . payer_id. is_none( ) ) ;
261266 tlv_stream. 0 . metadata = None ;
262- if !metadata. derives_keys ( ) {
267+ if !metadata. derives_payer_keys ( ) {
263268 tlv_stream. 2 . payer_id = self . payer_id . as_ref ( ) ;
264269 }
265270
@@ -682,7 +687,7 @@ impl InvoiceRequestContents {
682687 }
683688
684689 pub ( super ) fn derives_keys ( & self ) -> bool {
685- self . inner . payer . 0 . derives_keys ( )
690+ self . inner . payer . 0 . derives_payer_keys ( )
686691 }
687692
688693 pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -915,6 +920,7 @@ mod tests {
915920 #[ cfg( feature = "std" ) ]
916921 use core:: time:: Duration ;
917922 use crate :: sign:: KeyMaterial ;
923+ use crate :: ln:: channelmanager:: PaymentId ;
918924 use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
919925 use crate :: ln:: inbound_payment:: ExpandedKey ;
920926 use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1060,12 +1066,13 @@ mod tests {
10601066 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
10611067 let entropy = FixedEntropy { } ;
10621068 let secp_ctx = Secp256k1 :: new ( ) ;
1069+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
10631070
10641071 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
10651072 . amount_msats ( 1000 )
10661073 . build ( ) . unwrap ( ) ;
10671074 let invoice_request = offer
1068- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1075+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
10691076 . unwrap ( )
10701077 . build ( ) . unwrap ( )
10711078 . sign ( payer_sign) . unwrap ( ) ;
@@ -1075,7 +1082,10 @@ mod tests {
10751082 . unwrap ( )
10761083 . build ( ) . unwrap ( )
10771084 . sign ( recipient_sign) . unwrap ( ) ;
1078- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1085+ match invoice. verify ( & expanded_key, & secp_ctx) {
1086+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1087+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1088+ }
10791089
10801090 // Fails verification with altered fields
10811091 let (
@@ -1098,7 +1108,7 @@ mod tests {
10981108 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
10991109
11001110 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1101- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1111+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11021112
11031113 // Fails verification with altered metadata
11041114 let (
@@ -1121,20 +1131,21 @@ mod tests {
11211131 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11221132
11231133 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1124- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1134+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11251135 }
11261136
11271137 #[ test]
11281138 fn builds_invoice_request_with_derived_payer_id ( ) {
11291139 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
11301140 let entropy = FixedEntropy { } ;
11311141 let secp_ctx = Secp256k1 :: new ( ) ;
1142+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
11321143
11331144 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
11341145 . amount_msats ( 1000 )
11351146 . build ( ) . unwrap ( ) ;
11361147 let invoice_request = offer
1137- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1148+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
11381149 . unwrap ( )
11391150 . build_and_sign ( )
11401151 . unwrap ( ) ;
@@ -1143,7 +1154,10 @@ mod tests {
11431154 . unwrap ( )
11441155 . build ( ) . unwrap ( )
11451156 . sign ( recipient_sign) . unwrap ( ) ;
1146- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1157+ match invoice. verify ( & expanded_key, & secp_ctx) {
1158+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1159+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1160+ }
11471161
11481162 // Fails verification with altered fields
11491163 let (
@@ -1166,7 +1180,7 @@ mod tests {
11661180 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11671181
11681182 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1169- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1183+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11701184
11711185 // Fails verification with altered payer id
11721186 let (
@@ -1189,7 +1203,7 @@ mod tests {
11891203 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11901204
11911205 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1192- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1206+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11931207 }
11941208
11951209 #[ test]
0 commit comments