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, + ) +}