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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::*;
use crate::error::HoloHashError;
use crate::hash_type;
use crate::AgentPubKey;
use crate::EntryHash;
use std::convert::TryInto;
pub(crate) const AGENT_PREFIX: &[u8] = &[0x84, 0x20, 0x24];
pub(crate) const ENTRY_PREFIX: &[u8] = &[0x84, 0x21, 0x24];
pub(crate) const DHTOP_PREFIX: &[u8] = &[0x84, 0x24, 0x24];
pub(crate) const DNA_PREFIX: &[u8] = &[0x84, 0x2d, 0x24];
pub(crate) const NET_ID_PREFIX: &[u8] = &[0x84, 0x22, 0x24];
pub(crate) const HEADER_PREFIX: &[u8] = &[0x84, 0x29, 0x24];
pub(crate) const WASM_PREFIX: &[u8] = &[0x84, 0x2a, 0x24];
pub(crate) const EXTERNAL_PREFIX: &[u8] = &[0x84, 0x2f, 0x24];
pub trait PrimitiveHashType: HashType {
fn new() -> Self;
fn static_prefix() -> &'static [u8];
fn hash_name(self) -> &'static str;
}
impl<P: PrimitiveHashType> HashType for P {
fn get_prefix(self) -> &'static [u8] {
P::static_prefix()
}
fn try_from_prefix(prefix: &[u8]) -> HoloHashResult<Self> {
if prefix == P::static_prefix() {
Ok(P::new())
} else {
Err(HoloHashError::BadPrefix(
PrimitiveHashType::hash_name(P::new()).to_string(),
prefix.try_into().expect("3 byte prefix"),
))
}
}
fn hash_name(self) -> &'static str {
PrimitiveHashType::hash_name(self)
}
}
macro_rules! primitive_hash_type {
($name: ident, $display: ident, $visitor: ident, $prefix: ident) => {
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct $name;
impl PrimitiveHashType for $name {
fn new() -> Self {
Self
}
fn static_prefix() -> &'static [u8] {
&$prefix
}
fn hash_name(self) -> &'static str {
stringify!($display)
}
}
#[cfg(feature = "serialization")]
impl serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(self.get_prefix())
}
}
#[cfg(feature = "serialization")]
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_bytes($visitor)
}
}
#[cfg(feature = "serialization")]
struct $visitor;
#[cfg(feature = "serialization")]
impl<'de> serde::de::Visitor<'de> for $visitor {
type Value = $name;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a HoloHash of primitive hash_type")
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
$prefix => Ok($name),
_ => panic!("unknown hash prefix during hash deserialization {:?}", v),
}
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut vec = Vec::with_capacity(seq.size_hint().unwrap_or(0));
while let Some(b) = seq.next_element()? {
vec.push(b);
}
self.visit_bytes(&vec)
}
}
};
}
primitive_hash_type!(Agent, AgentPubKey, AgentVisitor, AGENT_PREFIX);
primitive_hash_type!(Entry, EntryHash, EntryVisitor, ENTRY_PREFIX);
primitive_hash_type!(Dna, DnaHash, DnaVisitor, DNA_PREFIX);
primitive_hash_type!(DhtOp, DhtOpHash, DhtOpVisitor, DHTOP_PREFIX);
primitive_hash_type!(Header, HeaderHash, HeaderVisitor, HEADER_PREFIX);
primitive_hash_type!(NetId, NetIdHash, NetIdVisitor, NET_ID_PREFIX);
primitive_hash_type!(Wasm, WasmHash, WasmVisitor, WASM_PREFIX);
primitive_hash_type!(External, ExternalHash, ExternalVisitor, EXTERNAL_PREFIX);
impl HashTypeSync for DhtOp {}
impl HashTypeSync for Entry {}
impl HashTypeSync for Header {}
impl HashTypeSync for Dna {}
impl HashTypeAsync for External {}
impl HashTypeAsync for NetId {}
impl HashTypeAsync for Wasm {}
impl From<AgentPubKey> for EntryHash {
fn from(hash: AgentPubKey) -> EntryHash {
hash.retype(hash_type::Entry)
}
}
impl From<EntryHash> for AgentPubKey {
fn from(hash: EntryHash) -> AgentPubKey {
hash.retype(hash_type::Agent)
}
}