core-blockchain-helper
v1.0.2
Published
Helpful blockchain utilities: addresses, hex, units, chain IDs, encoding
Maintainers
Readme
core-blockchain-helper
Blockchain utilities for EVM-style chains: addresses (EIP-55), hex/bytes, wei/ether units, chain IDs, and encoding helpers.
Install
npm install core-blockchain-helperUsage
const {
getAddress,
isAddress,
toChecksumAddress,
shortenAddress,
formatEther,
parseEther,
weiToGwei,
getChainName,
isHex,
hexToBytes,
toBytes32,
concatHex,
} = require('core-blockchain-helper');
// Address (EIP-55 checksum)
getAddress('0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359');
// => '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
isAddress('0x1234...'); // false
shortenAddress(addr); // '0xfB69...d359'
// Units
formatEther(1000000000000000000n); // '1'
parseEther('1.5'); // 1500000000000000000n
weiToGwei(1000000000n); // 1
// Chains
getChainName(1); // 'Ethereum Mainnet'
getChainName(137); // 'Polygon'
// Hex
isHex('0xdead'); // true
hexToBytes('0x0102'); // Uint8Array [1, 2]
toBytes32(255); // '0x...' 32-byte hex
concatHex('0x01', '0x02'); // '0x0102'Subpath imports
const { getAddress, toChecksumAddress } = require('core-blockchain-helper/address');
const { formatEther, parseEther } = require('core-blockchain-helper/units');
const { getChainName, CHAINS } = require('core-blockchain-helper/chains');
const { isHex, hexToBytes, bytesToHex } = require('core-blockchain-helper/hex');
const { toBytes32, leftPadHex } = require('core-blockchain-helper/encoding');API
Address (lib/address.js)
| Method | Description |
|--------|-------------|
| isAddress(value) | Valid 20-byte hex address |
| toChecksumAddress(address) | EIP-55 mixed-case checksum |
| verifyChecksum(address) | True if address matches its checksum |
| getAddress(address) | Checksummed address or throws |
| isZeroAddress(address) | All zeros |
| shortenAddress(address, chars?) | 0x1234...5678 |
Hex (lib/hex.js)
| Method | Description |
|--------|-------------|
| stripHex(value) | Remove 0x prefix |
| ensureHex(value) | Ensure 0x prefix |
| isHex(value, options?) | Valid hex string |
| hexToBytes(hex) | Hex → Uint8Array |
| bytesToHex(bytes) | Bytes → 0x hex |
| hexToBigInt(hex) | Hex → BigInt |
| bigIntToHex(value) | BigInt → hex |
| hexToUtf8(hex) | Hex → UTF-8 string |
| utf8ToHex(str) | UTF-8 string → hex |
| isTxHash(value) | Valid tx hash (0x + 64 hex) |
| shortenTxHash(txHash, chars?) | 0x1234...abcd |
Units (lib/units.js)
| Method | Description |
|--------|-------------|
| formatEther(wei) | Wei → decimal string (18 decimals) |
| formatUnits(wei, decimals) | Wei → decimal string |
| parseEther(ether) | Ether string → wei (BigInt) |
| parseUnits(value, decimals) | Decimal string → wei (BigInt) |
| weiToEther / etherToWei | Number conversion |
| weiToGwei / gweiToWei | Gwei conversion |
Chains (lib/chains.js)
| Method | Description |
|--------|-------------|
| CHAINS | Object of chainId → name |
| getChainName(chainId) | Human-readable network name |
| isMainnet(chainId) | chainId === 1 |
| isTestnet(chainId) | Known testnet |
Encoding (lib/encoding.js)
| Method | Description |
|--------|-------------|
| leftPadHex(hex, length?) | Pad to 32 bytes (default) |
| rightPadHex(hex, length?) | Right-pad to length |
| toBytes32(value) | Number/BigInt/hex → 32-byte hex |
| fromBytes32(hex) | 32-byte hex → BigInt |
| concatHex(...hexStrings) | Concatenate hex strings |
Dependencies
js-sha3– Keccak-256 for EIP-55 checksum
License
MIT
