Core utility functions for encoding, verification, and data manipulation in the Snapshot X SDK
The utils module provides essential utility functions for working with Snapshot X governance contracts, including encoding utilities, byte manipulation, merkle tree generation, and blockchain-specific helpers.
import { encoding } from '@snapshot-labs/sx';// Get the slot key for balances[address]const slotKey = encoding.getSlotKey( '0x1234...', // mapping key (e.g., user address) '0x00' // slot index of the mapping);
The slot index can be found from Solidity compiler artifacts and indicates where the mapping is defined in the contract storage layout.
import { encoding } from '@snapshot-labs/sx';// Pad left with zerosconst paddedLeft = encoding.hexPadLeft('0x123');// Result: '0x0000000000000000000000000000000000000000000000000000000000000123'// Pad right with zerosconst paddedRight = encoding.hexPadRight('0x123');// Result: '0x0123000000000000000000000000000000000000000000000000000000000000'
import { encoding } from '@snapshot-labs/sx';const signature = '0x...';const { r, s, v } = encoding.getRSVFromSig(signature);// r and s are SplitUint256 instances// v is a hex string ('0x1b' or '0x1c')
Handle 256-bit integers by splitting them into low and high 128-bit components:
import { splitUint256 } from '@snapshot-labs/sx';// From a BigIntconst split = splitUint256.SplitUint256.fromUint(123456789n);console.log(split.low, split.high);// From a hex stringconst split2 = splitUint256.SplitUint256.fromHex('0x123abc');// Convert back to BigIntconst original = split.toUint();// Convert to hexconst hex = split.toHex();
The SplitUint256 class is particularly useful for Starknet interactions, where 256-bit values are represented as two 128-bit felts.