@@ -63,6 +63,7 @@ use crate::sign::EntropySource;
63
63
use crate :: io;
64
64
use crate :: blinded_path:: BlindedPath ;
65
65
use crate :: ln:: PaymentHash ;
66
+ use crate :: ln:: channelmanager:: PaymentId ;
66
67
use crate :: ln:: features:: InvoiceRequestFeatures ;
67
68
use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN , Nonce } ;
68
69
use crate :: ln:: msgs:: DecodeError ;
@@ -127,10 +128,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
127
128
}
128
129
129
130
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 ,
131
133
) -> Self where ES :: Target : EntropySource {
132
134
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) ;
134
137
let metadata = Metadata :: Derived ( derivation_material) ;
135
138
Self {
136
139
offer,
@@ -144,10 +147,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
144
147
145
148
impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
146
149
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
148
152
) -> Self where ES :: Target : EntropySource {
149
153
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) ;
151
156
let metadata = Metadata :: DerivedSigningPubkey ( derivation_material) ;
152
157
Self {
153
158
offer,
@@ -258,7 +263,7 @@ impl<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> InvoiceRequestBuilder<'a
258
263
let mut tlv_stream = self . invoice_request . as_tlv_stream ( ) ;
259
264
debug_assert ! ( tlv_stream. 2 . payer_id. is_none( ) ) ;
260
265
tlv_stream. 0 . metadata = None ;
261
- if !metadata. derives_keys ( ) {
266
+ if !metadata. derives_payer_keys ( ) {
262
267
tlv_stream. 2 . payer_id = self . payer_id . as_ref ( ) ;
263
268
}
264
269
@@ -645,7 +650,7 @@ impl InvoiceRequestContents {
645
650
}
646
651
647
652
pub ( super ) fn derives_keys ( & self ) -> bool {
648
- self . inner . payer . 0 . derives_keys ( )
653
+ self . inner . payer . 0 . derives_payer_keys ( )
649
654
}
650
655
651
656
pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -836,6 +841,7 @@ mod tests {
836
841
#[ cfg( feature = "std" ) ]
837
842
use core:: time:: Duration ;
838
843
use crate :: sign:: KeyMaterial ;
844
+ use crate :: ln:: channelmanager:: PaymentId ;
839
845
use crate :: ln:: features:: InvoiceRequestFeatures ;
840
846
use crate :: ln:: inbound_payment:: ExpandedKey ;
841
847
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -940,12 +946,13 @@ mod tests {
940
946
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
941
947
let entropy = FixedEntropy { } ;
942
948
let secp_ctx = Secp256k1 :: new ( ) ;
949
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
943
950
944
951
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
945
952
. amount_msats ( 1000 )
946
953
. build ( ) . unwrap ( ) ;
947
954
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 )
949
956
. unwrap ( )
950
957
. build ( ) . unwrap ( )
951
958
. sign ( payer_sign) . unwrap ( ) ;
@@ -955,7 +962,10 @@ mod tests {
955
962
. unwrap ( )
956
963
. build ( ) . unwrap ( )
957
964
. 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
+ }
959
969
960
970
// Fails verification with altered fields
961
971
let (
@@ -978,7 +988,7 @@ mod tests {
978
988
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
979
989
980
990
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 ( ) ) ;
982
992
983
993
// Fails verification with altered metadata
984
994
let (
@@ -1001,20 +1011,21 @@ mod tests {
1001
1011
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1002
1012
1003
1013
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 ( ) ) ;
1005
1015
}
1006
1016
1007
1017
#[ test]
1008
1018
fn builds_invoice_request_with_derived_payer_id ( ) {
1009
1019
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1010
1020
let entropy = FixedEntropy { } ;
1011
1021
let secp_ctx = Secp256k1 :: new ( ) ;
1022
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1012
1023
1013
1024
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1014
1025
. amount_msats ( 1000 )
1015
1026
. build ( ) . unwrap ( ) ;
1016
1027
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 )
1018
1029
. unwrap ( )
1019
1030
. build_and_sign ( )
1020
1031
. unwrap ( ) ;
@@ -1023,7 +1034,10 @@ mod tests {
1023
1034
. unwrap ( )
1024
1035
. build ( ) . unwrap ( )
1025
1036
. 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
+ }
1027
1041
1028
1042
// Fails verification with altered fields
1029
1043
let (
@@ -1046,7 +1060,7 @@ mod tests {
1046
1060
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1047
1061
1048
1062
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 ( ) ) ;
1050
1064
1051
1065
// Fails verification with altered payer id
1052
1066
let (
@@ -1069,7 +1083,7 @@ mod tests {
1069
1083
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1070
1084
1071
1085
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 ( ) ) ;
1073
1087
}
1074
1088
1075
1089
#[ test]
0 commit comments