@@ -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
@@ -691,7 +696,7 @@ impl InvoiceRequestContents {
691696 }
692697
693698 pub ( super ) fn derives_keys ( & self ) -> bool {
694- self . inner . payer . 0 . derives_keys ( )
699+ self . inner . payer . 0 . derives_payer_keys ( )
695700 }
696701
697702 pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -924,6 +929,7 @@ mod tests {
924929 #[ cfg( feature = "std" ) ]
925930 use core:: time:: Duration ;
926931 use crate :: sign:: KeyMaterial ;
932+ use crate :: ln:: channelmanager:: PaymentId ;
927933 use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
928934 use crate :: ln:: inbound_payment:: ExpandedKey ;
929935 use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1069,12 +1075,13 @@ mod tests {
10691075 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
10701076 let entropy = FixedEntropy { } ;
10711077 let secp_ctx = Secp256k1 :: new ( ) ;
1078+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
10721079
10731080 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
10741081 . amount_msats ( 1000 )
10751082 . build ( ) . unwrap ( ) ;
10761083 let invoice_request = offer
1077- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1084+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
10781085 . unwrap ( )
10791086 . build ( ) . unwrap ( )
10801087 . sign ( payer_sign) . unwrap ( ) ;
@@ -1084,7 +1091,10 @@ mod tests {
10841091 . unwrap ( )
10851092 . build ( ) . unwrap ( )
10861093 . sign ( recipient_sign) . unwrap ( ) ;
1087- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1094+ match invoice. verify ( & expanded_key, & secp_ctx) {
1095+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1096+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1097+ }
10881098
10891099 // Fails verification with altered fields
10901100 let (
@@ -1107,7 +1117,7 @@ mod tests {
11071117 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11081118
11091119 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1110- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1120+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11111121
11121122 // Fails verification with altered metadata
11131123 let (
@@ -1130,20 +1140,21 @@ mod tests {
11301140 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11311141
11321142 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1133- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1143+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11341144 }
11351145
11361146 #[ test]
11371147 fn builds_invoice_request_with_derived_payer_id ( ) {
11381148 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
11391149 let entropy = FixedEntropy { } ;
11401150 let secp_ctx = Secp256k1 :: new ( ) ;
1151+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
11411152
11421153 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
11431154 . amount_msats ( 1000 )
11441155 . build ( ) . unwrap ( ) ;
11451156 let invoice_request = offer
1146- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1157+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
11471158 . unwrap ( )
11481159 . build_and_sign ( )
11491160 . unwrap ( ) ;
@@ -1152,7 +1163,10 @@ mod tests {
11521163 . unwrap ( )
11531164 . build ( ) . unwrap ( )
11541165 . sign ( recipient_sign) . unwrap ( ) ;
1155- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1166+ match invoice. verify ( & expanded_key, & secp_ctx) {
1167+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1168+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1169+ }
11561170
11571171 // Fails verification with altered fields
11581172 let (
@@ -1175,7 +1189,7 @@ mod tests {
11751189 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11761190
11771191 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1178- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1192+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
11791193
11801194 // Fails verification with altered payer id
11811195 let (
@@ -1198,7 +1212,7 @@ mod tests {
11981212 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
11991213
12001214 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1201- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1215+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
12021216 }
12031217
12041218 #[ test]
0 commit comments