The most critical fixed-size type in Ethereum - 256-bit values for storage slots, hashes, and merkle nodes
Try it Live
Run Bytes examples in the interactive playground
Most Important Bytes Type - Bytes32 is THE fundamental 256-bit type in Ethereum. Used everywhere: storage slots, hashes, merkle trees, and generic 32-byte values.
Bytes32 is a branded Uint8Array specialization of the generic Bytes<N> type, representing exactly 32 bytes (256 bits). It’s the most common fixed-size type in Ethereum and provides strict type safety for 256-bit values through Symbol-based branding.See the Bytes overview for generic Bytes<N> documentation and other size specializations.
import * as Bytes32 from 'tevm/primitives/Bytes/Bytes32';// Storage slotconst slot = Bytes32(0);// Keccak256 hash output (32 bytes, same size as Bytes32)const hash = Keccak256.hash(data);const hashAsBytes = Bytes32(hash);// Convert to/from bigintconst value = Bytes32.toBigint(slot);// Extract address (last 20 bytes)const addr = Bytes32.toAddress(hashAsBytes);
Bytes32 is a specialization of Bytes<32> using Symbol-based branding for compile-time type safety with zero runtime overhead:
import type { brand } from './brand.js';type Bytes32Type = Uint8Array & { readonly [brand]: "Bytes32"; readonly size: 32;};
The brand symbol is a unique TypeScript symbol that creates nominal typing - structurally identical types are incompatible if they have different brands:
const bytes32 = Bytes32(42);const bytes16 = Bytes16('0x' + '12'.repeat(16));// Type error: cannot assign Bytes16 to Bytes32const wrong: Bytes32.Bytes32Type = bytes16;
This branding system provides:
Compile-time safety - Cannot mix different sized Bytes types
Zero runtime cost - Brand only exists in TypeScript, erased after compilation
Hardcoded length - Type includes { readonly size: 32 } for static length checking
// From hexconst b1 = Bytes32('0x' + '12'.repeat(32));// From bytesconst b2 = Bytes32(Bytes32());// From number (padded)const b3 = Bytes32(42);// From bigint (padded)const b4 = Bytes32(123456789012345678901234567890n);
const bytes = Bytes32(42);const value = Bytes32.toBigint(bytes);console.log(value); // 42n// Works with large valuesconst large = Bytes32('0x' + 'ff'.repeat(32));const bigValue = Bytes32.toBigint(large);