holochain_integrity_types/
signature.rs1use holo_hash::AgentPubKey;
3use holochain_secure_primitive::secure_primitive;
4use holochain_serialized_bytes::prelude::*;
5
6pub const SIGNATURE_BYTES: usize = 64;
8
9#[derive(Clone, PartialOrd, Hash, Ord)]
11#[allow(clippy::derived_hash_with_manual_eq)]
15pub struct Signature(pub [u8; SIGNATURE_BYTES]);
16
17secure_primitive!(Signature, SIGNATURE_BYTES);
22
23#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
30pub struct EphemeralSignatures {
31 pub key: holo_hash::AgentPubKey,
33 pub signatures: Vec<Signature>,
35}
36
37#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, SerializedBytes)]
39pub struct VerifySignature {
40 pub key: holo_hash::AgentPubKey,
43
44 pub signature: Signature,
46
47 #[serde(with = "serde_bytes")]
49 pub data: Vec<u8>,
50}
51
52impl AsRef<Signature> for VerifySignature {
53 fn as_ref(&self) -> &Signature {
54 &self.signature
55 }
56}
57
58impl AsRef<holo_hash::AgentPubKey> for VerifySignature {
59 fn as_ref(&self) -> &AgentPubKey {
60 &self.key
61 }
62}
63
64impl VerifySignature {
65 pub fn as_data_ref(&self) -> &[u8] {
67 self.data.as_ref()
68 }
69
70 pub fn as_signature_ref(&self) -> &Signature {
72 self.as_ref()
73 }
74
75 pub fn as_key_ref(&self) -> &holo_hash::AgentPubKey {
77 self.as_ref()
78 }
79
80 pub fn new<D>(
82 key: holo_hash::AgentPubKey,
83 signature: Signature,
84 data: D,
85 ) -> Result<Self, SerializedBytesError>
86 where
87 D: serde::Serialize + std::fmt::Debug,
88 {
89 Ok(Self {
90 key,
91 signature,
92 data: holochain_serialized_bytes::encode(&data)?,
93 })
94 }
95
96 pub fn new_raw(key: holo_hash::AgentPubKey, signature: Signature, data: Vec<u8>) -> Self {
98 Self {
99 key,
100 signature,
101 data,
102 }
103 }
104}
105
106#[test]
107fn signature_roundtrip() {
108 let bytes = Signature::from([1u8; 64]);
109 let json = serde_json::to_string(&bytes).unwrap();
110 let _: Signature = serde_json::from_str(&json).unwrap();
111}