Skip to content

Commit

Permalink
Merge pull request #6402 from pldespaigne/issue#5742_update_ens_resol…
Browse files Browse the repository at this point in the history
…ver_to_eip_1577

✔️ Issue #5742 update ens resolver to eip 1577
  • Loading branch information
tmashuang authored May 30, 2019
2 parents 681f3f6 + 9a658ee commit d837703
Show file tree
Hide file tree
Showing 10 changed files with 9,056 additions and 8,340 deletions.
52 changes: 0 additions & 52 deletions app/404.html

This file was deleted.

79 changes: 0 additions & 79 deletions app/error.html

This file was deleted.

1 change: 1 addition & 0 deletions app/images/enslogo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 4 additions & 9 deletions app/loading.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 256px;
text-align: center;
}
#logo {
width: 100%;
width: 256px;
animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
Expand All @@ -33,13 +33,8 @@
</head>
<body>
<div id="div-logo">
<img id="logo" src="./images/loginglogo.svg">
<img id="logo" src="./images/enslogo.svg">
<h1 class="center">MetaMask is querying ENS ...</h1>
</div>
<script type="text/javascript">
// redirect to 404 after one minute
setTimeout(() => {
location.href = './404.html'
}, 60000)
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion app/scripts/lib/ens-ipfs/contracts/resolver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 34 additions & 18 deletions app/scripts/lib/ens-ipfs/resolver.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const namehash = require('eth-ens-namehash')
const multihash = require('multihashes')
const Eth = require('ethjs-query')
const EthContract = require('ethjs-contract')
const registrarAbi = require('./contracts/registrar')
const registryAbi = require('./contracts/registry')
const resolverAbi = require('./contracts/resolver')
const contentHash = require('content-hash')

module.exports = resolveEnsToIpfsContentId

Expand All @@ -12,43 +12,59 @@ async function resolveEnsToIpfsContentId ({ provider, name }) {
const eth = new Eth(provider)
const hash = namehash.hash(name)
const contract = new EthContract(eth)
// lookup registrar
// lookup registry
const chainId = Number.parseInt(await eth.net_version(), 10)
const registrarAddress = getRegistrarForChainId(chainId)
if (!registrarAddress) {
throw new Error(`EnsIpfsResolver - no known ens-ipfs registrar for chainId "${chainId}"`)
const registryAddress = getRegistryForChainId(chainId)
if (!registryAddress) {
throw new Error(`EnsIpfsResolver - no known ens-ipfs registry for chainId "${chainId}"`)
}
const Registrar = contract(registrarAbi).at(registrarAddress)
const Registry = contract(registryAbi).at(registryAddress)
// lookup resolver
const resolverLookupResult = await Registrar.resolver(hash)
const resolverLookupResult = await Registry.resolver(hash)
const resolverAddress = resolverLookupResult[0]
if (hexValueIsEmpty(resolverAddress)) {
throw new Error(`EnsIpfsResolver - no resolver found for name "${name}"`)
}
const Resolver = contract(resolverAbi).at(resolverAddress)
// lookup content id
const contentLookupResult = await Resolver.content(hash)
const contentHash = contentLookupResult[0]
if (hexValueIsEmpty(contentHash)) {
throw new Error(`EnsIpfsResolver - no content ID found for name "${name}"`)

const isEIP1577Compliant = await Resolver.supportsInterface('0xbc1c58d1')
const isLegacyResolver = await Resolver.supportsInterface('0xd8389dc5')
if (isEIP1577Compliant[0]) {
const contentLookupResult = await Resolver.contenthash(hash)
const rawContentHash = contentLookupResult[0]
const decodedContentHash = contentHash.decode(rawContentHash)
const type = contentHash.getCodec(rawContentHash)
return {type: type, hash: decodedContentHash}
}
if (isLegacyResolver[0]) {
// lookup content id
const contentLookupResult = await Resolver.content(hash)
const content = contentLookupResult[0]
if (hexValueIsEmpty(content)) {
throw new Error(`EnsIpfsResolver - no content ID found for name "${name}"`)
}
return {type: 'swarm-ns', hash: content.slice(2)}
}
const nonPrefixedHex = contentHash.slice(2)
const buffer = multihash.fromHexString(nonPrefixedHex)
const contentId = multihash.toB58String(multihash.encode(buffer, 'sha2-256'))
return contentId
throw new Error(`EnsIpfsResolver - the resolver for name "${name}" is not standard, it should either supports contenthash() or content()`)
}

function hexValueIsEmpty (value) {
return [undefined, null, '0x', '0x0', '0x0000000000000000000000000000000000000000000000000000000000000000'].includes(value)
}

function getRegistrarForChainId (chainId) {
function getRegistryForChainId (chainId) {
switch (chainId) {
// mainnet
case 1:
return '0x314159265dd8dbb310642f98f50c066173c1259b'
// ropsten
case 3:
return '0x112234455c3a32fd11230c42e7bccd4a84e02010'
// rinkeby
case 4:
return '0xe7410170f87102df0055eb195163a03b7f2bff4a'
// goerli
case 5:
return '0x112234455c3a32fd11230c42e7bccd4a84e02010'
}
}
30 changes: 14 additions & 16 deletions app/scripts/lib/ens-ipfs/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,25 @@ function setupEnsIpfsResolver ({ provider }) {

async function attemptResolve ({ tabId, name, path, search }) {
extension.tabs.update(tabId, { url: `loading.html` })
let url = `https://manager.ens.domains/name/${name}`
try {
const ipfsContentId = await resolveEnsToIpfsContentId({ provider, name })
const url = `https://gateway.ipfs.io/ipfs/${ipfsContentId}${path}${search || ''}`
try {
// check if ipfs gateway has result
const response = await fetch(url, { method: 'HEAD' })
// if failure, redirect to 404 page
if (response.status !== 200) {
extension.tabs.update(tabId, { url: '404.html' })
return
const {type, hash} = await resolveEnsToIpfsContentId({ provider, name })
if (type === 'ipfs-ns') {
const resolvedUrl = `https://gateway.ipfs.io/ipfs/${hash}${path}${search || ''}`
try {
// check if ipfs gateway has result
const response = await fetch(resolvedUrl, { method: 'HEAD' })
if (response.status === 200) url = resolvedUrl
} catch (err) {
console.warn(err)
}
// otherwise redirect to the correct page
extension.tabs.update(tabId, { url })
} catch (err) {
console.warn(err)
// if HEAD fetch failed, redirect so user can see relevant error page
extension.tabs.update(tabId, { url })
} else if (type === 'swarm-ns') {
url = `https://swarm-gateways.net/bzz:/${hash}${path}${search || ''}`
}
} catch (err) {
console.warn(err)
extension.tabs.update(tabId, { url: `error.html?name=${name}` })
} finally {
extension.tabs.update(tabId, { url })
}
}
}
Loading

0 comments on commit d837703

Please sign in to comment.