@@ -18,10 +18,80 @@ macro_rules! impl_pretty_debug {
1818 impl :: core:: fmt:: Debug for $thing {
1919 fn fmt( & self , f: & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
2020 write!( f, "{}(" , stringify!( $thing) ) ?;
21- for i in self [ ..] . iter ( ) . cloned ( ) {
21+ for i in & self [ ..] {
2222 write!( f, "{:02x}" , i) ?;
2323 }
24- write!( f, ")" )
24+ f. write_str( ")" )
25+ }
26+ }
27+ }
28+ }
29+
30+ macro_rules! impl_safe_debug {
31+ ( $thing: ident) => {
32+ #[ cfg( feature = "bitcoin_hashes" ) ]
33+ impl :: core:: fmt:: Debug for $thing {
34+ fn fmt( & self , f: & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
35+ use :: bitcoin_hashes:: { Hash , sha256} ;
36+ write!( f, "{}(#" , stringify!( $thing) ) ?;
37+ let hash = sha256:: Hash :: hash( & self . 0 [ ..] ) ;
38+ for i in & hash[ ..4 ] {
39+ write!( f, "{:02x}" , i) ?;
40+ }
41+ f. write_str( "..." ) ?;
42+ for i in & hash[ 28 ..] {
43+ write!( f, "{:02x}" , i) ?;
44+ }
45+ f. write_str( ")" )
46+ }
47+ }
48+
49+ #[ cfg( all( not( feature = "bitcoin_hashes" ) , feature = "std" ) ) ]
50+ impl :: core:: fmt:: Debug for $thing {
51+ fn fmt( & self , f: & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
52+ use :: core:: hash:: Hasher ;
53+ let mut hasher = :: std:: collections:: hash_map:: DefaultHasher :: new( ) ;
54+
55+ hasher. write( & self . 0 [ ..] ) ;
56+ let hash = hasher. finish( ) ;
57+
58+ write!( f, "{}(#{:016x})" , stringify!( $thing) , hash)
59+ }
60+ }
61+
62+ impl $thing {
63+ /// Formats the explicit byte value of the secret key kept inside the type as a
64+ /// little-endian hexadecimal string using the provided formatter.
65+ ///
66+ /// This is the only method that outputs the actual secret key value, and, thus,
67+ /// should be used with extreme precaution.
68+ #[ deprecated(
69+ note = "Caution: you are explicitly outputting secret key value! This can be done
70+ only in debug environment and that's why always considered as ``deprecated''"
71+ ) ]
72+ pub fn fmt_secret_key( & self , f: & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
73+ for i in & self . 0 [ ..] {
74+ write!( f, "{:02x}" , i) ?;
75+ }
76+ Ok ( ( ) )
77+ }
78+
79+ /// Formats the explicit byte value of the secret key kept inside the type as a
80+ /// little-endian hexadecimal string.
81+ ///
82+ /// This is the only method that outputs the actual secret key value, and, thus,
83+ /// should be used with extreme precaution.
84+ #[ deprecated(
85+ note = "Caution: you are explicitly outputting secret key value! This can be done
86+ only in debug environment and that's why always considered as ``deprecated''"
87+ ) ]
88+ #[ cfg( feature = "std" ) ]
89+ pub fn format_secret_key( & self ) -> String {
90+ let mut s = Vec :: with_capacity( self . 0 . len( ) * 2 ) ;
91+ for i in & self . 0 [ ..] {
92+ s. push( format!( "{:02x}" , i) ) ;
93+ }
94+ s. join( "" )
2595 }
2696 }
2797 }
0 commit comments