@@ -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
@@ -691,7 +696,7 @@ impl InvoiceRequestContents {
691
696
}
692
697
693
698
pub ( super ) fn derives_keys ( & self ) -> bool {
694
- self . inner . payer . 0 . derives_keys ( )
699
+ self . inner . payer . 0 . derives_payer_keys ( )
695
700
}
696
701
697
702
pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -924,6 +929,7 @@ mod tests {
924
929
#[ cfg( feature = "std" ) ]
925
930
use core:: time:: Duration ;
926
931
use crate :: sign:: KeyMaterial ;
932
+ use crate :: ln:: channelmanager:: PaymentId ;
927
933
use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
928
934
use crate :: ln:: inbound_payment:: ExpandedKey ;
929
935
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1069,12 +1075,13 @@ mod tests {
1069
1075
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1070
1076
let entropy = FixedEntropy { } ;
1071
1077
let secp_ctx = Secp256k1 :: new ( ) ;
1078
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1072
1079
1073
1080
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1074
1081
. amount_msats ( 1000 )
1075
1082
. build ( ) . unwrap ( ) ;
1076
1083
let invoice_request = offer
1077
- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1084
+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
1078
1085
. unwrap ( )
1079
1086
. build ( ) . unwrap ( )
1080
1087
. sign ( payer_sign) . unwrap ( ) ;
@@ -1084,7 +1091,10 @@ mod tests {
1084
1091
. unwrap ( )
1085
1092
. build ( ) . unwrap ( )
1086
1093
. sign ( recipient_sign) . unwrap ( ) ;
1087
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1094
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1095
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1096
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1097
+ }
1088
1098
1089
1099
// Fails verification with altered fields
1090
1100
let (
@@ -1107,7 +1117,7 @@ mod tests {
1107
1117
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1108
1118
1109
1119
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1110
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1120
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1111
1121
1112
1122
// Fails verification with altered metadata
1113
1123
let (
@@ -1130,20 +1140,21 @@ mod tests {
1130
1140
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1131
1141
1132
1142
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1133
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1143
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1134
1144
}
1135
1145
1136
1146
#[ test]
1137
1147
fn builds_invoice_request_with_derived_payer_id ( ) {
1138
1148
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1139
1149
let entropy = FixedEntropy { } ;
1140
1150
let secp_ctx = Secp256k1 :: new ( ) ;
1151
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1141
1152
1142
1153
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1143
1154
. amount_msats ( 1000 )
1144
1155
. build ( ) . unwrap ( ) ;
1145
1156
let invoice_request = offer
1146
- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1157
+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
1147
1158
. unwrap ( )
1148
1159
. build_and_sign ( )
1149
1160
. unwrap ( ) ;
@@ -1152,7 +1163,10 @@ mod tests {
1152
1163
. unwrap ( )
1153
1164
. build ( ) . unwrap ( )
1154
1165
. sign ( recipient_sign) . unwrap ( ) ;
1155
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1166
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1167
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1168
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1169
+ }
1156
1170
1157
1171
// Fails verification with altered fields
1158
1172
let (
@@ -1175,7 +1189,7 @@ mod tests {
1175
1189
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1176
1190
1177
1191
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1178
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1192
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1179
1193
1180
1194
// Fails verification with altered payer id
1181
1195
let (
@@ -1198,7 +1212,7 @@ mod tests {
1198
1212
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1199
1213
1200
1214
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1201
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1215
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1202
1216
}
1203
1217
1204
1218
#[ test]
0 commit comments