diff --git a/lib/utils/ens-cache.js b/lib/utils/ens-cache.js --- a/lib/utils/ens-cache.js +++ b/lib/utils/ens-cache.js @@ -14,12 +14,12 @@ +normalizedETHAddress: string, +cacheInsertionTime: number, // We normalize ENS names using eth-ens-namehash - +normalizedENSName: ?string, + +normalizedENSName: ?string | Promise, }; type ENSAddressQueryCacheEntry = { +normalizedENSName: string, +cacheInsertionTime: number, - +normalizedETHAddress: ?string, + +normalizedETHAddress: ?string | Promise, }; const normalizeETHAddress = (ethAddress: string) => ethAddress.toLowerCase(); @@ -92,7 +92,7 @@ return normalizedENSName; } - getCachedNameForAddress(ethAddress: string): ?string { + getCachedEntryForAddress(ethAddress: string): ?ENSNameQueryCacheEntry { const normalizedETHAddress = normalizeETHAddress(ethAddress); const cacheResult = this.nameQueryCache.get(normalizedETHAddress); @@ -100,12 +100,26 @@ return undefined; } - const { cacheInsertionTime, normalizedENSName } = cacheResult; + const { cacheInsertionTime } = cacheResult; if (cacheInsertionTime + cacheTimeout <= Date.now()) { this.nameQueryCache.delete(normalizedETHAddress); return undefined; } + return cacheResult; + } + + getCachedNameForAddress(ethAddress: string): ?string { + const cacheResult = this.getCachedEntryForAddress(ethAddress); + if (!cacheResult) { + return undefined; + } + + const { normalizedENSName } = cacheResult; + if (typeof normalizedENSName !== 'string') { + return undefined; + } + return normalizedENSName; } @@ -137,7 +151,7 @@ return normalizedETHAddress; } - getCachedAddressForName(ensName: string): ?string { + getCachedEntryForName(ensName: string): ?ENSAddressQueryCacheEntry { const normalizedENSName = normalizeENSName(ensName); if (normalizedENSName !== ensName) { return undefined; @@ -148,12 +162,26 @@ return undefined; } - const { cacheInsertionTime, normalizedETHAddress } = cacheResult; + const { cacheInsertionTime } = cacheResult; if (cacheInsertionTime + cacheTimeout <= Date.now()) { this.addressQueryCache.delete(normalizedENSName); return undefined; } + return cacheResult; + } + + getCachedAddressForName(ensName: string): ?string { + const cacheResult = this.getCachedEntryForName(ensName); + if (!cacheResult) { + return undefined; + } + + const { normalizedETHAddress } = cacheResult; + if (typeof normalizedETHAddress !== 'string') { + return undefined; + } + return normalizedETHAddress; } }