99
1010//! Tagged hashes for use in signature calculation and verification.
1111
12- use bitcoin:: hashes:: { Hash , HashEngine , sha256} ;
13- use bitcoin:: secp256k1:: { Message , PublicKey , Secp256k1 , self } ;
14- use bitcoin:: secp256k1:: schnorr:: Signature ;
1512use crate :: io;
1613use crate :: util:: ser:: { BigSize , Readable , Writeable , Writer } ;
14+ use bitcoin:: hashes:: { sha256, Hash , HashEngine } ;
15+ use bitcoin:: secp256k1:: schnorr:: Signature ;
16+ use bitcoin:: secp256k1:: { self , Message , PublicKey , Secp256k1 } ;
1717
1818#[ allow( unused_imports) ]
1919use crate :: prelude:: * ;
@@ -50,16 +50,12 @@ impl TaggedHash {
5050 ///
5151 /// Panics if `tlv_stream` is not a well-formed TLV stream containing at least one TLV record.
5252 pub ( super ) fn from_tlv_stream < ' a , I : core:: iter:: Iterator < Item = TlvRecord < ' a > > > (
53- tag : & ' static str , tlv_stream : I
53+ tag : & ' static str , tlv_stream : I ,
5454 ) -> Self {
5555 let tag_hash = sha256:: Hash :: hash ( tag. as_bytes ( ) ) ;
5656 let merkle_root = root_hash ( tlv_stream) ;
5757 let digest = Message :: from_digest ( tagged_hash ( tag_hash, merkle_root) . to_byte_array ( ) ) ;
58- Self {
59- tag,
60- merkle_root,
61- digest,
62- }
58+ Self { tag, merkle_root, digest }
6359 }
6460
6561 /// Returns the digest to sign.
@@ -227,9 +223,7 @@ pub(super) struct TlvStream<'a> {
227223
228224impl < ' a > TlvStream < ' a > {
229225 pub fn new ( data : & ' a [ u8 ] ) -> Self {
230- Self {
231- data : io:: Cursor :: new ( data) ,
232- }
226+ Self { data : io:: Cursor :: new ( data) }
233227 }
234228
235229 pub fn range < T > ( self , types : T ) -> impl core:: iter:: Iterator < Item = TlvRecord < ' a > >
@@ -271,9 +265,7 @@ impl<'a> Iterator for TlvStream<'a> {
271265
272266 self . data . set_position ( end) ;
273267
274- Some ( TlvRecord {
275- r#type, type_bytes, record_bytes, end : end as usize ,
276- } )
268+ Some ( TlvRecord { r#type, type_bytes, record_bytes, end : end as usize } )
277269 } else {
278270 None
279271 }
@@ -289,41 +281,47 @@ impl<'a> Writeable for TlvRecord<'a> {
289281
290282#[ cfg( test) ]
291283mod tests {
292- use super :: { SIGNATURE_TYPES , TlvStream } ;
284+ use super :: { TlvStream , SIGNATURE_TYPES } ;
293285
294- use bitcoin:: hashes:: { Hash , sha256} ;
295- use bitcoin:: hex:: FromHex ;
296- use bitcoin:: secp256k1:: { Keypair , Message , Secp256k1 , SecretKey } ;
297- use bitcoin:: secp256k1:: schnorr:: Signature ;
298286 use crate :: ln:: channelmanager:: PaymentId ;
299287 use crate :: ln:: inbound_payment:: ExpandedKey ;
288+ use crate :: offers:: invoice_request:: { InvoiceRequest , UnsignedInvoiceRequest } ;
300289 use crate :: offers:: nonce:: Nonce ;
301290 use crate :: offers:: offer:: { Amount , OfferBuilder } ;
302- use crate :: offers:: invoice_request:: { InvoiceRequest , UnsignedInvoiceRequest } ;
303291 use crate :: offers:: parse:: Bech32Encode ;
304292 use crate :: offers:: signer:: Metadata ;
305293 use crate :: offers:: test_utils:: recipient_pubkey;
306294 use crate :: util:: ser:: Writeable ;
295+ use bitcoin:: hashes:: { sha256, Hash } ;
296+ use bitcoin:: hex:: FromHex ;
297+ use bitcoin:: secp256k1:: schnorr:: Signature ;
298+ use bitcoin:: secp256k1:: { Keypair , Message , Secp256k1 , SecretKey } ;
307299
308300 #[ test]
309301 fn calculates_merkle_root_hash ( ) {
310302 // BOLT 12 test vectors
311303 const HEX_1 : & ' static str = "010203e8" ;
312- let bytes_1 = <Vec < u8 > >:: from_hex ( "b013756c8fee86503a0b4abdab4cddeb1af5d344ca6fc2fa8b6c08938caa6f93" ) . unwrap ( ) ;
304+ let bytes_1 =
305+ <Vec < u8 > >:: from_hex ( "b013756c8fee86503a0b4abdab4cddeb1af5d344ca6fc2fa8b6c08938caa6f93" )
306+ . unwrap ( ) ;
313307 assert_eq ! (
314308 super :: root_hash( TlvStream :: new( & <Vec <u8 >>:: from_hex( HEX_1 ) . unwrap( ) ) ) ,
315309 sha256:: Hash :: from_slice( & bytes_1) . unwrap( ) ,
316310 ) ;
317311
318- const HEX_2 : & ' static str = concat ! ( "010203e8" , "02080000010000020003" ) ;
319- let bytes_2 = <Vec < u8 > >:: from_hex ( "c3774abbf4815aa54ccaa026bff6581f01f3be5fe814c620a252534f434bc0d1" ) . unwrap ( ) ;
312+ const HEX_2 : & ' static str = concat ! ( "010203e8" , "02080000010000020003" ) ;
313+ let bytes_2 =
314+ <Vec < u8 > >:: from_hex ( "c3774abbf4815aa54ccaa026bff6581f01f3be5fe814c620a252534f434bc0d1" )
315+ . unwrap ( ) ;
320316 assert_eq ! (
321317 super :: root_hash( TlvStream :: new( & <Vec <u8 >>:: from_hex( HEX_2 ) . unwrap( ) ) ) ,
322318 sha256:: Hash :: from_slice( & bytes_2) . unwrap( ) ,
323319 ) ;
324320
325321 const HEX_3 : & ' static str = concat ! ( "010203e8" , "02080000010000020003" , "03310266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c0351800000000000000010000000000000002" ) ;
326- let bytes_3 = <Vec < u8 > >:: from_hex ( "ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d" ) . unwrap ( ) ;
322+ let bytes_3 =
323+ <Vec < u8 > >:: from_hex ( "ab2e79b1283b0b31e0b035258de23782df6b89a38cfa7237bde69aed1a658c5d" )
324+ . unwrap ( ) ;
327325 assert_eq ! (
328326 super :: root_hash( TlvStream :: new( & <Vec <u8 >>:: from_hex( HEX_3 ) . unwrap( ) ) ) ,
329327 sha256:: Hash :: from_slice( & bytes_3) . unwrap( ) ,
@@ -338,12 +336,18 @@ mod tests {
338336 let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
339337
340338 let recipient_pubkey = {
341- let secret_bytes = <Vec < u8 > >:: from_hex ( "4141414141414141414141414141414141414141414141414141414141414141" ) . unwrap ( ) ;
339+ let secret_bytes = <Vec < u8 > >:: from_hex (
340+ "4141414141414141414141414141414141414141414141414141414141414141" ,
341+ )
342+ . unwrap ( ) ;
342343 let secret_key = SecretKey :: from_slice ( & secret_bytes) . unwrap ( ) ;
343344 Keypair :: from_secret_key ( & secp_ctx, & secret_key) . public_key ( )
344345 } ;
345346 let payer_keys = {
346- let secret_bytes = <Vec < u8 > >:: from_hex ( "4242424242424242424242424242424242424242424242424242424242424242" ) . unwrap ( ) ;
347+ let secret_bytes = <Vec < u8 > >:: from_hex (
348+ "4242424242424242424242424242424242424242424242424242424242424242" ,
349+ )
350+ . unwrap ( ) ;
347351 let secret_key = SecretKey :: from_slice ( & secret_bytes) . unwrap ( ) ;
348352 Keypair :: from_secret_key ( & secp_ctx, & secret_key)
349353 } ;
@@ -354,30 +358,30 @@ mod tests {
354358 . amount ( Amount :: Currency { iso4217_code : * b"USD" , amount : 100 } )
355359 . build_unchecked ( )
356360 // Override the payer metadata and signing pubkey to match the test vectors
357- . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
361+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id)
362+ . unwrap ( )
358363 . payer_metadata ( Metadata :: Bytes ( vec ! [ 0 ; 8 ] ) )
359364 . payer_signing_pubkey ( payer_keys. public_key ( ) )
360365 . build_unchecked ( )
361- . sign ( |message : & UnsignedInvoiceRequest |
366+ . sign ( |message : & UnsignedInvoiceRequest | {
362367 Ok ( secp_ctx. sign_schnorr_no_aux_rand ( message. as_ref ( ) . as_digest ( ) , & payer_keys) )
363- )
368+ } )
364369 . unwrap ( ) ;
365370 assert_eq ! (
366371 invoice_request. to_string( ) ,
367372 "lnr1qqyqqqqqqqqqqqqqqcp4256ypqqkgzshgysy6ct5dpjk6ct5d93kzmpq23ex2ct5d9ek293pqthvwfzadd7jejes8q9lhc4rvjxd022zv5l44g6qah82ru5rdpnpjkppqvjx204vgdzgsqpvcp4mldl3plscny0rt707gvpdh6ndydfacz43euzqhrurageg3n7kafgsek6gz3e9w52parv8gs2hlxzk95tzeswywffxlkeyhml0hh46kndmwf4m6xma3tkq2lu04qz3slje2rfthc89vss" ,
368373 ) ;
369374
370- let bytes = <Vec < u8 > >:: from_hex ( "608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624" ) . unwrap ( ) ;
375+ let bytes =
376+ <Vec < u8 > >:: from_hex ( "608407c18ad9a94d9ea2bcdbe170b6c20c462a7833a197621c916f78cf18e624" )
377+ . unwrap ( ) ;
371378 assert_eq ! (
372379 super :: root_hash( TlvStream :: new( & invoice_request. bytes[ ..] ) ) ,
373380 sha256:: Hash :: from_slice( & bytes) . unwrap( ) ,
374381 ) ;
375382
376383 let bytes = <Vec < u8 > >:: from_hex ( "b8f83ea3288cfd6ea510cdb481472575141e8d8744157f98562d162cc1c472526fdb24befefbdebab4dbb726bbd1b7d8aec057f8fa805187e5950d2bbe0e5642" ) . unwrap ( ) ;
377- assert_eq ! (
378- invoice_request. signature( ) ,
379- Signature :: from_slice( & bytes) . unwrap( ) ,
380- ) ;
384+ assert_eq ! ( invoice_request. signature( ) , Signature :: from_slice( & bytes) . unwrap( ) , ) ;
381385 }
382386
383387 #[ test]
@@ -389,8 +393,10 @@ mod tests {
389393
390394 let unsigned_invoice_request = OfferBuilder :: new ( recipient_pubkey ( ) )
391395 . amount_msats ( 1000 )
392- . build ( ) . unwrap ( )
393- . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
396+ . build ( )
397+ . unwrap ( )
398+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id)
399+ . unwrap ( )
394400 . payer_note ( "bar" . into ( ) )
395401 . build_unchecked ( ) ;
396402
@@ -399,7 +405,9 @@ mod tests {
399405 let tagged_hash = unsigned_invoice_request. as_ref ( ) ;
400406 let expected_digest = unsigned_invoice_request. as_ref ( ) . as_digest ( ) ;
401407 let tag = sha256:: Hash :: hash ( tagged_hash. tag ( ) . as_bytes ( ) ) ;
402- let actual_digest = Message :: from_digest ( super :: tagged_hash ( tag, tagged_hash. merkle_root ( ) ) . to_byte_array ( ) ) ;
408+ let actual_digest = Message :: from_digest (
409+ super :: tagged_hash ( tag, tagged_hash. merkle_root ( ) ) . to_byte_array ( ) ,
410+ ) ;
403411 assert_eq ! ( * expected_digest, actual_digest) ;
404412 }
405413
@@ -418,8 +426,10 @@ mod tests {
418426 let invoice_request = OfferBuilder :: new ( recipient_pubkey)
419427 . amount_msats ( 100 )
420428 . build_unchecked ( )
421- . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
422- . build_and_sign ( ) . unwrap ( ) ;
429+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id)
430+ . unwrap ( )
431+ . build_and_sign ( )
432+ . unwrap ( ) ;
423433
424434 let mut bytes_without_signature = Vec :: new ( ) ;
425435 let tlv_stream_without_signatures = TlvStream :: new ( & invoice_request. bytes )
@@ -450,11 +460,13 @@ mod tests {
450460 let invoice_request = OfferBuilder :: new ( recipient_pubkey)
451461 . amount_msats ( 100 )
452462 . build_unchecked ( )
453- . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
463+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id)
464+ . unwrap ( )
454465 . build_and_sign ( )
455466 . unwrap ( ) ;
456467
457- let tlv_stream = TlvStream :: new ( & invoice_request. bytes ) . range ( 0 ..1 )
468+ let tlv_stream = TlvStream :: new ( & invoice_request. bytes )
469+ . range ( 0 ..1 )
458470 . chain ( TlvStream :: new ( & invoice_request. bytes ) . range ( 1 ..80 ) )
459471 . chain ( TlvStream :: new ( & invoice_request. bytes ) . range ( 80 ..160 ) )
460472 . chain ( TlvStream :: new ( & invoice_request. bytes ) . range ( 160 ..240 ) )
0 commit comments