HatsIdUtilities.sol
HatsIdUtilities
Inherits: IHatsIdUtilities
Author: Haberdasher Labs
Functions for working with Hat Ids from Hats Protocol. Factored out of Hats.sol for easier use by other contracts.
State Variables
linkedTreeRequests
Mapping of tophats requesting to link to admin hats in other trees
Linkage only occurs if request is approved by the new admin
linkedTreeAdmins
Mapping of approved & linked tophats to admin hats in other trees, used for grafting one hats tree onto another
Trees can only be linked to another tree via their tophat
TOPHAT_ADDRESS_SPACE
Hat Ids serve as addresses. A given Hat's Id represents its location in its hat tree: its level, its admin, its admin's admin (etc, all the way up to the tophat). The top level consists of 4 bytes and references all tophats. Each level below consists of 16 bits, and contains up to 65,536 child hats. A uint256 contains 4 bytes of space for tophat addresses, giving room for ((256 - 32) / 16) = 14 levels of delegation, with the admin at each level having space for 65,536 different child hats. A hat tree consists of a single tophat and has a max depth of 14 levels.
Number of bits of address space for tophat ids, ie the tophat domain
LOWER_LEVEL_ADDRESS_SPACE
Number of bits of address space for each level below the tophat
MAX_LEVELS
Maximum number of levels below the tophat, ie max tree depth (256 - TOPHAT_ADDRESS_SPACE) / LOWER_LEVEL_ADDRESS_SPACE;
Functions
buildHatId
Constructs a valid hat id for a new hat underneath a given admin
Reverts if the admin has already reached MAX_LEVELS
Parameters
_admin
uint256
the id of the admin for the new hat
_newHat
uint16
the uint16 id of the new hat
Returns
id
uint256
The constructed hat id
getHatLevel
Identifies the level a given hat in its hat tree
Parameters
_hatId
uint256
the id of the hat in question
Returns
level
uint32
(0 to type(uint32).max)
getLocalHatLevel
Identifies the level a given hat in its local hat tree
Similar to getHatLevel, but does not account for linked trees
Parameters
_hatId
uint256
the id of the hat in question
Returns
level
uint32
The local level, from 0 to 14
isTopHat
Checks whether a hat is a topHat
Parameters
_hatId
uint256
The hat in question
Returns
_isTopHat
bool
Whether the hat is a topHat
isLocalTopHat
Checks whether a hat is a topHat in its local hat tree
Similar to isTopHat, but does not account for linked trees
Parameters
_hatId
uint256
The hat in question
Returns
_isLocalTopHat
bool
Whether the hat is a topHat for its local tree
isValidHatId
getAdminAtLevel
Gets the hat id of the admin at a given level of a given hat
This function traverses trees by following the linkedTreeAdmin pointer to a hat located in a different tree
Parameters
_hatId
uint256
the id of the hat in question
_level
uint32
the admin level of interest
Returns
admin
uint256
The hat id of the resulting admin
getAdminAtLocalLevel
Gets the hat id of the admin at a given level of a given hat local to the tree containing the hat.
Parameters
_hatId
uint256
the id of the hat in question
_level
uint32
the admin level of interest
Returns
admin
uint256
The hat id of the resulting admin
getTopHatDomain
Gets the tophat domain of a given hat
A domain is the identifier for a given hat tree, stored in the first 4 bytes of a hat's id
Parameters
_hatId
uint256
the id of the hat in question
Returns
domain
uint32
The domain of the hat's tophat
getTippyTopHatDomain
Gets the domain of the highest parent tophat โ the "tippy tophat"
Parameters
_topHatDomain
uint32
the 32 bit domain of a (likely linked) tophat
Returns
domain
uint32
The tippy tophat domain
noCircularLinkage
Checks For any circular linkage of trees
Parameters
_topHatDomain
uint32
the 32 bit domain of the tree to be linked
_linkedAdmin
uint256
the hatId of the potential tree admin
Returns
notCircular
bool
circular link has not been found
sameTippyTopHatDomain
Checks that a tophat domain and its potential linked admin are from the same tree, ie have the same tippy tophat domain
Parameters
_topHatDomain
uint32
The 32 bit domain of the tophat to be linked
_newAdminHat
uint256
The new admin for the linked tree
Returns
sameDomain
bool
Whether the _topHatDomain and the domain of its potential linked _newAdminHat domains are the same
Last updated