tezos_smart_rollup_encoding/
dac.rs

1// SPDX-FileCopyrightText: 2023 TriliTech <contact@trili.tech>
2//
3// SPDX-License-Identifier: MIT
4
5//! Defines dac related types and functions.
6
7#[cfg(feature = "crypto")]
8pub mod certificate;
9pub mod pages;
10
11#[deprecated = "These items have moved, please import from dac::pages instead"]
12pub use pages::*;
13
14#[cfg(feature = "alloc")]
15#[doc(inline)]
16pub use self::alloc::PreimageHash;
17
18#[cfg(feature = "alloc")]
19mod alloc {
20
21    use tezos_data_encoding::enc::BinWriter;
22    use tezos_data_encoding::encoding::HasEncoding;
23    use tezos_data_encoding::nom::NomReader;
24    use tezos_smart_rollup_core::PREIMAGE_HASH_SIZE;
25    /// A 33-byte hash corresponding to a preimage.
26    #[derive(Eq, PartialEq, Debug, HasEncoding, NomReader, BinWriter, Clone, Hash)]
27    pub struct PreimageHash {
28        #[encoding(sized = "PREIMAGE_HASH_SIZE", bytes)]
29        hash: Vec<u8>,
30    }
31
32    impl AsRef<[u8; PREIMAGE_HASH_SIZE]> for PreimageHash {
33        fn as_ref(&self) -> &[u8; PREIMAGE_HASH_SIZE] {
34            self.hash
35                .as_slice()
36                .try_into()
37                .expect("Must be PREIMAGE_HASH_SIZE")
38        }
39    }
40
41    impl From<&[u8; PREIMAGE_HASH_SIZE]> for PreimageHash {
42        fn from(hash: &[u8; PREIMAGE_HASH_SIZE]) -> Self {
43            let hash = hash.as_slice().to_vec();
44
45            Self { hash }
46        }
47    }
48
49    impl From<Vec<u8>> for PreimageHash {
50        fn from(hash: Vec<u8>) -> Self {
51            Self { hash }
52        }
53    }
54
55    impl From<PreimageHash> for Vec<u8> {
56        fn from(val: PreimageHash) -> Self {
57            val.hash
58        }
59    }
60
61    impl From<PreimageHash> for [u8; PREIMAGE_HASH_SIZE] {
62        fn from(value: PreimageHash) -> Self {
63            value.hash.try_into().unwrap_or_else(|_| {
64                unreachable!("Impossible to happen since it's been smart-constructed")
65            })
66        }
67    }
68}