-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
145 lines (121 loc) · 3.68 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// External Deps
const Eth = require('ethjs-query')
const EthContract = require('ethjs-contract')
const namehash = require('eth-ens-namehash')
// ABIs
const registryAbi = require('./abis/registry.json')
const resolverAbi = require('./abis/resolver.json')
// Map network to known ENS registries
const networkMap = require('ethereum-ens-network-map')
const emptyHash = '0x0000000000000000000000000000000000000000000000000000000000000000'
const emptyAddr = '0x0000000000000000000000000000000000000000'
const NotFoundError = new Error('ENS name not defined.')
const BadCharacterError = new Error('Illegal Character for ENS.')
class Ens {
constructor (opts = {}) {
const { provider, network } = opts
let { registryAddress } = opts
// Validations
if (!provider) {
throw new Error('The EthJsENS Constructor requires a provider.')
}
// Requires EITHER a network or a registryAddress
if (!network && !registryAddress) {
throw new Error('The EthJsENS Constructor requires a network or registry address.')
}
this.provider = provider
this.eth = new Eth(this.provider)
this.contract = new EthContract(this.eth)
this.namehash = namehash
// Link to Registry
this.Registry = this.contract(registryAbi)
if (!registryAddress && network) {
registryAddress = networkMap[network]
}
this.registry = this.Registry.at(registryAddress)
// Create Resolver class
this.Resolver = this.contract(resolverAbi)
}
lookup (name = '') {
return this.getNamehash(name)
.then((node) => {
if (node === emptyHash) {
return Promise.reject(NotFoundError)
}
return this.resolveAddressForNode(node)
})
}
getNamehash (name) {
try {
return Promise.resolve(namehash(name))
} catch (e) {
return Promise.reject(BadCharacterError)
}
}
getOwner (name = '') {
return this.getNamehash(name)
.then(node => this.getOwnerForNode(node))
}
getOwnerForNode (node) {
if (node === emptyHash) {
return Promise.reject(NotFoundError)
}
return this.registry.owner(node)
.then((result) => {
const ownerAddress = result[0]
if (ownerAddress === emptyAddr) {
throw NotFoundError
}
return ownerAddress
})
}
getResolver (name = '') {
return this.getNamehash(name)
.then(node => this.getResolverForNode(node))
}
getResolverAddress (name = '') {
return this.getNamehash(name)
.then(node => this.getResolverAddressForNode(node))
}
getResolverForNode (node) {
if (!node.startsWith('0x')) {
node = `0x${node}`
}
return this.getResolverAddressForNode(node)
.then((resolverAddress) => {
return this.Resolver.at(resolverAddress)
})
}
getResolverAddressForNode (node) {
return this.registry.resolver(node)
.then((result) => {
const resolverAddress = result[0]
if (resolverAddress === emptyAddr) {
throw NotFoundError
}
return resolverAddress
})
}
resolveAddressForNode (node) {
return this.getResolverForNode(node)
.then((resolver) => {
return resolver.addr(node)
})
.then(result => result[0])
}
reverse (address) {
if (!address) {
return Promise.reject(new Error('Must supply an address to reverse lookup.'))
}
if (address.startsWith('0x')) {
address = address.slice(2)
}
const name = `${address.toLowerCase()}.addr.reverse`
const node = namehash(name)
return this.getNamehash(name)
.then(node => this.getResolverForNode(node))
.then(resolver => resolver.name(node))
.then(results => results[0])
}
}
module.exports = Ens