hdp_primitives/solidity_types/
module.rs

1use alloy::{
2    dyn_abi::DynSolValue,
3    primitives::{keccak256, B256},
4};
5
6use crate::{task::module::Module, utils::felt_to_bytes32};
7
8impl Module {
9    pub fn encode_task(&self) -> Vec<u8> {
10        let class_hash: DynSolValue =
11            DynSolValue::FixedBytes(felt_to_bytes32(self.program_hash), 32);
12        let module_inputs: DynSolValue = DynSolValue::FixedArray(
13            self.inputs
14                .iter()
15                .map(|input| DynSolValue::FixedBytes(felt_to_bytes32(*input), 32))
16                .collect(),
17        );
18        let input_length: DynSolValue = self.inputs.len().into();
19        // offset of class hash
20        let offset: DynSolValue = (64).into();
21        let module_tuple_value =
22            DynSolValue::Tuple(vec![class_hash, offset, input_length, module_inputs]);
23        module_tuple_value.abi_encode()
24    }
25
26    pub fn commit(&self) -> B256 {
27        let encoded_task = self.encode_task();
28        keccak256(encoded_task)
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use starknet_crypto::FieldElement;
35    use std::str::FromStr;
36
37    use super::*;
38
39    #[test]
40    pub fn module_encode() {
41        let module = Module {
42            program_hash: FieldElement::from_hex_be(
43                "0x00af1333b8346c1ac941efe380f3122a71c1f7cbad19301543712e74f765bfca",
44            )
45            .unwrap(),
46            inputs: vec![
47                FieldElement::from_hex_be("0x4F21E5").unwrap(),
48                FieldElement::from_hex_be("0x4F21E8").unwrap(),
49                FieldElement::from_hex_be("0x13cb6ae34a13a0977f4d7101ebc24b87bb23f0d5").unwrap(),
50            ],
51            local_class_path: None,
52        };
53
54        let expected_commit = module.commit();
55        assert_eq!(
56            expected_commit,
57            B256::from_str("0x879869b6d237b92bfdd3f3f7b76baaa9ebb2a3ad5e8478d12cca258d3def05af")
58                .unwrap()
59        );
60    }
61}