ens-normalize.js
Normalizador de Nombres de Ethereum Name Service (ENS) sin dependencias.npm i @adraffy/ens-normalize ✓
- 🏛️ Sigue ENSIP-15: Estándar de Normalización de Nombres ENS
- Unicode:
17.0.0• CLDR:47 - Otras implementaciones:
- Python — namehash/ens-normalize-python
- Rust — sevenzing/ens-normalize-rs
- Zig — evmts/z-ens-normalize
- C# — adraffy/ENSNormalize.cs
- Java — adraffy/ENSNormalize.java
- Swift – adraffy/ENSNormalize.swift
- Go — adraffy/go-ens-normalize
- Swift – adraffy/ENSNormalize.swift
- Implementación previa:
- Javascript — ensdomains/eth-ens-namehash
- Informes de desglose de ENSIP-1
- ✅️ Pasa 100% las Pruebas de Validación ENSIP-15
- ✅️ Pasa 100% las Pruebas de Normalización Unicode
- Tamaños de archivo minificados:
29KB— NFC nativo vía nf-native.js usandoString.normalize()⚠️38KBPor defecto — NFC personalizado vía nf.js
44KB ¡Todo!* — NFC personalizado + subbibliotecas: parts.js, utils.js
- Aplicaciones incluidas:
- Demo del Resolver ⭐
- Emoji soportados
- Visor de caracteres
- Explicador Confundido
- Proyectos relacionados:
- Registros recientes .eth • Renovaciones • Expiraciones
- Base de datos de etiquetas • Labelhash⁻¹ • Fuerza bruta
- Explorador de frecuencia de emoji
- adraffy/punycode.js • Codificador Punycode
- adraffy/keccak.js • Hasher Keccak
- adraffy/emoji.js • Parser de Emoji
- Más enlaces al final de Demo del Resolver
import {ens_normalize} from '@adraffy/ens-normalize'; // or require()
// browser: https://cdn.jsdelivr.net/npm/@adraffy/ens-normalize@latest/dist/index.min.mjs (or .cjs)// ALL errors thrown by this library are safe to print
// - characters are shown as {HEX} if should_escape()
// - potentially different bidi directions inside "quotes"
// - 200E is used near "quotes" to prevent spillover
// - an "error type" can be extracted by slicing up to the first (:)
// - labels are middle-truncated with ellipsis (…) at 63 cps
// string -> string
// throws on invalid names
// output ready for namehash
let normalized = ens_normalize('RaFFY🚴♂️.eTh');
// => "raffy🚴♂.eth"
// note: does not enforce .eth registrar 3-character minimum
Formatee los nombres con emoji totalmente cualificado:// works like ens_normalize()
// output ready for display
let pretty = ens_beautify('1⃣2⃣.eth');
// => "1️⃣2️⃣.eth"// note: normalization is unchanged:
// ens_normalize(ens_beautify(x)) == ens_normalize(x)
Normalizar fragmentos de nombres para búsqueda de subcadenas:// these fragments fail ens_normalize()
// but will normalize fine as fragments
let frag1 = ens_normalize_fragment('AB--'); // expected error: label ext
let frag2 = ens_normalize_fragment('\u{303}'); // expected error: leading cm
let frag3 = ens_normalize_fragment('οо'); // expected error: mixture
Tokenización basada en la entrada:// string -> Token[]
// never throws
let tokens = ens_tokenize('_R💩\u{FE0F}a\u{FE0F}\u{304}\u{AD}./');
// [
// { type: 'valid', cps: [ 95 ] }, // valid (as-is)
// {
// type: 'mapped',
// cp: 82, // input
// cps: [ 114 ] // output
// },
// {
// type: 'emoji',
// input: Emoji(2) [ 128169, 65039 ], // input
// emoji: [ 128169, 65039 ], // fully-qualified
// cps: Emoji(1) [ 128169 ] // output (normalized)
// },
// {
// type: 'nfc',
// input: [ 97, 772 ], // input (before nfc)
// cps: [ 257 ], // output (after nfc)
// tokens0: [ // tokens (before nfc)
// { type: 'valid', cps: [ 97 ] },
// { type: 'ignored', cp: 65039 },
// { type: 'valid', cps: [ 772 ] }
// ],
// tokens: [ // tokens (after nfc)
// { type: 'valid', cps: [ 257 ] }
// ]
// },
// { type: 'ignored', cp: 173 },
// { type: 'stop', cp: 46 },
// { type: 'disallowed', cp: 47 }
// ]// note: if name is normalizable, then:
// ens_normalize(ens_tokenize(name).map(token => {
// convert valid/mapped/nfc/stop to string
// }).join('')) == ens_normalize(name)
Tokenización basada en la salida:// string -> Label[]
// never throws
let labels = ens_split('💩Raffy.eth_');
// [
// {
// input: [ 128169, 82, 97, 102, 102, 121 ],
// offset: 0, // index of codepoint, not substring index!
// // (corresponding length can be inferred from input)
// tokens: [
// Emoji(2) [ 128169, 65039 ], // emoji
// [ 114, 97, 102, 102, 121 ] // nfc-text
// ],
// output: [ 128169, 114, 97, 102, 102, 121 ],
// emoji: true,
// type: 'Latin'
// },
// {
// input: [ 101, 116, 104, 95 ],
// offset: 7,
// tokens: [ [ 101, 116, 104, 95 ] ],
// output: [ 101, 116, 104, 95 ],
// error: Error('underscore allowed only at start')
// }
// ]
Generar un arreglo ordenado de puntos de código emoji compatibles (embellecidos):// () -> number[][]
let emojis = ens_emoji();
// [
// [ 2764 ],
// [ 128169, 65039 ],
// [ 128105, 127997, 8205, 9877, 65039 ],
// ...
// ]
Determinar si un carácter no debe imprimirse directamente:// number -> bool
should_escape(0x202E); // eg. RIGHT-TO-LEFT OVERRIDE => true
Determinar si un carácter es una marca combinante:// number -> bool
is_combining_mark(0x20E3); // eg. COMBINING ENCLOSING KEYCAP => true
Formatear puntos de código como cadena segura para impresión:// number[] -> string
safe_str_from_cps([0x300, 0, 32, 97]); // "◌̀{00} a"
safe_str_from_cps(Array(100).fill(97), 4); // "aa…aa" => middle-truncatedConstrucción
git cloneeste repositorio, luegonpm install- Siga las instrucciones en /derive/ para generar archivos de datos
npm run derive- spec.json
- nf.json
- nf-tests.json
npm run make— comprime archivos de datos de /derive/output/- include-ens.js
- include-nf.js
- include-versions.js
- Siga las instrucciones en /validate/ para generar pruebas de validación
npm run validate- tests.json
npm run test— realizar pruebas de validaciónnpm run build— crear /dist/npm run rebuild— ejecutar todos los comandos anterioresnpm run order— crear un orden óptimo de grupos y reconstruir de nuevo
Seguridad
- Construcción y comparación con include-versions.js
spec_hash— SHA-256 de los bytes de spec.jsonens_hash_base64— SHA-256 del literal base64 de include-ens.jsnf_hash_base64— SHA-256 del literal base64 de include-nf.js