forked from Uniswap/widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolveENSContentHash.ts
67 lines (63 loc) · 1.67 KB
/
resolveENSContentHash.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Provider } from '@ethersproject/abstract-provider'
import { Contract } from '@ethersproject/contracts'
import { namehash } from '@ethersproject/hash'
const REGISTRAR_ABI = [
{
constant: true,
inputs: [
{
name: 'node',
type: 'bytes32',
},
],
name: 'resolver',
outputs: [
{
name: 'resolverAddress',
type: 'address',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
]
const REGISTRAR_ADDRESS = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e'
const RESOLVER_ABI = [
{
constant: true,
inputs: [
{
internalType: 'bytes32',
name: 'node',
type: 'bytes32',
},
],
name: 'contenthash',
outputs: [
{
internalType: 'bytes',
name: '',
type: 'bytes',
},
],
payable: false,
stateMutability: 'view',
type: 'function',
},
]
// cache the resolver contracts since most of them are the public resolver
function resolverContract(resolverAddress: string, provider: Provider): Contract {
return new Contract(resolverAddress, RESOLVER_ABI, provider)
}
/**
* Fetches and decodes the result of an ENS contenthash lookup on mainnet to a URI
* @param ensName to resolve
* @param provider provider to use to fetch the data
*/
export default async function resolveENSContentHash(ensName: string, provider: Provider): Promise<string> {
const ensRegistrarContract = new Contract(REGISTRAR_ADDRESS, REGISTRAR_ABI, provider)
const hash = namehash(ensName)
const resolverAddress = await ensRegistrarContract.resolver(hash)
return resolverContract(resolverAddress, provider).contenthash(hash)
}