1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 04:46:10 +01:00
metamask-extension/app/scripts/lib/resolver.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-05-04 13:57:19 +02:00
const namehash = require('eth-ens-namehash')
const multihash = require('multihashes')
const HttpProvider = require('ethjs-provider-http')
const Eth = require('ethjs-query')
const EthContract = require('ethjs-contract')
const registrarAbi = require('./contracts/registrar')
const resolverAbi = require('./contracts/resolver')
2018-05-24 18:08:02 +02:00
function ens (name, provider) {
2018-05-29 13:29:12 +02:00
const eth = new Eth(new HttpProvider(getProvider(provider.type)))
2018-05-24 18:08:02 +02:00
const hash = namehash.hash(name)
const contract = new EthContract(eth)
2018-05-29 13:29:12 +02:00
const Registrar = contract(registrarAbi).at(getRegistrar(provider.type))
2018-05-04 13:57:19 +02:00
return new Promise((resolve, reject) => {
2018-05-29 13:29:12 +02:00
if (provider.type === 'mainnet' || provider.type === 'ropsten') {
Registrar.resolver(hash).then((address) => {
if (address === '0x0000000000000000000000000000000000000000') {
reject(null)
} else {
const Resolver = contract(resolverAbi).at(address['0'])
return Resolver.content(hash)
}
}).then((contentHash) => {
if (contentHash['0'] === '0x0000000000000000000000000000000000000000000000000000000000000000') reject(null)
if (contentHash.ret !== '0x') {
const hex = contentHash['0'].substring(2)
const buf = multihash.fromHexString(hex)
resolve(multihash.toB58String(multihash.encode(buf, 'sha2-256')))
} else {
reject(null)
}
})
} else {
return reject('unsupport')
}
2018-05-04 13:57:19 +02:00
})
}
2018-05-24 18:08:02 +02:00
2018-05-29 13:29:12 +02:00
function getProvider (type) {
switch (type) {
case 'mainnet':
return 'https://mainnet.infura.io/'
case 'ropsten':
return 'https://ropsten.infura.io/'
default:
2018-05-30 03:57:31 +02:00
return 'http://localhost:8545/'
2018-05-29 13:29:12 +02:00
}
}
function getRegistrar (type) {
switch (type) {
case 'mainnet':
return '0x314159265dd8dbb310642f98f50c066173c1259b'
case 'ropsten':
return '0x112234455c3a32fd11230c42e7bccd4a84e02010'
default:
return '0x0000000000000000000000000000000000000000'
}
}
2018-05-04 13:57:19 +02:00
module.exports.resolve = function (name, provider) {
2018-05-24 18:08:02 +02:00
const path = name.split('.')
2018-07-16 15:05:08 +02:00
const topLevelDomain = path[path.length - 1]
if (topLevelDomain === 'eth' || topLevelDomain === 'test') {
2018-05-24 18:08:02 +02:00
return ens(name, provider)
2018-05-04 13:57:19 +02:00
} else {
return new Promise((resolve, reject) => {
reject(null)
})
}
}