diff options
author | Yung chieh Tsai <x01ep23i@hotmail.com> | 2018-05-25 00:08:02 +0800 |
---|---|---|
committer | Yung chieh Tsai <x01ep23i@hotmail.com> | 2018-05-29 12:44:37 +0800 |
commit | 829deacb57a23ec8027269c93cdef3f3735d1710 (patch) | |
tree | 04fbd566d1ad90307f4aba22d4c9be1439732434 /app/scripts/lib | |
parent | 86ead431c3e07eff3ff9deae42ab0994ca998229 (diff) | |
download | tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.tar tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.tar.gz tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.tar.bz2 tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.tar.lz tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.tar.xz tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.tar.zst tangerine-wallet-browser-829deacb57a23ec8027269c93cdef3f3735d1710.zip |
Rename files
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/ipfsContent.js | 36 | ||||
-rw-r--r-- | app/scripts/lib/portalnetwork.js | 36 | ||||
-rw-r--r-- | app/scripts/lib/resolver.js | 37 |
3 files changed, 55 insertions, 54 deletions
diff --git a/app/scripts/lib/ipfsContent.js b/app/scripts/lib/ipfsContent.js new file mode 100644 index 000000000..4d66745e9 --- /dev/null +++ b/app/scripts/lib/ipfsContent.js @@ -0,0 +1,36 @@ +const extension = require('extensionizer') +const resolver = require('./resolver.js') + +module.exports = function (provider) { + extension.webRequest.onBeforeRequest.addListener(details => { + const name = details.url.substring(7, details.url.length - 1) + let clearTime = null + extension.tabs.getSelected(null, tab => { + extension.tabs.update(tab.id, { url: 'loading.html' }) + + clearTime = setTimeout(() => { + return extension.tabs.update(tab.id, { url: '404.html' }) + }, 60000) + + resolver.resolve(name, provider).then(ipfsHash => { + clearTimeout(clearTime) + let url = 'https://gateway.ipfs.io/ipfs/' + ipfsHash + return fetch(url, { method: 'HEAD' }).then(response => response.status).then(statusCode => { + if (statusCode !== 200) return 'Local' + extension.tabs.update(tab.id, { url: url }) + }) + .catch(err => { + url = 'https://gateway.ipfs.io/ipfs/' + ipfsHash + extension.tabs.update(tab.id, {url: url}) + return err + }) + }) + .catch(err => { + clearTimeout(clearTime) + const url = err === 'unsupport' ? 'unsupport' : 'error' + extension.tabs.update(tab.id, {url: `${url}.html?name=${name}`}) + }) + }) + return { cancel: true } + }, {urls: ['*://*.eth/']}) +} diff --git a/app/scripts/lib/portalnetwork.js b/app/scripts/lib/portalnetwork.js deleted file mode 100644 index 977f17926..000000000 --- a/app/scripts/lib/portalnetwork.js +++ /dev/null @@ -1,36 +0,0 @@ -const extension = require('extensionizer') -const resolver = require('./resolver.js'); -module.exports = function (provider) { - extension.webRequest.onBeforeRequest.addListener(details => { - let name = details.url.substring(7, details.url.length - 1); - let clearTime = null; - extension.tabs.getSelected(null, tab => { - extension.tabs.update(tab.id, { url: "loading.html" }); - - clearTime = setTimeout(() => { - return extension.tabs.update(tab.id, { url: "404.html" }); - }, 60000); - - resolver.resolve(name, provider).then(ipfsHash => { - clearTimeout(clearTime); - let url = "https://gateway.ipfs.io/ipfs/" + ipfsHash; - return fetch(url, { method: "HEAD" }).then(response => response.status).then(statusCode => { - if (statusCode !== 200) return "Local" - extension.tabs.update(tab.id, { url: url }) - }) - .catch(err => { - url = "https://gateway.ipfs.io/ipfs/" + ipfsHash - extension.tabs.update(tab.id, {url: url}) - return err - }) - }) - .catch(err => { - clearTimeout(clearTime); - let nameWithoutTld = name.substring(0, name.lastIndexOf('.')) - let url = err === "no_mainnet" ? "no_mainnet" : "error" - extension.tabs.update(tab.id, {url: `${url}.html?name=${name}`}) - }) - }) - return { cancel: true } - }, {urls: ["*://*.eth/"]}) -} diff --git a/app/scripts/lib/resolver.js b/app/scripts/lib/resolver.js index 43ccec0cc..dec43c481 100644 --- a/app/scripts/lib/resolver.js +++ b/app/scripts/lib/resolver.js @@ -1,43 +1,44 @@ const namehash = require('eth-ens-namehash') const multihash = require('multihashes') -const REGISTRAR_ENS_MAIN_NET = "0x314159265dd8dbb310642f98f50c066173c1259b" +const REGISTRAR_ENS_MAIN_NET = '0x314159265dd8dbb310642f98f50c066173c1259b' const HttpProvider = require('ethjs-provider-http') const Eth = require('ethjs-query') const EthContract = require('ethjs-contract') const registrarAbi = require('./contracts/registrar') const resolverAbi = require('./contracts/resolver') -function ens(name, provider) { - // provider need mainnet - let eth = new Eth(new HttpProvider(provider.rpcTarget)) - let hash = namehash.hash(name) - let contract = new EthContract(eth) - let Registrar = contract(registrarAbi).at(REGISTRAR_ENS_MAIN_NET) + +function ens (name, provider) { + const eth = new Eth(new HttpProvider(provider.rpcTarget)) + const hash = namehash.hash(name) + const contract = new EthContract(eth) + const Registrar = contract(registrarAbi).at(REGISTRAR_ENS_MAIN_NET) return new Promise((resolve, reject) => { - if (provider.type !== "mainnet") reject('no_mainnet') + if (provider.type !== 'mainnet') reject('unsupport') Registrar.resolver(hash).then((address) => { if (address === '0x0000000000000000000000000000000000000000') { reject(null) } else { - let Resolver = contract(resolverAbi).at(address["0"]) + const Resolver = contract(resolverAbi).at(address['0']) return Resolver.content(hash) } }).then((contentHash) => { - if (contentHash["0"] === '0x0000000000000000000000000000000000000000000000000000000000000000') reject(null) - if (contentHash.ret !== "0x") { - let hex = contentHash["0"].substring(2) - let buf = multihash.fromHexString(hex) - resolve(multihash.toB58String(multihash.encode(buf, 'sha2-256'))) + if (contentHash['0'] === '0x0000000000000000000000000000000000000000000000000000000000000000') reject(null) + if (contentHash.ret !== '0x') { + const hex = contentHash['0'].substring(2) + const buf = multihash.fromHexString(hex) + resolve(multihash.toB58String(buf)) } else { - reject('fisk') + reject(null) } }) }) } + module.exports.resolve = function (name, provider) { - let path = name.split("."); - let tld = path[path.length - 1]; + const path = name.split('.') + const tld = path[path.length - 1] if (tld === 'eth') { - return ens(name, provider); + return ens(name, provider) } else { return new Promise((resolve, reject) => { reject(null) |