miraland_program/
keccak.rs1use {
6 crate::sanitize::Sanitize,
7 borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
8 sha3::{Digest, Keccak256},
9 std::{convert::TryFrom, fmt, mem, str::FromStr},
10 thiserror::Error,
11};
12
13pub const HASH_BYTES: usize = 32;
14const MAX_BASE58_LEN: usize = 44;
16#[derive(
17 Serialize,
18 Deserialize,
19 BorshSerialize,
20 BorshDeserialize,
21 BorshSchema,
22 Clone,
23 Copy,
24 Default,
25 Eq,
26 PartialEq,
27 Ord,
28 PartialOrd,
29 Hash,
30 AbiExample,
31)]
32#[borsh(crate = "borsh")]
33#[repr(transparent)]
34pub struct Hash(pub [u8; HASH_BYTES]);
35
36#[derive(Clone, Default)]
37pub struct Hasher {
38 hasher: Keccak256,
39}
40
41impl Hasher {
42 pub fn hash(&mut self, val: &[u8]) {
43 self.hasher.update(val);
44 }
45 pub fn hashv(&mut self, vals: &[&[u8]]) {
46 for val in vals {
47 self.hash(val);
48 }
49 }
50 pub fn result(self) -> Hash {
51 Hash(self.hasher.finalize().into())
52 }
53}
54
55impl Sanitize for Hash {}
56
57impl AsRef<[u8]> for Hash {
58 fn as_ref(&self) -> &[u8] {
59 &self.0[..]
60 }
61}
62
63impl fmt::Debug for Hash {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 write!(f, "{}", bs58::encode(self.0).into_string())
66 }
67}
68
69impl fmt::Display for Hash {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 write!(f, "{}", bs58::encode(self.0).into_string())
72 }
73}
74
75#[derive(Debug, Clone, PartialEq, Eq, Error)]
76pub enum ParseHashError {
77 #[error("string decoded to wrong size for hash")]
78 WrongSize,
79 #[error("failed to decoded string to hash")]
80 Invalid,
81}
82
83impl FromStr for Hash {
84 type Err = ParseHashError;
85
86 fn from_str(s: &str) -> Result<Self, Self::Err> {
87 if s.len() > MAX_BASE58_LEN {
88 return Err(ParseHashError::WrongSize);
89 }
90 let bytes = bs58::decode(s)
91 .into_vec()
92 .map_err(|_| ParseHashError::Invalid)?;
93 if bytes.len() != mem::size_of::<Hash>() {
94 Err(ParseHashError::WrongSize)
95 } else {
96 Ok(Hash::new(&bytes))
97 }
98 }
99}
100
101impl Hash {
102 pub fn new(hash_slice: &[u8]) -> Self {
103 Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
104 }
105
106 pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
107 Self(hash_array)
108 }
109
110 pub fn new_unique() -> Self {
112 use crate::atomic_u64::AtomicU64;
113 static I: AtomicU64 = AtomicU64::new(1);
114
115 let mut b = [0u8; HASH_BYTES];
116 let i = I.fetch_add(1);
117 b[0..8].copy_from_slice(&i.to_le_bytes());
118 Self::new(&b)
119 }
120
121 pub fn to_bytes(self) -> [u8; HASH_BYTES] {
122 self.0
123 }
124}
125
126pub fn hashv(vals: &[&[u8]]) -> Hash {
128 #[cfg(not(target_os = "solana"))]
131 {
132 let mut hasher = Hasher::default();
133 hasher.hashv(vals);
134 hasher.result()
135 }
136 #[cfg(target_os = "solana")]
138 {
139 let mut hash_result = [0; HASH_BYTES];
140 unsafe {
141 crate::syscalls::sol_keccak256(
142 vals as *const _ as *const u8,
143 vals.len() as u64,
144 &mut hash_result as *mut _ as *mut u8,
145 );
146 }
147 Hash::new_from_array(hash_result)
148 }
149}
150
151pub fn hash(val: &[u8]) -> Hash {
153 hashv(&[val])
154}
155
156pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
158 let mut hash_data = id.as_ref().to_vec();
159 hash_data.extend_from_slice(val);
160 hash(&hash_data)
161}