From 793e9bd6c434fb686257dcba52a2319fc14cbb6a Mon Sep 17 00:00:00 2001 From: tate Date: Tue, 20 Jun 2023 10:39:20 +1000 Subject: [PATCH] use resolver authorisation check --- src/hooks/useSelfAbilities.test.ts | 21 ++++++++++----------- src/hooks/useSelfAbilities.ts | 15 +++++++++------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/hooks/useSelfAbilities.test.ts b/src/hooks/useSelfAbilities.test.ts index 7122f5c25..507c5c1d5 100644 --- a/src/hooks/useSelfAbilities.test.ts +++ b/src/hooks/useSelfAbilities.test.ts @@ -1,9 +1,8 @@ import { mockFunction, renderHook } from '@app/test-utils' import { useBasicName } from '@app/hooks/useBasicName' -import { emptyAddress } from '@app/utils/constants' -import { useRegistryResolver } from './resolver/useRegistryResolver' +import { useResolverIsAuthorized } from './resolver/useResolverIsAuthorized' import { getFunctionCallDetails, getPermittedActions, useSelfAbilities } from './useSelfAbilities' type DeepPartial = { @@ -29,10 +28,10 @@ type MockData = { } jest.mock('@app/hooks/useBasicName') -jest.mock('./resolver/useRegistryResolver') +jest.mock('./resolver/useResolverIsAuthorized') const mockUseBasicName = mockFunction(useBasicName) -const mockUseRegistryResolver = mockFunction(useRegistryResolver) +const mockUseResolverIsAuthorized = mockFunction(useResolverIsAuthorized) const ownerAddress = '0x123' const account = ownerAddress @@ -1182,35 +1181,35 @@ describe('useSelfAbilities', () => { }, }, }) - mockUseRegistryResolver.mockReturnValue({ data: '0xresolver' }) + mockUseResolverIsAuthorized.mockReturnValue({ data: { isAuthorized: true, isValid: true } }) const { result } = renderHook(() => useSelfAbilities(account, name)) expect(result.current.canSend).toBe(false) }) - it('should return canEdit as true if there is a resolver', () => { + it('should return canEdit as true if resolver is authorised', () => { mockUseBasicName.mockReturnValue({ ownerData: { owner: account, }, }) - mockUseRegistryResolver.mockReturnValue({ data: '0xresolver' }) + mockUseResolverIsAuthorized.mockReturnValue({ data: { isAuthorized: true, isValid: true } }) const { result } = renderHook(() => useSelfAbilities(account, name)) expect(result.current.canEdit).toBe(true) }) - it('should return canEdit as true if there is no resolver but CANNOT_SET_RESOLVER has not been burned', () => { + it('should return canEdit as true if resolver is not authorised but CANNOT_SET_RESOLVER has not been burned', () => { mockUseBasicName.mockReturnValue({ ownerData: { owner: account, }, }) - mockUseRegistryResolver.mockReturnValue({ data: emptyAddress }) + mockUseResolverIsAuthorized.mockReturnValue({ data: { isAuthorized: false, isValid: true } }) const { result } = renderHook(() => useSelfAbilities(account, name)) expect(result.current.canEdit).toBe(true) }) - it('shold return canEdit as false if there is no resolver and CANNOT_SET_RESOLVER has been burned', () => { + it('shold return canEdit as false if resolver is not authorised and CANNOT_SET_RESOLVER has been burned', () => { mockUseBasicName.mockReturnValue({ ownerData: { owner: account, @@ -1221,7 +1220,7 @@ describe('useSelfAbilities', () => { }, }, }) - mockUseRegistryResolver.mockReturnValue({ data: emptyAddress }) + mockUseResolverIsAuthorized.mockReturnValue({ data: { isAuthorized: false, isValid: true } }) const { result } = renderHook(() => useSelfAbilities(account, name)) diff --git a/src/hooks/useSelfAbilities.ts b/src/hooks/useSelfAbilities.ts index 1830dbb18..8143afec3 100644 --- a/src/hooks/useSelfAbilities.ts +++ b/src/hooks/useSelfAbilities.ts @@ -1,10 +1,9 @@ import { useMemo } from 'react' import { useBasicName } from '@app/hooks/useBasicName' -import { emptyAddress } from '@app/utils/constants' import { checkSubname } from '@app/utils/utils' -import { useRegistryResolver } from './resolver/useRegistryResolver' +import { useResolverIsAuthorized } from './resolver/useResolverIsAuthorized' interface SendPermissions { canSendOwner: boolean @@ -280,7 +279,9 @@ export const useSelfAbilities = (address: string | undefined, name?: string | nu const is2LDEth = name?.split('.')?.length === 2 && name?.split('.')?.[1] === 'eth' const basicNameData = useBasicName(name, { skipGraph: false }) - const registryResolverData = useRegistryResolver(name || '', { enabled: !!name }) + const resolverAuthorisation = useResolverIsAuthorized(name || undefined, { + enabled: !!name && basicNameData.wrapperData?.child.CANNOT_SET_RESOLVER, + }) const parentBasicNameData = useBasicName(parent, { skipGraph: false }) return useMemo(() => { @@ -338,11 +339,13 @@ export const useSelfAbilities = (address: string | undefined, name?: string | nu abilities.canSendManager = canSendManager abilities.canSend = canSendManager || canSendOwner - const hasResolver = !!(registryResolverData.data && registryResolverData.data !== emptyAddress) + const hasAuthorisedResolver = !!( + resolverAuthorisation.data && resolverAuthorisation.data.isAuthorized + ) const canSetResolver = !basicNameData.wrapperData?.child.CANNOT_SET_RESOLVER // if resolver is empty and user cannot set resolver, they cannot edit - if (basicNameData?.ownerData?.owner === address && (hasResolver || canSetResolver)) { + if (basicNameData?.ownerData?.owner === address && (canSetResolver || hasAuthorisedResolver)) { abilities.canEdit = true } @@ -359,5 +362,5 @@ export const useSelfAbilities = (address: string | undefined, name?: string | nu } return abilities - }, [name, address, basicNameData, parentBasicNameData, is2LDEth, registryResolverData.data]) + }, [name, address, basicNameData, parentBasicNameData, is2LDEth, resolverAuthorisation.data]) }