| 1 | import { isHex, type Address } from "viem"; |
| 2 | import { normalize } from "viem/ens"; |
| 3 | import { ensClient, type ResolveOptions } from "./"; |
| 4 | |
| 5 | export async function getNameAndAddress(options: ResolveOptions) { |
| 6 | let name: string | null = null; |
| 7 | let address: Address | null = null; |
| 8 | let input: "address" | "name"; |
| 9 | |
| 10 | try { |
| 11 | // Handle name or address |
| 12 | if (isHex(options.input)) { |
| 13 | address = options.input; |
| 14 | input = "address"; |
| 15 | name = await ensClient.getEnsName({ |
| 16 | address: options.input as Address, |
| 17 | }); |
| 18 | } else { |
| 19 | name = options.input; |
| 20 | input = "name"; |
| 21 | address = await ensClient.getEnsAddress({ |
| 22 | name: normalize(options.input as string), |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | return { name, address, input }; |
| 27 | } catch (_error) { |
| 28 | console.error(`Failed to resolve: ${options.input}`); |
| 29 | return { name: null, address: null, input: "name" }; |
| 30 | } |
| 31 | } |