ens-proxy/pages/api/name.ts

27 lines
808 B
TypeScript
Raw Normal View History

import { NextApiRequest, NextApiResponse } from 'next'
2022-08-18 15:04:49 +02:00
import { getEns } from './_utils'
2022-08-18 15:51:38 +02:00
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)
2022-08-18 15:51:38 +02:00
const name = await getEnsName(accountId)
2022-08-22 13:30:39 +02:00
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')
response.status(200).send(name)
} catch (error) {
2022-08-18 15:04:49 +02:00
response.status(500).send(`${error}`)
}
}