Skip to content

Commit

Permalink
namehash
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Oct 4, 2023
1 parent e7d98d6 commit 19d862e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@hazae41/base64url": "^1.0.4",
"@hazae41/berith": "^1.2.4",
"@hazae41/binary": "^1.3.1",
"@hazae41/box": "^1.0.3",
"@hazae41/bytes": "^1.2.2",
"@hazae41/cadenas": "^0.2.42",
"@hazae41/cascade": "^1.1.27",
Expand Down Expand Up @@ -65,6 +66,7 @@
"@scure/bip32": "^1.3.2",
"@scure/bip39": "^1.2.1",
"ethers": "^6.7.1",
"idna-uts46-hx": "^5.1.0",
"next": "13.5.4",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
42 changes: 42 additions & 0 deletions src/libs/ethereum/mods/ens/ens.ts
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
}

}

0 comments on commit 19d862e

Please sign in to comment.