Skip to main content

Try it Live

Run Bytes examples in the interactive playground
Type-Safe Primitive - Bytes16 provides compile-time guarantees for 16-byte values like UUIDs and medium hashes.

Overview

Bytes16 is a branded Uint8Array type representing exactly 16 bytes (128 bits). It provides strict size validation and type safety for fixed-size byte values.
import * as Bytes16 from 'tevm/primitives/Bytes/Bytes16';

// Create from hex
const bytes = Bytes16('0x' + '12'.repeat(16));
console.log(bytes.length); // 16

// Create zero-filled
const zeros = Bytes16.zero();
console.log(Bytes16.isZero(zeros)); // true

// Size is strictly enforced
Bytes16(new Uint8Array(15)); // Error: must be 16 bytes

Use Cases

UUIDs (128-bit)
const uuid = Bytes16('550e8400e29b41d4a716446655440000');
Medium Hashes
// MD5, RIPEMD-128, etc.
const hash = Bytes16('0x...');
Fixed-Size Keys
const key = Bytes16('0x' + 'ab'.repeat(16));

Type Safety

Bytes16 uses Symbol branding for compile-time safety:
type Bytes16Type = Uint8Array & {
  readonly [Symbol.for("Bytes16")]: true;
  readonly length: 16;
};
This prevents mixing different byte types:
const bytes16 = Bytes16('0x' + '12'.repeat(16));
const bytes32 = Bytes32('0x' + '12'.repeat(32));

// Type error: cannot assign Bytes32 to Bytes16
const wrong: Bytes16.Bytes16Type = bytes32;

Hex Variants

Bytes16 also supports hex string variants with case flags:
// Lowercase hex (default)
type HexBytes16 = string & {
  readonly [Symbol.for("HexBytes16")]: true;
  readonly length: 34; // "0x" + 32 hex chars
  readonly lowercase: true;
};

// Uppercase hex
type HexBytes16Upper = string & {
  readonly [Symbol.for("HexBytes16")]: true;
  readonly length: 34;
  readonly uppercase: true;
};
For generic byte concepts, see Bytes.

Constructors

from

Create Bytes16 from hex string or bytes:
// From hex
const b1 = Bytes16('0x' + '12'.repeat(16));

// From bytes
const b2 = Bytes16(Bytes16());

fromHex

Create from hex string with strict validation:
const bytes = Bytes16('0x1234567890abcdef1234567890abcdef');

// Without 0x prefix
const bytes2 = Bytes16('ab'.repeat(16));

// Throws on wrong length
Bytes16('0x1234'); // Error: must be 32 hex chars

fromBytes

Create from Uint8Array with size check:
const arr = Bytes16();
const bytes = Bytes16(arr);

// Throws on wrong size
Bytes16(new Uint8Array(15)); // Error

zero

Create zero-filled Bytes16:
const zeros = Bytes16.zero();
console.log(Bytes16.isZero(zeros)); // true

Conversions

toHex

Convert to hex string:
const bytes = Bytes16(Bytes16());
const hex = Bytes16.toHex(bytes);
console.log(hex); // "0x00000000000000000000000000000000"

toUint8Array

Convert to raw bytes:
const bytes = Bytes16('0x' + '12'.repeat(16));
const arr = Bytes16.toUint8Array(bytes);
console.log(arr instanceof Uint8Array); // true

Operations

equals

Check equality:
const a = Bytes16('0x' + 'aa'.repeat(16));
const b = Bytes16('0x' + 'aa'.repeat(16));
console.log(Bytes16.equals(a, b)); // true

compare

Compare two values:
const a = Bytes16('0x' + '01'.repeat(16));
const b = Bytes16('0x' + '02'.repeat(16));
console.log(Bytes16.compare(a, b)); // -1 (a < b)

clone

Create independent copy:
const original = Bytes16('0x' + '12'.repeat(16));
const copy = Bytes16.clone(original);

isZero

Check if all zeros:
const zeros = Bytes16.zero();
console.log(Bytes16.isZero(zeros)); // true

const nonZero = Bytes16('0x' + '00'.repeat(15) + '01');
console.log(Bytes16.isZero(nonZero)); // false

size

Get size (always 16):
const bytes = Bytes16('0x' + '12'.repeat(16));
console.log(Bytes16.size(bytes)); // 16

Constants

SIZE

Bytes16 size in bytes:
console.log(Bytes16.SIZE); // 16

ZERO

Zero-filled constant:
console.log(Bytes16.ZERO.length); // 16
console.log(Bytes16.isZero(Bytes16.ZERO)); // true

Error Handling

Bytes16 validates size strictly:
try {
  Bytes16(new Uint8Array(15));
} catch (error) {
  console.log(error.code); // "BYTES16_INVALID_LENGTH"
  console.log(error.message); // "Bytes16 must be 16 bytes, got 15"
}

try {
  Bytes16('0x1234');
} catch (error) {
  console.log(error.code); // "BYTES16_INVALID_HEX_LENGTH"
  console.log(error.message); // "Bytes16 hex must be 32 characters..."
}
  • Bytes32 - 256-bit fixed-size bytes (most common)
  • Bytes64 - 512-bit fixed-size bytes
  • Keccak256 - 256-bit cryptographic hashes
  • Hex - Variable-length hex strings