freenet_stdlib/
code_hash.rs1use std::{fmt::Display, ops::Deref};
2
3use blake3::{traits::digest::Digest, Hasher as Blake3};
4use serde::{Deserialize, Serialize};
5use serde_with::serde_as;
6
7const CONTRACT_KEY_SIZE: usize = 32;
8
9#[serde_as]
10#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)]
11#[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))]
12pub struct CodeHash(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] pub(crate) [u8; CONTRACT_KEY_SIZE]);
13
14impl CodeHash {
15 pub const fn new(value: [u8; CONTRACT_KEY_SIZE]) -> Self {
16 Self(value)
17 }
18
19 pub fn from_code(wasm_code: &[u8]) -> Self {
20 let mut hasher = Blake3::new();
21 hasher.update(wasm_code);
22 let hashed = hasher.finalize();
23 let mut delegate_key = [0; CONTRACT_KEY_SIZE];
24 delegate_key.copy_from_slice(&hashed);
25 Self(delegate_key)
26 }
27
28 pub fn encode(&self) -> String {
29 bs58::encode(self.0)
30 .with_alphabet(bs58::Alphabet::BITCOIN)
31 .into_string()
32 .to_lowercase()
33 }
34}
35
36impl Deref for CodeHash {
37 type Target = [u8; CONTRACT_KEY_SIZE];
38
39 fn deref(&self) -> &Self::Target {
40 &self.0
41 }
42}
43
44impl AsRef<[u8]> for CodeHash {
45 fn as_ref(&self) -> &[u8] {
46 self.0.as_slice()
47 }
48}
49
50impl From<&[u8; CONTRACT_KEY_SIZE]> for CodeHash {
51 fn from(value: &[u8; CONTRACT_KEY_SIZE]) -> Self {
52 Self(*value)
53 }
54}
55
56impl TryFrom<&[u8]> for CodeHash {
57 type Error = std::io::Error;
58
59 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
60 if value.len() != CONTRACT_KEY_SIZE {
61 return Err(std::io::ErrorKind::InvalidData.into());
62 }
63 let mut this = [0u8; CONTRACT_KEY_SIZE];
64 this.copy_from_slice(value);
65 Ok(Self(this))
66 }
67}
68
69impl Display for CodeHash {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 write!(f, "{}", self.encode())
72 }
73}
74
75impl std::fmt::Debug for CodeHash {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 f.debug_tuple("CodeHash").field(&self.encode()).finish()
78 }
79}