@@ -64,6 +64,7 @@ use crate::sign::EntropySource;
64
64
use crate :: io;
65
65
use crate :: blinded_path:: BlindedPath ;
66
66
use crate :: ln:: PaymentHash ;
67
+ use crate :: ln:: channelmanager:: PaymentId ;
67
68
use crate :: ln:: features:: InvoiceRequestFeatures ;
68
69
use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN , Nonce } ;
69
70
use crate :: ln:: msgs:: DecodeError ;
@@ -128,10 +129,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
128
129
}
129
130
130
131
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 ,
132
134
) -> Self where ES :: Target : EntropySource {
133
135
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) ;
135
138
let metadata = Metadata :: Derived ( derivation_material) ;
136
139
Self {
137
140
offer,
@@ -145,10 +148,12 @@ impl<'a, 'b, T: secp256k1::Signing> InvoiceRequestBuilder<'a, 'b, ExplicitPayerI
145
148
146
149
impl < ' a , ' b , T : secp256k1:: Signing > InvoiceRequestBuilder < ' a , ' b , DerivedPayerId , T > {
147
150
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
149
153
) -> Self where ES :: Target : EntropySource {
150
154
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) ;
152
157
let metadata = Metadata :: DerivedSigningPubkey ( derivation_material) ;
153
158
Self {
154
159
offer,
@@ -259,7 +264,7 @@ impl<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> InvoiceRequestBuilder<'a
259
264
let mut tlv_stream = self . invoice_request . as_tlv_stream ( ) ;
260
265
debug_assert ! ( tlv_stream. 2 . payer_id. is_none( ) ) ;
261
266
tlv_stream. 0 . metadata = None ;
262
- if !metadata. derives_keys ( ) {
267
+ if !metadata. derives_payer_keys ( ) {
263
268
tlv_stream. 2 . payer_id = self . payer_id . as_ref ( ) ;
264
269
}
265
270
@@ -682,7 +687,7 @@ impl InvoiceRequestContents {
682
687
}
683
688
684
689
pub ( super ) fn derives_keys ( & self ) -> bool {
685
- self . inner . payer . 0 . derives_keys ( )
690
+ self . inner . payer . 0 . derives_payer_keys ( )
686
691
}
687
692
688
693
pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -915,6 +920,7 @@ mod tests {
915
920
#[ cfg( feature = "std" ) ]
916
921
use core:: time:: Duration ;
917
922
use crate :: sign:: KeyMaterial ;
923
+ use crate :: ln:: channelmanager:: PaymentId ;
918
924
use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
919
925
use crate :: ln:: inbound_payment:: ExpandedKey ;
920
926
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1060,12 +1066,13 @@ mod tests {
1060
1066
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1061
1067
let entropy = FixedEntropy { } ;
1062
1068
let secp_ctx = Secp256k1 :: new ( ) ;
1069
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1063
1070
1064
1071
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1065
1072
. amount_msats ( 1000 )
1066
1073
. build ( ) . unwrap ( ) ;
1067
1074
let invoice_request = offer
1068
- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1075
+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
1069
1076
. unwrap ( )
1070
1077
. build ( ) . unwrap ( )
1071
1078
. sign ( payer_sign) . unwrap ( ) ;
@@ -1075,7 +1082,10 @@ mod tests {
1075
1082
. unwrap ( )
1076
1083
. build ( ) . unwrap ( )
1077
1084
. sign ( recipient_sign) . unwrap ( ) ;
1078
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1085
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1086
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1087
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1088
+ }
1079
1089
1080
1090
// Fails verification with altered fields
1081
1091
let (
@@ -1098,7 +1108,7 @@ mod tests {
1098
1108
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1099
1109
1100
1110
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1101
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1111
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1102
1112
1103
1113
// Fails verification with altered metadata
1104
1114
let (
@@ -1121,20 +1131,21 @@ mod tests {
1121
1131
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1122
1132
1123
1133
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1124
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1134
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1125
1135
}
1126
1136
1127
1137
#[ test]
1128
1138
fn builds_invoice_request_with_derived_payer_id ( ) {
1129
1139
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1130
1140
let entropy = FixedEntropy { } ;
1131
1141
let secp_ctx = Secp256k1 :: new ( ) ;
1142
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1132
1143
1133
1144
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1134
1145
. amount_msats ( 1000 )
1135
1146
. build ( ) . unwrap ( ) ;
1136
1147
let invoice_request = offer
1137
- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1148
+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
1138
1149
. unwrap ( )
1139
1150
. build_and_sign ( )
1140
1151
. unwrap ( ) ;
@@ -1143,7 +1154,10 @@ mod tests {
1143
1154
. unwrap ( )
1144
1155
. build ( ) . unwrap ( )
1145
1156
. sign ( recipient_sign) . unwrap ( ) ;
1146
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1157
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1158
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1159
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1160
+ }
1147
1161
1148
1162
// Fails verification with altered fields
1149
1163
let (
@@ -1166,7 +1180,7 @@ mod tests {
1166
1180
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1167
1181
1168
1182
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1169
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1183
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1170
1184
1171
1185
// Fails verification with altered payer id
1172
1186
let (
@@ -1189,7 +1203,7 @@ mod tests {
1189
1203
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1190
1204
1191
1205
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1192
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1206
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1193
1207
}
1194
1208
1195
1209
#[ test]
0 commit comments