Collection of utility functions used in web3.js for Ethereum dApp development
—
Keccak-256 hashing functions and Solidity-style packed encoding for data integrity and smart contract interaction. These functions provide cryptographic hashing capabilities essential for Ethereum development.
/**
* Wrapper for keccak256 hashing
* @param data - Data to hash (Bytes, Numbers, string, or ReadonlyArray<number>)
* @returns Keccak-256 hash as hex string
*/
function keccak256Wrapper(data: Bytes | Numbers | string | ReadonlyArray<number>): string;
/**
* Alias for keccak256Wrapper
*/
const keccak256 = keccak256Wrapper;/**
* Computes Keccak-256, returns undefined for empty data
* @param data - Bytes data to hash
* @returns Keccak-256 hash or undefined for empty input
*/
function sha3(data: Bytes): string | undefined;
/**
* Like sha3 but returns hash even for empty data
* @param data - Bytes data to hash
* @returns Keccak-256 hash string
*/
function sha3Raw(data: Bytes): string;/**
* Processes arguments for Solidity-style packing
* @param arg - Input argument to process
* @returns Processed hex string
*/
function processSolidityEncodePackedArgs(arg: Sha3Input): string;
/**
* Encodes packed arguments to hexstring
* @param values - Arguments to encode and pack
* @returns Packed hex string
*/
function encodePacked(...values: Sha3Input[]): string;/**
* Solidity-style hash with tight packing
* @param values - Values to hash with Solidity-style packing
* @returns Keccak-256 hash or undefined for empty input
*/
function soliditySha3(...values: Sha3Input[]): string | undefined;
/**
* Like soliditySha3 but returns hash even for empty data
* @param values - Typed objects to hash
* @returns Keccak-256 hash string
*/
function soliditySha3Raw(...values: TypedObject[] | TypedObjectAbbreviated[]): string;/**
* Gets storage slot number for long strings
* @param mainSlotNumber - Main slot number (number or string)
* @returns Storage slot string or undefined
*/
function getStorageSlotNumForLongString(mainSlotNumber: number | string): string | undefined;import {
keccak256, sha3, soliditySha3, encodePacked
} from "web3-utils";
// Basic Keccak-256 hashing
const hash1 = keccak256("hello world");
// "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
const hash2 = keccak256(new Uint8Array([1, 2, 3, 4]));
// Hash of byte array
// SHA3 with empty handling
const hash3 = sha3("data"); // Returns hash
const hash4 = sha3(""); // Returns undefined
const hash5 = sha3Raw(""); // Returns hash even for empty
// Solidity-style packed hashing
const packedHash = soliditySha3(
{ type: "uint256", value: "1234567890" },
{ type: "string", value: "Hello" },
{ type: "address", value: "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD" }
);
// Encoding packed arguments
const packed = encodePacked(
{ type: "uint256", value: "123" },
{ type: "string", value: "test" }
);
// Returns hex string of packed data
// Hash the packed data
const finalHash = keccak256(packed);// Input types for Solidity-style hashing
type Sha3Input =
| string
| number
| bigint
| boolean
| TypedObject
| TypedObjectAbbreviated;
interface TypedObject {
type: string;
value: string | number | boolean | bigint;
}
interface TypedObjectAbbreviated {
t: string; // Abbreviated form of 'type'
v: string | number | boolean | bigint; // Abbreviated form of 'value'
}// Calculate event signature hash
const eventSignature = "Transfer(address,address,uint256)";
const eventHash = keccak256(eventSignature);
// Used for filtering events
// Function selector (first 4 bytes)
const functionSignature = "transfer(address,uint256)";
const selector = keccak256(functionSignature).slice(0, 10); // "0x" + 8 chars// Hash leaf nodes
const leaf1 = keccak256("data1");
const leaf2 = keccak256("data2");
// Hash internal nodes
const parent = keccak256(encodePacked(
{ type: "bytes32", value: leaf1 },
{ type: "bytes32", value: leaf2 }
));// Calculate storage slot for mapping
const key = "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD";
const slot = 0;
const storageSlot = keccak256(encodePacked(
{ type: "address", value: key },
{ type: "uint256", value: slot }
));Install with Tessl CLI
npx tessl i tessl/npm-web3-utils