Solidity coder for non-standard (tight) packing.
npx @tessl/cli install tessl/npm-ethersproject--solidity@5.8.0The @ethersproject/solidity package provides utilities for Solidity-specific packed (non-standard) encoding operations. It enables JavaScript/TypeScript applications to generate tightly packed binary data that matches Solidity's packed encoding behavior, useful for creating merkle proofs, generating contract-compatible signatures, and implementing off-chain computation that matches on-chain behavior.
npm install @ethersproject/solidityimport { pack, keccak256, sha256 } from "@ethersproject/solidity";For CommonJS:
const { pack, keccak256, sha256 } = require("@ethersproject/solidity");import { pack, keccak256, sha256 } from "@ethersproject/solidity";
// Pack data according to Solidity rules
const packedData = pack(
["address", "uint256", "bool"],
["0x8ba1f109551bd432803012645ac136ddd64dba72", "42", true]
);
// Result: "0x8ba1f109551bd432803012645ac136ddd64dba72000000000000000000000000000000000000000000000000000000000000002a01"
// Hash packed data with keccak256
const hash = keccak256(
["address", "uint256"],
["0x8ba1f109551bd432803012645ac136ddd64dba72", "123"]
);
// Result: "0x..."
// Hash packed data with sha256
const sha256Hash = sha256(
["string", "uint256"],
["hello", "42"]
);
// Result: "0x..."Packs values according to Solidity's packed encoding rules, creating tightly packed binary data without padding.
/**
* Packs values according to Solidity packed encoding rules
* @param types - Array of Solidity type strings
* @param values - Array of corresponding values to encode
* @returns Hex string of packed binary data
*/
function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;Supported Types:
address - Ethereum addresses (20 bytes when packed)string - UTF-8 encoded strings (variable length)bytes - Raw bytes data (variable length)bool - Boolean values (1 byte: 0x00 or 0x01)uint<N> - Unsigned integers where N is 8-256 and multiple of 8 (N/8 bytes)int<N> - Signed integers where N is 8-256 and multiple of 8 (N/8 bytes)bytes<N> - Fixed-size byte arrays where N is 1-32 (N bytes)uint256[], address[5] (32 bytes per element when in arrays)Usage Examples:
import { pack } from "@ethersproject/solidity";
// Simple address packing
const addressPacked = pack(["address"], ["0x1234567890123456789012345678901234567890"]);
// Result: "0x1234567890123456789012345678901234567890"
// Mixed types
const mixedPacked = pack(
["uint256", "address", "bool"],
["12345", "0x1234567890123456789012345678901234567890", false]
);
// Fixed-size bytes
const bytesPacked = pack(
["bytes32"],
["0x1234567890123456789012345678901234567890123456789012345678901234"]
);
// Array types (elements are padded to 32 bytes in arrays)
const arrayPacked = pack(
["uint256[]"],
[[1, 2, 3]]
);Computes the keccak256 hash of packed encoded data.
/**
* Computes keccak256 hash of packed encoded data
* @param types - Array of Solidity type strings
* @param values - Array of corresponding values to encode
* @returns Hex string of keccak256 hash
*/
function keccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;Usage Examples:
import { keccak256 } from "@ethersproject/solidity";
// Hash an address and number
const hash1 = keccak256(
["address", "uint256"],
["0x8ba1f109551bd432803012645ac136ddd64dba72", "42"]
);
// Hash multiple values for signature verification
const messageHash = keccak256(
["string", "address", "uint256"],
["Transfer", "0x1234567890123456789012345678901234567890", "1000"]
);
// Common pattern for merkle tree leaves
const leafHash = keccak256(
["address", "uint256"],
[userAddress, amount]
);Computes the sha256 hash of packed encoded data.
/**
* Computes sha256 hash of packed encoded data
* @param types - Array of Solidity type strings
* @param values - Array of corresponding values to encode
* @returns Hex string of sha256 hash
*/
function sha256(types: ReadonlyArray<string>, values: ReadonlyArray<any>): string;Usage Examples:
import { sha256 } from "@ethersproject/solidity";
// Hash with sha256 instead of keccak256
const hash = sha256(
["string", "uint256"],
["hello world", "12345"]
);
// Use in Bitcoin-compatible applications
const btcCompatibleHash = sha256(
["bytes", "uint32"],
["0x48656c6c6f", 123456]
);// Function parameter types
type SolidityType =
| "address"
| "string"
| "bytes"
| "bool"
| `uint${number}`
| `int${number}`
| `bytes${number}`
| `${string}[]`
| `${string}[${number}]`;
// Input arrays are readonly to prevent mutation
type ReadonlyArray<T> = readonly T[];The package throws errors for:
// These will throw errors:
pack(["uint0"], [1]); // Invalid type: uint0
pack(["bytes33"], ["0x00"]); // Invalid type: bytes33
pack(["uint256", "address"], [42]); // Argument count mismatch
pack(["bytes32"], ["0x123"]); // Invalid value: too short for bytes32