aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/ens-ipfs/resolver.js
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2018-10-21 17:48:15 +0800
committerGitHub <noreply@github.com>2018-10-21 17:48:15 +0800
commit6d09f60bbfe5ee737ff7a138260cc33e3db5ca46 (patch)
treea0642d45087aca57ad3e06bd93dac6bfc042f233 /app/scripts/lib/ens-ipfs/resolver.js
parentb0c649a4e3c68293e08e764bbf4d53939df88e2d (diff)
downloadtangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.tar
tangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.tar.gz
tangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.tar.bz2
tangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.tar.lz
tangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.tar.xz
tangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.tar.zst
tangerine-wallet-browser-6d09f60bbfe5ee737ff7a138260cc33e3db5ca46.zip
ens-ipfs - refactor for readability (#5568)
* ens-ipfs - refactor for readability * ens-ipfs - use official ipfs gateway for better performance * lint - remove unused code * ens-ipfs - support path and search * lint - gotta love that linter * ens-ipfs - improve loading page formatting * ens-ipfs - loading - redirect to 404 after 1 min timeout * ens-ipfs - destructure for cleaner code
Diffstat (limited to 'app/scripts/lib/ens-ipfs/resolver.js')
-rw-r--r--app/scripts/lib/ens-ipfs/resolver.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/app/scripts/lib/ens-ipfs/resolver.js b/app/scripts/lib/ens-ipfs/resolver.js
new file mode 100644
index 000000000..fe2dc1134
--- /dev/null
+++ b/app/scripts/lib/ens-ipfs/resolver.js
@@ -0,0 +1,54 @@
+const namehash = require('eth-ens-namehash')
+const multihash = require('multihashes')
+const Eth = require('ethjs-query')
+const EthContract = require('ethjs-contract')
+const registrarAbi = require('./contracts/registrar')
+const resolverAbi = require('./contracts/resolver')
+
+module.exports = resolveEnsToIpfsContentId
+
+
+async function resolveEnsToIpfsContentId ({ provider, name }) {
+ const eth = new Eth(provider)
+ const hash = namehash.hash(name)
+ const contract = new EthContract(eth)
+ // lookup registrar
+ const chainId = Number.parseInt(await eth.net_version(), 10)
+ const registrarAddress = getRegistrarForChainId(chainId)
+ if (!registrarAddress) {
+ throw new Error(`EnsIpfsResolver - no known ens-ipfs registrar for chainId "${chainId}"`)
+ }
+ const Registrar = contract(registrarAbi).at(registrarAddress)
+ // lookup resolver
+ const resolverLookupResult = await Registrar.resolver(hash)
+ const resolverAddress = resolverLookupResult[0]
+ if (hexValueIsEmpty(resolverAddress)) {
+ throw new Error(`EnsIpfsResolver - no resolver found for name "${name}"`)
+ }
+ const Resolver = contract(resolverAbi).at(resolverAddress)
+ // lookup content id
+ const contentLookupResult = await Resolver.content(hash)
+ const contentHash = contentLookupResult[0]
+ if (hexValueIsEmpty(contentHash)) {
+ throw new Error(`EnsIpfsResolver - no content ID found for name "${name}"`)
+ }
+ const nonPrefixedHex = contentHash.slice(2)
+ const buffer = multihash.fromHexString(nonPrefixedHex)
+ const contentId = multihash.toB58String(multihash.encode(buffer, 'sha2-256'))
+ return contentId
+}
+
+function hexValueIsEmpty(value) {
+ return [undefined, null, '0x', '0x0', '0x0000000000000000000000000000000000000000000000000000000000000000'].includes(value)
+}
+
+function getRegistrarForChainId (chainId) {
+ switch (chainId) {
+ // mainnet
+ case 1:
+ return '0x314159265dd8dbb310642f98f50c066173c1259b'
+ // ropsten
+ case 3:
+ return '0x112234455c3a32fd11230c42e7bccd4a84e02010'
+ }
+}