ens-normalize.js
0 依赖 以太坊域名服务(ENS)名称标准化工具。npm i @adraffy/ens-normalize ✓
- 🏛️ 遵循 ENSIP-15:ENS 名称标准化规范
- Unicode:
17.0.0• CLDR:47 - 其他实现:
- 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
- 早期实现:
- Javascript — ensdomains/eth-ens-namehash
- ENSIP-1 解析报告
- ✅️ 通过 100% ENSIP-15 验证测试
- ✅️ 通过 100% Unicode 标准化测试
- 压缩文件大小:
29KB— 原生 NFC 通过 nf-native.js 使用String.normalize()⚠️38KB默认 — 自定义 NFC 通过 nf.js
44KB 全部功能!* — 自定义 NFC + 子库:parts.js, utils.js
- 包含应用:
- 解析器演示 ⭐
- 支持的表情符号
- 字符查看器
- 混淆说明
- 相关项目:
- 近期 .eth 注册 • 续费 • 过期
- 标签数据库 • Labelhash⁻¹ • 暴力破解
- 表情符号频率探查器
- adraffy/punycode.js • Punycode 编码器
- adraffy/keccak.js • Keccak 哈希器
- adraffy/emoji.js • 表情符号解析器
- 更多链接见 解析器演示 底部
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
用完全限定的表情符号格式化名称:// 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)
规范化名称片段以进行子字符串搜索:
// 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
基于输入的分词:// 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)
基于输出的分词:// 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')
// }
// ]
生成一个已排序的(美化过的)支持的表情符号代码点数组:// () -> number[][]
let emojis = ens_emoji();
// [
// [ 2764 ],
// [ 128169, 65039 ],
// [ 128105, 127997, 8205, 9877, 65039 ],
// ...
// ]
判断一个字符是否不应直接打印:// number -> bool
should_escape(0x202E); // eg. RIGHT-TO-LEFT OVERRIDE => true
确定字符是否为组合标记:// number -> bool
is_combining_mark(0x20E3); // eg. COMBINING ENCLOSING KEYCAP => true将代码点格式化为打印安全字符串:
// number[] -> string
safe_str_from_cps([0x300, 0, 32, 97]); // "◌̀{00} a"
safe_str_from_cps(Array(100).fill(97), 4); // "aa…aa" => middle-truncated构建
- 使用
git clone克隆此仓库,然后运行npm install - 按照 /derive/ 中的说明生成数据文件
- 运行
npm run derive - spec.json
- nf.json
- nf-tests.json
- 运行
npm run make— 压缩来自 /derive/output/ 的数据文件 - include-ens.js
- include-nf.js
- include-versions.js
- 按照 /validate/ 中的说明生成验证测试
- 运行
npm run validate - tests.json
- 运行
npm run test— 执行验证测试 - 运行
npm run build— 创建 /dist/ - 运行
npm run rebuild— 运行上述所有命令 - 运行
npm run order— 创建最优组顺序并重新构建
安全
- 构建 并与 include-versions.js 进行比较
spec_hash— spec.json 字节的 SHA-256ens_hash_base64— include-ens.js 的 base64 字面量的 SHA-256nf_hash_base64— include-nf.js 的 base64 字面量的 SHA-256