Skip to content

Commit b9e3c9d

Browse files
elichaidr-orlovsky
authored andcommitted
use to_hex in serde conversion, using stack allocation
# Conflicts: # src/key.rs # src/macros.rs
1 parent 39f1e65 commit b9e3c9d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/key.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ impl SecretKey {
218218
impl ::serde::Serialize for SecretKey {
219219
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
220220
if s.is_human_readable() {
221-
s.collect_str(self)
221+
let mut buf = [0u8; constants::SECRET_KEY_SIZE*2];
222+
s.serialize_str(::to_hex(&self.0[..], &mut buf).expect("Should never fail, the buffer is big enough."))
222223
} else {
223224
s.serialize_bytes(&self[..])
224225
}
@@ -701,9 +702,10 @@ mod test {
701702
let pk = PublicKey::from_secret_key(&s, &sk);
702703
#[cfg(fuzzing)]
703704
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");
705+
let mut buf = [0u8; constants::SECRET_KEY_SIZE * 2];
704706

705707
assert_eq!(
706-
sk.to_string(),
708+
to_hex(&sk[..], &mut buf).unwrap(),
707709
"01010101010101010001020304050607ffff0000ffff00006363636363636363"
708710
);
709711
assert_eq!(

0 commit comments

Comments
 (0)