@@ -21,11 +21,11 @@ use std::fmt;
2121use std:: fmt:: Write as _;
2222use std:: str:: FromStr ;
2323
24- use bitcoin:: base58;
25- use bitcoin:: PublicKey ;
26- use crate :: bech32:: { Bech32 , Bech32m , ByteIterExt , Fe32 , Hrp , Fe32IterExt } ;
24+ use crate :: bech32:: { Bech32 , Bech32m , ByteIterExt , Fe32 , Fe32IterExt , Hrp } ;
2725use crate :: blech32:: { Blech32 , Blech32m } ;
2826use crate :: hashes:: Hash ;
27+ use bitcoin:: base58;
28+ use bitcoin:: PublicKey ;
2929use secp256k1_zkp;
3030use secp256k1_zkp:: Secp256k1 ;
3131use secp256k1_zkp:: Verification ;
@@ -35,8 +35,8 @@ use serde;
3535use crate :: schnorr:: { TapTweak , TweakedPublicKey , UntweakedPublicKey } ;
3636use crate :: taproot:: TapNodeHash ;
3737
38- use crate :: { PubkeyHash , ScriptHash , WPubkeyHash , WScriptHash } ;
3938use crate :: { opcodes, script} ;
39+ use crate :: { PubkeyHash , ScriptHash , WPubkeyHash , WScriptHash } ;
4040
4141/// Encoding error
4242#[ derive( Debug , PartialEq ) ]
@@ -89,10 +89,18 @@ impl fmt::Display for AddressError {
8989 write ! ( f, "invalid witness script version: {}" , wver)
9090 }
9191 AddressError :: InvalidWitnessProgramLength ( ref len) => {
92- write ! ( f, "the witness program must be between 2 and 40 bytes in length, not {}" , len)
92+ write ! (
93+ f,
94+ "the witness program must be between 2 and 40 bytes in length, not {}" ,
95+ len
96+ )
9397 }
9498 AddressError :: InvalidSegwitV0ProgramLength ( ref len) => {
95- write ! ( f, "a v0 witness program must be length 20 or 32, not {}" , len)
99+ write ! (
100+ f,
101+ "a v0 witness program must be length 20 or 32, not {}" ,
102+ len
103+ )
96104 }
97105 AddressError :: InvalidBlindingPubKey ( ref e) => {
98106 write ! ( f, "an invalid blinding pubkey was encountered: {}" , e)
@@ -212,7 +220,8 @@ impl Address {
212220 params : & ' static AddressParams ,
213221 ) -> Address {
214222 let mut hash_engine = PubkeyHash :: engine ( ) ;
215- pk. write_into ( & mut hash_engine) . expect ( "engines don't error" ) ;
223+ pk. write_into ( & mut hash_engine)
224+ . expect ( "engines don't error" ) ;
216225
217226 Address {
218227 params,
@@ -244,7 +253,8 @@ impl Address {
244253 params : & ' static AddressParams ,
245254 ) -> Address {
246255 let mut hash_engine = WPubkeyHash :: engine ( ) ;
247- pk. write_into ( & mut hash_engine) . expect ( "engines don't error" ) ;
256+ pk. write_into ( & mut hash_engine)
257+ . expect ( "engines don't error" ) ;
248258
249259 Address {
250260 params,
@@ -264,7 +274,8 @@ impl Address {
264274 params : & ' static AddressParams ,
265275 ) -> Address {
266276 let mut hash_engine = ScriptHash :: engine ( ) ;
267- pk. write_into ( & mut hash_engine) . expect ( "engines don't error" ) ;
277+ pk. write_into ( & mut hash_engine)
278+ . expect ( "engines don't error" ) ;
268279
269280 let builder = script:: Builder :: new ( )
270281 . push_int ( 0 )
@@ -401,7 +412,9 @@ impl Address {
401412 Payload :: WitnessProgram {
402413 version : witver,
403414 program : ref witprog,
404- } => script:: Builder :: new ( ) . push_int ( witver. to_u8 ( ) as i64 ) . push_slice ( witprog) ,
415+ } => script:: Builder :: new ( )
416+ . push_int ( witver. to_u8 ( ) as i64 )
417+ . push_slice ( witprog) ,
405418 }
406419 . into_script ( )
407420 }
@@ -450,10 +463,7 @@ impl Address {
450463
451464 Ok ( Address {
452465 params,
453- payload : Payload :: WitnessProgram {
454- version,
455- program,
456- } ,
466+ payload : Payload :: WitnessProgram { version, program } ,
457467 blinding_pubkey,
458468 } )
459469 }
@@ -574,14 +584,23 @@ impl fmt::Display for Address {
574584 // FIXME: surely we can fix this logic to not be so repetitive.
575585 if self . is_blinded ( ) {
576586 if let Some ( ref blinder) = self . blinding_pubkey {
577- let byte_iter = IntoIterator :: into_iter ( blinder. serialize ( ) ) . chain ( witprog. iter ( ) . copied ( ) ) ;
587+ let byte_iter = IntoIterator :: into_iter ( blinder. serialize ( ) )
588+ . chain ( witprog. iter ( ) . copied ( ) ) ;
578589 let fe_iter = byte_iter. bytes_to_fes ( ) ;
579590 if witver. to_u8 ( ) == 0 {
580- for c in fe_iter. with_checksum :: < Blech32 > ( & hrp) . with_witness_version ( witver) . chars ( ) {
591+ for c in fe_iter
592+ . with_checksum :: < Blech32 > ( & hrp)
593+ . with_witness_version ( witver)
594+ . chars ( )
595+ {
581596 fmt. write_char ( c) ?;
582597 }
583598 } else {
584- for c in fe_iter. with_checksum :: < Blech32m > ( & hrp) . with_witness_version ( witver) . chars ( ) {
599+ for c in fe_iter
600+ . with_checksum :: < Blech32m > ( & hrp)
601+ . with_witness_version ( witver)
602+ . chars ( )
603+ {
585604 fmt. write_char ( c) ?;
586605 }
587606 }
@@ -592,11 +611,19 @@ impl fmt::Display for Address {
592611 let byte_iter = witprog. iter ( ) . copied ( ) ;
593612 let fe_iter = byte_iter. bytes_to_fes ( ) ;
594613 if witver. to_u8 ( ) == 0 {
595- for c in fe_iter. with_checksum :: < Bech32 > ( & hrp) . with_witness_version ( witver) . chars ( ) {
614+ for c in fe_iter
615+ . with_checksum :: < Bech32 > ( & hrp)
616+ . with_witness_version ( witver)
617+ . chars ( )
618+ {
596619 fmt. write_char ( c) ?;
597620 }
598621 } else {
599- for c in fe_iter. with_checksum :: < Bech32m > ( & hrp) . with_witness_version ( witver) . chars ( ) {
622+ for c in fe_iter
623+ . with_checksum :: < Bech32m > ( & hrp)
624+ . with_witness_version ( witver)
625+ . chars ( )
626+ {
600627 fmt. write_char ( c) ?;
601628 }
602629 }
@@ -734,9 +761,9 @@ impl serde::Serialize for Address {
734761#[ cfg( test) ]
735762mod test {
736763 use super :: * ;
764+ use crate :: Script ;
737765 use bitcoin:: key;
738766 use secp256k1_zkp:: { PublicKey , Secp256k1 } ;
739- use crate :: Script ;
740767 #[ cfg( feature = "serde" ) ]
741768 use serde_json;
742769
@@ -755,7 +782,9 @@ mod test {
755782 ) ;
756783 #[ cfg( feature = "serde" ) ]
757784 assert_eq ! (
758- serde_json:: from_value:: <Address >( serde_json:: to_value( addr) . unwrap( ) ) . ok( ) . as_ref( ) ,
785+ serde_json:: from_value:: <Address >( serde_json:: to_value( addr) . unwrap( ) )
786+ . ok( )
787+ . as_ref( ) ,
759788 Some ( addr)
760789 ) ;
761790 }
@@ -831,7 +860,12 @@ mod test {
831860
832861 for & ( a, blinded, ref params) in & addresses {
833862 let result = a. parse ( ) ;
834- assert ! ( result. is_ok( ) , "vector: {}, err: \" {}\" " , a, result. unwrap_err( ) ) ;
863+ assert ! (
864+ result. is_ok( ) ,
865+ "vector: {}, err: \" {}\" " ,
866+ a,
867+ result. unwrap_err( )
868+ ) ;
835869 let addr: Address = result. unwrap ( ) ;
836870 assert_eq ! ( a, & addr. to_string( ) , "vector: {}" , a) ;
837871 assert_eq ! ( blinded, addr. is_blinded( ) ) ;
@@ -858,7 +892,8 @@ mod test {
858892 "blech32 error: invalid checksum" , // is valid blech32m, but should be blech32
859893 ) ;
860894
861- let address: Result < Address , _ > = "ert130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqqu2tys" . parse ( ) ;
895+ let address: Result < Address , _ > =
896+ "ert130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqqu2tys" . parse ( ) ;
862897 assert_eq ! (
863898 address. err( ) . unwrap( ) . to_string( ) ,
864899 "bech32 error: invalid segwit witness version: 3" , // FIXME https://github.com/rust-bitcoin/rust-bech32/issues/162 should be 17
@@ -879,14 +914,18 @@ mod test {
879914 ) ;
880915 }
881916
882-
883917 #[ test]
884918 fn test_fixed_addresses ( ) {
885- let pk = bitcoin:: PublicKey :: from_str ( "0212bf0ea45b733dfde8ecb5e896306c4165c666c99fc5d1ab887f71393a975cea" )
886- . unwrap ( ) ;
919+ let pk = bitcoin:: PublicKey :: from_str (
920+ "0212bf0ea45b733dfde8ecb5e896306c4165c666c99fc5d1ab887f71393a975cea" ,
921+ )
922+ . unwrap ( ) ;
887923 let script = Script :: default ( ) ;
888924 let secp = Secp256k1 :: verification_only ( ) ;
889- let internal_key = UntweakedPublicKey :: from_str ( "93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51" ) . unwrap ( ) ;
925+ let internal_key = UntweakedPublicKey :: from_str (
926+ "93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51" ,
927+ )
928+ . unwrap ( ) ;
890929 let tap_node_hash = TapNodeHash :: all_zeros ( ) ;
891930
892931 let mut expected = IntoIterator :: into_iter ( [
@@ -934,9 +973,12 @@ mod test {
934973 "tlq1pqgft7r4ytdenml0gaj67393sd3qkt3nxex0ut5dt3plhzwf6jaww5vx8c8vs0ywzejta7jjcc5f4asnacdtu0wlaas0upmsq90enaz2lekytucqf82vs" ,
935974 ] ) ;
936975
937- for params in [ & AddressParams :: ELEMENTS , & AddressParams :: LIQUID , & AddressParams :: LIQUID_TESTNET ] {
976+ for params in [
977+ & AddressParams :: ELEMENTS ,
978+ & AddressParams :: LIQUID ,
979+ & AddressParams :: LIQUID_TESTNET ,
980+ ] {
938981 for blinder in [ None , Some ( pk. inner ) ] {
939-
940982 let addr = Address :: p2pkh ( & pk, blinder, params) ;
941983 assert_eq ! ( & addr. to_string( ) , expected. next( ) . unwrap( ) ) ;
942984
@@ -959,6 +1001,5 @@ mod test {
9591001 assert_eq ! ( & addr. to_string( ) , expected. next( ) . unwrap( ) ) ;
9601002 }
9611003 }
962-
9631004 }
9641005}
0 commit comments