@@ -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
@@ -680,7 +685,7 @@ impl InvoiceRequestContents {
680
685
}
681
686
682
687
pub ( super ) fn derives_keys ( & self ) -> bool {
683
- self . inner . payer . 0 . derives_keys ( )
688
+ self . inner . payer . 0 . derives_payer_keys ( )
684
689
}
685
690
686
691
pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -913,6 +918,7 @@ mod tests {
913
918
#[ cfg( feature = "std" ) ]
914
919
use core:: time:: Duration ;
915
920
use crate :: sign:: KeyMaterial ;
921
+ use crate :: ln:: channelmanager:: PaymentId ;
916
922
use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
917
923
use crate :: ln:: inbound_payment:: ExpandedKey ;
918
924
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1058,12 +1064,13 @@ mod tests {
1058
1064
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1059
1065
let entropy = FixedEntropy { } ;
1060
1066
let secp_ctx = Secp256k1 :: new ( ) ;
1067
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1061
1068
1062
1069
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1063
1070
. amount_msats ( 1000 )
1064
1071
. build ( ) . unwrap ( ) ;
1065
1072
let invoice_request = offer
1066
- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1073
+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
1067
1074
. unwrap ( )
1068
1075
. build ( ) . unwrap ( )
1069
1076
. sign ( payer_sign) . unwrap ( ) ;
@@ -1073,7 +1080,10 @@ mod tests {
1073
1080
. unwrap ( )
1074
1081
. build ( ) . unwrap ( )
1075
1082
. sign ( recipient_sign) . unwrap ( ) ;
1076
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1083
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1084
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1085
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1086
+ }
1077
1087
1078
1088
// Fails verification with altered fields
1079
1089
let (
@@ -1096,7 +1106,7 @@ mod tests {
1096
1106
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1097
1107
1098
1108
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1099
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1109
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1100
1110
1101
1111
// Fails verification with altered metadata
1102
1112
let (
@@ -1119,20 +1129,21 @@ mod tests {
1119
1129
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1120
1130
1121
1131
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1122
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1132
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1123
1133
}
1124
1134
1125
1135
#[ test]
1126
1136
fn builds_invoice_request_with_derived_payer_id ( ) {
1127
1137
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1128
1138
let entropy = FixedEntropy { } ;
1129
1139
let secp_ctx = Secp256k1 :: new ( ) ;
1140
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1130
1141
1131
1142
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1132
1143
. amount_msats ( 1000 )
1133
1144
. build ( ) . unwrap ( ) ;
1134
1145
let invoice_request = offer
1135
- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1146
+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
1136
1147
. unwrap ( )
1137
1148
. build_and_sign ( )
1138
1149
. unwrap ( ) ;
@@ -1141,7 +1152,10 @@ mod tests {
1141
1152
. unwrap ( )
1142
1153
. build ( ) . unwrap ( )
1143
1154
. sign ( recipient_sign) . unwrap ( ) ;
1144
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1155
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1156
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1157
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1158
+ }
1145
1159
1146
1160
// Fails verification with altered fields
1147
1161
let (
@@ -1164,7 +1178,7 @@ mod tests {
1164
1178
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1165
1179
1166
1180
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1167
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1181
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1168
1182
1169
1183
// Fails verification with altered payer id
1170
1184
let (
@@ -1187,7 +1201,7 @@ mod tests {
1187
1201
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1188
1202
1189
1203
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1190
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1204
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1191
1205
}
1192
1206
1193
1207
#[ test]
0 commit comments