neo_solidity/codegen/
interop.rs

1// Compute Neo N3 interop ID (first 4 bytes of SHA-256 of method name, little-endian order)
2/// Compute the 4-byte interop ID for a Neo syscall name
3/// This is used to identify syscalls in NeoVM bytecode
4pub fn interop_id_bytes(name: &str) -> [u8; 4] {
5    let mut hasher = Sha256::new();
6    hasher.update(name.as_bytes());
7    let digest = hasher.finalize();
8    [digest[0], digest[1], digest[2], digest[3]]
9}
10
11#[cfg(test)]
12mod interop_tests {
13    use super::interop_id_bytes;
14
15    #[test]
16    fn test_interop_ids() {
17        assert_eq!(
18            interop_id_bytes("System.Contract.Call"),
19            [0x62, 0x7D, 0x5B, 0x52]
20        );
21        assert_eq!(
22            interop_id_bytes("System.Storage.Put"),
23            [0xE6, 0x3F, 0x18, 0x84]
24        );
25        assert_eq!(
26            interop_id_bytes("System.Storage.Get"),
27            [0x92, 0x5D, 0xE8, 0x31]
28        );
29        assert_eq!(
30            interop_id_bytes("Neo.Crypto.Keccak256"),
31            [0xDC, 0xB1, 0x21, 0xE0]
32        );
33    }
34}