2022-08-18 13:26:10 +02:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-08-22 14:09:50 +02:00
|
|
|
import { getProvider } from './_utils'
|
2022-08-18 13:26:10 +02:00
|
|
|
|
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-18 13:26:10 +02:00
|
|
|
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 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-18 13:26:10 +02:00
|
|
|
response.status(200).send(name)
|
|
|
|
} catch (error) {
|
2022-08-18 15:04:49 +02:00
|
|
|
response.status(500).send(`${error}`)
|
2022-08-18 13:26:10 +02:00
|
|
|
}
|
|
|
|
}
|