Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

@xylabs/hex

Package Overview
Dependencies
Maintainers
5
Versions
427
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xylabs/hex

Base functionality used throughout XY Labs TypeScript/JavaScript libraries

latest
Source
npmnpm
Version
5.0.100
Version published
Weekly downloads
3.8K
27.14%
Maintainers
5
Weekly downloads
 
Created
Source

@xylabs/hex

npm license

Base functionality used throughout XY Labs TypeScript/JavaScript libraries

Install

Using npm:

npm install {{name}}

Using yarn:

yarn add {{name}}

Using pnpm:

pnpm add {{name}}

Using bun:

bun add {{name}}

License

See the LICENSE file for license rights and limitations (LGPL-3.0-only).

Reference

packages

hex

### .temp-typedoc

  ### functions

    ### <a id="HexRegExMinMax"></a>HexRegExMinMax

@xylabs/hex

function HexRegExMinMax(minBytes?, maxBytes?): RegExp;

Creates a RegExp matching lowercase hex strings with a byte length in the given range.

Parameters

minBytes?

number = 0

Minimum number of bytes (default 0)

maxBytes?

number = ...

Maximum number of bytes

Returns

RegExp

A RegExp for validating hex strings within the byte range

    ### <a id="HexRegExMinMaxMixedCaseWithPrefix"></a>HexRegExMinMaxMixedCaseWithPrefix

@xylabs/hex

function HexRegExMinMaxMixedCaseWithPrefix(minBytes?, maxBytes?): RegExp;

Creates a RegExp matching mixed-case hex strings with a 0x prefix and a byte length in the given range.

Parameters

minBytes?

number = 0

Minimum number of bytes (default 0)

maxBytes?

number = ...

Maximum number of bytes

Returns

RegExp

A RegExp for validating prefixed hex strings within the byte range

    ### <a id="asAddress"></a>asAddress

@xylabs/hex

Call Signature

function asAddress(value?): BrandedAddress | undefined;

Attempts to coerce a value into an Address type, returning undefined or throwing based on the assert config.

Parameters

value?

unknown

The value to coerce (must be a string)

Returns

BrandedAddress | undefined

The value as Address, or undefined if coercion fails and assert is not set

Call Signature

function asAddress(value, assert): BrandedAddress;

Attempts to coerce a value into an Address type, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

assert

AssertConfig

If provided, throws on failure instead of returning undefined

Returns

BrandedAddress

The value as Address, or undefined if coercion fails and assert is not set

    ### <a id="asAddressV2"></a>asAddressV2

@xylabs/hex

function asAddressV2(value?, assert?): BrandedAddress | undefined;

Alpha

Parameters

value?

unknown

assert?

boolean = false

Returns

BrandedAddress | undefined

    ### <a id="asEthAddress"></a>asEthAddress

@xylabs/hex

Call Signature

function asEthAddress(value): EthAddress | undefined;

Attempts to coerce a value into an EthAddress, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

Returns

EthAddress | undefined

The value as EthAddress, or undefined if coercion fails and assert is not set

Call Signature

function asEthAddress(value, assert): EthAddress;

Attempts to coerce a value into an EthAddress, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

assert

AssertConfig

If provided, throws on failure instead of returning undefined

Returns

EthAddress

The value as EthAddress, or undefined if coercion fails and assert is not set

    ### <a id="asHash"></a>asHash

@xylabs/hex

Call Signature

function asHash(value): BrandedHash | undefined;

Attempts to coerce a value into a Hash type, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

Returns

BrandedHash | undefined

The value as Hash, or undefined if coercion fails and assert is not set

Call Signature

function asHash(value, assert): BrandedHash;

Attempts to coerce a value into a Hash type, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

assert

AssertConfig

If provided, throws on failure instead of returning undefined

Returns

BrandedHash

The value as Hash, or undefined if coercion fails and assert is not set

    ### <a id="asHex"></a>asHex

@xylabs/hex

Call Signature

