diff options
author | MikeCheng1208 <a3804430@hotmail.com> | 2018-05-04 19:57:19 +0800 |
---|---|---|
committer | Yung chieh Tsai <x01ep23i@hotmail.com> | 2018-05-29 12:38:25 +0800 |
commit | 695b157e7b511a94a18ada1578ece2e4e838f869 (patch) | |
tree | 1a79d3e776330d82f53f8f903e06b90c28ba104e /app/scripts/lib/resolver.js | |
parent | 8e672a82e2ba7838370a5332c4775a8399c961b9 (diff) | |
download | tangerine-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/resolver.js')
-rw-r--r-- | app/scripts/lib/resolver.js | 46 |
1 files changed, 46 insertions, 0 deletions
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) + }) + } +} |