@@ -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
@@ -685,7 +690,7 @@ impl InvoiceRequestContents {
685
690
}
686
691
687
692
pub ( super ) fn derives_keys ( & self ) -> bool {
688
- self . inner . payer . 0 . derives_keys ( )
693
+ self . inner . payer . 0 . derives_payer_keys ( )
689
694
}
690
695
691
696
pub ( super ) fn chain ( & self ) -> ChainHash {
@@ -918,6 +923,7 @@ mod tests {
918
923
#[ cfg( feature = "std" ) ]
919
924
use core:: time:: Duration ;
920
925
use crate :: sign:: KeyMaterial ;
926
+ use crate :: ln:: channelmanager:: PaymentId ;
921
927
use crate :: ln:: features:: { InvoiceRequestFeatures , OfferFeatures } ;
922
928
use crate :: ln:: inbound_payment:: ExpandedKey ;
923
929
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
@@ -1063,12 +1069,13 @@ mod tests {
1063
1069
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1064
1070
let entropy = FixedEntropy { } ;
1065
1071
let secp_ctx = Secp256k1 :: new ( ) ;
1072
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1066
1073
1067
1074
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1068
1075
. amount_msats ( 1000 )
1069
1076
. build ( ) . unwrap ( ) ;
1070
1077
let invoice_request = offer
1071
- . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy)
1078
+ . request_invoice_deriving_metadata ( payer_id, & expanded_key, & entropy, payment_id )
1072
1079
. unwrap ( )
1073
1080
. build ( ) . unwrap ( )
1074
1081
. sign ( payer_sign) . unwrap ( ) ;
@@ -1078,7 +1085,10 @@ mod tests {
1078
1085
. unwrap ( )
1079
1086
. build ( ) . unwrap ( )
1080
1087
. sign ( recipient_sign) . unwrap ( ) ;
1081
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1088
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1089
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1090
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1091
+ }
1082
1092
1083
1093
// Fails verification with altered fields
1084
1094
let (
@@ -1101,7 +1111,7 @@ mod tests {
1101
1111
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1102
1112
1103
1113
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1104
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1114
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1105
1115
1106
1116
// Fails verification with altered metadata
1107
1117
let (
@@ -1124,20 +1134,21 @@ mod tests {
1124
1134
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1125
1135
1126
1136
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1127
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1137
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1128
1138
}
1129
1139
1130
1140
#[ test]
1131
1141
fn builds_invoice_request_with_derived_payer_id ( ) {
1132
1142
let expanded_key = ExpandedKey :: new ( & KeyMaterial ( [ 42 ; 32 ] ) ) ;
1133
1143
let entropy = FixedEntropy { } ;
1134
1144
let secp_ctx = Secp256k1 :: new ( ) ;
1145
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1135
1146
1136
1147
let offer = OfferBuilder :: new ( "foo" . into ( ) , recipient_pubkey ( ) )
1137
1148
. amount_msats ( 1000 )
1138
1149
. build ( ) . unwrap ( ) ;
1139
1150
let invoice_request = offer
1140
- . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx)
1151
+ . request_invoice_deriving_payer_id ( & expanded_key, & entropy, & secp_ctx, payment_id )
1141
1152
. unwrap ( )
1142
1153
. build_and_sign ( )
1143
1154
. unwrap ( ) ;
@@ -1146,7 +1157,10 @@ mod tests {
1146
1157
. unwrap ( )
1147
1158
. build ( ) . unwrap ( )
1148
1159
. sign ( recipient_sign) . unwrap ( ) ;
1149
- assert ! ( invoice. verify( & expanded_key, & secp_ctx) ) ;
1160
+ match invoice. verify ( & expanded_key, & secp_ctx) {
1161
+ Ok ( payment_id) => assert_eq ! ( payment_id, PaymentId ( [ 1 ; 32 ] ) ) ,
1162
+ Err ( ( ) ) => panic ! ( "verification failed" ) ,
1163
+ }
1150
1164
1151
1165
// Fails verification with altered fields
1152
1166
let (
@@ -1169,7 +1183,7 @@ mod tests {
1169
1183
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1170
1184
1171
1185
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1172
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1186
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1173
1187
1174
1188
// Fails verification with altered payer id
1175
1189
let (
@@ -1192,7 +1206,7 @@ mod tests {
1192
1206
signature_tlv_stream. write ( & mut encoded_invoice) . unwrap ( ) ;
1193
1207
1194
1208
let invoice = Bolt12Invoice :: try_from ( encoded_invoice) . unwrap ( ) ;
1195
- assert ! ( ! invoice. verify( & expanded_key, & secp_ctx) ) ;
1209
+ assert ! ( invoice. verify( & expanded_key, & secp_ctx) . is_err ( ) ) ;
1196
1210
}
1197
1211
1198
1212
#[ test]
0 commit comments