Skip to content

Commit 3cbf051

Browse files
committed
Getting rid of SecretKey display methods
1 parent bb88842 commit 3cbf051

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

src/key.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,6 @@ pub struct SecretKey(pub(crate) [u8; constants::SECRET_KEY_SIZE]);
3131
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
3232
impl_safe_debug!(SecretKey);
3333

34-
impl fmt::LowerHex for SecretKey {
35-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36-
for ch in &self.0[..] {
37-
write!(f, "{:02x}", *ch)?;
38-
}
39-
Ok(())
40-
}
41-
}
42-
43-
impl fmt::Display for SecretKey {
44-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45-
fmt::LowerHex::fmt(self, f)
46-
}
47-
}
48-
4934
impl str::FromStr for SecretKey {
5035
type Err = Error;
5136
fn from_str(s: &str) -> Result<SecretKey, Error> {
@@ -218,7 +203,8 @@ impl SecretKey {
218203
impl ::serde::Serialize for SecretKey {
219204
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
220205
if s.is_human_readable() {
221-
s.collect_str(self)
206+
#[allow(deprecated)]
207+
s.serialize_str(&self.format_secret_key())
222208
} else {
223209
s.serialize_bytes(&self[..])
224210
}
@@ -705,10 +691,11 @@ mod test {
705691
#[cfg(fuzzing)]
706692
let pk = PublicKey::from_slice(&[0x02, 0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
707693

694+
#[allow(deprecated)] {
708695
assert_eq!(
709-
sk.to_string(),
696+
sk.format_secret_key(),
710697
"01010101010101010001020304050607ffff0000ffff00006363636363636363"
711-
);
698+
) };
712699
assert_eq!(
713700
SecretKey::from_str("01010101010101010001020304050607ffff0000ffff00006363636363636363").unwrap(),
714701
sk

src/macros.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,9 @@ macro_rules! impl_safe_debug {
8585
note = "Caution: you are explicitly outputting secret key value! This can be done
8686
only in debug environment and that's why always considered as ``deprecated''"
8787
)]
88-
#[cfg(feature = "std")]
8988
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("")
89+
use ::bitcoin_hashes::hex::ToHex;
90+
self.0[..].to_hex()
9591
}
9692
}
9793
}

0 commit comments

Comments
 (0)