@@ -63,6 +63,7 @@ use crate::sign::EntropySource;
6363use crate :: io;
6464use crate :: blinded_path:: BlindedPath ;
6565use crate :: ln:: PaymentHash ;
66+ use crate :: ln:: channelmanager:: PaymentId ;
6667use crate :: ln:: features:: InvoiceRequestFeatures ;
6768use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN , Nonce } ;
6869use crate :: ln:: msgs:: DecodeError ;
@@ -127,10 +128,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
127128 }
128129
129130 pub ( super ) fn deriving_metadata < ES : Deref > (
130- offer : & ' a Offer , payer_id : PublicKey , expanded_key : & ExpandedKey , entropy_source : ES
131+ offer : & ' a Offer , payer_id : PublicKey , expanded_key : & ExpandedKey , entropy_source : ES ,
132+ payment_id : PaymentId ,
131133 ) -> Self where ES :: Target : EntropySource {
132134 let nonce = Nonce :: from_entropy_source ( entropy_source) ;
133- let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES ) ;
135+ let payment_id = Some ( payment_id) ;
136+ let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES , payment_id) ;
134137 let metadata = Metadata :: Derived ( derivation_material) ;
135138 Self {
136139 offer,
@@ -144,10 +147,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
144147
145148impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
146149 pub ( super ) fn deriving_payer_id < ES : Deref > (
147- offer : & ' a Offer , expanded_key : & ExpandedKey , entropy_source : ES , secp_ctx : & ' b Secp256k1 < T >
150+ offer : & ' a Offer , expanded_key : & ExpandedKey , entropy_source : ES ,
151+ secp_ctx : & ' b Secp256k1 < T > , payment_id : PaymentId
148152 ) -> Self where ES :: Target : EntropySource {
149153 let nonce = Nonce :: from_entropy_source ( entropy_source) ;
150- let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES ) ;
154+ let payment_id = Some ( payment_id) ;
155+ let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES , payment_id) ;
151156 let metadata = Metadata :: DerivedSigningPubkey ( derivation_material) ;
152157 Self {
153158 offer,
@@ -258,7 +263,7 @@ impl<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> InvoiceRequestBuilder<'a
258263 let mut tlv_stream = self . invoice_request . as_tlv_stream ( ) ;
259264 debug_assert ! ( tlv_stream. 2 . payer_id. is_none( ) ) ;
260265 tlv_stream. 0 . metadata = None ;
261- if !metadata. derives_keys ( ) {
266+ if !metadata. derives_payer_keys ( ) {
262267 tlv_stream. 2 . payer_id = self . payer_id . as_ref ( ) ;
263268 }
264269
@@ -645,7 +650,7 @@ impl InvoiceRequestContents {
645650 }
646651
647652 pub ( super ) fn derives_keys ( & self ) -> bool {
648- self . inner . payer . 0 . derives_keys ( )
653+ self . inner . payer . 0 . derives_payer_keys ( )
649654 }
650655
651656 pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -836,6 +841,7 @@ mod tests {
836841 #[ cfg( feature = "std" ) ]
837842 use core:: time:: Duration ;
838843 use crate :: sign:: KeyMaterial ;
844+ use crate :: ln:: channelmanager:: PaymentId ;
839845 use crate :: ln:: features:: InvoiceRequestFeatures ;
840846 use crate :: ln:: inbound_payment:: ExpandedKey ;
841847 use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -940,12 +946,13 @@ mod tests {
940946 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
941947 let entropy = FixedEntropy { } ;
942948 let secp_ctx = Secp256k1 :: new ( ) ;
949+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
943950
944951 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
945952 . amount_msats ( 1000 )
946953 . build ( ) . unwrap ( ) ;
947954 let invoice_request = offer
948- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
955+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
949956 . unwrap ( )
950957 . build ( ) . unwrap ( )
951958 . sign ( payer_sign) . unwrap ( ) ;
@@ -955,7 +962,10 @@ mod tests {
955962 . unwrap ( )
956963 . build ( ) . unwrap ( )
957964 . sign ( recipient_sign) . unwrap ( ) ;
958- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
965+ match invoice. verify ( & expanded_key, & secp_ctx) {
966+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
967+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
968+ }
959969
960970 // Fails verification with altered fields
961971 let (
@@ -978,7 +988,7 @@ mod tests {
978988 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
979989
980990 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
981- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
991+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
982992
983993 // Fails verification with altered metadata
984994 let (
@@ -1001,20 +1011,21 @@ mod tests {
10011011 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
10021012
10031013 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1004- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1014+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
10051015 }
10061016
10071017 #[ test]
10081018 fn builds_invoice_request_with_derived_payer_id ( ) {
10091019 let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
10101020 let entropy = FixedEntropy { } ;
10111021 let secp_ctx = Secp256k1 :: new ( ) ;
1022+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
10121023
10131024 let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
10141025 . amount_msats ( 1000 )
10151026 . build ( ) . unwrap ( ) ;
10161027 let invoice_request = offer
1017- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1028+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
10181029 . unwrap ( )
10191030 . build_and_sign ( )
10201031 . unwrap ( ) ;
@@ -1023,7 +1034,10 @@ mod tests {
10231034 . unwrap ( )
10241035 . build ( ) . unwrap ( )
10251036 . sign ( recipient_sign) . unwrap ( ) ;
1026- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1037+ match invoice. verify ( & expanded_key, & secp_ctx) {
1038+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1039+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1040+ }
10271041
10281042 // Fails verification with altered fields
10291043 let (
@@ -1046,7 +1060,7 @@ mod tests {
10461060 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
10471061
10481062 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1049- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1063+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
10501064
10511065 // Fails verification with altered payer id
10521066 let (
@@ -1069,7 +1083,7 @@ mod tests {
10691083 signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
10701084
10711085 let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1072- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1086+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
10731087 }
10741088
10751089 #[ test]
0 commit comments