inkpad_support/convert/
mod.rs

1use crate::types::StorageKey;
2
3/// Convert bytes to u32
4pub fn to_u32(b: &[u8]) -> Option<u32> {
5    if b.len() != 4 {
6        None
7    } else {
8        let mut r = [0; 4];
9        r.copy_from_slice(b);
10        Some(u32::from_ne_bytes(r))
11    }
12}
13
14/// Convert bytes tot storage key
15pub fn to_storage_key(b: &[u8]) -> Option<StorageKey> {
16    if b.len() != 32 {
17        None
18    } else {
19        let mut r = [0; 32];
20        r.copy_from_slice(b);
21        Some(r)
22    }
23}
24
25/// Trim 0x prefix
26pub fn step_hex(h: &str) -> Option<Vec<u8>> {
27    if let Some(stripped) = h.strip_prefix("0x") {
28        hex::decode(stripped)
29    } else {
30        hex::decode(&h)
31    }
32    .ok()
33}
34
35/// Parse code hash from string
36pub fn parse_code_hash(h: &str) -> Option<[u8; 32]> {
37    let hash = step_hex(h)?;
38    let mut res = [0; 32];
39    if hash.len() != 32 {
40        None
41    } else {
42        res.copy_from_slice(&hash);
43        Some(res)
44    }
45}