Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jannis
Last active September 25, 2019 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jannis/3fc8ddb5b95f63ab27fc5ab336d46166 to your computer and use it in GitHub Desktop.
Save Jannis/3fc8ddb5b95f63ab27fc5ab336d46166 to your computer and use it in GitHub Desktop.
const { utils } = require('ethers')
const fetch = require('cross-fetch')
const name = 'coin-mountain.argent.xyz'
const namehash = name =>
name
.split('.')
.reverse()
.reduce((parentHash, label) => {
console.log('parent hash:', utils.hexlify(parentHash))
console.log('label:', label)
return utils.solidityKeccak256(
['bytes', 'bytes'],
[parentHash, utils.solidityKeccak256(['string'], [label])]
)
}, utils.formatBytes32String(''))
const run = async () => {
let response = await fetch('https://api.thegraph.com/subgraphs/name/ensdomains/ens', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
query:
'query domain($id: ID!) { domain(id: $id) { id labelhash name parent { id labelhash name parent { id labelhash name } } } }',
variables: { id: namehash(name) },
}),
})
let data = await response.json()
console.log(JSON.stringify(data, undefined, 2))
}
run()
@Jannis
Copy link
Author

Jannis commented Sep 25, 2019

Output:

➜  namehash node index.js
parent hash: 0x0000000000000000000000000000000000000000000000000000000000000000
label: xyz
parent hash: 0xa87a11c7f15e38a7398517fda2ae1b40d870aa24b34b4b09aa09afc71f2c9d26
label: argent
parent hash: 0xf0b914d803bfbcc81715a4b6f6abb05dd0e6b106f3574a8c36ef7dce598567a4
label: coin-mountain
{
  "data": {
    "domain": {
      "id": "0x3bc734acade0984a0ca64f7d47edc4a7af0cd5148cdd993a1d73a693076bb6f1",
      "labelhash": "0x37f764f1969c3e5ac9e78d38a77f7ec3c70628d5c5013b8a3f42272851314bf3",
      "name": "[37f764f1969c3e5ac9e78d38a77f7ec3c70628d5c5013b8a3f42272851314bf3].argent.xyz",
      "parent": {
        "id": "0xf0b914d803bfbcc81715a4b6f6abb05dd0e6b106f3574a8c36ef7dce598567a4",
        "labelhash": "0xa866f2769f724b98535c1247d03357be7b732e79a1d05367de3bb81af69b245e",
        "name": "argent.xyz",
        "parent": {
          "id": "0xa87a11c7f15e38a7398517fda2ae1b40d870aa24b34b4b09aa09afc71f2c9d26",
          "labelhash": "0x9dd2c369a187b4e6b9c402f030e50743e619301ea62aa4c0737d4ef7e10a3d49",
          "name": "xyz"
        }
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment