2020-01-09 04:34:58 +01:00
|
|
|
import extension from 'extensionizer'
|
2020-08-14 13:46:45 +02:00
|
|
|
import resolveEnsToIpfsContentId from './resolver'
|
2018-10-21 11:48:15 +02:00
|
|
|
|
|
|
|
const supportedTopLevelDomains = ['eth']
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function setupEnsIpfsResolver({
|
|
|
|
provider,
|
|
|
|
getCurrentNetwork,
|
|
|
|
getIpfsGateway,
|
|
|
|
}) {
|
2018-10-21 11:48:15 +02:00
|
|
|
// install listener
|
2020-02-15 21:34:12 +01:00
|
|
|
const urlPatterns = supportedTopLevelDomains.map((tld) => `*://*.${tld}/*`)
|
2020-11-03 00:41:28 +01:00
|
|
|
extension.webRequest.onErrorOccurred.addListener(webRequestDidFail, {
|
|
|
|
urls: urlPatterns,
|
|
|
|
types: ['main_frame'],
|
|
|
|
})
|
2018-10-21 11:48:15 +02:00
|
|
|
|
|
|
|
// return api object
|
|
|
|
return {
|
|
|
|
// uninstall listener
|
2020-11-03 00:41:28 +01:00
|
|
|
remove() {
|
2018-10-21 11:48:15 +02:00
|
|
|
extension.webRequest.onErrorOccurred.removeListener(webRequestDidFail)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function webRequestDidFail(details) {
|
2018-10-21 11:48:15 +02:00
|
|
|
const { tabId, url } = details
|
|
|
|
// ignore requests that are not associated with tabs
|
2020-01-30 01:57:14 +01:00
|
|
|
// only attempt ENS resolution on mainnet
|
|
|
|
if (tabId === -1 || getCurrentNetwork() !== '1') {
|
2019-11-20 01:03:20 +01:00
|
|
|
return
|
|
|
|
}
|
2018-10-21 11:48:15 +02:00
|
|
|
// parse ens name
|
2020-10-22 16:03:45 +02:00
|
|
|
const { hostname: name, pathname, search, hash: fragment } = new URL(url)
|
2018-10-21 11:48:15 +02:00
|
|
|
const domainParts = name.split('.')
|
|
|
|
const topLevelDomain = domainParts[domainParts.length - 1]
|
|
|
|
// if unsupported TLD, abort
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!supportedTopLevelDomains.includes(topLevelDomain)) {
|
|
|
|
return
|
|
|
|
}
|
2018-10-21 11:48:15 +02:00
|
|
|
// otherwise attempt resolve
|
2020-10-22 15:15:26 +02:00
|
|
|
attemptResolve({ tabId, name, pathname, search, fragment })
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function attemptResolve({ tabId, name, pathname, search, fragment }) {
|
2019-12-12 20:28:07 +01:00
|
|
|
const ipfsGateway = getIpfsGateway()
|
2018-10-21 11:48:15 +02:00
|
|
|
extension.tabs.update(tabId, { url: `loading.html` })
|
2019-12-02 14:56:02 +01:00
|
|
|
let url = `https://app.ens.domains/name/${name}`
|
2018-10-21 11:48:15 +02:00
|
|
|
try {
|
2019-12-03 21:50:55 +01:00
|
|
|
const { type, hash } = await resolveEnsToIpfsContentId({ provider, name })
|
2020-05-14 12:26:27 +02:00
|
|
|
if (type === 'ipfs-ns' || type === 'ipns-ns') {
|
2020-11-03 00:41:28 +01:00
|
|
|
const resolvedUrl = `https://${hash}.${type.slice(
|
|
|
|
0,
|
|
|
|
4,
|
|
|
|
)}.${ipfsGateway}${pathname}${search || ''}${fragment || ''}`
|
2019-04-04 17:15:57 +02:00
|
|
|
try {
|
|
|
|
// check if ipfs gateway has result
|
2020-04-15 19:23:27 +02:00
|
|
|
const response = await window.fetch(resolvedUrl, { method: 'HEAD' })
|
2019-11-20 01:03:20 +01:00
|
|
|
if (response.status === 200) {
|
|
|
|
url = resolvedUrl
|
|
|
|
}
|
2019-04-04 17:15:57 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.warn(err)
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
2019-04-04 17:15:57 +02:00
|
|
|
} else if (type === 'swarm-ns') {
|
2020-11-03 00:41:28 +01:00
|
|
|
url = `https://swarm-gateways.net/bzz:/${hash}${pathname}${
|
|
|
|
search || ''
|
|
|
|
}${fragment || ''}`
|
2019-07-23 21:29:20 +02:00
|
|
|
} else if (type === 'onion' || type === 'onion3') {
|
2020-10-22 15:15:26 +02:00
|
|
|
url = `http://${hash}.onion${pathname}${search || ''}${fragment || ''}`
|
2019-10-31 19:37:06 +01:00
|
|
|
} else if (type === 'zeronet') {
|
2020-11-03 00:41:28 +01:00
|
|
|
url = `http://127.0.0.1:43110/${hash}${pathname}${search || ''}${
|
|
|
|
fragment || ''
|
|
|
|
}`
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err)
|
2019-05-04 18:57:19 +02:00
|
|
|
} finally {
|
|
|
|
extension.tabs.update(tabId, { url })
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|