aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
authorMikeCheng1208 <a3804430@hotmail.com>2018-05-04 19:57:19 +0800
committerYung chieh Tsai <x01ep23i@hotmail.com>2018-05-29 12:38:25 +0800
commit695b157e7b511a94a18ada1578ece2e4e838f869 (patch)
tree1a79d3e776330d82f53f8f903e06b90c28ba104e /app/scripts/lib
parent8e672a82e2ba7838370a5332c4775a8399c961b9 (diff)
downloadtangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.tar
tangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.tar.gz
tangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.tar.bz2
tangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.tar.lz
tangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.tar.xz
tangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.tar.zst
tangerine-wallet-browser-695b157e7b511a94a18ada1578ece2e4e838f869.zip
Intergrate ENS with IPFS
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/contracts/registrar.js1
-rw-r--r--app/scripts/lib/contracts/resolver.js2
-rw-r--r--app/scripts/lib/portalnetwork.js33
-rw-r--r--app/scripts/lib/resolver.js46
4 files changed, 82 insertions, 0 deletions
diff --git a/app/scripts/lib/contracts/registrar.js b/app/scripts/lib/contracts/registrar.js
new file mode 100644
index 000000000..980a64d7f
--- /dev/null
+++ b/app/scripts/lib/contracts/registrar.js
@@ -0,0 +1 @@
+module.exports = [{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"resolver","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"label","type":"bytes32"},{"name":"owner","type":"address"}],"name":"setSubnodeOwner","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"ttl","type":"uint64"}],"name":"setTTL","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"ttl","outputs":[{"name":"","type":"uint64"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"resolver","type":"address"}],"name":"setResolver","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"owner","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"owner","type":"address"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":true,"name":"label","type":"bytes32"},{"indexed":false,"name":"owner","type":"address"}],"name":"NewOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"resolver","type":"address"}],"name":"NewResolver","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"ttl","type":"uint64"}],"name":"NewTTL","type":"event"}] \ No newline at end of file
diff --git a/app/scripts/lib/contracts/resolver.js b/app/scripts/lib/contracts/resolver.js
new file mode 100644
index 000000000..f42777cc7
--- /dev/null
+++ b/app/scripts/lib/contracts/resolver.js
@@ -0,0 +1,2 @@
+module.exports =
+[{"constant":true,"inputs":[{"name":"interfaceID","type":"bytes4"}],"name":"supportsInterface","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"},{"name":"contentTypes","type":"uint256"}],"name":"ABI","outputs":[{"name":"contentType","type":"uint256"},{"name":"data","type":"bytes"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"x","type":"bytes32"},{"name":"y","type":"bytes32"}],"name":"setPubkey","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"content","outputs":[{"name":"ret","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"addr","outputs":[{"name":"ret","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"contentType","type":"uint256"},{"name":"data","type":"bytes"}],"name":"setABI","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"name","outputs":[{"name":"ret","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"name","type":"string"}],"name":"setName","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"hash","type":"bytes32"}],"name":"setContent","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"node","type":"bytes32"}],"name":"pubkey","outputs":[{"name":"x","type":"bytes32"},{"name":"y","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"node","type":"bytes32"},{"name":"addr","type":"address"}],"name":"setAddr","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"ensAddr","type":"address"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"a","type":"address"}],"name":"AddrChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"hash","type":"bytes32"}],"name":"ContentChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"name","type":"string"}],"name":"NameChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":true,"name":"contentType","type":"uint256"}],"name":"ABIChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"node","type":"bytes32"},{"indexed":false,"name":"x","type":"bytes32"},{"indexed":false,"name":"y","type":"bytes32"}],"name":"PubkeyChanged","type":"event"}] \ No newline at end of file
diff --git a/app/scripts/lib/portalnetwork.js b/app/scripts/lib/portalnetwork.js
new file mode 100644
index 000000000..5ff4f5ecf
--- /dev/null
+++ b/app/scripts/lib/portalnetwork.js
@@ -0,0 +1,33 @@
+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);
+ extension.tabs.getSelected(null, tab => {
+ extension.tabs.update(tab.id, { url: "loading.html" });
+
+ setTimeout(() => {
+ return extension.tabs.update(tab.id, { url: "404.html" });
+ }, 60000);
+
+ resolver.resolve(name, provider).then(ipfsHash => {
+ 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 => {
+ 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/"]})
+} \ No newline at end of file
diff --git a/app/scripts/lib/resolver.js b/app/scripts/lib/resolver.js
new file mode 100644
index 000000000..43ccec0cc
--- /dev/null
+++ b/app/scripts/lib/resolver.js
@@ -0,0 +1,46 @@
+const namehash = require('eth-ens-namehash')
+const multihash = require('multihashes')
+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)
+ return new Promise((resolve, reject) => {
+ if (provider.type !== "mainnet") reject('no_mainnet')
+ Registrar.resolver(hash).then((address) => {
+ if (address === '0x0000000000000000000000000000000000000000') {
+ reject(null)
+ } else {
+ let 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')))
+ } else {
+ reject('fisk')
+ }
+ })
+ })
+}
+module.exports.resolve = function (name, provider) {
+ let path = name.split(".");
+ let tld = path[path.length - 1];
+ if (tld === 'eth') {
+ return ens(name, provider);
+ } else {
+ return new Promise((resolve, reject) => {
+ reject(null)
+ })
+ }
+}