A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/ens-ipfs/setup.js

73 lines
2.5 KiB

import urlUtil from 'url'
import extension from 'extensionizer'
import resolveEnsToIpfsContentId from './resolver.js'
const supportedTopLevelDomains = ['eth']
export default setupEnsIpfsResolver
function setupEnsIpfsResolver ({ provider, getIpfsGateway }) {
// install listener
const urlPatterns = supportedTopLevelDomains.map(tld => `*://*.${tld}/*`)
5 years ago
extension.webRequest.onErrorOccurred.addListener(webRequestDidFail, { urls: urlPatterns, types: ['main_frame'] })
// return api object
return {
// uninstall listener
remove () {
extension.webRequest.onErrorOccurred.removeListener(webRequestDidFail)
},
}
async function webRequestDidFail (details) {
const { tabId, url } = details
// ignore requests that are not associated with tabs
if (tabId === -1) {
return
}
// parse ens name
const urlData = urlUtil.parse(url)
const { hostname: name, path, search, hash: fragment } = urlData
const domainParts = name.split('.')
const topLevelDomain = domainParts[domainParts.length - 1]
// if unsupported TLD, abort
if (!supportedTopLevelDomains.includes(topLevelDomain)) {
return
}
// otherwise attempt resolve
attemptResolve({ tabId, name, path, search, fragment })
}
async function attemptResolve ({ tabId, name, path, search, fragment }) {
const ipfsGateway = getIpfsGateway()
extension.tabs.update(tabId, { url: `loading.html` })
let url = `https://app.ens.domains/name/${name}`
try {
5 years ago
const { type, hash } = await resolveEnsToIpfsContentId({ provider, name })
6 years ago
if (type === 'ipfs-ns') {
const resolvedUrl = `https://${hash}.${ipfsGateway}${path}${search || ''}${fragment || ''}`
6 years ago
try {
// check if ipfs gateway has result
const response = await fetch(resolvedUrl, { method: 'HEAD' })
if (response.status === 200) {
url = resolvedUrl
}
6 years ago
} catch (err) {
console.warn(err)
}
6 years ago
} else if (type === 'swarm-ns') {
url = `https://swarm-gateways.net/bzz:/${hash}${path}${search || ''}${fragment || ''}`
} else if (type === 'onion' || type === 'onion3') {
url = `http://${hash}.onion${path}${search || ''}${fragment || ''}`
} else if (type === 'zeronet') {
url = `http://127.0.0.1:43110/${hash}${path}${search || ''}${fragment || ''}`
}
} catch (err) {
console.warn(err)
} finally {
extension.tabs.update(tabId, { url })
}
}
}