ens-proxy/api/name.ts

28 lines
829 B
TypeScript
Raw Normal View History

2022-08-22 14:42:40 +02:00
import type { VercelRequest, VercelResponse } from '@vercel/node'
2022-08-22 14:09:50 +02:00
import { getProvider } from './_utils'
2022-08-18 15:51:38 +02:00
export async function getEnsName(accountId: string) {
2022-08-22 14:09:50 +02:00
const provider = await getProvider()
let name = await provider.lookupAddress(accountId)
2022-08-18 15:51:38 +02:00
// Check to be sure the reverse record is correct.
2022-08-22 14:09:50 +02:00
const reverseAccountId = await provider.resolveName(name)
2022-08-18 15:51:38 +02:00
if (accountId.toLowerCase() !== reverseAccountId.toLowerCase()) name = null
return name
}
export default async function nameApi(
2022-08-22 14:42:40 +02:00
request: VercelRequest,
response: VercelResponse
) {
try {
const accountId = String(request.query.accountId)
2022-08-18 15:51:38 +02:00
const name = await getEnsName(accountId)
2022-08-22 14:09:50 +02:00
2022-08-22 13:30:39 +02:00
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')
2022-08-24 15:49:17 +02:00
response.status(200).send({ name })
} catch (error) {
2022-08-24 15:49:17 +02:00
response.send({ error })
}
}