1pub use hdk_extensions::hdi;
2pub use hdk_extensions::holo_hash;
3pub use hdk_extensions::hdk;
4pub use hdk_extensions::hdi_extensions;
5pub use hdk_extensions;
6
7use std::collections::BTreeMap;
8use hex;
9use hdi_extensions::guest_error;
10use hdi::prelude::*;
11use hdk::prelude::sys_time;
12use hmac::{ Hmac, Mac };
13use sha2::{ Sha256, Digest };
14use rmp_serde;
15
16type HmacSha256 = Hmac<Sha256>;
17
18
19
20pub fn now() -> ExternResult<u64> {
25 sys_time()
26 .map( |t| (t.as_micros() / 1000) as u64 )
27}
28
29pub fn sha256<T>(data: &T) -> ExternResult<[u8; 32]>
31where
32 T: Serialize + std::fmt::Debug,
33{
34 let bytes = rmp_serde::to_vec( &data )
35 .or(Err(guest_error!(format!("Failed to serialize input; {:#?}", data))))?;
36 let mut hasher = Sha256::new();
37 hasher.update( &bytes );
38 Ok(
39 <[u8; 32]>::from( hasher.finalize() )
40 )
41}
42
43
44
45pub trait CommonFields<'a> {
48 fn metadata(&'a self) -> &'a BTreeMap<String, rmpv::Value>;
50}
51
52#[macro_export]
67macro_rules! common_fields {
68 ( $name:ident ) => {
69 impl<'a> CommonFields<'a> for $name {
70 fn metadata(&'a self) -> &'a BTreeMap<String, rmpv::Value> {
71 &self.metadata
72 }
73 }
74 };
75}
76
77
78
79#[hdk_entry_helper]
84#[derive(Clone)]
85pub struct LeafDataBlock {
86 pub label: String,
88 pub value: rmpv::Value,
90 #[serde(with = "serde_bytes")]
92 pub salt: Vec<u8>,
93}
94
95impl LeafDataBlock {
96 pub fn hash(&self) -> ExternResult<[u8; 32]> {
98 sha256( &self )
99 }
100}
101
102#[derive(Clone, Debug, Serialize, Deserialize)]
104pub struct LeafProofPayload {
105 pub proof: Vec<[u8; 32]>,
107 pub index: u64,
109 pub target: LeafDataBlock,
111 pub leaf: [u8; 32],
113 pub root: [u8; 32],
115 pub total_leaves: u64,
117}
118
119
120
121#[hdk_entry_helper]
126#[derive(Clone)]
127pub struct DataBlocksEntry {
128 pub blocks: Vec<LeafDataBlock>,
130
131 pub metadata: BTreeMap<String, rmpv::Value>,
133}
134common_fields!( DataBlocksEntry );
135
136
137#[hdk_entry_helper]
139#[derive(Clone)]
140pub struct TreeEntry {
141 pub data_blocks: ActionHash,
143 pub leaves: Vec<[u8; 32]>,
145 pub entropy: Vec<u8>,
147 pub root: [u8; 32],
149
150 pub metadata: BTreeMap<String, rmpv::Value>,
152}
153common_fields!( TreeEntry );
154
155impl TreeEntry {
156 pub fn root_as_hex(&self) -> String {
158 hex::encode( self.root.to_owned() )
159 }
160}
161
162
163
164#[hdk_entry_helper]
169#[derive(Clone)]
170pub struct ClaimEntry {
171 pub name: String,
173 pub author: AgentPubKey,
175 pub root: [u8; 32],
177
178 pub metadata: BTreeMap<String, rmpv::Value>,
180}
181common_fields!( ClaimEntry );
182
183
184
185#[derive(Clone, Debug, Serialize, Deserialize)]
190pub struct LeafInput {
191 pub label: String,
193 pub value: rmpv::Value,
195}
196
197impl LeafInput {
198 pub fn into_data_block(self, entropy: &Vec<u8>, index: usize) -> ExternResult<LeafDataBlock> {
202 let mut hmac = HmacSha256::new_from_slice( entropy.as_slice() )
203 .or(Err(guest_error!(format!("Failed to create hmac with entropy: {:#?}", entropy ))))?;
204
205 hmac.update( &index.to_le_bytes() );
206 let result = hmac.finalize();
207
208 Ok(
209 LeafDataBlock {
210 label: self.label,
211 value: self.value,
212 salt: result.into_bytes().to_vec(),
213 }
214 )
215 }
216}
217
218type OptionalBytes = Option<serde_bytes::ByteBuf>;
219
220#[derive(Clone, Debug, Serialize, Deserialize)]
222pub struct CreateTreeInput {
223 pub leaves: Vec<LeafInput>,
225 pub entropy: OptionalBytes,
227}
228
229#[derive(Clone, Debug, Serialize, Deserialize)]
231pub struct GetLeafProofInput {
232 pub tree_id: ActionHash,
234 pub label: String,
236}
237
238#[derive(Clone, Debug, Serialize, Deserialize)]
240pub struct VerifyLeafProofInput {
241 pub proof: Vec<[u8; 32]>,
243 pub index: u64,
245 pub leaf: [u8; 32],
247 pub root: [u8; 32],
249 pub total_leaves: u64,
251}
252
253
254
255#[cfg(test)]
256mod tests {
257 use super::{ sha256, Serialize };
258
259 #[test]
260 fn test_sha256() {
261 #[derive(Debug, Serialize)]
262 pub struct Data( Option<u8> );
263
264 assert_eq!( sha256( &Data(None) ).unwrap(), [
265 228, 255, 94, 125, 122, 127, 8, 233,
266 128, 10, 62, 37, 203, 119, 69, 51,
267 203, 32, 4, 13, 243, 11, 107, 161,
268 15, 149, 111, 154, 205, 14, 179, 247
269 ] );
270 }
271}