9
9
10
10
//! Utilities for signing offer messages and verifying metadata.
11
11
12
- use bitcoin:: hashes:: { Hash , HashEngine } ;
13
- use bitcoin:: hashes:: cmp:: fixed_time_eq;
14
- use bitcoin:: hashes:: hmac:: { Hmac , HmacEngine } ;
15
- use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
16
- use bitcoin:: secp256k1:: { Keypair , PublicKey , Secp256k1 , SecretKey , self } ;
17
- use types:: payment:: PaymentHash ;
18
- use core:: fmt;
19
12
use crate :: blinded_path:: payment:: UnauthenticatedReceiveTlvs ;
20
13
use crate :: ln:: channelmanager:: PaymentId ;
21
14
use crate :: ln:: inbound_payment:: { ExpandedKey , IV_LEN } ;
22
15
use crate :: offers:: merkle:: TlvRecord ;
23
16
use crate :: offers:: nonce:: Nonce ;
24
17
use crate :: util:: ser:: Writeable ;
18
+ use bitcoin:: hashes:: cmp:: fixed_time_eq;
19
+ use bitcoin:: hashes:: hmac:: { Hmac , HmacEngine } ;
20
+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
21
+ use bitcoin:: hashes:: { Hash , HashEngine } ;
22
+ use bitcoin:: secp256k1:: { self , Keypair , PublicKey , Secp256k1 , SecretKey } ;
23
+ use core:: fmt;
24
+ use types:: payment:: PaymentHash ;
25
25
26
26
use crate :: prelude:: * ;
27
27
@@ -99,15 +99,24 @@ impl Metadata {
99
99
pub fn as_bytes ( & self ) -> Option < & Vec < u8 > > {
100
100
match self {
101
101
Metadata :: Bytes ( bytes) => Some ( bytes) ,
102
- _ => { debug_assert ! ( false ) ; None } ,
102
+ _ => {
103
+ debug_assert ! ( false ) ;
104
+ None
105
+ } ,
103
106
}
104
107
}
105
108
106
109
pub fn has_derivation_material ( & self ) -> bool {
107
110
match self {
108
111
Metadata :: Bytes ( _) => false ,
109
- Metadata :: RecipientData ( _) => { debug_assert ! ( false ) ; false } ,
110
- Metadata :: PayerData ( _) => { debug_assert ! ( false ) ; false } ,
112
+ Metadata :: RecipientData ( _) => {
113
+ debug_assert ! ( false ) ;
114
+ false
115
+ } ,
116
+ Metadata :: PayerData ( _) => {
117
+ debug_assert ! ( false ) ;
118
+ false
119
+ } ,
111
120
Metadata :: Derived ( _) => true ,
112
121
Metadata :: DerivedSigningPubkey ( _) => true ,
113
122
}
@@ -151,20 +160,32 @@ impl Metadata {
151
160
pub fn without_keys ( self ) -> Self {
152
161
match self {
153
162
Metadata :: Bytes ( _) => self ,
154
- Metadata :: RecipientData ( _) => { debug_assert ! ( false ) ; self } ,
155
- Metadata :: PayerData ( _) => { debug_assert ! ( false ) ; self } ,
163
+ Metadata :: RecipientData ( _) => {
164
+ debug_assert ! ( false ) ;
165
+ self
166
+ } ,
167
+ Metadata :: PayerData ( _) => {
168
+ debug_assert ! ( false ) ;
169
+ self
170
+ } ,
156
171
Metadata :: Derived ( _) => self ,
157
172
Metadata :: DerivedSigningPubkey ( material) => Metadata :: Derived ( material) ,
158
173
}
159
174
}
160
175
161
176
pub fn derive_from < W : Writeable , T : secp256k1:: Signing > (
162
- self , iv_bytes : & [ u8 ; IV_LEN ] , tlv_stream : W , secp_ctx : Option < & Secp256k1 < T > >
177
+ self , iv_bytes : & [ u8 ; IV_LEN ] , tlv_stream : W , secp_ctx : Option < & Secp256k1 < T > > ,
163
178
) -> ( Self , Option < Keypair > ) {
164
179
match self {
165
180
Metadata :: Bytes ( _) => ( self , None ) ,
166
- Metadata :: RecipientData ( _) => { debug_assert ! ( false ) ; ( self , None ) } ,
167
- Metadata :: PayerData ( _) => { debug_assert ! ( false ) ; ( self , None ) } ,
181
+ Metadata :: RecipientData ( _) => {
182
+ debug_assert ! ( false ) ;
183
+ ( self , None )
184
+ } ,
185
+ Metadata :: PayerData ( _) => {
186
+ debug_assert ! ( false ) ;
187
+ ( self , None )
188
+ } ,
168
189
Metadata :: Derived ( metadata_material) => {
169
190
( Metadata :: Bytes ( metadata_material. derive_metadata ( iv_bytes, tlv_stream) ) , None )
170
191
} ,
@@ -190,8 +211,14 @@ impl AsRef<[u8]> for Metadata {
190
211
Metadata :: Bytes ( bytes) => & bytes,
191
212
Metadata :: RecipientData ( nonce) => & nonce. 0 ,
192
213
Metadata :: PayerData ( bytes) => bytes. as_slice ( ) ,
193
- Metadata :: Derived ( _) => { debug_assert ! ( false ) ; & [ ] } ,
194
- Metadata :: DerivedSigningPubkey ( _) => { debug_assert ! ( false ) ; & [ ] } ,
214
+ Metadata :: Derived ( _) => {
215
+ debug_assert ! ( false ) ;
216
+ & [ ]
217
+ } ,
218
+ Metadata :: DerivedSigningPubkey ( _) => {
219
+ debug_assert ! ( false ) ;
220
+ & [ ]
221
+ } ,
195
222
}
196
223
}
197
224
}
@@ -212,10 +239,12 @@ impl fmt::Debug for Metadata {
212
239
impl PartialEq for Metadata {
213
240
fn eq ( & self , other : & Self ) -> bool {
214
241
match self {
215
- Metadata :: Bytes ( bytes) => if let Metadata :: Bytes ( other_bytes) = other {
216
- bytes == other_bytes
217
- } else {
218
- false
242
+ Metadata :: Bytes ( bytes) => {
243
+ if let Metadata :: Bytes ( other_bytes) = other {
244
+ bytes == other_bytes
245
+ } else {
246
+ false
247
+ }
219
248
} ,
220
249
Metadata :: RecipientData ( _) => false ,
221
250
Metadata :: PayerData ( _) => false ,
@@ -237,15 +266,10 @@ pub(super) struct MetadataMaterial {
237
266
impl MetadataMaterial {
238
267
pub fn new ( nonce : Nonce , expanded_key : & ExpandedKey , payment_id : Option < PaymentId > ) -> Self {
239
268
// Encrypt payment_id
240
- let encrypted_payment_id = payment_id. map ( |payment_id| {
241
- expanded_key. crypt_for_offer ( payment_id. 0 , nonce)
242
- } ) ;
243
-
244
- Self {
245
- nonce,
246
- hmac : expanded_key. hmac_for_offer ( ) ,
247
- encrypted_payment_id,
248
- }
269
+ let encrypted_payment_id =
270
+ payment_id. map ( |payment_id| expanded_key. crypt_for_offer ( payment_id. 0 , nonce) ) ;
271
+
272
+ Self { nonce, hmac : expanded_key. hmac_for_offer ( ) , encrypted_payment_id }
249
273
}
250
274
251
275
fn derive_metadata < W : Writeable > ( mut self , iv_bytes : & [ u8 ; IV_LEN ] , tlv_stream : W ) -> Vec < u8 > {
@@ -263,7 +287,7 @@ impl MetadataMaterial {
263
287
}
264
288
265
289
fn derive_metadata_and_keys < W : Writeable , T : secp256k1:: Signing > (
266
- mut self , iv_bytes : & [ u8 ; IV_LEN ] , tlv_stream : W , secp_ctx : & Secp256k1 < T >
290
+ mut self , iv_bytes : & [ u8 ; IV_LEN ] , tlv_stream : W , secp_ctx : & Secp256k1 < T > ,
267
291
) -> ( Vec < u8 > , Keypair ) {
268
292
self . hmac . input ( iv_bytes) ;
269
293
self . hmac . input ( & self . nonce . 0 ) ;
@@ -315,7 +339,7 @@ pub(super) fn derive_keys(nonce: Nonce, expanded_key: &ExpandedKey) -> Keypair {
315
339
pub ( super ) fn verify_payer_metadata < ' a , T : secp256k1:: Signing > (
316
340
metadata : & [ u8 ] , expanded_key : & ExpandedKey , iv_bytes : & [ u8 ; IV_LEN ] ,
317
341
signing_pubkey : PublicKey , tlv_stream : impl core:: iter:: Iterator < Item = TlvRecord < ' a > > ,
318
- secp_ctx : & Secp256k1 < T >
342
+ secp_ctx : & Secp256k1 < T > ,
319
343
) -> Result < PaymentId , ( ) > {
320
344
if metadata. len ( ) < PaymentId :: LENGTH {
321
345
return Err ( ( ) ) ;
@@ -324,14 +348,16 @@ pub(super) fn verify_payer_metadata<'a, T: secp256k1::Signing>(
324
348
let mut encrypted_payment_id = [ 0u8 ; PaymentId :: LENGTH ] ;
325
349
encrypted_payment_id. copy_from_slice ( & metadata[ ..PaymentId :: LENGTH ] ) ;
326
350
327
- let mut hmac = hmac_for_message (
328
- & metadata[ PaymentId :: LENGTH ..] , expanded_key, iv_bytes, tlv_stream
329
- ) ?;
351
+ let mut hmac =
352
+ hmac_for_message ( & metadata[ PaymentId :: LENGTH ..] , expanded_key, iv_bytes, tlv_stream) ?;
330
353
hmac. input ( WITH_ENCRYPTED_PAYMENT_ID_HMAC_INPUT ) ;
331
354
hmac. input ( & encrypted_payment_id) ;
332
355
333
356
verify_metadata (
334
- & metadata[ PaymentId :: LENGTH ..] , Hmac :: from_engine ( hmac) , signing_pubkey, secp_ctx
357
+ & metadata[ PaymentId :: LENGTH ..] ,
358
+ Hmac :: from_engine ( hmac) ,
359
+ signing_pubkey,
360
+ secp_ctx,
335
361
) ?;
336
362
337
363
let nonce = Nonce :: try_from ( & metadata[ PaymentId :: LENGTH ..] [ ..Nonce :: LENGTH ] ) . unwrap ( ) ;
@@ -351,7 +377,7 @@ pub(super) fn verify_payer_metadata<'a, T: secp256k1::Signing>(
351
377
pub ( super ) fn verify_recipient_metadata < ' a , T : secp256k1:: Signing > (
352
378
metadata : & [ u8 ] , expanded_key : & ExpandedKey , iv_bytes : & [ u8 ; IV_LEN ] ,
353
379
signing_pubkey : PublicKey , tlv_stream : impl core:: iter:: Iterator < Item = TlvRecord < ' a > > ,
354
- secp_ctx : & Secp256k1 < T >
380
+ secp_ctx : & Secp256k1 < T > ,
355
381
) -> Result < Option < Keypair > , ( ) > {
356
382
let mut hmac = hmac_for_message ( metadata, expanded_key, iv_bytes, tlv_stream) ?;
357
383
hmac. input ( WITHOUT_ENCRYPTED_PAYMENT_ID_HMAC_INPUT ) ;
@@ -360,11 +386,12 @@ pub(super) fn verify_recipient_metadata<'a, T: secp256k1::Signing>(
360
386
}
361
387
362
388
fn verify_metadata < T : secp256k1:: Signing > (
363
- metadata : & [ u8 ] , hmac : Hmac < Sha256 > , signing_pubkey : PublicKey , secp_ctx : & Secp256k1 < T >
389
+ metadata : & [ u8 ] , hmac : Hmac < Sha256 > , signing_pubkey : PublicKey , secp_ctx : & Secp256k1 < T > ,
364
390
) -> Result < Option < Keypair > , ( ) > {
365
391
if metadata. len ( ) == Nonce :: LENGTH {
366
392
let derived_keys = Keypair :: from_secret_key (
367
- secp_ctx, & SecretKey :: from_slice ( hmac. as_byte_array ( ) ) . unwrap ( )
393
+ secp_ctx,
394
+ & SecretKey :: from_slice ( hmac. as_byte_array ( ) ) . unwrap ( ) ,
368
395
) ;
369
396
if fixed_time_eq ( & signing_pubkey. serialize ( ) , & derived_keys. public_key ( ) . serialize ( ) ) {
370
397
Ok ( Some ( derived_keys) )
@@ -384,7 +411,7 @@ fn verify_metadata<T: secp256k1::Signing>(
384
411
385
412
fn hmac_for_message < ' a > (
386
413
metadata : & [ u8 ] , expanded_key : & ExpandedKey , iv_bytes : & [ u8 ; IV_LEN ] ,
387
- tlv_stream : impl core:: iter:: Iterator < Item = TlvRecord < ' a > >
414
+ tlv_stream : impl core:: iter:: Iterator < Item = TlvRecord < ' a > > ,
388
415
) -> Result < HmacEngine < Sha256 > , ( ) > {
389
416
if metadata. len ( ) < Nonce :: LENGTH {
390
417
return Err ( ( ) ) ;
@@ -420,7 +447,11 @@ pub(crate) fn hmac_for_offer_payment_id(
420
447
pub ( crate ) fn verify_offer_payment_id (
421
448
payment_id : PaymentId , hmac : Hmac < Sha256 > , nonce : Nonce , expanded_key : & ExpandedKey ,
422
449
) -> Result < ( ) , ( ) > {
423
- if hmac_for_offer_payment_id ( payment_id, nonce, expanded_key) == hmac { Ok ( ( ) ) } else { Err ( ( ) ) }
450
+ if hmac_for_offer_payment_id ( payment_id, nonce, expanded_key) == hmac {
451
+ Ok ( ( ) )
452
+ } else {
453
+ Err ( ( ) )
454
+ }
424
455
}
425
456
426
457
pub ( crate ) fn hmac_for_payment_hash (
@@ -439,7 +470,11 @@ pub(crate) fn hmac_for_payment_hash(
439
470
pub ( crate ) fn verify_payment_hash (
440
471
payment_hash : PaymentHash , hmac : Hmac < Sha256 > , nonce : Nonce , expanded_key : & ExpandedKey ,
441
472
) -> Result < ( ) , ( ) > {
442
- if hmac_for_payment_hash ( payment_hash, nonce, expanded_key) == hmac { Ok ( ( ) ) } else { Err ( ( ) ) }
473
+ if hmac_for_payment_hash ( payment_hash, nonce, expanded_key) == hmac {
474
+ Ok ( ( ) )
475
+ } else {
476
+ Err ( ( ) )
477
+ }
443
478
}
444
479
445
480
#[ cfg( async_payments) ]
@@ -453,7 +488,11 @@ pub(crate) fn hmac_for_async_payment_id(
453
488
pub ( crate ) fn verify_async_payment_id (
454
489
payment_id : PaymentId , hmac : Hmac < Sha256 > , nonce : Nonce , expanded_key : & ExpandedKey ,
455
490
) -> Result < ( ) , ( ) > {
456
- if hmac_for_async_payment_id ( payment_id, nonce, expanded_key) == hmac { Ok ( ( ) ) } else { Err ( ( ) ) }
491
+ if hmac_for_async_payment_id ( payment_id, nonce, expanded_key) == hmac {
492
+ Ok ( ( ) )
493
+ } else {
494
+ Err ( ( ) )
495
+ }
457
496
}
458
497
459
498
fn hmac_for_payment_id (
@@ -486,7 +525,11 @@ pub(crate) fn verify_payment_tlvs(
486
525
receive_tlvs : & UnauthenticatedReceiveTlvs , hmac : Hmac < Sha256 > , nonce : Nonce ,
487
526
expanded_key : & ExpandedKey ,
488
527
) -> Result < ( ) , ( ) > {
489
- if hmac_for_payment_tlvs ( receive_tlvs, nonce, expanded_key) == hmac { Ok ( ( ) ) } else { Err ( ( ) ) }
528
+ if hmac_for_payment_tlvs ( receive_tlvs, nonce, expanded_key) == hmac {
529
+ Ok ( ( ) )
530
+ } else {
531
+ Err ( ( ) )
532
+ }
490
533
}
491
534
492
535
#[ cfg( async_payments) ]
0 commit comments