Utilities

Hat & Tree ID Utils

Following are utility functions to handle various hat & tree ID formats. More information about hat IDs can be found here.

hatIdDecimalToHex

Convert a hat ID from decimal to hex.

import { hatIdDecimalToHex } from "@hatsprotocol/sdk-v1-core";

const hatIdHex = hatIdDecimalToHex(hatId);

Arguments:

hatId: bigint

hatId - Hat ID in decimal format.

Response:

`0x${string}`

Hat ID in a hex format.

hatIdHexToDecimal

Convert a hat ID from hex to decimal.

import { hatIdHexToDecimal } from "@hatsprotocol/sdk-v1-core";

const hatIdDecimal = hatIdHexToDecimal(hatId);

Arguments:

hatId: string

hatId - Hat ID in hex format.

Response:

bigint

Hat ID in a decimal format.

treeIdDecimalToHex

Convert a tree ID from decimal to hex. A tree ID is the first 4 bytes in a hat ID.

import { treeIdDecimalToHex } from "@hatsprotocol/sdk-v1-core";

const treeIdHex = treeIdDecimalToHex(treeId);

Arguments:

treeId: number

treeId - Tree ID in decimal format.

Response:

`0x${string}`

Tree ID in a hex format.

treeIdHexToDecimal

Convert a tree ID from hex to decimal. A tree ID is the first 4 bytes in a hat ID.

import { treeIdHexToDecimal } from "@hatsprotocol/sdk-v1-core";

const treeIdDecimal = treeIdHexToDecimal(treeId);

Arguments:

treeId: string

treeId - Tree ID in hex format.

Response:

number

Tree ID in a decimal format.

treeIdToTopHatId

Convert a tree ID to its top-hat ID. A tree ID is the first 4 bytes in a hat ID.

import { treeIdToTopHatId } from "@hatsprotocol/sdk-v1-core";

const tophatId = treeIdToTopHatId(treeId);

Arguments:

treeId: number

treeId - Tree ID in decimal format.

Response:

bigint

Top-hat ID in decimal format.

hatIdToTreeId

Convert a hat ID to its tree ID. A tree ID is the first 4 bytes in a hat ID.

import { hatIdToTreeId } from "@hatsprotocol/sdk-v1-core";

const treeId = hatIdToTreeId(hatId);

Arguments:

hatId: bigint

hatId - Hat ID in decimal format.

Response:

number

Tree ID of the hat, in a decimal format.

hatIdDecimalToIp

The IP format may be used as a "pretty" hat ID format for presenting.

For example, a hat with a hex ID of: 0x00000001000a0002000000000000000000000000000000000000000000000000 will have an IP format of 1.10.2 - Each level is separated by a dot and presented as a decimal number, excluding zeros.

import { hatIdDecimalToIp } from "@hatsprotocol/sdk-v1-core";

const hatIdIp = hatIdDecimalToIp(hatId);

Arguments:

hatId: bigint

hatId - Hat ID in decimal format.

Response:

string

Hat ID in IP format.

hatIdIpToDecimal

Convert a hat ID from an IP format, to a decimal format.

import { hatIdIpToDecimal } from "@hatsprotocol/sdk-v1-core";

const hatIdDecimal = hatIdIpToDecimal(hatId);

Arguments:

hatId: string

hatId - Hat ID in IP format.

Response:

bigint

Hat ID in decimal format.

Constants

Following are Hats-specific exported constant values.

import {   
  HATS_V1, // Hats-Protocol v1 contract address
  MAX_LEVELS, // Max levels on a Hats tree
  MAX_LEVEL_HATS, // Max amount of hats on each level (excluding level 0)
  ZERO_ID, // The zero Hat ID in hex format 
  FALLBACK_ADDRESS, // The fallback address for eligibility and toggle 
} from "@hatsprotocol/sdk-v1-core";

Last updated