Skip to content

Commit efaf561

Browse files
committed
Removing unnecessary to_hex function
1 parent a828af6 commit efaf561

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

src/key.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ impl SecretKey {
203203
impl ::serde::Serialize for SecretKey {
204204
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
205205
if s.is_human_readable() {
206-
let mut buf = [0u8; constants::SECRET_KEY_SIZE*2];
207-
s.serialize_str(::to_hex(&self.0[..], &mut buf).expect("Should never fail, the buffer is big enough."))
206+
s.serialize_str(&self.display_secret().to_string())
208207
} else {
209208
s.serialize_bytes(&self[..])
210209
}
@@ -687,11 +686,10 @@ mod test {
687686
let pk = PublicKey::from_secret_key(&s, &sk);
688687
#[cfg(fuzzing)]
689688
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");
690-
let mut buf = [0u8; constants::SECRET_KEY_SIZE * 2];
691689

692690
#[allow(deprecated)] {
693691
assert_eq!(
694-
to_hex(&sk[..], &mut buf).unwrap(),
692+
sk.display_secret().to_string(),
695693
"01010101010101010001020304050607ffff0000ffff00006363636363636363"
696694
) };
697695
assert_eq!(

src/lib.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -852,29 +852,6 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
852852
Ok(idx / 2)
853853
}
854854

855-
/// Utility function used to encode hex into a target u8 buffer. Returns
856-
/// a reference to the target buffer as an str.
857-
// it returns an error if the target buffer isn't big enough
858-
#[inline]
859-
fn to_hex<'a>(src: &[u8], target: &'a mut [u8]) -> Result<&'a str, ()> {
860-
let hex_len = src.len() * 2;
861-
if target.len() < hex_len {
862-
return Err(());
863-
}
864-
const HEX_TABLE: [u8; 16] = *b"0123456789abcdef";
865-
866-
let mut i = 0;
867-
for &b in src {
868-
target[i] = HEX_TABLE[usize::from(b >> 4)];
869-
target[i+1] = HEX_TABLE[usize::from(b & 0b00001111)];
870-
i +=2 ;
871-
}
872-
let result = &target[..hex_len];
873-
debug_assert!(str::from_utf8(result).is_ok());
874-
return unsafe { Ok(str::from_utf8_unchecked(result)) };
875-
}
876-
877-
878855
#[cfg(test)]
879856
mod tests {
880857
use rand::{RngCore, thread_rng};

src/secret.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ macro_rules! impl_display_secret {
5858
/// ```
5959
/// use secp256k1::key::ONE_KEY;
6060
/// let key = ONE_KEY;
61-
/// println!("{}", key.display_secret());
62-
/// println!("{:?}", key.display_secret());
61+
/// assert_eq!(
62+
/// "0000000000000000000000000000000000000000000000000000000000000001",
63+
/// format!("{}", key.display_secret())
64+
/// );
65+
/// assert_eq!(
66+
/// "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]",
67+
/// format!("{:?}", key.display_secret())
68+
/// );
6369
/// ```
6470
///
6571
/// [`Display`]: fmt::Display

0 commit comments

Comments
 (0)