From cf8d8b25c63f6c4a6687f20447d7d2b31e7d5bf0 Mon Sep 17 00:00:00 2001 From: Luc Date: Thu, 5 Sep 2024 00:21:43 +0000 Subject: [PATCH] Introduce useEnsCredentials --- packages/react/src/hooks.ts | 1 + packages/react/src/hooks/useEnsCredentials.ts | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 packages/react/src/hooks/useEnsCredentials.ts diff --git a/packages/react/src/hooks.ts b/packages/react/src/hooks.ts index 765ed1b2..8ecca250 100644 --- a/packages/react/src/hooks.ts +++ b/packages/react/src/hooks.ts @@ -4,3 +4,4 @@ export { useEnsResolverInterfaces } from './hooks/useEnsResolverInterfaces.js' export { useDecodedName } from './hooks/useDecodedName.js' export { useEnsRecordsWrite } from './hooks/useEnsRecordsWrite.js' export { useEnsExpiry } from './hooks/useEnsExpiry.js' +export { useEnsCredentials } from './hooks/useEnsCredentials.js' diff --git a/packages/react/src/hooks/useEnsCredentials.ts b/packages/react/src/hooks/useEnsCredentials.ts new file mode 100644 index 00000000..6345f5b8 --- /dev/null +++ b/packages/react/src/hooks/useEnsCredentials.ts @@ -0,0 +1,46 @@ +import { getTextRecord } from '@ensdomains/ensjs/public' +import type { ParamWithClients } from '../client.js' +import { useQuery, type UseQueryReturnType } from './useQuery.js' + +export type UseEnsCredentialsParams = ParamWithClients<{ name: string }> + +export type ExternalCredential = { + url: string +} + +export type UseEnsCredentialsReturnType = ExternalCredential[] + +/** + * Returns credentials from a name + * + * @param params - {@link UseEnsCredentialsParams} + * @returns - {@link UseEnsCredentialsReturnType} + */ +export const useEnsCredentials = ( + params: UseEnsCredentialsParams, +): UseQueryReturnType => { + const { name, client, queryClient } = params + + return useQuery( + ['ensjs', 'credentials', params.name], + { + queryFn: async () => { + const result = await getTextRecord(client, { + name, + key: 'verifications', + }) + + if (!result) return [] + + const credentials = (JSON.parse(result) as string[]) + .filter((url) => new URL(url)) + .map((url) => ({ + url, + })) + + return credentials + }, + }, + queryClient, + ) +}