CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3

Ethereum JavaScript API for interacting with the Ethereum blockchain with comprehensive TypeScript support, modular architecture, and plugin extensibility.

Pending
Overview
Eval results
Files

utilities.mddocs/

Utilities and Encoding

The utils module provides essential utility functions for data conversion, validation, hashing, and Ethereum-specific operations. These utilities handle common tasks like unit conversion, address validation, data formatting, and cryptographic operations.

Capabilities

Unit Conversion

Convert between different Ethereum units and number formats.

/**
 * Convert value to wei (smallest unit)
 * @param value - Value to convert
 * @param unit - Unit to convert from (ether, gwei, etc.)
 * @returns Value in wei as string
 */
toWei(value: NumberLike, unit?: EtherUnits): string;

/**
 * Convert value from wei to specified unit
 * @param value - Value in wei to convert
 * @param unit - Unit to convert to (ether, gwei, etc.)
 * @returns Converted value as string
 */
fromWei(value: NumberLike, unit?: EtherUnits): string;

/**
 * Convert value to hex representation
 * @param value - Value to convert (number, string, bytes, boolean)
 * @returns Hex string with 0x prefix
 */
toHex(value: Numbers | Bytes | Address | boolean): HexString;

/**
 * Convert hex to number
 * @param hex - Hex string to convert
 * @returns Number value
 */
hexToNumber(hex: HexString): number;

/**
 * Convert hex to bigint
 * @param hex - Hex string to convert
 * @returns BigInt value
 */
hexToBigInt(hex: HexString): bigint;

/**
 * Convert number to hex
 * @param value - Number to convert
 * @returns Hex string
 */
numberToHex(value: Numbers): HexString;

Usage Examples:

// Unit conversions
const weiValue = web3.utils.toWei('1', 'ether');
console.log(weiValue); // '1000000000000000000'

const etherValue = web3.utils.fromWei('1000000000000000000', 'ether');
console.log(etherValue); // '1'

const gweiValue = web3.utils.fromWei('20000000000', 'gwei');
console.log(gweiValue); // '20'

// Hex conversions
const hexValue = web3.utils.toHex(255);
console.log(hexValue); // '0xff'

const numberValue = web3.utils.hexToNumber('0xff');
console.log(numberValue); // 255

const bigIntValue = web3.utils.hexToBigInt('0x1fffffffffffff');
console.log(bigIntValue); // 9007199254740991n

Address Validation and Formatting

Validate and format Ethereum addresses with checksum support.

/**
 * Check if string is valid Ethereum address
 * @param address - Address string to validate
 * @returns Boolean indicating validity
 */
isAddress(address: string): boolean;

/**
 * Convert address to checksum format
 * @param address - Address to convert
 * @returns Checksummed address
 */
toChecksumAddress(address: string): string;

/**
 * Check if address has valid checksum
 * @param address - Address to check
 * @returns Boolean indicating valid checksum
 */
checkAddressChecksum(address: string): boolean;

/**
 * Check if addresses are equal (case-insensitive)
 * @param address1 - First address
 * @param address2 - Second address
 * @returns Boolean indicating equality
 */
isAddressEqual(address1: string, address2: string): boolean;

Usage Examples:

// Address validation
const address = '0x742c1382dfa68ea2a8b37ae3b72b476e1bb51aef';
console.log(web3.utils.isAddress(address)); // true

// Checksum conversion
const checksummed = web3.utils.toChecksumAddress(address);
console.log(checksummed); // '0x742C1382DfA68eA2a8b37ae3B72b476e1bB51AeF'

// Checksum validation
console.log(web3.utils.checkAddressChecksum(checksummed)); // true

// Address comparison
const addr1 = '0x742c1382dfa68ea2a8b37ae3b72b476e1bb51aef';
const addr2 = '0x742C1382DfA68eA2a8b37ae3B72b476e1bB51AeF';
console.log(web3.utils.isAddressEqual(addr1, addr2)); // true

Hashing Functions

Cryptographic hashing functions for data integrity and Ethereum operations.

/**
 * Calculate Keccak-256 hash
 * @param data - Data to hash
 * @returns Hash as hex string
 */
keccak256(data: Bytes): string;

/**
 * Calculate SHA-3 hash (alias for keccak256)
 * @param data - Data to hash
 * @returns Hash as hex string
 */
sha3(data: Bytes): string;

/**
 * Calculate SHA-3 hash handling null input
 * @param data - Data to hash
 * @returns Hash as hex string or null
 */
sha3Raw(data: Bytes): string | null;

/**
 * Calculate Solidity-compatible packed hash
 * @param values - Values to pack and hash
 * @returns Hash as hex string
 */
soliditySha3(...values: unknown[]): string | null;

/**
 * Calculate Solidity-compatible packed hash (raw)
 * @param values - Values to pack and hash
 * @returns Hash as hex string
 */
soliditySha3Raw(...values: unknown[]): string;

Usage Examples:

// Basic hashing
const hash = web3.utils.keccak256('Hello, Web3!');
console.log(hash); // '0x...'

const sha3Hash = web3.utils.sha3('Hello, Web3!');
console.log(sha3Hash); // Same as keccak256

// Solidity-style hashing
const packedHash = web3.utils.soliditySha3('uint256', 123, 'string', 'Hello');
console.log(packedHash);

// Packed hash with types
const typedHash = web3.utils.soliditySha3(
  { type: 'uint256', value: '123' },
  { type: 'string', value: 'Hello' }
);

Random Number Generation

Generate cryptographically secure random values.

/**
 * Generate random hex string
 * @param bytesSize - Number of bytes to generate
 * @returns Random hex string
 */
