ic_certification/
lib.rs

1//! # IC Certification
2
3#![deny(clippy::all)]
4#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
5
6use hex::FromHexError;
7
8pub mod certificate;
9pub mod hash_tree;
10pub use crate::hash_tree::*;
11pub mod rb_tree;
12pub use crate::rb_tree::*;
13pub mod nested_rb_tree;
14pub use crate::nested_rb_tree::*;
15
16#[doc(inline)]
17pub use hash_tree::LookupResult;
18
19/// A HashTree representing a full tree.
20pub type HashTree = hash_tree::HashTree<Vec<u8>>;
21/// A HashTreeNode representing a node in a tree.
22pub type HashTreeNode = hash_tree::HashTreeNode<Vec<u8>>;
23/// For labeled [`HashTreeNode`](hash_tree::HashTreeNode)
24pub type Label = hash_tree::Label<Vec<u8>>;
25/// A result of looking up for a subtree.
26pub type SubtreeLookupResult = hash_tree::SubtreeLookupResult<Vec<u8>>;
27
28/// A `Delegation` as defined in <https://internetcomputer.org/docs/current/references/ic-interface-spec/#certification-delegation>
29pub type Delegation = certificate::Delegation<Vec<u8>>;
30/// A `Certificate` as defined in <https://internetcomputer.org/docs/current/references/ic-interface-spec/#certificate>
31pub type Certificate = certificate::Certificate<Vec<u8>>;
32
33/// Create an empty hash tree.
34#[inline]
35pub fn empty() -> HashTree {
36    hash_tree::empty()
37}
38
39/// Create a forked tree from two trees or node.
40#[inline]
41pub fn fork(left: HashTree, right: HashTree) -> HashTree {
42    hash_tree::fork(left, right)
43}
44
45/// Create a labeled hash tree.
46#[inline]
47pub fn labeled<L: Into<Label>, N: Into<HashTree>>(label: L, node: N) -> HashTree {
48    hash_tree::label(label, node)
49}
50
51/// Create a leaf in the tree.
52#[inline]
53pub fn leaf<L: Into<Vec<u8>>>(leaf: L) -> HashTree {
54    hash_tree::leaf(leaf)
55}
56
57/// Create a pruned tree node.
58#[inline]
59pub fn pruned<C: Into<Hash>>(content: C) -> HashTree {
60    hash_tree::pruned(content)
61}
62
63/// Create a pruned tree node, from a hex representation of the data. Useful for
64/// testing or hard coded values.
65#[inline]
66pub fn pruned_from_hex<C: AsRef<str>>(content: C) -> Result<HashTree, FromHexError> {
67    hash_tree::pruned_from_hex(content)
68}
69
70#[cfg(feature = "serde")]
71mod serde_impl {
72    use std::borrow::Cow;
73
74    use serde::Deserialize;
75    use serde_bytes::{ByteBuf, Bytes};
76
77    /// A trait to genericize deserializing owned or borrowed bytes
78    pub trait Storage {
79        type Temp<'a>: Deserialize<'a>;
80        type Value<'a>: AsRef<[u8]>;
81        fn convert(t: Self::Temp<'_>) -> Self::Value<'_>;
82    }
83
84    /// `Vec<u8>`
85    pub struct VecStorage;
86    /// `&[u8]`
87    pub struct SliceStorage;
88    /// `Cow<[u8]>`
89    pub struct CowStorage;
90
91    impl Storage for VecStorage {
92        type Temp<'a> = ByteBuf;
93        type Value<'a> = Vec<u8>;
94        fn convert(t: Self::Temp<'_>) -> Self::Value<'_> {
95            t.into_vec()
96        }
97    }
98
99    impl Storage for SliceStorage {
100        type Temp<'a> = &'a Bytes;
101        type Value<'a> = &'a [u8];
102        fn convert(t: Self::Temp<'_>) -> Self::Value<'_> {
103            t.as_ref()
104        }
105    }
106
107    impl Storage for CowStorage {
108        type Temp<'a> = &'a Bytes;
109        type Value<'a> = Cow<'a, [u8]>;
110        fn convert(t: Self::Temp<'_>) -> Self::Value<'_> {
111            Cow::Borrowed(t.as_ref())
112        }
113    }
114}