2020-10-22 16:03:45 +02:00
|
|
|
import punycode from 'punycode/punycode'
|
2020-01-09 04:34:58 +01:00
|
|
|
import ethUtil from 'ethereumjs-util'
|
2020-12-16 22:14:49 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store'
|
2020-01-09 04:34:58 +01:00
|
|
|
import log from 'loglevel'
|
|
|
|
import Ens from './ens'
|
2019-11-01 18:54:00 +01:00
|
|
|
|
|
|
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
|
|
|
const ZERO_X_ERROR_ADDRESS = '0x'
|
|
|
|
|
2020-05-06 00:19:38 +02:00
|
|
|
export default class EnsController {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor({ ens, provider, networkStore } = {}) {
|
2019-11-01 18:54:00 +01:00
|
|
|
const initState = {
|
|
|
|
ensResolutionsByAddress: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
this._ens = ens
|
|
|
|
if (!this._ens) {
|
|
|
|
const network = networkStore.getState()
|
|
|
|
if (Ens.getNetworkEnsSupport(network)) {
|
|
|
|
this._ens = new Ens({
|
|
|
|
network,
|
|
|
|
provider,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store = new ObservableStore(initState)
|
|
|
|
networkStore.subscribe((network) => {
|
|
|
|
this.store.putState(initState)
|
2019-11-11 03:15:50 +01:00
|
|
|
if (Ens.getNetworkEnsSupport(network)) {
|
|
|
|
this._ens = new Ens({
|
|
|
|
network,
|
|
|
|
provider,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
delete this._ens
|
|
|
|
}
|
2019-11-01 18:54:00 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
reverseResolveAddress(address) {
|
2019-11-01 18:54:00 +01:00
|
|
|
return this._reverseResolveAddress(ethUtil.toChecksumAddress(address))
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async _reverseResolveAddress(address) {
|
2019-11-01 18:54:00 +01:00
|
|
|
if (!this._ens) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = this.store.getState()
|
|
|
|
if (state.ensResolutionsByAddress[address]) {
|
|
|
|
return state.ensResolutionsByAddress[address]
|
|
|
|
}
|
|
|
|
|
2019-11-11 03:15:59 +01:00
|
|
|
let domain
|
|
|
|
try {
|
|
|
|
domain = await this._ens.reverse(address)
|
|
|
|
} catch (error) {
|
|
|
|
log.debug(error)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
let registeredAddress
|
|
|
|
try {
|
|
|
|
registeredAddress = await this._ens.lookup(domain)
|
|
|
|
} catch (error) {
|
|
|
|
log.debug(error)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
registeredAddress === ZERO_ADDRESS ||
|
|
|
|
registeredAddress === ZERO_X_ERROR_ADDRESS
|
|
|
|
) {
|
2019-11-01 18:54:00 +01:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ethUtil.toChecksumAddress(registeredAddress) !== address) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateResolutionsByAddress(address, punycode.toASCII(domain))
|
|
|
|
return domain
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_updateResolutionsByAddress(address, domain) {
|
2019-11-01 18:54:00 +01:00
|
|
|
const oldState = this.store.getState()
|
|
|
|
this.store.putState({
|
|
|
|
ensResolutionsByAddress: {
|
|
|
|
...oldState.ensResolutionsByAddress,
|
|
|
|
[address]: domain,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|