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