Skip to main content

Try it Live

Run Blob examples in the interactive playground
import { Blob } from 'tevm';

const commitment = new Uint8Array(48); // From toCommitment()
const versionedHash = Blob.toVersionedHash(commitment);

console.log(versionedHash.length); // 32
console.log(versionedHash[0]); // 0x01 (KZG version)

Versioned Hash Format

┌─────────────┬──────────────────────────────────────┐
│  Version    │  SHA256(commitment)[1:32]            │
├─────────────┼──────────────────────────────────────┤
│  0x01       │  31 bytes from hash                  │
│  (1 byte)   │                                      │
└─────────────┴──────────────────────────────────────┘
  Byte 0        Bytes 1-31

Total: 32 bytes

Manual Computation

import { Blob } from 'tevm';
import { sha256 } from '@noble/hashes/sha256';

const blob = Blob.fromData(data);
const commitment = blob.toCommitment();

// Compute SHA256 of commitment
const hash = sha256(commitment);

// Create versioned hash: 0x01 + hash[1:32]
const versionedHash = Bytes32();
versionedHash[0] = 0x01; // KZG version
versionedHash.set(hash.slice(1), 1);

// Equivalent to:
const auto = blob.toVersionedHash();
console.log(Blob.equals(versionedHash, auto)); // true

From Commitment

Compute versioned hash directly from commitment:
import { Blob } from 'tevm';

const commitment = blob.toCommitment();

// Using Commitment namespace
const versionedHash = Blob.Commitment.toVersionedHash(commitment);

console.log(versionedHash[0]); // 0x01
console.log(versionedHash.length); // 32

Version Validation

import { Blob } from 'tevm';

const versionedHash = blob.toVersionedHash();

// Validate format
console.log(Blob.isValidVersion(versionedHash)); // true

// Check version byte
const version = Blob.VersionedHash.getVersion(versionedHash);
console.log(version); // 1 (KZG)

// Manual validation
if (versionedHash[0] !== 0x01) {
  throw new Error('Unsupported blob version');
}

Transaction Usage

import { Blob } from 'tevm';

// Create blobs
const blobs = [blob1, blob2, blob3];

// Generate commitments and versioned hashes
const commitments = blobs.map(b => b.toCommitment());
const versionedHashes = blobs.map(b => b.toVersionedHash());

// Create EIP-4844 transaction
const tx = {
  type: '0x03',
  blobVersionedHashes: versionedHashes, // Only hashes on-chain
  maxFeePerBlobGas: 100_000_000n,
  // ... other fields

  // Blob sidecar (mempool only, not on-chain)
  blobs: blobs,
  commitments: commitments,
  proofs: blobs.map(b => b.toProof()),
};

See Also