Skip to main content

Try it Live

Run Bytecode examples in the interactive playground
New to bytecode? Start with Fundamentals for guided examples and concepts.

Type Definition

Branded Uint8Array with utils for analyzing, validating, and iterating over EVM bytecode.
import type { brand } from 'tevm/brand';

export type BrandedBytecode = Uint8Array & { readonly [brand]: "Bytecode" };

Native Uint8Array Methods

JavaScript builtins available on all Uint8Array instances:
  • setFromBase64() - Populate from base64 string
  • setFromHex() - Populate from hex string
  • toBase64() - Encode to base64 string
  • toHex() - Encode to hex string
MDN Reference
toHex() and setFromHex() are available as of 2025 (Node.js 22+, Chrome 131+). Use Tevm’s Hex.toHex() for broader compatibility.

Quick Reference

    Effect Schema

    import { BytecodeSchema } from '@tevm/voltaire/Bytecode/effect'
    
    const code = BytecodeSchema.fromHex('0x6001')
    code.toHex() // '0x6001'
    

    API Methods

    Constructors

    Iteration

    Analysis

    Validation

    Metadata

    Formatting

    Conversions

    Utilities

    Reference

    Types

    import type { brand } from 'tevm/brand';
    
    export type BrandedBytecode = Uint8Array & {
      readonly [brand]: "Bytecode";
    };
    
    Main branded type. Runtime is Uint8Array, TypeScript enforces type safety with symbol branding.

    Usage Patterns

    Analyzing Contract Bytecode

    const bytecode = await provider.getCode("0x...");
    const code = Bytecode(bytecode);
    
    const analysis = code.analyze();
    console.log(`Valid: ${analysis.valid}`);
    console.log(`Jump destinations: ${analysis.jumpDestinations.size}`);
    console.log(`Instructions: ${analysis.instructions.length}`);
    
    const isValidTarget = code.isValidJumpDest(100);
    

    Comparing Bytecode

    const deployed = Bytecode(await provider.getCode(address));
    const expected = Bytecode(compiledBytecode);
    
    if (deployed.equals(expected)) {
      console.log("Contract verified");
    } else {
      console.log(`Hash deployed: ${deployed.hash()}`);
      console.log(`Hash expected: ${expected.hash()}`);
    }
    

    Extracting Runtime Code

    const deploymentCode = Bytecode("0x608060...");
    const runtimeCode = deploymentCode.extractRuntime();
    const cleanCode = runtimeCode.stripMetadata();
    

    Tree-Shaking

    Import only what you need for optimal bundle size:
    // Import specific functions (tree-shakeable)
    import { fromHex, analyze, formatInstructions } from 'tevm/BrandedBytecode';
    
    const code = fromHex("0x6001");
    const analysis = analyze(code);
    const disassembly = formatInstructions(code);
    
    // Only these 3 functions included in bundle
    

    Core Documentation

    Advanced Features

    • Opcode - EVM instruction definitions and gas costs
    • Keccak256 - Keccak256 hashing for bytecode verification
    • Hex - Hex string encoding and decoding

    Specification