@stellarshift/abi-tools
v1.0.0
Published
Compute Ethereum function selectors and event topic0 hashes from canonical ABI signatures.
Maintainers
Readme
@stellarshift/abi-tools
Compute function selectors and event topic0 hashes from canonical Solidity ABI type strings.
Handy for calldata decoders, integration tests, and interview exercises that ask candidates to match on-chain logs without importing full ethers ABI codecs.
Install
npm install @stellarshift/abi-toolsUsage
const {
getFunctionSelector,
getEventTopic0,
buildFunctionSignature,
selectorFromSignature,
} = require('@stellarshift/abi-tools');
getFunctionSelector('transfer', ['address', 'uint256']);
// → '0xa9059cbb'
buildFunctionSignature('approve', ['address', 'uint256']);
// → 'approve(address,uint256)'
selectorFromSignature('balanceOf(address)');
// → '0x70a08231'
getEventTopic0('Transfer', ['address', 'address', 'uint256']);
// → '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'API
| Function | Returns |
|----------|---------|
| getFunctionSelector(name, types[]) | 4-byte 0x selector |
| getEventTopic0(name, types[]) | 32-byte topic hash |
| buildFunctionSignature(name, types[]) | Canonical string |
| selectorFromSignature(sig) | Selector from full signature |
| normalizeType(type) | uint → uint256, etc. |
| isSelector(hex) | Validates 0x + 8 hex |
Implementation
Uses js-sha3 for Keccak-256 (Ethereum variant).
Does not ABI-encode arguments — selectors / topics only.
License
MIT © StellarShift Labs
