-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Base16 } from "@hazae41/base16" | ||
import { Box } from "@hazae41/box" | ||
import { Bytes } from "@hazae41/bytes" | ||
import { ZeroHexString } from "@hazae41/cubane" | ||
import { Copiable, Copied, Keccak256 } from "@hazae41/keccak256" | ||
import * as Uts46 from "idna-uts46-hx" | ||
|
||
export class Slot<T extends Disposable> implements Disposable { | ||
|
||
constructor( | ||
public inner: T | ||
) { } | ||
|
||
[Symbol.dispose]() { | ||
this.inner[Symbol.dispose]() | ||
} | ||
|
||
} | ||
|
||
export namespace Ens { | ||
|
||
export function namehash(name: string): ZeroHexString { | ||
if (name.length === 0) | ||
return "0x".padEnd(2 + 32, "0") as ZeroHexString | ||
|
||
const uts46 = Uts46.toUnicode(name, { useStd3ASCII: true }) | ||
const labels = uts46.split('.').reverse() | ||
|
||
using node: Slot<Box<Copiable>> = new Slot(new Box(new Copied(new Uint8Array(32)))) | ||
|
||
for (const label of labels) { | ||
using previous = node.inner.unwrap() | ||
using hashed = Keccak256.get().tryHash(Bytes.fromUtf8(label)).unwrap() | ||
|
||
const concat = Bytes.concat([previous.bytes, hashed.bytes]) | ||
node.inner = new Box(Keccak256.get().tryHash(concat).unwrap()) | ||
} | ||
|
||
return "0x" + Base16.get().tryEncode(node.inner.unwrap().bytes).unwrap() as ZeroHexString | ||
} | ||
|
||
} |