use std::fmt::{self, Debug};
use super::{Command, Response};
use {Adapter, CommandType, ObjectId, Session, SessionError};
pub const ED25519_SIGNATURE_SIZE: usize = 64;
pub fn sign_ed25519<A, T>(
session: &mut Session<A>,
key_id: ObjectId,
data: T,
) -> Result<Ed25519Signature, SessionError>
where
A: Adapter,
T: Into<Vec<u8>>,
{
session.send_command(SignDataEdDSACommand {
key_id,
data: data.into(),
})
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct SignDataEdDSACommand {
pub key_id: ObjectId,
pub data: Vec<u8>,
}
impl Command for SignDataEdDSACommand {
type ResponseType = Ed25519Signature;
}
pub struct Ed25519Signature(pub [u8; ED25519_SIGNATURE_SIZE]);
impl Response for Ed25519Signature {
const COMMAND_TYPE: CommandType = CommandType::SignDataEdDSA;
}
impl AsRef<[u8]> for Ed25519Signature {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl Debug for Ed25519Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Ed25519Signature(")?;
for (i, byte) in self.0.iter().enumerate() {
write!(f, "{:02x}", byte)?;
write!(
f,
"{}",
if i == ED25519_SIGNATURE_SIZE - 1 {
")"
} else {
":"
}
)?;
}
Ok(())
}
}
impl_array_serializers!(Ed25519Signature, ED25519_SIGNATURE_SIZE);