function asHex(value): BrandedHex | undefined;

Attempts to coerce a value into a Hex type, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

Returns

BrandedHex | undefined

The value as Hex, or undefined if coercion fails and assert is not set

Call Signature

function asHex(value, assert): BrandedHex;

Attempts to coerce a value into a Hex type, returning undefined or throwing based on the assert config.

Parameters

value

unknown

The value to coerce (must be a string)

assert

AssertConfig

If provided, throws on failure instead of returning undefined

Returns

BrandedHex

The value as Hex, or undefined if coercion fails and assert is not set

    ### <a id="bitsToNibbles"></a>bitsToNibbles

@xylabs/hex

function bitsToNibbles(value): number;

Converts a bit count to the equivalent number of hex nibbles (4 bits each).

Parameters

value

number

The number of bits (must be a multiple of 4)

Returns

number

The number of nibbles

    ### <a id="hexFrom"></a>hexFrom

@xylabs/hex

function hexFrom(value, config?): BrandedHex;

Takes unknown value and tries our best to convert it to a hex string

Parameters

value

string | number | bigint | ArrayBufferLike

Supported types are string, number, bigint, and ArrayBuffer

config?

HexConfig

Configuration of output format and validation

Returns

BrandedHex

    ### <a id="hexFromArrayBuffer"></a>hexFromArrayBuffer

@xylabs/hex

function hexFromArrayBuffer(buffer, config?): BrandedHex;

Convert an ArrayBuffer to a hex string

Parameters

buffer

ArrayBufferLike

The buffer to be converted

config?

HexConfig

Configuration of output format and validation

Returns

BrandedHex

    ### <a id="hexFromBigInt"></a>hexFromBigInt

@xylabs/hex

function hexFromBigInt(value, config?): BrandedHex;

Convert a bigint to a hex string

Parameters

value

bigint

The bigint to be converted

config?

HexConfig = {}

Configuration of output format and validation

Returns

BrandedHex

    ### <a id="hexFromHexString"></a>hexFromHexString

@xylabs/hex

function hexFromHexString(value, config?): BrandedHex;

Normalizes a hex string by stripping an optional 0x prefix, lowercasing, and padding to byte/bit boundaries.

Parameters

value

string

The hex string to normalize (with or without 0x prefix)

config?

HexConfig = {}

Configuration for prefix, byteSize, and bitLength padding

Returns

BrandedHex

The normalized Hex string

    ### <a id="hexFromNumber"></a>hexFromNumber

@xylabs/hex

function hexFromNumber(value, config?): BrandedHex;

Converts a number to a hex string by converting to BigInt first.

Parameters

value

number

The number to convert

config?

HexConfig

Optional hex output configuration

Returns

BrandedHex

The hex string representation

    ### <a id="hexToBigInt"></a>hexToBigInt

@xylabs/hex

function hexToBigInt(hex): bigint;

Converts a Hex string to a BigInt.

Parameters

hex

BrandedHex

The hex string to convert

Returns

bigint

The BigInt representation of the hex value

    ### <a id="isAddress"></a>isAddress

@xylabs/hex

function isAddress(value?, config?): value is BrandedAddress;

Type guard that checks whether a value is a valid 160-bit address.

Parameters

value?

unknown

The value to check

config?

HexConfig = {}

Optional hex config (defaults to 160-bit, no prefix)

Returns

value is BrandedAddress

True if the value is a valid Address

    ### <a id="isAddressV2"></a>isAddressV2

@xylabs/hex

function isAddressV2(value?): value is BrandedAddress;

Alpha

Parameters

value?

unknown

Returns

value is BrandedAddress

    ### <a id="isEthAddress"></a>isEthAddress

@xylabs/hex

function isEthAddress(value?, config?): value is EthAddress;

Type guard that checks whether a value is a valid 0x-prefixed Ethereum address.

Parameters

value?

unknown

The value to check

config?

HexConfig = {}

