import { DisallowedToken, EmojiToken, fns_beautify, fns_emoji, fns_normalize, fns_normalize_fragment, fns_split, fns_tokenize, IgnoredToken, is_combining_mark, Label, MappedToken, NFCToken, should_escape, StopToken, TextToken, Token, ValidToken, } from '@0xftm/fns-normalize' import { concat, hexlify } from '@ethersproject/bytes' import { keccak256 } from '@ethersproject/keccak256' import { toUtf8Bytes } from '@ethersproject/strings' import { decodeLabelhash, isEncodedLabelhash } from './labels' const zeros = new Uint8Array(32) zeros.fill(0) export const normalise = (name: string) => (name ? fns_normalize(name) : name) export const namehash = (name: string): string => { let result: string | Uint8Array = zeros if (name) { const labels = name.split('.') for (let i = labels.length - 1; i >= 0; i -= 1) { let labelSha: string if (isEncodedLabelhash(labels[i])) { labelSha = decodeLabelhash(labels[i]) } else { const normalised = normalise(labels[i]) labelSha = keccak256(toUtf8Bytes(normalised)) } result = keccak256(concat([result, labelSha])) } } else { result = hexlify(zeros) } return result as string } export const beautify = fns_beautify export const emoji = fns_emoji export const normalizeFragment = fns_normalize_fragment export const split = fns_split export const tokenise = fns_tokenize export const isCombiningMark = is_combining_mark export const shouldEscape = should_escape export type { DisallowedToken, EmojiToken, IgnoredToken, Label, MappedToken, NFCToken, StopToken, TextToken, Token, ValidToken, }