Skip to content

Commit

Permalink
use resolver authorisation check
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jun 20, 2023
1 parent b1e0907 commit 793e9bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
21 changes: 10 additions & 11 deletions src/hooks/useSelfAbilities.test.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -1221,7 +1220,7 @@ describe('useSelfAbilities', () => {
},
},
})
mockUseRegistryResolver.mockReturnValue({ data: emptyAddress })
mockUseResolverIsAuthorized.mockReturnValue({ data: { isAuthorized: false, isValid: true } })

const { result } = renderHook(() => useSelfAbilities(account, name))

Expand Down
15 changes: 9 additions & 6 deletions src/hooks/useSelfAbilities.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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
}

Expand All @@ -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])
}

0 comments on commit 793e9bd

Please sign in to comment.