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