Merge pull request #109 from jonasnick/human-sigs
Add human readable serialization to Signatures
This commit is contained in:
commit
0bf88fe444
36
src/lib.rs
36
src/lib.rs
|
@ -190,8 +190,8 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
);
|
);
|
||||||
debug_assert!(err == 1);
|
debug_assert!(err == 1);
|
||||||
}
|
}
|
||||||
for ch in &v[..] {
|
for i in 0..len {
|
||||||
write!(f, "{:02x}", *ch)?;
|
write!(f, "{:02x}", v[i])?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -400,7 +400,12 @@ impl From<ffi::Signature> for Signature {
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
impl ::serde::Serialize for Signature {
|
impl ::serde::Serialize for Signature {
|
||||||
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
s.serialize_bytes(&self.serialize_der())
|
if s.is_human_readable() {
|
||||||
|
s.collect_str(self)
|
||||||
|
} else {
|
||||||
|
s.serialize_bytes(&self.serialize_der())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,9 +413,14 @@ impl ::serde::Serialize for Signature {
|
||||||
impl<'de> ::serde::Deserialize<'de> for Signature {
|
impl<'de> ::serde::Deserialize<'de> for Signature {
|
||||||
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Signature, D::Error> {
|
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Signature, D::Error> {
|
||||||
use ::serde::de::Error;
|
use ::serde::de::Error;
|
||||||
|
use str::FromStr;
|
||||||
let sl: &[u8] = ::serde::Deserialize::deserialize(d)?;
|
if d.is_human_readable() {
|
||||||
Signature::from_der(sl).map_err(D::Error::custom)
|
let sl: &str = ::serde::Deserialize::deserialize(d)?;
|
||||||
|
Signature::from_str(sl).map_err(D::Error::custom)
|
||||||
|
} else {
|
||||||
|
let sl: &[u8] = ::serde::Deserialize::deserialize(d)?;
|
||||||
|
Signature::from_der(sl).map_err(D::Error::custom)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -840,6 +850,11 @@ mod tests {
|
||||||
72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45\
|
72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45\
|
||||||
72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45"
|
72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45"
|
||||||
).is_err());
|
).is_err());
|
||||||
|
|
||||||
|
// 71 byte signature
|
||||||
|
let hex_str = "30450221009d0bad576719d32ae76bedb34c774866673cbde3f4e12951555c9408e6ce774b02202876e7102f204f6bfee26c967c3926ce702cf97d4b010062e193f763190f6776";
|
||||||
|
let sig = Signature::from_str(&hex_str).expect("byte str decode");
|
||||||
|
assert_eq!(&format!("{}", sig), hex_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -966,7 +981,7 @@ mod tests {
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_signature_serde() {
|
fn test_signature_serde() {
|
||||||
use serde_test::{Token, assert_tokens};
|
use serde_test::{Configure, Token, assert_tokens};
|
||||||
|
|
||||||
let s = Secp256k1::new();
|
let s = Secp256k1::new();
|
||||||
|
|
||||||
|
@ -980,8 +995,13 @@ mod tests {
|
||||||
226, 108, 150, 124, 57, 38, 206, 112, 44, 249, 125, 75, 1, 0, 98, 225,
|
226, 108, 150, 124, 57, 38, 206, 112, 44, 249, 125, 75, 1, 0, 98, 225,
|
||||||
147, 247, 99, 25, 15, 103, 118
|
147, 247, 99, 25, 15, 103, 118
|
||||||
];
|
];
|
||||||
|
static SIG_STR: &'static str = "\
|
||||||
|
30450221009d0bad576719d32ae76bedb34c774866673cbde3f4e12951555c9408e6ce77\
|
||||||
|
4b02202876e7102f204f6bfee26c967c3926ce702cf97d4b010062e193f763190f6776\
|
||||||
|
";
|
||||||
|
|
||||||
assert_tokens(&sig, &[Token::BorrowedBytes(&SIG_BYTES[..])]);
|
assert_tokens(&sig.compact(), &[Token::BorrowedBytes(&SIG_BYTES[..])]);
|
||||||
|
assert_tokens(&sig.readable(), &[Token::BorrowedStr(SIG_STR)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue