Skip to main content

neo_devpack_solidity/
interop.rs

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