UNPKG

@ensnode/ensnode-sdk

Version:

A utility library for interacting with ENSNode and ENS data

120 lines (117 loc) 3.45 kB
// src/utils/constants.ts import { namehash } from "viem"; var ROOT_NODE = namehash(""); var REVERSE_ROOT_NODES = /* @__PURE__ */ new Set([namehash("addr.reverse")]); var ETH_COIN_TYPE = 60n; // src/utils/types.ts var PluginName = /* @__PURE__ */ ((PluginName2) => { PluginName2["Subgraph"] = "subgraph"; PluginName2["Basenames"] = "basenames"; PluginName2["Lineanames"] = "lineanames"; PluginName2["ThreeDNS"] = "threedns"; return PluginName2; })(PluginName || {}); // src/utils/cache.ts var LruCache = class { _cache = /* @__PURE__ */ new Map(); _capacity; /** * Create a new LRU cache with the given capacity. * * @param capacity The maximum number of items in the cache. If set to 0, the cache is effectively disabled. * @throws Error if capacity is not a non-negative integer. */ constructor(capacity) { if (!Number.isInteger(capacity)) { throw new Error( `LruCache requires capacity to be an integer but a capacity of ${capacity} was requested.` ); } if (capacity < 0) { throw new Error( `LruCache requires a non-negative capacity but a capacity of ${capacity} was requested.` ); } this._capacity = capacity; } set(key, value) { this._cache.set(key, value); if (this._cache.size > this._capacity) { const oldestKey = this._cache.keys().next().value; this._cache.delete(oldestKey); } } get(key) { const value = this._cache.get(key); if (value) { this._cache.delete(key); this._cache.set(key, value); } return value; } clear() { this._cache.clear(); } get size() { return this._cache.size; } get capacity() { return this._capacity; } }; // src/utils/subname-helpers.ts import { concat, isAddress, isHash, keccak256, toHex } from "viem"; import { labelhash } from "viem/ens"; var makeSubdomainNode = (labelHash, node) => keccak256(concat([node, labelHash])); var addrReverseLabel = (address) => address.slice(2).toLowerCase(); var maybeHealLabelByReverseAddress = ({ maybeReverseAddress, labelHash }) => { if (!isAddress(maybeReverseAddress)) { throw new Error( `Invalid reverse address: '${maybeReverseAddress}'. Must be a valid EVM Address.` ); } if (!isHash(labelHash)) { throw new Error( `Invalid labelHash: '${labelHash}'. Must start with '0x' and represent 32 bytes.` ); } const assumedLabel = addrReverseLabel(maybeReverseAddress); if (labelhash(assumedLabel) === labelHash) return assumedLabel; return null; }; var uint256ToHex32 = (num) => toHex(num, { size: 32 }); var UNINDEXABLE_LABEL_CHARACTERS = [ "\0", // null byte: PostgreSQL does not allow storing this character in text fields. ".", // conflicts with ENS label separator logic. "[", // conflicts with "unknown label" representations. "]" // conflicts with "unknown label" representations. ]; var UNINDEXABLE_LABEL_CHARACTER_CODES = new Set( UNINDEXABLE_LABEL_CHARACTERS.map((char) => char.charCodeAt(0)) ); var isLabelIndexable = (label) => { if (!label) return false; for (let i = 0; i < label.length; i++) { if (UNINDEXABLE_LABEL_CHARACTER_CODES.has(label.charCodeAt(i))) return false; } return true; }; export { ETH_COIN_TYPE, LruCache, PluginName, REVERSE_ROOT_NODES, ROOT_NODE, isLabelIndexable, makeSubdomainNode, maybeHealLabelByReverseAddress, uint256ToHex32 }; //# sourceMappingURL=index.js.map