randomHex(bytesSize: number): HexString;

/**
 * Generate random bytes
 * @param bytesSize - Number of bytes to generate
 * @returns Random bytes as Uint8Array
 */
randomBytes(bytesSize: number): Uint8Array;

Usage Examples:

// Generate random hex
const randomHex = web3.utils.randomHex(32);
console.log(randomHex); // '0x...' (64 hex characters + 0x)

// Generate random bytes
const randomBytes = web3.utils.randomBytes(32);
console.log(randomBytes); // Uint8Array(32) [...]

String Manipulation

String utility functions for various data operations.

/**
 * Convert string to hex
 * @param str - String to convert
 * @returns Hex representation
 */
asciiToHex(str: string): HexString;

/**
 * Convert hex to string
 * @param hex - Hex string to convert
 * @returns ASCII string
 */
hexToAscii(hex: HexString): string;

/**
 * Convert string to hex (UTF-8)
 * @param str - String to convert
 * @returns Hex representation
 */
utf8ToHex(str: string): HexString;

/**
 * Convert hex to string (UTF-8)
 * @param hex - Hex string to convert
 * @returns UTF-8 string
 */
hexToUtf8(hex: HexString): string;

/**
 * Convert string to bytes
 * @param str - String to convert
 * @returns Bytes representation
 */
stringToBytes(str: string): Uint8Array;

/**
 * Convert bytes to string
 * @param bytes - Bytes to convert
 * @returns String representation
 */
bytesToString(bytes: Uint8Array): string;

Usage Examples:

// String to hex conversion
const hexFromAscii = web3.utils.asciiToHex('Hello');
console.log(hexFromAscii); // '0x48656c6c6f'

const asciiFromHex = web3.utils.hexToAscii('0x48656c6c6f');
console.log(asciiFromHex); // 'Hello'

// UTF-8 conversions
const utf8Hex = web3.utils.utf8ToHex('Hello, 世界!');
const utf8String = web3.utils.hexToUtf8(utf8Hex);
console.log(utf8String); // 'Hello, 世界!'

// Bytes conversions
const bytes = web3.utils.stringToBytes('Hello');
const string = web3.utils.bytesToString(bytes);
console.log(string); // 'Hello'

Data Validation

Validation functions for various data types and formats.

/**
 * Check if value is hex string
 * @param value - Value to check
 * @returns Boolean indicating if hex
 */
isHex(value: unknown): boolean;

/**
 * Check if value is hex string with specific byte length
 * @param value - Value to check
 * @param length - Expected byte length
 * @returns Boolean indicating valid hex with length
 */
isHexStrict(value: unknown, length?: number): boolean;

/**
 * Check if value is valid block number
 * @param value - Value to check
 * @returns Boolean indicating validity
 */
isBlockNumber(value: unknown): boolean;

/**
 * Check if value represents bloom filter
 * @param bloom - Value to check
 * @returns Boolean indicating validity
 */
isBloom(bloom: unknown): boolean;

/**
 * Check if value is valid topic
 * @param topic - Value to check
 * @returns Boolean indicating validity
 */
isTopic(topic: unknown): boolean;

Usage Examples:

// Hex validation
console.log(web3.utils.isHex('0x123')); // true
console.log(web3.utils.isHex('123')); // false

console.log(web3.utils.isHexStrict('0x123abc', 3)); // true (3 bytes)
console.log(web3.utils.isHexStrict('0x123', 3)); // false (not 3 bytes)

// Block number validation
console.log(web3.utils.isBlockNumber(123)); // true
console.log(web3.utils.isBlockNumber('0x7b')); // true
console.log(web3.utils.isBlockNumber('latest')); // true

Data Formatting

Functions for formatting and padding data.

/**
 * Pad string to specified length with character
 * @param str - String to pad
 * @param characterAmount - Target length
 * @param sign - Character to pad with
 * @returns Padded string
 */
padLeft(str: string, characterAmount: number, sign?: string): string;

/**
 * Pad string to right with specified character
 * @param str - String to pad
 * @param characterAmount - Target length
 * @param sign - Character to pad with
 * @returns Padded string
 */
padRight(str: string, characterAmount: number, sign?: string): string;

/**
 * Left pad hex string to specified bytes
 * @param hex - Hex string to pad
 * @param bytes - Target byte length
 * @returns Padded hex string
 */
leftPad(hex: string, bytes: number): string;

/**
 * Right pad hex string to specified bytes
 * @param hex - Hex string to pad
 * @param bytes - Target byte length
 * @returns Padded hex string
 */
rightPad(hex: string, bytes: number): string;

Types

type EtherUnits = 
  | 'noether'
  | 'wei'
  | 'kwei'
  | 'Kwei'
  | 'babbage'
  | 'femtoether'
  | 'mwei'
  | 'Mwei'
  | 'lovelace'
  | 'picoether'
  | 'gwei'
  | 'Gwei'
  | 'shannon'
  | 'nanoether'
  | 'nano'
  | 'szabo'
  | 'microether'
  | 'micro'
  | 'finney'
  | 'milliether'
  | 'milli'
  | 'ether'
  | 'kether'
  | 'grand'
  | 'mether'
  | 'gether'
  | 'tether';

type NumberLike = number | bigint | string;
type Numbers = number | bigint | string | HexString;
type Bytes = string | Uint8Array;
type HexString = string;
type Address = string;

Install with Tessl CLI

npx tessl i tessl/npm-web3

docs

account-management.md

core-web3.md

ens.md

ethereum-interaction.md

index.md

providers.md

smart-contracts.md

utilities.md

tile.json