2021-07-16 17:22:04 +02:00
|
|
|
import base32Encode from 'base32-encode';
|
|
|
|
import base64 from 'base64-js';
|
2022-03-18 20:07:05 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
|
|
|
|
2021-07-16 17:22:04 +02:00
|
|
|
import getFetchWithTimeout from '../../../../shared/modules/fetch-with-timeout';
|
2021-02-04 19:15:23 +01:00
|
|
|
import resolveEnsToIpfsContentId from './resolver';
|
2018-10-21 11:48:15 +02:00
|
|
|
|
2022-07-19 18:13:45 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout();
|
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,
|
2023-07-28 18:21:43 +02:00
|
|
|
getUseAddressBarEnsResolution,
|
2020-11-03 00:41:28 +01:00
|
|
|
}) {
|
2018-10-21 11:48:15 +02:00
|
|
|
// install listener
|
2021-02-04 19:15:23 +01:00
|
|
|
const urlPatterns = supportedTopLevelDomains.map((tld) => `*://*.${tld}/*`);
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.webRequest.onErrorOccurred.addListener(webRequestDidFail, {
|
2020-11-03 00:41:28 +01:00
|
|
|
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() {
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.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
|
2023-07-28 18:21:43 +02:00
|
|
|
if (
|
|
|
|
(tabId === -1 || getCurrentChainId() !== '0x1') &&
|
|
|
|
// E2E tests use a chain other than 0x1, so for testing,
|
|
|
|
// allow the reuqest to pass through
|
|
|
|
!process.env.IN_TEST
|
|
|
|
) {
|
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();
|
2023-07-28 18:21:43 +02:00
|
|
|
const useAddressBarEnsResolution = getUseAddressBarEnsResolution();
|
|
|
|
|
|
|
|
if (!useAddressBarEnsResolution || ipfsGateway === '') {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-14 00:41:10 +01:00
|
|
|
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.tabs.update(tabId, { url: `loading.html` });
|
2023-07-28 18:21:43 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let url = `https://app.ens.domains/name/${name}`;
|
2023-07-28 18:21:43 +02:00
|
|
|
|
|
|
|
// If we're testing ENS domain resolution support,
|
|
|
|
// we assume the ENS domains URL
|
|
|
|
if (process.env.IN_TEST) {
|
|
|
|
browser.tabs.update(tabId, { url });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-12-02 00:46:26 +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
|
|
|
}`;
|
2021-07-16 17:22:04 +02:00
|
|
|
} else if (type === 'skynet-ns') {
|
|
|
|
const padded = hash.padEnd(hash.length + 4 - (hash.length % 4), '=');
|
|
|
|
const decoded = base64.toByteArray(padded);
|
|
|
|
|
|
|
|
const options = { padding: false };
|
|
|
|
const base32EncodedSkylink = base32Encode(
|
|
|
|
decoded,
|
|
|
|
'RFC4648-HEX',
|
|
|
|
options,
|
|
|
|
).toLowerCase();
|
|
|
|
url = `https://${base32EncodedSkylink}.siasky.net${pathname}${
|
|
|
|
search || ''
|
|
|
|
}${fragment || ''}`;
|
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 {
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.tabs.update(tabId, { url });
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|