Optional hex config (defaults to 160-bit with prefix)

Returns

value is EthAddress

True if the value is a valid EthAddress

    ### <a id="isHash"></a>isHash

@xylabs/hex

function isHash(value, bitLength?): value is BrandedHash;

Type guard that checks whether a value is a valid hash of the specified bit length.

Parameters

value

unknown

The value to check

bitLength?

HashBitLength = 256

The expected bit length of the hash (defaults to 256)

Returns

value is BrandedHash

True if the value is a valid Hash

    ### <a id="isHashBitLength"></a>isHashBitLength

@xylabs/hex

function isHashBitLength(value): value is HashBitLength;

Type guard that checks whether a value is a valid hash bit length.

Parameters

value

unknown

The value to check

Returns

value is HashBitLength

True if the value is one of the supported HashBitLength values

    ### <a id="isHex"></a>isHex

@xylabs/hex

function isHex(value, config?): value is BrandedHex;

Type guard that checks whether a value is a valid hex string.

Parameters

value

unknown

The value to check

config?

HexConfig

Optional configuration for prefix and bit length validation

Returns

value is BrandedHex

True if the value is a valid Hex string

    ### <a id="isHexZero"></a>isHexZero

@xylabs/hex

function isHexZero(value?): boolean | undefined;

Checks whether a hex string represents a zero value.

Parameters

value?

string

The hex string to check

Returns

boolean | undefined

True if zero, false if non-zero, or undefined if the input is not a string

    ### <a id="nibblesToBits"></a>nibblesToBits

@xylabs/hex

function nibblesToBits(value): number;

Converts a nibble count to the equivalent number of bits.

Parameters

value

number

The number of nibbles

Returns

number

The number of bits

    ### <a id="toAddress"></a>toAddress

@xylabs/hex

function toAddress(value, config?): BrandedAddress;

Converts a value to a 160-bit Address hex string.

Parameters

value

string | number | bigint | ArrayBufferLike

The value to convert (string, number, bigint, or ArrayBuffer)

config?

HexConfig = {}

Optional hex config (defaults to 160-bit, no prefix)

Returns

BrandedAddress

The value as an Address

    ### <a id="toAddressV2"></a>toAddressV2

@xylabs/hex

function toAddressV2(value, assert?): BrandedAddress | undefined;

Alpha

Parameters

value

unknown

assert?

boolean = false

Returns

BrandedAddress | undefined

    ### <a id="toEthAddress"></a>toEthAddress

@xylabs/hex

function toEthAddress(value, config?): EthAddress;

Converts a value to a 0x-prefixed Ethereum address string.

Parameters

value

string | number | bigint | ArrayBufferLike

The value to convert (string, number, bigint, or ArrayBuffer)

config?

HexConfig = {}

Optional hex config (defaults to 160-bit, no inner prefix)

Returns

EthAddress

The value as an EthAddress

    ### <a id="toHex"></a>toHex

@xylabs/hex

function toHex(value, config?): BrandedHex;

takes any value and tries our best to convert it to a hex string

Parameters

value

string | number | bigint | ArrayBufferLike

Supported types are string, number, bigint, and ArrayBuffer

config?

HexConfig = {}

Configuration of output format and validation

Returns

BrandedHex

    ### <a id="toHexLegacy"></a>toHexLegacy

@xylabs/hex

function toHexLegacy(buffer): string;

Converts an ArrayBuffer to a hex string without padding or normalization.

Parameters

buffer

ArrayBuffer

The ArrayBuffer to convert

Returns

string

A lowercase hex string representation of the buffer

  ### interfaces

    ### <a id="HexConfig"></a>HexConfig

@xylabs/hex

Configuration of validation and output format

Properties

bitLength?

optional bitLength?: number;

byteSize?

optional byteSize?: number;

prefix?

optional prefix?: boolean;
  ### type-aliases

    ### <a id="Address"></a>Address

@xylabs/hex

type Address = z.infer<typeof AddressZod>;

