|
| 1 | +// Bitcoin secp256k1 bindings |
| 2 | +// Written in 2021 by |
| 3 | +// Maxim Orlovsky <orlovsky@pandoracore.com> |
| 4 | +// |
| 5 | +// To the extent possible under law, the author(s) have dedicated all |
| 6 | +// copyright and related and neighboring rights to this software to |
| 7 | +// the public domain worldwide. This software is distributed without |
| 8 | +// any warranty. |
| 9 | +// |
| 10 | +// You should have received a copy of the CC0 Public Domain Dedication |
| 11 | +// along with this software. |
| 12 | +// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 | +// |
| 14 | + |
| 15 | +//! Helpers for displaying secret values |
| 16 | +
|
| 17 | +use ::core::fmt; |
| 18 | +use SecretKey; |
| 19 | + |
| 20 | +macro_rules! impl_display_secret { |
| 21 | + // Default hasher exists only in standard library and not alloc |
| 22 | + ($thing:ident) => { |
| 23 | + #[cfg(feature = "std")] |
| 24 | + impl ::core::fmt::Debug for $thing { |
| 25 | + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 26 | + use ::core::hash::Hasher; |
| 27 | + const DEBUG_HASH_TAG: &[u8] = &[ |
| 28 | + 0x66, 0xa6, 0x77, 0x1b, 0x9b, 0x6d, 0xae, 0xa1, 0xb2, 0xee, 0x4e, 0x07, 0x49, |
| 29 | + 0x4a, 0xac, 0x87, 0xa9, 0xb8, 0x5b, 0x4b, 0x35, 0x02, 0xaa, 0x6d, 0x0f, 0x79, |
| 30 | + 0xcb, 0x63, 0xe6, 0xf8, 0x66, 0x22 |
| 31 | + ]; // =SHA256(b"rust-secp256k1DEBUG"); |
| 32 | + |
| 33 | + let mut hasher = ::std::collections::hash_map::DefaultHasher::new(); |
| 34 | + |
| 35 | + hasher.write(DEBUG_HASH_TAG); |
| 36 | + hasher.write(DEBUG_HASH_TAG); |
| 37 | + hasher.write(&self.0[..]); |
| 38 | + let hash = hasher.finish(); |
| 39 | + |
| 40 | + f.debug_tuple(stringify!($thing)) |
| 41 | + .field(&format_args!("#{:016x}", hash)) |
| 42 | + .finish() |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Helper struct for safely printing secrets (like [`SecretKey`] value). |
| 49 | +/// Formats the explicit byte value of the secret kept inside the type as a |
| 50 | +/// little-endian hexadecimal string using the provided formatter. |
| 51 | +/// |
| 52 | +/// Secrets should not implement neither [`Debug`] and [`Display`] traits directly, |
| 53 | +/// and instead provide `fn display_secret<'a>(&'a self) -> DisplaySecret<'a>` |
| 54 | +/// function to be used in different display contexts (see "examples" below). |
| 55 | +/// |
| 56 | +/// # Example |
| 57 | +/// |
| 58 | +/// ``` |
| 59 | +/// use secp256k1::key::ONE_KEY; |
| 60 | +/// let key = ONE_KEY; |
| 61 | +/// println!("{}", key.display_secret()); |
| 62 | +/// println!("{:?}", key.display_secret()); |
| 63 | +/// ``` |
| 64 | +/// |
| 65 | +/// [`Display`]: fmt::Display |
| 66 | +/// [`Debug`]: fmt::Debug |
| 67 | +pub struct DisplaySecret<'a> { |
| 68 | + secret: &'a [u8] |
| 69 | +} |
| 70 | + |
| 71 | +impl<'a> fmt::Debug for DisplaySecret<'a> { |
| 72 | + #[inline] |
| 73 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 74 | + fmt::Debug::fmt(&self.secret, f) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a> fmt::Display for DisplaySecret<'a> { |
| 79 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 80 | + for i in self.secret { |
| 81 | + write!(f, "{:02x}", i)?; |
| 82 | + } |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl SecretKey { |
| 88 | + /// Formats the explicit byte value of the secret key kept inside the type as a |
| 89 | + /// little-endian hexadecimal string using the provided formatter. |
| 90 | + /// |
| 91 | + /// This is the only method that outputs the actual secret key value, and, thus, |
| 92 | + /// should be used with extreme precaution. |
| 93 | + /// |
| 94 | + /// # Example |
| 95 | + /// |
| 96 | + /// ``` |
| 97 | + /// use secp256k1::key::ONE_KEY; |
| 98 | + /// let key = ONE_KEY; |
| 99 | + /// println!("{}", key.display_secret()); |
| 100 | + /// println!("{:?}", key.display_secret()); |
| 101 | + /// ``` |
| 102 | + #[inline] |
| 103 | + pub fn display_secret<'a>(&'a self) -> DisplaySecret<'a> { |
| 104 | + DisplaySecret { secret: &self.0 } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// TODO: Uncomment the below once #308 got merged |
| 109 | +/* |
| 110 | +impl KeyPair { |
| 111 | + /// Formats the explicit byte value of the secret key kept inside the type as a |
| 112 | + /// little-endian hexadecimal string using the provided formatter. |
| 113 | + /// |
| 114 | + /// This is the only method that outputs the actual secret key value, and, thus, |
| 115 | + /// should be used with extreme precaution. |
| 116 | + #[inline] |
| 117 | + pub fn display_secret<'a>(&'a self) -> DisplaySecret<'a> { |
| 118 | + DisplaySecret(self.serialize_sec()) |
| 119 | + } |
| 120 | +} |
| 121 | +*/ |
0 commit comments