1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use crate::Bytes;
use holo_hash::AgentPubKey;
use holochain_serialized_bytes::prelude::*;
pub const SIGNATURE_BYTES: usize = 64;
#[derive(Debug, PartialEq, Serialize, Deserialize, SerializedBytes, Clone)]
pub struct Sign {
pub key: holo_hash::AgentPubKey,
pub data: Bytes,
}
impl Sign {
pub fn new<S>(key: holo_hash::AgentPubKey, input: S) -> Result<Self, SerializedBytesError>
where
S: Serialize + std::fmt::Debug,
{
Ok(Self::new_raw(
key,
holochain_serialized_bytes::encode(&input)?,
))
}
pub fn new_raw(key: holo_hash::AgentPubKey, data: Vec<u8>) -> Self {
Self {
key,
data: Bytes::from(data),
}
}
pub fn key(&self) -> &AgentPubKey {
&self.key
}
pub fn data(&self) -> &[u8] {
&self.data
}
}
#[derive(Clone, PartialOrd, Hash, Ord)]
#[allow(clippy::derive_hash_xor_eq)]
pub struct Signature(pub [u8; SIGNATURE_BYTES]);
#[cfg(feature = "arbitrary")]
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))
}
}
crate::secure_primitive!(Signature, SIGNATURE_BYTES);
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(transparent)]
pub struct SignEphemeral(pub Vec<Bytes>);
impl SignEphemeral {
pub fn new<S>(inputs: Vec<S>) -> Result<Self, SerializedBytesError>
where
S: Serialize + std::fmt::Debug,
{
let datas: Result<Vec<_>, _> = inputs
.into_iter()
.map(|s| holochain_serialized_bytes::encode(&s))
.collect();
Ok(Self::new_raw(datas?))
}
pub fn new_raw(datas: Vec<Vec<u8>>) -> Self {
Self(datas.into_iter().map(Bytes::from).collect())
}
pub fn into_inner(self) -> Vec<Bytes> {
self.0
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct EphemeralSignatures {
pub key: holo_hash::AgentPubKey,
pub signatures: Vec<Signature>,
}
#[derive(Clone, Debug, PartialEq, 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,
}
}
}