@@ -65,6 +65,7 @@ use crate::sign::EntropySource;
65
65
use crate :: io;
66
66
use crate :: blinded_path:: BlindedPath ;
67
67
use crate :: ln:: PaymentHash ;
68
+ use crate :: ln:: channelmanager:: PaymentId ;
68
69
use crate :: ln:: features:: InvoiceRequestFeatures ;
69
70
use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN , Nonce } ;
70
71
use crate :: ln:: msgs:: DecodeError ;
@@ -129,10 +130,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
129
130
}
130
131
131
132
pub ( super ) fn deriving_metadata < ES : Deref > (
132
- offer : & ' a Offer , payer_id : PublicKey , expanded_key : & ExpandedKey , entropy_source : ES
133
+ offer : & ' a Offer , payer_id : PublicKey , expanded_key : & ExpandedKey , entropy_source : ES ,
134
+ payment_id : PaymentId ,
133
135
) -> Self where ES :: Target : EntropySource {
134
136
let nonce = Nonce :: from_entropy_source ( entropy_source) ;
135
- let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES ) ;
137
+ let payment_id = Some ( payment_id) ;
138
+ let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES , payment_id) ;
136
139
let metadata = Metadata :: Derived ( derivation_material) ;
137
140
Self {
138
141
offer,
@@ -146,10 +149,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
146
149
147
150
impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
148
151
pub ( super ) fn deriving_payer_id < ES : Deref > (
149
- offer : & ' a Offer , expanded_key : & ExpandedKey , entropy_source : ES , secp_ctx : & ' b Secp256k1 < T >
152
+ offer : & ' a Offer , expanded_key : & ExpandedKey , entropy_source : ES ,
153
+ secp_ctx : & ' b Secp256k1 < T > , payment_id : PaymentId
150
154
) -> Self where ES :: Target : EntropySource {
151
155
let nonce = Nonce :: from_entropy_source ( entropy_source) ;
152
- let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES ) ;
156
+ let payment_id = Some ( payment_id) ;
157
+ let derivation_material = MetadataMaterial :: new ( nonce, expanded_key, IV_BYTES , payment_id) ;
153
158
let metadata = Metadata :: DerivedSigningPubkey ( derivation_material) ;
154
159
Self {
155
160
offer,
@@ -260,7 +265,7 @@ impl<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> InvoiceRequestBuilder<'a
260
265
let mut tlv_stream = self . invoice_request . as_tlv_stream ( ) ;
261
266
debug_assert ! ( tlv_stream. 2 . payer_id. is_none( ) ) ;
262
267
tlv_stream. 0 . metadata = None ;
263
- if !metadata. derives_keys ( ) {
268
+ if !metadata. derives_payer_keys ( ) {
264
269
tlv_stream. 2 . payer_id = self . payer_id . as_ref ( ) ;
265
270
}
266
271
@@ -648,7 +653,7 @@ impl InvoiceRequestContents {
648
653
}
649
654
650
655
pub ( super ) fn derives_keys ( & self ) -> bool {
651
- self . inner . payer . 0 . derives_keys ( )
656
+ self . inner . payer . 0 . derives_payer_keys ( )
652
657
}
653
658
654
659
pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -839,6 +844,7 @@ mod tests {
839
844
#[ cfg( feature = "std" ) ]
840
845
use core:: time:: Duration ;
841
846
use crate :: sign:: KeyMaterial ;
847
+ use crate :: ln:: channelmanager:: PaymentId ;
842
848
use crate :: ln:: features:: InvoiceRequestFeatures ;
843
849
use crate :: ln:: inbound_payment:: ExpandedKey ;
844
850
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -945,12 +951,13 @@ mod tests {
945
951
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
946
952
let entropy = FixedEntropy { } ;
947
953
let secp_ctx = Secp256k1 :: new ( ) ;
954
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
948
955
949
956
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
950
957
. amount_msats ( 1000 )
951
958
. build ( ) . unwrap ( ) ;
952
959
let invoice_request = offer
953
- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
960
+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
954
961
. unwrap ( )
955
962
. build ( ) . unwrap ( )
956
963
. sign ( payer_sign) . unwrap ( ) ;
@@ -960,7 +967,10 @@ mod tests {
960
967
. unwrap ( )
961
968
. build ( ) . unwrap ( )
962
969
. sign ( recipient_sign) . unwrap ( ) ;
963
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
970
+ match invoice. verify ( & expanded_key, & secp_ctx) {
971
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
972
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
973
+ }
964
974
965
975
// Fails verification with altered fields
966
976
let (
@@ -985,7 +995,7 @@ mod tests {
985
995
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
986
996
987
997
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
988
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
998
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
989
999
990
1000
// Fails verification with altered metadata
991
1001
let (
@@ -1010,20 +1020,21 @@ mod tests {
1010
1020
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1011
1021
1012
1022
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1013
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1023
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1014
1024
}
1015
1025
1016
1026
#[ test]
1017
1027
fn builds_invoice_request_with_derived_payer_id ( ) {
1018
1028
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1019
1029
let entropy = FixedEntropy { } ;
1020
1030
let secp_ctx = Secp256k1 :: new ( ) ;
1031
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1021
1032
1022
1033
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1023
1034
. amount_msats ( 1000 )
1024
1035
. build ( ) . unwrap ( ) ;
1025
1036
let invoice_request = offer
1026
- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1037
+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
1027
1038
. unwrap ( )
1028
1039
. build_and_sign ( )
1029
1040
. unwrap ( ) ;
@@ -1032,7 +1043,10 @@ mod tests {
1032
1043
. unwrap ( )
1033
1044
. build ( ) . unwrap ( )
1034
1045
. sign ( recipient_sign) . unwrap ( ) ;
1035
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1046
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1047
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1048
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1049
+ }
1036
1050
1037
1051
// Fails verification with altered fields
1038
1052
let (
@@ -1057,7 +1071,7 @@ mod tests {
1057
1071
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1058
1072
1059
1073
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1060
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1074
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1061
1075
1062
1076
// Fails verification with altered payer id
1063
1077
let (
@@ -1082,7 +1096,7 @@ mod tests {
1082
1096
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1083
1097
1084
1098
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1085
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1099
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1086
1100
}
1087
1101
1088
1102
#[ test]
0 commit comments