A validated 20-byte address string type, inferred from the AddressZod schema.

    ### <a id="AddressTransformZodType"></a>AddressTransformZodType

@xylabs/hex

type AddressTransformZodType = z.infer<typeof AddressTransformZod>;

The output type of AddressTransformZod after parsing and transformation.

    ### <a id="AddressValidationZodType"></a>AddressValidationZodType

@xylabs/hex

type AddressValidationZodType = z.infer<typeof AddressValidationZod>;

The output type of AddressValidationZod after parsing.

    ### <a id="BrandedHash"></a>BrandedHash

@xylabs/hex

type BrandedHash = Brand<Hex, {
  __hash: true;
}>;

Branded type representing a validated hash hex string.

    ### <a id="BrandedHex"></a>BrandedHex

@xylabs/hex

type BrandedHex = Brand<Lowercase<string>, {
  __hex: true;
}>;

Branded type representing a validated lowercase hex string.

    ### <a id="EthAddress"></a>EthAddress

@xylabs/hex

type EthAddress = Brand<string, {
  __eth_address: true;
}>;

Branded type representing a validated Ethereum address with 0x prefix.

    ### <a id="Hash"></a>Hash

@xylabs/hex

type Hash = z.infer<typeof HashZod>;

A validated hash string type, inferred from the HashZod schema.

    ### <a id="HashBitLength"></a>HashBitLength

@xylabs/hex

type HashBitLength = 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;

Valid bit lengths for hash values.

    ### <a id="Hex"></a>Hex

@xylabs/hex

type Hex = z.infer<typeof HexZod>;

A validated hex string type, inferred from the HexZod schema.

  ### variables

    ### <a id="ADDRESS_LENGTH"></a>ADDRESS_LENGTH

@xylabs/hex

const ADDRESS_LENGTH: 40;

The character length of an address hex string (40 hex characters / 20 bytes).

    ### <a id="AddressRegEx"></a>AddressRegEx

@xylabs/hex

const AddressRegEx: RegExp;

Regular expression matching a 20-byte (40 hex character) address string.

    ### <a id="AddressTransformZod"></a>AddressTransformZod

@xylabs/hex

const AddressTransformZod: ZodPipe<ZodPipe<ZodUnion<readonly [ZodString, ZodBigInt, ZodNumber]>, ZodTransform<string, string | number | bigint>>, ZodTransform<BrandedAddress, string>>;

Zod schema that accepts a string, bigint, or number and transforms it into a validated Address.

    ### <a id="AddressValidationZod"></a>AddressValidationZod

@xylabs/hex

const AddressValidationZod: ZodPipe<ZodString, ZodTransform<BrandedAddress, string>>;

Zod schema that validates a string is a properly formatted 40-character hex address.

    ### <a id="AddressZod"></a>AddressZod

@xylabs/hex

const AddressZod: ZodPipe<ZodString, ZodTransform<BrandedAddress, string>>;

Zod schema that validates and transforms a string into a branded Address type.

    ### <a id="BigIntToJsonZod"></a>BigIntToJsonZod

@xylabs/hex

const BigIntToJsonZod: ZodPipe<ZodBigInt, ZodTransform<BrandedHex, bigint>>;

Zod schema that transforms a non-negative BigInt into a hex string for JSON serialization.

    ### <a id="ETH_ZERO_ADDRESS"></a>ETH_ZERO_ADDRESS

@xylabs/hex

const ETH_ZERO_ADDRESS: EthAddress;

The zero Ethereum address constant (0x followed by 40 zero characters).

    ### <a id="EthAddressFromStringSchema"></a>EthAddressFromStringSchema

@xylabs/hex

const EthAddressFromStringSchema: ZodPipe<ZodString, ZodTransform<EthAddress, string>> = EthAddressFromStringZod;

Deprecated

use EthAddressFromStringZod

    ### <a id="EthAddressFromStringZod"></a>EthAddressFromStringZod

