|
| 1 | +// Bitcoin secp256k1 bindings |
| 2 | +// Written in 2021 by |
| 3 | +// Maxim Orlovsky <[email protected]> |
| 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, schnorrsig::KeyPair, to_hex}; |
| 19 | +use constants::SECRET_KEY_SIZE; |
| 20 | + |
| 21 | +macro_rules! impl_display_secret { |
| 22 | + // Default hasher exists only in standard library and not alloc |
| 23 | + ($thing:ident) => { |
| 24 | + #[cfg(feature = "std")] |
| 25 | + impl ::core::fmt::Debug for $thing { |
| 26 | + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { |
| 27 | + use ::core::hash::Hasher; |
| 28 | + const DEBUG_HASH_TAG: &[u8] = &[ |
| 29 | + 0x66, 0xa6, 0x77, 0x1b, 0x9b, 0x6d, 0xae, 0xa1, 0xb2, 0xee, 0x4e, 0x07, 0x49, |
| 30 | + 0x4a, 0xac, 0x87, 0xa9, 0xb8, 0x5b, 0x4b, 0x35, 0x02, 0xaa, 0x6d, 0x0f, 0x79, |
| 31 | + 0xcb, 0x63, 0xe6, 0xf8, 0x66, 0x22 |
| 32 | + ]; // =SHA256(b"rust-secp256k1DEBUG"); |
| 33 | + |
| 34 | + let mut hasher = ::std::collections::hash_map::DefaultHasher::new(); |
| 35 | + |
| 36 | + hasher.write(DEBUG_HASH_TAG); |
| 37 | + hasher.write(DEBUG_HASH_TAG); |
| 38 | + hasher.write(&self.0[..]); |
| 39 | + let hash = hasher.finish(); |
| 40 | + |
| 41 | + f.debug_tuple(stringify!($thing)) |
| 42 | + .field(&format_args!("#{:016x}", hash)) |
| 43 | + .finish() |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Helper struct for safely printing secrets (like [`SecretKey`] value). |
| 50 | +/// Formats the explicit byte value of the secret kept inside the type as a |
| 51 | +/// little-endian hexadecimal string using the provided formatter. |
| 52 | +/// |
| 53 | +/// Secrets should not implement neither [`Debug`] and [`Display`] traits directly, |
| 54 | +/// and instead provide `fn display_secret<'a>(&'a self) -> DisplaySecret<'a>` |
| 55 | +/// function to be used in different display contexts (see "examples" below). |
| 56 | +/// |
| 57 | +/// [`Display`]: fmt::Display |
| 58 | +/// [`Debug`]: fmt::Debug |
| 59 | +pub struct DisplaySecret { |
| 60 | + secret: [u8; SECRET_KEY_SIZE] |
| 61 | +} |
| 62 | + |
| 63 | +impl fmt::Debug for DisplaySecret { |
| 64 | + #[inline] |
| 65 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 | + let mut slice = [0u8; 64]; |
| 67 | + let hex = to_hex(&self.secret, &mut slice).expect("fixed-size hex serializer failed"); |
| 68 | + f.debug_tuple("DisplaySecret") |
| 69 | + .field(&hex) |
| 70 | + .finish() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl fmt::Display for DisplaySecret { |
| 75 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 76 | + for i in self.secret { |
| 77 | + write!(f, "{:02x}", i)?; |
| 78 | + } |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl SecretKey { |
| 84 | + /// Formats the explicit byte value of the secret key kept inside the type as a |
| 85 | + /// little-endian hexadecimal string using the provided formatter. |
| 86 | + /// |
| 87 | + /// This is the only method that outputs the actual secret key value, and, thus, |
| 88 | + /// should be used with extreme precaution. |
| 89 | + /// |
| 90 | + /// # Example |
| 91 | + /// |
| 92 | + /// ``` |
| 93 | + /// use secp256k1::key::ONE_KEY; |
| 94 | + /// let key = ONE_KEY; |
| 95 | + /// // Normal display hides value |
| 96 | + /// assert_eq!( |
| 97 | + /// "SecretKey(#2518682f7819fb2d)", |
| 98 | + /// format!("{:?}", key) |
| 99 | + /// ); |
| 100 | + /// // Here we explicitly display the secret value: |
| 101 | + /// assert_eq!( |
| 102 | + /// "0000000000000000000000000000000000000000000000000000000000000001", |
| 103 | + /// format!("{}", key.display_secret()) |
| 104 | + /// ); |
| 105 | + /// assert_eq!( |
| 106 | + /// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")", |
| 107 | + /// format!("{:?}", key.display_secret()) |
| 108 | + /// ); |
| 109 | + /// ``` |
| 110 | + #[inline] |
| 111 | + pub fn display_secret(&self) -> DisplaySecret { |
| 112 | + DisplaySecret { secret: self.0 } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl KeyPair { |
| 117 | + /// Formats the explicit byte value of the secret key kept inside the type as a |
| 118 | + /// little-endian hexadecimal string using the provided formatter. |
| 119 | + /// |
| 120 | + /// This is the only method that outputs the actual secret key value, and, thus, |
| 121 | + /// should be used with extreme precaution. |
| 122 | + /// |
| 123 | + /// # Example |
| 124 | + /// |
| 125 | + /// ``` |
| 126 | + /// use secp256k1::key::ONE_KEY; |
| 127 | + /// use secp256k1::schnorrsig::KeyPair; |
| 128 | + /// use secp256k1::Secp256k1; |
| 129 | + /// |
| 130 | + /// let secp = Secp256k1::new(); |
| 131 | + /// let key = ONE_KEY; |
| 132 | + /// let key = KeyPair::from_secret_key(&secp, key); |
| 133 | + /// |
| 134 | + /// // Normal display hides value |
| 135 | + /// assert_eq!( |
| 136 | + /// "KeyPair(#fb132a98a1fae666)", |
| 137 | + /// format!("{:?}", key) |
| 138 | + /// ); |
| 139 | + /// // Here we explicitly display the secret value: |
| 140 | + /// assert_eq!( |
| 141 | + /// "0000000000000000000000000000000000000000000000000000000000000001", |
| 142 | + /// format!("{}", key.display_secret()) |
| 143 | + /// ); |
| 144 | + /// assert_eq!( |
| 145 | + /// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")", |
| 146 | + /// format!("{:?}", key.display_secret()) |
| 147 | + /// ); |
| 148 | + #[inline] |
| 149 | + pub fn display_secret(&self) -> DisplaySecret { |
| 150 | + DisplaySecret { secret: self.serialize_secret() } |
| 151 | + } |
| 152 | +} |
0 commit comments