Hash UTF-8 encoded string with Keccak-256.Signature:
Copy
Ask AI
function hashString(str: string): Keccak256Hash
Parameters:
str (string) - String to hash (UTF-8 encoded before hashing)
Returns:Keccak256Hash (32-byte hash)Throws: Never throws for valid input
Basic Usage
Function Signatures
Event Signatures
Unicode Handling
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';const hash = Keccak256.hashString('hello');// Equivalent to: Keccak256.hash(new TextEncoder().encode('hello'))console.log(hash.length); // 32
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// Hash function signature (without taking selector)const signature = 'transfer(address,uint256)';const fullHash = Keccak256.hashString(signature);// First 4 bytes would be function selectorconst selector = fullHash.slice(0, 4);
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// Hash event signature for topic0const eventSig = 'Transfer(address,address,uint256)';const topic0 = Keccak256.hashString(eventSig);// Full 32-byte hash used in logsconsole.log(topic0); // Topic for filtering Transfer events
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// UTF-8 encoding handles Unicode correctlyconst emoji = Keccak256.hashString('Hello 👋 World');const chinese = Keccak256.hashString('你好世界');const arabic = Keccak256.hashString('مرحبا بالعالم');// All produce deterministic 32-byte hashes
Technical Notes:
Uses TextEncoder for UTF-8 conversion
Handles all Unicode code points correctly
No normalization applied - different encodings produce different hashes
Hash hex-encoded string with Keccak-256.Signature:
Copy
Ask AI
function hashHex(hex: string): Keccak256Hash
Parameters:
hex (string) - Hex string to hash (with or without “0x” prefix)
Returns:Keccak256Hash (32-byte hash)Throws: Never throws for valid hex string
Basic Usage
Hashing Addresses
Hashing Calldata
Public Key Hashing
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// With 0x prefixconst hash1 = Keccak256.hashHex('0x1234abcd');// Without 0x prefixconst hash2 = Keccak256.hashHex('1234abcd');// Both produce same resultconsole.log(hash1.length); // 32
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// Hash address for various purposesconst address = '0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e';const addressHash = Keccak256.hashHex(address);// Used in Merkle proofs, state trees, etc.
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// Hash transaction calldataconst calldata = '0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b844bc9e7595f51e3e0000000000000000000000000000000000000000000000000de0b6b3a7640000';const calldataHash = Keccak256.hashHex(calldata);// Reference for calldata verification
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// Hash uncompressed public key (65 bytes, starts with 04)const publicKey = '0x04' + 'c7a8e33b7b8e67e4d7c8e33b7b8e67e4d7c8e33b7b8e67e4d7c8e33b7b8e67e4' + 'd7c8e33b7b8e67e4d7c8e33b7b8e67e4d7c8e33b7b8e67e4d7c8e33b7b8e67e4';const pubKeyHash = Keccak256.hashHex(publicKey);// Last 20 bytes = Ethereum addressconst address = pubKeyHash.slice(12);
Technical Notes:
Hex string decoded to bytes before hashing
Accepts both uppercase and lowercase hex
“0x” prefix optional and stripped automatically
Odd-length hex padded with leading zero: "abc" → "0abc"
function hashMultiple(chunks: readonly Uint8Array[]): Keccak256Hash
Parameters:
chunks (readonly Uint8Array[]) - Array of byte chunks to hash
Returns:Keccak256Hash (32-byte hash)Throws: Never throws for valid input
Basic Usage
Efficient Concatenation
Streaming Data
Merkle Tree Nodes
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';import { Hex } from '@tevm/voltaire/Hex';const chunk1 = Hex.toBytes('0x010203');const chunk2 = Hex.toBytes('0x040506');const chunk3 = Hex.toBytes('0x070809');const hash = Keccak256.hashMultiple([chunk1, chunk2, chunk3]);// Same as: Keccak256.hash(Hex.toBytes('0x010203040506070809'))
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// More efficient than manual concatenationconst parts = [header, body, footer];// Instead of:// const combined = new Uint8Array(totalLength);// ... copy parts into combined ...// const hash = Keccak256.hash(combined);// Use hashMultiple:const hash = Keccak256.hashMultiple(parts);// Avoids intermediate allocation
Copy
Ask AI
import { Keccak256 } from '@tevm/voltaire/Keccak256';// Hash data arriving in chunksconst chunks: Uint8Array[] = [];// Accumulate chunkschunks.push(chunk1);chunks.push(chunk2);chunks.push(chunk3);// Hash all at onceconst hash = Keccak256.hashMultiple(chunks);