Skip to main content

Overview

TransactionHash is a branded Uint8Array type representing a 32-byte transaction identifier in Ethereum. Every transaction has a unique hash computed from its contents.
import * as TransactionHash from '@tevm/primitives/TransactionHash'

const hash = TransactionHash.fromHex(
  '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
)

Type Definition

type TransactionHashType = Uint8Array & {
  readonly [brand]: "TransactionHash";
  readonly length: 32;
}

Creating TransactionHashes

from

Universal constructor accepting hex string or bytes.
// From hex string
const hash1 = TransactionHash.from(
  '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
)

// From bytes
const bytes = new Uint8Array(32)
const hash2 = TransactionHash.from(bytes)

fromHex

Parse from hex string (with or without 0x prefix).
const hash = TransactionHash.fromHex(
  '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
)
Validation: Must be 64 hex characters (32 bytes).

fromBytes

Create from Uint8Array.
const bytes = new Uint8Array(32)
const hash = TransactionHash.fromBytes(bytes)
Validation: Must be exactly 32 bytes.

Converting TransactionHashes

toHex

Convert to lowercase hex string with 0x prefix.
const hex = TransactionHash.toHex(hash)
// '0x1234567890abcdef...'

Comparing TransactionHashes

equals

Check equality using constant-time byte comparison.
const a = TransactionHash.fromHex('0x1234...')
const b = TransactionHash.fromHex('0x1234...')

if (TransactionHash.equals(a, b)) {
  console.log('Hashes are equal')
}

Errors

InvalidTransactionHashLengthError: Bytes length is not 32. InvalidTransactionHashFormatError: Invalid hex format or unsupported type.

See Also