2021-02-04 19:15:23 +01:00
|
|
|
import extension from 'extensionizer';
|
|
|
|
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout';
|
|
|
|
import resolveEnsToIpfsContentId from './resolver';
|
2018-10-21 11:48:15 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(30000);
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const supportedTopLevelDomains = ['eth'];
|
2018-10-21 11:48:15 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function setupEnsIpfsResolver({
|
|
|
|
provider,
|
2021-02-25 12:40:57 +01:00
|
|
|
getCurrentChainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
getIpfsGateway,
|
|
|
|
}) {
|
2018-10-21 11:48:15 +02:00
|
|
|
// install listener
|
2021-02-04 19:15:23 +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'],
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-10-21 11:48:15 +02:00
|
|
|
|
|
|
|
// return api object
|
|
|
|
return {
|
|
|
|
// uninstall listener
|
2020-11-03 00:41:28 +01:00
|
|
|
remove() {
|
2021-02-04 19:15:23 +01:00
|
|
|
extension.webRequest.onErrorOccurred.removeListener(webRequestDidFail);
|
2018-10-21 11:48:15 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-10-21 11:48:15 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function webRequestDidFail(details) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { tabId, url } = details;
|
2018-10-21 11:48:15 +02:00
|
|
|
// ignore requests that are not associated with tabs
|
2020-01-30 01:57:14 +01:00
|
|
|
// only attempt ENS resolution on mainnet
|
2021-02-25 12:40:57 +01:00
|
|
|
if (tabId === -1 || getCurrentChainId() !== '0x1') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-10-21 11:48:15 +02:00
|
|
|
// parse ens name
|
2021-02-04 19:15:23 +01:00
|
|
|
const { hostname: name, pathname, search, hash: fragment } = new URL(url);
|
|
|
|
const domainParts = name.split('.');
|
|
|
|
const topLevelDomain = domainParts[domainParts.length - 1];
|
2018-10-21 11:48:15 +02:00
|
|
|
// if unsupported TLD, abort
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!supportedTopLevelDomains.includes(topLevelDomain)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-10-21 11:48:15 +02:00
|
|
|
// otherwise attempt resolve
|
2021-02-04 19:15:23 +01: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 }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const ipfsGateway = getIpfsGateway();
|
|
|
|
extension.tabs.update(tabId, { url: `loading.html` });
|
|
|
|
let url = `https://app.ens.domains/name/${name}`;
|
2018-10-21 11:48:15 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +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,
|
2021-02-04 19:15:23 +01:00
|
|
|
)}.${ipfsGateway}${pathname}${search || ''}${fragment || ''}`;
|
2019-04-04 17:15:57 +02:00
|
|
|
try {
|
|
|
|
// check if ipfs gateway has result
|
2021-01-19 17:41:57 +01:00
|
|
|
const response = await fetchWithTimeout(resolvedUrl, {
|
|
|
|
method: 'HEAD',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-11-20 01:03:20 +01:00
|
|
|
if (response.status === 200) {
|
2021-02-04 19:15:23 +01:00
|
|
|
url = resolvedUrl;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2019-04-04 17:15:57 +02:00
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
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 || ''
|
2021-02-04 19:15:23 +01:00
|
|
|
}${fragment || ''}`;
|
2019-07-23 21:29:20 +02:00
|
|
|
} else if (type === 'onion' || type === 'onion3') {
|
2021-02-04 19:15:23 +01: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 || ''
|
2021-02-04 19:15:23 +01:00
|
|
|
}`;
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.warn(err);
|
2019-05-04 18:57:19 +02:00
|
|
|
} finally {
|
2021-02-04 19:15:23 +01:00
|
|
|
extension.tabs.update(tabId, { url });
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|