Skip to content

Commit 5c10485

Browse files
committed
Make serialization of SchnorrSignature consistent with H512
This is a follow-up patch for 1b8970f.
1 parent 67a06c5 commit 5c10485

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

key/src/schnorr.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use primitives::H512;
2424
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
2525
use rustc_hex::{FromHex, ToHex};
2626
use secp256k1::{key, schnorr, Error as SecpError, Message as SecpMessage};
27-
use serde::de::Error as SerdeError;
2827
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2928

3029
use super::{public_to_address, Address, Error, Message, Private, Public, SECP256K1};
@@ -153,38 +152,30 @@ impl Serialize for SchnorrSignature {
153152
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
154153
where
155154
S: Serializer, {
156-
serializer.serialize_str(&self.0.to_hex())
155+
let data: H512 = self.0.into();
156+
data.serialize(serializer)
157157
}
158158
}
159159

160160
impl<'a> Deserialize<'a> for SchnorrSignature {
161161
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
162162
where
163163
D: Deserializer<'a>, {
164-
let s = String::deserialize(deserializer)?;
165-
let data = s.from_hex().map_err(|e| SerdeError::custom(format!("Invalid signature {}", e)))?;
166-
if data.len() != 64 {
167-
return Err(SerdeError::custom(format!("Invalid signature")))
168-
}
169-
let bytes = {
170-
let mut array = [0; 64];
171-
array.copy_from_slice(&data);
172-
array
173-
};
174-
Ok(SchnorrSignature(bytes))
164+
let data = H512::deserialize(deserializer)?;
165+
Ok(Self::from(data))
175166
}
176167
}
177168

178169
impl Encodable for SchnorrSignature {
179170
fn rlp_append(&self, s: &mut RlpStream) {
180171
let data: H512 = self.0.into();
181-
s.append_single_value(&data);
172+
data.rlp_append(s);
182173
}
183174
}
184175

185176
impl Decodable for SchnorrSignature {
186177
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
187-
let data: H512 = rlp.as_val()?;
178+
let data = H512::decode(rlp)?;
188179
Ok(SchnorrSignature::from(data))
189180
}
190181
}

0 commit comments

Comments
 (0)