use holo_hash::AgentPubKey;
use holochain_secure_primitive::secure_primitive;
use holochain_serialized_bytes::prelude::*;
pub const SIGNATURE_BYTES: usize = 64;
#[derive(Clone, PartialOrd, Hash, Ord)]
#[cfg_attr(feature = "fuzzing", derive(proptest_derive::Arbitrary))]
#[allow(clippy::derived_hash_with_manual_eq)]
pub struct Signature(pub [u8; SIGNATURE_BYTES]);
#[cfg(feature = "fuzzing")]
impl<'a> arbitrary::Arbitrary<'a> for Signature {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let mut buf = [0; SIGNATURE_BYTES];
u.fill_buffer(&mut buf)?;
Ok(Signature(buf))
}
}
secure_primitive!(Signature, SIGNATURE_BYTES);
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct EphemeralSignatures {
pub key: holo_hash::AgentPubKey,
pub signatures: Vec<Signature>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, SerializedBytes)]
pub struct VerifySignature {
pub key: holo_hash::AgentPubKey,
pub signature: Signature,
#[serde(with = "serde_bytes")]
pub data: Vec<u8>,
}
impl AsRef<Signature> for VerifySignature {
fn as_ref(&self) -> &Signature {
&self.signature
}
}
impl AsRef<holo_hash::AgentPubKey> for VerifySignature {
fn as_ref(&self) -> &AgentPubKey {
&self.key
}
}
impl VerifySignature {
pub fn as_data_ref(&self) -> &[u8] {
self.data.as_ref()
}
pub fn as_signature_ref(&self) -> &Signature {
self.as_ref()
}
pub fn as_key_ref(&self) -> &holo_hash::AgentPubKey {
self.as_ref()
}
pub fn new<D>(
key: holo_hash::AgentPubKey,
signature: Signature,
data: D,
) -> Result<Self, SerializedBytesError>
where
D: serde::Serialize + std::fmt::Debug,
{
Ok(Self {
key,
signature,
data: holochain_serialized_bytes::encode(&data)?,
})
}
pub fn new_raw(key: holo_hash::AgentPubKey, signature: Signature, data: Vec<u8>) -> Self {
Self {
key,
signature,
data,
}
}
}
#[test]
fn signature_roundtrip() {
let bytes = Signature::from([1u8; 64]);
let json = serde_json::to_string(&bytes).unwrap();
let _: Signature = serde_json::from_str(&json).unwrap();
}