alloy_encode_packed/
lib.rs

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
use alloy::primitives::{Address, U256};

pub struct TakeLastXBytes(pub usize);

pub enum SolidityDataType<'a> {
    String(&'a str),
    Address(Address),
    Bytes(&'a [u8]),
    Bool(bool),
    Number(U256),
    NumberWithShift(U256, TakeLastXBytes),
}

pub mod abi {
    use super::SolidityDataType;

    /// Pack a single `SolidityDataType` into bytes
    fn pack<'a>(data_type: &'a SolidityDataType) -> Vec<u8> {
        let mut res = Vec::new();
        match data_type {
            SolidityDataType::String(s) => {
                res.extend(s.as_bytes());
            }
            SolidityDataType::Address(a) => {
                res.extend(a.0);
            }
            SolidityDataType::Number(n) => {
                res.extend(n.to_be_bytes::<32>());
            }
            SolidityDataType::Bytes(b) => {
                res.extend(*b);
            }
            SolidityDataType::Bool(b) => {
                if *b {
                    res.push(1);
                } else {
                    res.push(0);
                }
            }
            SolidityDataType::NumberWithShift(n, to_take) => {
                let local_res = n.to_be_bytes::<32>().to_vec();

                let to_skip = local_res.len() - (to_take.0 / 8);
                let local_res = local_res.into_iter().skip(to_skip).collect::<Vec<u8>>();
                res.extend(local_res);
            }
        };
        return res;
    }

    pub fn encode_packed(items: &[SolidityDataType]) -> (Vec<u8>, String) {
        let res = items.iter().fold(Vec::new(), |mut acc, i| {
            let pack = pack(i);
            acc.push(pack);
            acc
        });
        let res = res.join(&[][..]);
        let hexed = hex::encode(&res);
        (res, hexed)
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryInto;

    use super::*;

    #[test]
    fn test_normal_use_case() {
        let address = hex::decode("d8b934580fcE35a11B58C6D73aDeE468a2833fa8").unwrap();
        let address: [u8; 20] = address.try_into().unwrap();
        let input = vec![
            SolidityDataType::NumberWithShift(U256::from(3838), TakeLastXBytes(24)),
            SolidityDataType::Number(U256::from(4001)),
            SolidityDataType::String("this-is-a-sample-string"),
            SolidityDataType::Address(Address::from(address)),
            SolidityDataType::Number(U256::from(1)),
        ];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0x000efe0000000000000000000000000000000000000000000000000000000000000fa1746869732d69732d612d73616d706c652d737472696e67d8b934580fce35a11b58c6d73adee468a2833fa80000000000000000000000000000000000000000000000000000000000000001";
        assert_eq!(hash, expected);
    }

    #[test]
    fn test_uint24() {
        let input = vec![SolidityDataType::NumberWithShift(
            U256::from(4001),
            TakeLastXBytes(24),
        )];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0x000fa1";
        assert_eq!(hash, expected);
    }

    #[test]
    fn test_uint256() {
        let input = vec![SolidityDataType::Number(U256::from(3838110))];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0x00000000000000000000000000000000000000000000000000000000003a909e";
        assert_eq!(hash, expected);
    }

    #[test]
    fn test_string() {
        let input = vec![SolidityDataType::String("this-is-a-sample-string")];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0x746869732d69732d612d73616d706c652d737472696e67";
        assert_eq!(hash, expected);
    }

    #[test]
    fn test_address() {
        let address = hex::decode("d8b934580fcE35a11B58C6D73aDeE468a2833fa8").unwrap();
        let address: [u8; 20] = address.try_into().unwrap();
        let input = vec![SolidityDataType::Address(Address::from(address))];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0xd8b934580fce35a11b58c6d73adee468a2833fa8";
        assert_eq!(hash, expected);
    }

    #[test]
    fn test_bool() {
        let input = vec![SolidityDataType::Bool(false)];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0x00";
        assert_eq!(hash, expected);
    }
    #[test]
    fn test_normal_bytes() {
        let bytes = "abababababababababababababababababababababababababababababab";
        let bytes = hex::decode(bytes).unwrap();
        let bytes: [u8; 30] = bytes.try_into().unwrap();

        let input = vec![SolidityDataType::Bytes(&bytes)];
        let (_bytes, hash) = abi::encode_packed(&input);
        let hash = format!("0x{:}", hash);
        let expected = "0xabababababababababababababababababababababababababababababab";
        assert_eq!(hash, expected);
    }
}