Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace eth-ens-namehash with ens-normalize.js #37

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
node_modules
node_modules/
__sync/
*.tgz
7 changes: 0 additions & 7 deletions .npmignore

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Andrew Raffensperger

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
158 changes: 138 additions & 20 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,37 +1,155 @@
# Eth ENS Namehash (Fork)
# eth-ens-namehash

A javascript library for generating Ethereum Name Service (ENS) namehashes per [spec](https://github.com/ethereum/EIPs/issues/137).
[Ethereum Name Service](https://ens.domains/) (ENS) name normalizer and hasher.

[Available on NPM](https://www.npmjs.com/package/@ensdomains/eth-ens-namehash)
* Follows [ENSIP-15: Normalization Standard](https://docs.ens.domains/ens-improvement-proposals/ensip-15-normalization-standard)
* Derived from [@adraffy/ens-normalize.js](https://github.com/adraffy/ens-normalize.js)
* Passes [**100%**](https://adraffy.github.io/ens-normalize.js/test/validate.html) Validation Tests
* Passes [**100%**](https://adraffy.github.io/ens-normalize.js/test/report-nf.html) Unicode `15.0.0` Normalization Tests

## Installation

`npm install @ensdomains/eth-ens-namehash -S`
* [Available on NPM](https://www.npmjs.com/package/@ensdomains/eth-ens-namehash)
* Depends on [js-sha3](https://www.npmjs.com/package/js-sha3)

## Usage
```sh
npm install @ensdomains/eth-ens-namehash --save
```

## Usage Examples

```Javascript
import * as lib from '@ensdomains/eth-ens-namehash'; // ESM
// const lib = require("@ensdomains/eth-ens-namehash"); // CJS

```javascript
var namehash = require('@ensdomains/eth-ens-namehash')
var hash = namehash.hash('foo.eth')
// '0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f'
// *** 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 (:)

// Also supports normalizing strings to ENS compatibility:
var input = getUserInput()
var normalized = namehash.normalize(input)
// note: does not enforce .eth TLD 3-character minimum
```

## Security Warning
Normalize a name:
```Javascript
// string -> string
// throws if unnormalizable
// output ready for namehash
let input = '👨️‍💻nIcK.EtH';
let normalized = lib.normalize(input);
// "👨‍💻nick.eth"
```

Compute the hash of a name:
```Javascript
// string -> 0x-prefixed 64-char hex uint256
let node1 = lib.namehash(normalized); // same as hash(x)
// "0x6d3ff59a43ac0182b379d3213c30db92d385fbfc34cd77bf66012bf117445848"

// normhash(x) === namehash(normalize(x))
// throws if unnormalizable
let node2 = lib.normhash(input);
// "0x6d3ff59a43ac0182b379d3213c30db92d385fbfc34cd77bf66012bf117445848" (same)
// node1 === node2
```

ENS Supports UTF-8 characters, and so many duplicate names are possible. For example:
Compute the hash of a label:
```Javascript
// string -> 0x-prefixed 64-char hex uint256
let hash = lib.labelhash("abc");
// "0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45"
```

- faceboоk.eth
- facebook.eth
Format names with fully-qualified emoji:
```JavaScript
// works like normalize()
// output ready for display
let pretty = lib.beautify('1⃣2⃣.eth');
// "1️⃣2️⃣.eth"
// note: normalization is unchanged:
// normalize(beautify(x)) == normalize(x)
```

The first one has non-ascii chars. (control+F on this page and search for facebook, only the second one will match).
Input-based tokenization:
```Javascript
// string -> Token[]
// never throws
let tokens = lib.tokenize('_R💩\u{FE0F}a\u{FE0F}\u{304}\u{AD}./');
// [
// { type: 'valid', cp: [ 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)
// tokens0: [ // tokens (before nfc)
// { type: 'valid', cps: [ 97 ] },
// { type: 'ignored', cp: 65039 },
// { type: 'valid', cps: [ 772 ] }
// ],
// cps: [ 257 ], // output (after nfc)
// tokens: [ // tokens (after nfc)
// { type: 'valid', cps: [ 257 ] }
// ]
// },
// { type: 'ignored', cp: 173 },
// { type: 'stop', cp: 46 },
// { type: 'disallowed', cp: 47 }
// ]
```

namehash.normalize() doesn't automagically remap those, and so other precautions should be taken to avoid user phishing.
Output-based tokenization:
```Javascript
// string -> Label[]
// never throws
let labels = lib.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')
// }
// ]
```

## Development
Generate a sorted array of supported emoji codepoints:
```Javascript
// () -> number[][]
console.log(lib.allEmoji());
// [
// [ 2764 ],
// [ 128169, 65039 ],
// [ 128105, 127997, 8205, 9877, 65039 ],
// ...
// ]
```

This module supports advanced JavaScript syntax, but exports an ES5-compatible module. To re-build the exported module after making changes, run `npm run bundle` (must have [browserify](http://browserify.org/) installed).
## Synchronize with `@adraffy/ens-normalize.js`

1. `chmod +x sync.sh`
2. `npm run sync`
3. `npm run test`
4 changes: 0 additions & 4 deletions circle.yml

This file was deleted.

Loading