mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-05 20:30:57 +00:00
Add additional visitor methods for deserialization
This commit is contained in:
parent
36a51acbf0
commit
3d9d11dcdf
1 changed files with 36 additions and 2 deletions
|
|
@ -18,6 +18,8 @@ use serde::de::Error as SerdeError;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::de::Visitor;
|
use serde::de::Visitor;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::de::SeqAccess;
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
|
|
||||||
pub use sha2::Sha512;
|
pub use sha2::Sha512;
|
||||||
|
|
@ -431,15 +433,47 @@ impl<'d> Deserialize<'d> for Keypair {
|
||||||
where
|
where
|
||||||
E: SerdeError,
|
E: SerdeError,
|
||||||
{
|
{
|
||||||
|
if bytes.len() != KEYPAIR_LENGTH {
|
||||||
|
return Err(SerdeError::invalid_length(bytes.len(), &self));
|
||||||
|
}
|
||||||
|
|
||||||
let secret_key = SecretKey::from_bytes(&bytes[..SECRET_KEY_LENGTH]);
|
let secret_key = SecretKey::from_bytes(&bytes[..SECRET_KEY_LENGTH]);
|
||||||
let public_key = PublicKey::from_bytes(&bytes[SECRET_KEY_LENGTH..]);
|
let public_key = PublicKey::from_bytes(&bytes[SECRET_KEY_LENGTH..]);
|
||||||
|
|
||||||
if secret_key.is_ok() && public_key.is_ok() {
|
if let (Ok(secret), Ok(public)) = (secret_key, public_key) {
|
||||||
Ok(Keypair{ secret: secret_key.unwrap(), public: public_key.unwrap() })
|
Ok(Keypair{ secret, public })
|
||||||
} else {
|
} else {
|
||||||
Err(SerdeError::invalid_length(bytes.len(), &self))
|
Err(SerdeError::invalid_length(bytes.len(), &self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A>(self, mut seq: A) -> Result<Keypair, A::Error>
|
||||||
|
where
|
||||||
|
A: SeqAccess<'d>
|
||||||
|
{
|
||||||
|
if let Some(len) = seq.size_hint() {
|
||||||
|
if len != KEYPAIR_LENGTH {
|
||||||
|
return Err(SerdeError::invalid_length(len, &self));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: We could do this with `MaybeUninit` to avoid unnecessary initialization costs
|
||||||
|
let mut bytes: [u8; KEYPAIR_LENGTH] = [0u8; KEYPAIR_LENGTH];
|
||||||
|
|
||||||
|
for i in 0..KEYPAIR_LENGTH {
|
||||||
|
bytes[i] = seq.next_element()?.ok_or_else(|| SerdeError::invalid_length(i, &self))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let secret_key = SecretKey::from_bytes(&bytes[..SECRET_KEY_LENGTH]);
|
||||||
|
let public_key = PublicKey::from_bytes(&bytes[SECRET_KEY_LENGTH..]);
|
||||||
|
|
||||||
|
if let (Ok(secret), Ok(public)) = (secret_key, public_key) {
|
||||||
|
Ok(Keypair{ secret, public })
|
||||||
|
} else {
|
||||||
|
Err(SerdeError::invalid_length(bytes.len(), &self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
deserializer.deserialize_bytes(KeypairVisitor)
|
deserializer.deserialize_bytes(KeypairVisitor)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue