aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/ens-ipfs/setup.js
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2018-10-30 07:49:53 +0800
committerGitHub <noreply@github.com>2018-10-30 07:49:53 +0800
commitad009a4606249649fd54420972fb6a5dd5382fc2 (patch)
tree45f50fb1eca1ad7cb3773446a01ad4867283f365 /app/scripts/lib/ens-ipfs/setup.js
parentf0602ca354a6d352c13a79a1cba884f54e5b9a83 (diff)
parent18e530221b0f960907d45995c4703b04ed562dc8 (diff)
downloadtangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.tar
tangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.tar.gz
tangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.tar.bz2
tangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.tar.lz
tangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.tar.xz
tangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.tar.zst
tangerine-wallet-browser-ad009a4606249649fd54420972fb6a5dd5382fc2.zip
Merge branch 'develop' into sentry-enhancements2
Diffstat (limited to 'app/scripts/lib/ens-ipfs/setup.js')
-rw-r--r--app/scripts/lib/ens-ipfs/setup.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/scripts/lib/ens-ipfs/setup.js b/app/scripts/lib/ens-ipfs/setup.js
new file mode 100644
index 000000000..45eb1ce14
--- /dev/null
+++ b/app/scripts/lib/ens-ipfs/setup.js
@@ -0,0 +1,63 @@
+const urlUtil = require('url')
+const extension = require('extensionizer')
+const resolveEnsToIpfsContentId = require('./resolver.js')
+
+const supportedTopLevelDomains = ['eth']
+
+module.exports = setupEnsIpfsResolver
+
+function setupEnsIpfsResolver({ provider }) {
+
+ // install listener
+ const urlPatterns = supportedTopLevelDomains.map(tld => `*://*.${tld}/*`)
+ extension.webRequest.onErrorOccurred.addListener(webRequestDidFail, { urls: urlPatterns })
+
+ // 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 } = 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 })
+ }
+
+ async function attemptResolve({ tabId, name, path, search }) {
+ extension.tabs.update(tabId, { url: `loading.html` })
+ try {
+ const ipfsContentId = await resolveEnsToIpfsContentId({ provider, name })
+ let 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
+ }
+ // 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 })
+ }
+ } catch (err) {
+ console.warn(err)
+ extension.tabs.update(tabId, { url: `error.html?name=${name}` })
+ }
+ }
+}