-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* reverse ens caching * fix export * clean up * Remove logger
- Loading branch information
Bruno Barbieri
authored
Aug 5, 2019
1 parent
9032e21
commit 511ddf1
Showing
2 changed files
with
48 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Engine from '../core/Engine'; | ||
import networkMap from 'ethjs-ens/lib/network-map.json'; | ||
import ENS from 'ethjs-ens'; | ||
|
||
/** | ||
* Utility class with the single responsibility | ||
* of caching ENS names | ||
*/ | ||
class ENSCache { | ||
static cache = {}; | ||
} | ||
|
||
export default async function doENSReverseLookup(address, network) { | ||
const cache = ENSCache.cache[address]; | ||
if (cache) { | ||
return Promise.resolve(cache); | ||
} | ||
|
||
const { provider } = Engine.context.NetworkController; | ||
|
||
const networkHasEnsSupport = Boolean(networkMap[network]); | ||
|
||
if (networkHasEnsSupport) { | ||
this.ens = new ENS({ provider, network }); | ||
try { | ||
const name = await this.ens.reverse(address); | ||
const resolvedAddress = await this.ens.lookup(name); | ||
if (address.toLowerCase() === resolvedAddress.toLowerCase()) { | ||
ENSCache.cache[address] = name; | ||
return name; | ||
} | ||
// eslint-disable-next-line no-empty | ||
} catch (e) {} | ||
} | ||
} |