1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use candid::CandidType;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
mod test;
mod store;
pub use store::*;
mod utils;
pub use utils::*;
mod types;
pub use types::*;
pub mod traits;
/// Represents a WebAssembly (Wasm) binary.
#[derive(CandidType, Deserialize, Serialize, Clone)]
pub struct Wasm(pub ByteBuf);
impl Default for Wasm {
fn default() -> Self {
Self(ByteBuf::new())
}
}
impl Wasm {
/// Loads a Wasm binary blob into the current Wasm instance.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
/// use b3_utils::wasm::Blob;
///
/// let mut wasm = Wasm::default();
/// let blob: Blob = vec![0x00, 0x61, 0x73, 0x6D];
/// wasm.load(&blob);
/// ```
pub fn load(&mut self, blob: &Blob) -> WasmSize {
self.0.extend(blob);
self.0.len()
}
/// Unloads the current Wasm binary.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// wasm.unload();
/// ```
pub fn unload(&mut self) -> WasmSize {
self.0.clear();
self.0.len()
}
/// Returns the size of the current Wasm binary.
/// The size of the current Wasm binary is equal to the number of bytes in the current Wasm binary.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert_eq!(wasm.len(), 0);
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert_eq!(wasm.len(), 4);
/// ```
pub fn len(&self) -> WasmSize {
self.0.len()
}
/// Returns the current Wasm binary.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
/// use b3_utils::wasm::Blob;
///
/// let mut wasm = Wasm::default();
/// assert_eq!(wasm.bytes(), Blob::default());
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert_eq!(wasm.bytes(), Blob::from(vec![0x00, 0x61, 0x73, 0x6D]));
/// ```
pub fn bytes(&self) -> Blob {
self.0.to_vec()
}
/// Returns true if the current Wasm binary is empty.
/// The current Wasm binary is empty if its size is equal to zero.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert!(wasm.is_empty());
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert!(!wasm.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.0.len() == 0
}
/// Returns true if the current Wasm binary is loading.
/// The current Wasm binary is loading if it is empty or if its size is less than the given size.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert!(wasm.is_loading(0));
/// assert!(wasm.is_loading(1));
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert!(wasm.is_loading(5));
/// ```
pub fn is_loading(&self, size: usize) -> bool {
self.0.is_empty() || self.0.len() < size
}
/// Returns true if the current Wasm binary is loaded.
/// The current Wasm binary is loaded if its size is equal to the given size.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert!(!wasm.is_loaded(1));
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert!(wasm.is_loaded(4));
/// ```
pub fn is_loaded(&self, size: usize) -> bool {
self.0.len() == size
}
/// Returns true if the current Wasm binary is unloaded.
/// The current Wasm binary is unloaded if its size is equal to zero.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert!(wasm.is_unloaded());
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert!(!wasm.is_empty());
/// wasm.unload();
/// assert!(wasm.is_unloaded());
/// ```
pub fn is_unloaded(&self) -> bool {
self.0.is_empty()
}
/// Generates a hash of the current Wasm binary.
/// Returns a default hash if the current Wasm binary is empty.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert_eq!(wasm.hash().to_vec(), vec![0x00; 32]);
///
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// let hash = wasm.hash();
/// assert_eq!(hash.to_vec(), vec![205, 93, 73, 53, 164, 140, 6, 114, 203, 6, 64, 123, 180, 67, 188, 0, 135, 175, 249, 71, 198, 184, 100, 186, 200, 134, 152, 44, 115, 179, 2, 127]);
/// ```
pub fn hash(&self) -> WasmHash {
if self.0.is_empty() {
return WasmHash::default();
}
sha256_wasm_hash(&self.0)
}
/// Generates a hash string of the current Wasm binary.
/// Returns a default hash string if the current Wasm binary is empty.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert_eq!(wasm.hash_string(), String::default());
///
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// let hash_string = wasm.hash_string();
/// assert_eq!(hash_string, "cd5d4935a48c0672cb06407bb443bc0087aff947c6b864bac886982c73b3027f");
/// ```
pub fn hash_string(&self) -> String {
if self.0.is_empty() {
return String::default();
}
sha256_wasm_hash_string(&self.0)
}
/// Verifies the given hash against the current Wasm binary.
/// Returns true if the given hash matches the current Wasm binary.
///
/// # Example
/// ```
/// use b3_utils::wasm::Wasm;
///
/// let mut wasm = Wasm::default();
/// assert!(wasm.verify_hash(&[0x00; 32]));
///
/// wasm.load(&vec![0x00, 0x61, 0x73, 0x6D]);
/// assert!(wasm.verify_hash(&[205, 93, 73, 53, 164, 140, 6, 114, 203, 6, 64, 123, 180, 67, 188, 0, 135, 175, 249, 71, 198, 184, 100, 186, 200, 134, 152, 44, 115, 179, 2, 127]));
/// ```
pub fn verify_hash(&self, hash: &WasmHash) -> bool {
self.hash() == *hash
}
}