CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3-utils

Collection of utility functions used in web3.js for Ethereum dApp development

Pending
Overview
Eval results
Files

hashing.mddocs/

Cryptographic Hashing

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.

Capabilities

Keccak-256 Hashing

Basic Keccak-256

/**
 * 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;

SHA3 Functions

/**
 * 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;

Solidity-Style Hashing

Packed Encoding

/**
 * 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 SHA3

/**
 * 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;

Storage Utilities

/**
 * 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;

Usage Examples

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);

Types

// 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'
}

Common Use Cases

Smart Contract Event Signatures

// 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

Merkle Tree Construction

// 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 }
));

Storage Slot Calculation

// 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

docs

conversion.md

data-manipulation.md

events.md

hashing.md

index.md

json-rpc.md

promises.md

providers.md

random-validation.md

tile.json