@xylabs/hex

const EthAddressFromStringZod: ZodPipe<ZodString, ZodTransform<EthAddress, string>>;

Zod schema that validates and transforms a string into an EthAddress type.

    ### <a id="EthAddressRegEx"></a>EthAddressRegEx

@xylabs/hex

const EthAddressRegEx: RegExp;

Regular expression matching a 20-byte Ethereum address with 0x prefix (mixed case).

    ### <a id="EthAddressToStringSchema"></a>EthAddressToStringSchema

@xylabs/hex

const EthAddressToStringSchema: ZodString = EthAddressToStringZod;

Deprecated

use EthAddressToStringZod

    ### <a id="EthAddressToStringZod"></a>EthAddressToStringZod

@xylabs/hex

const EthAddressToStringZod: ZodString;

Zod schema that validates a string is a properly formatted Ethereum address.

    ### <a id="EthAddressZod"></a>EthAddressZod

@xylabs/hex

const EthAddressZod: ZodString & ZodType<EthAddress, string, $ZodTypeInternals<EthAddress, string>>;

Zod schema that validates a string as a properly formatted Ethereum address using regex and type guard.

    ### <a id="HASH_LENGTH"></a>HASH_LENGTH

@xylabs/hex

const HASH_LENGTH: 32;

The byte length of a standard hash (32 bytes / 256 bits).

    ### <a id="HashBitLength"></a>HashBitLength

@xylabs/hex

HashBitLength: HashBitLength[];

Array of all valid hash bit lengths for runtime validation.

    ### <a id="HashRegEx"></a>HashRegEx

@xylabs/hex

const HashRegEx: RegExp;

Regular expression matching a 32-byte (64 hex character) hash string.

    ### <a id="HashToJsonZod"></a>HashToJsonZod

@xylabs/hex

const HashToJsonZod: ZodPipe<ZodPipe<ZodString, ZodTransform<BrandedHash, string>>, ZodTransform<string, BrandedHash>>;

Zod schema that transforms a Hash to a plain string for JSON serialization.

    ### <a id="HashZod"></a>HashZod

@xylabs/hex

const HashZod: ZodPipe<ZodString, ZodTransform<BrandedHash, string>>;

Zod schema that validates and transforms a string into a branded Hash type.

    ### <a id="HexRegEx"></a>HexRegEx

@xylabs/hex

const HexRegEx: RegExp;

Regular expression matching a lowercase hex string without prefix.

    ### <a id="HexRegExWithPrefix"></a>HexRegExWithPrefix

@xylabs/hex

const HexRegExWithPrefix: RegExp;

Regular expression matching a lowercase hex string with a 0x prefix.

    ### <a id="HexZod"></a>HexZod

@xylabs/hex

const HexZod: ZodPipe<ZodString, ZodTransform<BrandedHex, string>>;

Zod schema that validates and transforms a string into a branded Hex type.

    ### <a id="JsonToBigIntZod"></a>JsonToBigIntZod

@xylabs/hex

const JsonToBigIntZod: ZodPipe<ZodPipe<ZodString, ZodTransform<BrandedHex, string>>, ZodTransform<bigint, BrandedHex>>;

Zod schema that parses a JSON hex string into a BigInt.

    ### <a id="JsonToHashZod"></a>JsonToHashZod

@xylabs/hex

const JsonToHashZod: ZodPipe<ZodString, ZodTransform<BrandedHash, string>>;

Zod schema that parses a JSON string into a validated Hash, throwing on invalid input.

    ### <a id="ZERO_ADDRESS"></a>ZERO_ADDRESS

@xylabs/hex

const ZERO_ADDRESS: BrandedAddress;

A 160-bit zero address constant.

    ### <a id="ZERO_HASH"></a>ZERO_HASH

@xylabs/hex

const ZERO_HASH: BrandedHash;

A 256-bit zero hash constant.

Keywords

hex

FAQs

Package last updated on 22 Apr 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts