ens-proxy/pages/api/name.ts
2022-08-18 16:51:38 +03:00

27 lines
797 B
TypeScript

import { NextApiRequest, NextApiResponse } from 'next'
import { getEns } from './_utils'
export async function getEnsName(accountId: string) {
const ens = await getEns()
let name = await ens.getName(accountId)
// Check to be sure the reverse record is correct.
const reverseAccountId = await ens.name(name.name).getAddress()
if (accountId.toLowerCase() !== reverseAccountId.toLowerCase()) name = null
return name
}
export default async function nameApi(
request: NextApiRequest,
response: NextApiResponse
) {
try {
const accountId = String(request.query.accountId)
const name = await getEnsName(accountId)
response.setHeader('Cache-Control', 's-maxage=86400')
response.status(200).send(name)
} catch (error) {
response.status(500).send(`${error}`)
}
}