Skip to main content

freenet_stdlib/
code_hash.rs

1use 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    }
36}
37
38impl Deref for CodeHash {
39    type Target = [u8; CONTRACT_KEY_SIZE];
40
41    fn deref(&self) -> &Self::Target {
42        &self.0
43    }
44}
45
46impl AsRef<[u8]> for CodeHash {
47    fn as_ref(&self) -> &[u8] {
48        self.0.as_slice()
49    }
50}
51
52impl From<&[u8; CONTRACT_KEY_SIZE]> for CodeHash {
53    fn from(value: &[u8; CONTRACT_KEY_SIZE]) -> Self {
54        Self(*value)
55    }
56}
57
58impl TryFrom<&[u8]> for CodeHash {
59    type Error = std::io::Error;
60
61    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
62        if value.len() != CONTRACT_KEY_SIZE {
63            return Err(std::io::ErrorKind::InvalidData.into());
64        }
65        let mut this = [0u8; CONTRACT_KEY_SIZE];
66        this.copy_from_slice(value);
67        Ok(Self(this))
68    }
69}
70
71impl Display for CodeHash {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        write!(f, "{}", self.encode())
74    }
75}
76
77impl std::fmt::Debug for CodeHash {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        f.debug_tuple("CodeHash").field(&self.encode()).finish()
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn encode_roundtrips_with_case_sensitive_alphabet() {
89        // Bytes chosen so the base58 encoding contains uppercase characters,
90        // which a lowercasing `encode` would corrupt.
91        let original = CodeHash([0xFF; CONTRACT_KEY_SIZE]);
92        let encoded = original.encode();
93        assert!(
94            encoded.chars().any(|c| c.is_ascii_uppercase()),
95            "test fixture must contain uppercase base58 chars, got {encoded}"
96        );
97
98        let mut decoded = [0u8; CONTRACT_KEY_SIZE];
99        bs58::decode(&encoded)
100            .with_alphabet(bs58::Alphabet::BITCOIN)
101            .onto(&mut decoded)
102            .expect("encoded CodeHash must decode with the BITCOIN alphabet");
103
104        assert_eq!(original.0, decoded);
105    }
106}