@@ -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
@@ -685,7 +690,7 @@ impl InvoiceRequestContents {
685690 }
686691
687692 pub ( super ) fn derives_keys ( & self ) -> bool {
688- self . inner . payer . 0 . derives_keys ( )
693+ self . inner . payer . 0 . derives_payer_keys ( )
689694 }
690695
691696 pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -918,6 +923,7 @@ mod tests {
918923 #[ cfg( feature = "std" ) ]
919924 use core:: time:: Duration ;
920925 use crate :: sign:: KeyMaterial ;
926+ use crate :: ln:: channelmanager:: PaymentId ;
921927 use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
922928 use crate :: ln:: inbound_payment:: ExpandedKey ;
923929 use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1063,12 +1069,13 @@ mod tests {
10631069 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
10641070 let entropy = FixedEntropy { } ;
10651071 let secp_ctx = Secp256k1 :: new ( ) ;
1072+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
10661073
10671074 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
10681075 . amount_msats ( 1000 )
10691076 . build ( ) . unwrap ( ) ;
10701077 let invoice_request = offer
1071- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1078+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
10721079 . unwrap ( )
10731080 . build ( ) . unwrap ( )
10741081 . sign ( payer_sign) . unwrap ( ) ;
@@ -1078,7 +1085,10 @@ mod tests {
10781085 . unwrap ( )
10791086 . build ( ) . unwrap ( )
10801087 . sign ( recipient_sign) . unwrap ( ) ;
1081- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1088+ match invoice. verify ( & expanded_key, & secp_ctx) {
1089+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1090+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1091+ }
10821092
10831093 // Fails verification with altered fields
10841094 let (
@@ -1101,7 +1111,7 @@ mod tests {
11011111 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11021112
11031113 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1104- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1114+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11051115
11061116 // Fails verification with altered metadata
11071117 let (
@@ -1124,20 +1134,21 @@ mod tests {
11241134 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11251135
11261136 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1127- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1137+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11281138 }
11291139
11301140 #[ test]
11311141 fn builds_invoice_request_with_derived_payer_id ( ) {
11321142 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
11331143 let entropy = FixedEntropy { } ;
11341144 let secp_ctx = Secp256k1 :: new ( ) ;
1145+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
11351146
11361147 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
11371148 . amount_msats ( 1000 )
11381149 . build ( ) . unwrap ( ) ;
11391150 let invoice_request = offer
1140- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1151+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
11411152 . unwrap ( )
11421153 . build_and_sign ( )
11431154 . unwrap ( ) ;
@@ -1146,7 +1157,10 @@ mod tests {
11461157 . unwrap ( )
11471158 . build ( ) . unwrap ( )
11481159 . sign ( recipient_sign) . unwrap ( ) ;
1149- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1160+ match invoice. verify ( & expanded_key, & secp_ctx) {
1161+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1162+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1163+ }
11501164
11511165 // Fails verification with altered fields
11521166 let (
@@ -1169,7 +1183,7 @@ mod tests {
11691183 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11701184
11711185 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1172- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1186+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11731187
11741188 // Fails verification with altered payer id
11751189 let (
@@ -1192,7 +1206,7 @@ mod tests {
11921206 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11931207
11941208 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1195- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1209+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11961210 }
11971211
11981212 #[ test]
0 commit comments