Skip to content

Commit

Permalink
fix(core): fallback to defaults if ENS entry is not available (#2753)
Browse files Browse the repository at this point in the history
  • Loading branch information
szkl authored Nov 3, 2023
1 parent 55ac7f6 commit 0a7bec6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
31 changes: 11 additions & 20 deletions packages/platform-clients/ens-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ENSRes = {
}

class ENSUtils implements IENSUtils {
async getEnsEntry(address: string): Promise<ENSRes> {
async getEnsEntry(address: string): Promise<ENSRes | undefined> {
const ensRes = await fetch(
`https://api.ensideas.com/ens/resolve/${address}`,
{
Expand All @@ -24,33 +24,24 @@ class ENSUtils implements IENSUtils {
}
)

const res: ENSRes = await ensRes.json()
if (ensRes.status === 200) return ensRes.json<ENSRes>()

if (res.error) {
console.error(`Error requesting ens from address: ${res.error}`)

throw new Error(res.error)
}

return res
console.error(`ENSIdeasError: ${await ensRes.text()}`)
}

async getENSDisplayName(addressOrEns: string): Promise<string | null> {
const { displayName } = await this.getEnsEntry(addressOrEns)

return displayName
async getENSDisplayName(addressOrEns: string): Promise<string> {
const profile = await this.getEnsEntry(addressOrEns)
return profile?.displayName || ''
}

async getENSAddress(addressOrEns: string): Promise<string> {
const { address } = await this.getEnsEntry(addressOrEns)

return address
const profile = await this.getEnsEntry(addressOrEns)
return profile?.address || ''
}

async getENSAddressAvatar(addressOrEns: string): Promise<string | null> {
const { avatar } = await this.getEnsEntry(addressOrEns)

return avatar
async getENSAddressAvatar(addressOrEns: string): Promise<string> {
const profile = await this.getEnsEntry(addressOrEns)
return profile?.avatar || ''
}
}

Expand Down
3 changes: 2 additions & 1 deletion platform/account/src/jsonrpc/methods/getIdentityByAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const getIdentityByAliasMethod = async ({
}): Promise<GetIdentityByAliasResult> => {
let alias = input.alias
if (input.provider === 'eth' && input.alias.endsWith('.eth')) {
alias = (await new ENSUtils().getEnsEntry(input.alias)).address
const addressFromENS = await new ENSUtils().getENSAddress(input.alias)
if (addressFromENS) alias = addressFromENS
}

const caller = router.createCaller(ctx)
Expand Down
3 changes: 2 additions & 1 deletion platform/account/src/jsonrpc/middlewares/checkCryptoNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export const checkCryptoNodes: BaseMiddlewareFunction<Context> = async ({
if (alias && alias.endsWith('.eth')) {
const ensClient = new ENSUtils()
const response = await ensClient.getEnsEntry(alias)
const { address: ethAddress } = response
if (!response) return next({ ctx })

const { address: ethAddress } = response
const hash = generateHashedIDRef(CryptoAccountType.ETH, ethAddress)

if (hash !== hashedIdref) {
Expand Down
6 changes: 3 additions & 3 deletions platform/account/src/nodes/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ const getCryptoAccountProfile = async (
address: string
): Promise<CryptoAccountProfile> => {
const ensClient = new ENSUtils()
const { avatar, displayName } = await ensClient.getEnsEntry(address)
const profile = await ensClient.getEnsEntry(address)

return {
address,
title: displayName || '',
icon: avatar || '',
title: profile?.displayName || '',
icon: profile?.avatar || '',
type: CryptoAccountType.ETH,
}
}

0 comments on commit 0a7bec6

Please sign in to comment.