Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix offchain dns nametype + [root] validation #700

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/@molecules/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ export const SearchInput = ({
)
if (ownerData) {
const registrationStatus = getRegistrationStatus({
name: selectedItem.value,
timestamp: Date.now(),
validation: currentValidation,
ownerData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const useExpiryDetails = ({ name, details }: Input, options: Options = {}
'dns-wrapped-2ld',
'dns-emancipated-2ld',
'dns-locked-2ld',
'dns-offchain-2ld',
'dns-unwrapped-subname',
'dns-wrapped-subname',
'dns-emancipated-subname',
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/nameType/getNameType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type NameType =
| 'eth-pcc-expired-subname'
| 'dns-unwrapped-2ld'
| 'dns-wrapped-2ld'
| 'dns-offchain-2ld'
| 'dns-emancipated-2ld' // *
| 'dns-locked-2ld' // *
| 'dns-unwrapped-subname'
Expand Down Expand Up @@ -86,6 +87,9 @@ export const getNameType = ({
return `${_tldType}-${_wrapLevel}-2ld` as const
},
)
.with(['dns', 'unwrapped', '2ld', 'imported'], () =>
ownerData ? ('dns-unwrapped-2ld' as const) : ('dns-offchain-2ld' as const),
)
.with(['dns', P._, '2ld', P._], ([, _wrapLevel]) => `dns-${_wrapLevel}-2ld` as const)
.with([P._, P._, 'subname', P._], ([_tldType, _wrapLevel]) =>
pccExpired
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/nameType/useNameType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest'
import { useNameType } from './useNameType'
import { makeMockUseContractAddress } from '../../../test/mock/makeMockUseContractAddress'
import { makeMockUseBasicName } from '../../../test/mock/makeMockUseBasicName'
import { mockUseNameTypeConfig, mockUseNameTypes } from '../../../test/mock/makeMockUseNameType'

const mockBasicData = vi.fn()
vi.mock('@app/hooks/useBasicName', () => ({
Expand Down Expand Up @@ -165,4 +166,13 @@ describe('useNameType', () => {
expect(result.current.data).toEqual('eth-locked-2ld:grace-period')
})
})

describe('mocks', () => {
it.each(mockUseNameTypes)('should return the correct data for: %s', async (type) => {
const config = mockUseNameTypeConfig[type]
mockBasicData.mockReturnValue(makeMockUseBasicName(config.basicNameType))
const { result } = renderHook(() => useNameType(config.name))
expect(result.current.data).toEqual(type)
})
})
})
4 changes: 4 additions & 0 deletions src/hooks/ownership/useRoles/utils/getRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export const getRoles = ({
{ address: owner, role: 'manager' as const },
{ address: ethAddress, role: 'eth-record' as const },
])
.with('dns-offchain-2ld', () => [
{ address: dnsOwner, role: 'dns-owner' as const },
{ address: ethAddress, role: 'eth-record' as const },
])
.with(P.union(P.nullish, 'tld', 'root'), () => [])
.exhaustive()
}
1 change: 1 addition & 0 deletions src/hooks/useBasicName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const useBasicName = ({

const registrationStatus = !publicCallsLoading
? getRegistrationStatus({
name: normalisedName,
timestamp: registrationStatusTimestamp,
validation,
ownerData,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const validate = (input: string) => {
...parsedInput,
name: outputName,
beautifiedName: tryBeautify(outputName),
isNonASCII,
isNonASCII: input !== '[root]' && isNonASCII,
labelCount: parsedInput.labelDataArray.length,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const senderRole = (nameType: ReturnType<typeof useNameType>['data']) =>
P.union(
'dns-unwrapped-2ld',
'dns-wrapped-2ld',
'dns-offchain-2ld',
'eth-emancipated-2ld:grace-period',
'eth-locked-2ld:grace-period',
'eth-unwrapped-2ld:grace-period',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const checkCanSyncManager = ({
'eth-pcc-expired-subname',
'dns-locked-2ld',
'dns-emancipated-2ld',
'dns-offchain-2ld',
'dns-unwrapped-subname',
'dns-wrapped-subname',
'dns-emancipated-subname',
Expand Down
4 changes: 4 additions & 0 deletions src/utils/registrationStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type RegistrationStatus =
| 'unsupportedTLD'

export const getRegistrationStatus = ({
name,
timestamp,
validation: { isETH, is2LD, isShort, type },
ownerData,
Expand All @@ -32,6 +33,7 @@ export const getRegistrationStatus = ({
addrData,
supportedTLD,
}: {
name: string
timestamp: number
validation: Partial<Omit<ParsedInputResult, 'normalised' | 'isValid'>>
ownerData?: GetOwnerReturnType
Expand All @@ -41,6 +43,8 @@ export const getRegistrationStatus = ({
addrData?: GetAddressRecordReturnType
supportedTLD?: boolean | null
}): RegistrationStatus => {
if (name === '[root]') return 'owned'

if (isETH && is2LD && isShort) {
return 'short'
}
Expand Down
65 changes: 65 additions & 0 deletions test/mock/makeMockUseBasicName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type MockUseBasicNameConfig = {
}

export const mockUseBasicNameConfig = {
root: {
useValidateType: 'root',
useOwnerType: 'root',
useExpiryType: 'root',
usePriceType: 'root',
} as MockUseBasicNameConfig,
eth: {
useValidateType: 'eth',
useOwnerType: 'eth',
Expand Down Expand Up @@ -179,6 +185,10 @@ export const mockUseBasicNameConfig = {
useOwnerType: 'namewrapper:unowned',
useWrapperDataType: 'locked:unowned',
} as MockUseBasicNameConfig,
'eth-pcc-expired-subname': {
useValidateType: 'valid-subname',
useOwnerType: 'registry:pcc-expired',
} as MockUseBasicNameConfig,
// DNS
dns: {
useValidateType: 'dns',
Expand Down Expand Up @@ -209,6 +219,17 @@ export const mockUseBasicNameConfig = {
useValidateType: 'valid-2ld:dns',
useAddrRecordType: 'owned',
} as MockUseBasicNameConfig,
'dns-unwrapped-subname': {
useValidateType: 'valid-subname:dns',
useOwnerType: 'registry',
useAddrRecordType: 'owned',
} as MockUseBasicNameConfig,
'dns-wrapped-subname': {
useValidateType: 'valid-subname:dns',
useOwnerType: 'namewrapper',
useWrapperDataType: 'wrapped',
useAddrRecordType: 'owned',
} as MockUseBasicNameConfig,
} as const

export type MockUseBasicNameType = keyof typeof mockUseBasicNameConfig
Expand Down Expand Up @@ -236,6 +257,17 @@ export const makeMockUseBasicName = (type: MockUseBasicNameType) => {
gracePeriodEndDate,
}
return match(type)
.with('root', () => ({
...BaseBasicName,
normalisedName: '[root]',
truncatedName: '[root]',
canBeWrapped: false,
pccExpired: false,
registrationStatus: 'owned' as const,
isCachedData: false,
isLoading: false,
isWrapped: false,
}))
.with('eth', () => ({
...BaseBasicName,
normalisedName: 'eth',
Expand Down Expand Up @@ -373,6 +405,17 @@ export const makeMockUseBasicName = (type: MockUseBasicNameType) => {
isCachedData: false,
}),
)
.with('eth-pcc-expired-subname', () => ({
...BaseBasicName,
normalisedName: 'subname.name.eth',
truncatedName: 'subname.name.eth',
registrationStatus: 'owned' as const,
isWrapped: false,
pccExpired: true,
canBeWrapped: true,
isLoading: false,
isCachedData: false,
}))
.with('dns', () => ({
...BaseBasicName,
normalisedName: 'com',
Expand Down Expand Up @@ -414,5 +457,27 @@ export const makeMockUseBasicName = (type: MockUseBasicNameType) => {
isLoading: false,
isCachedData: false,
}))
.with('dns-unwrapped-subname', () => ({
...BaseBasicName,
normalisedName: 'subname.name.com',
truncatedName: 'subname.name.com',
registrationStatus: 'owned' as const,
isWrapped: false,
pccExpired: false,
canBeWrapped: false,
isLoading: false,
isCachedData: false,
}))
.with('dns-wrapped-subname', () => ({
...BaseBasicName,
normalisedName: 'subname.name.com',
truncatedName: 'subname.name.com',
registrationStatus: 'owned' as const,
isWrapped: true,
pccExpired: false,
canBeWrapped: false,
isLoading: false,
isCachedData: false,
}))
.exhaustive()
}
3 changes: 2 additions & 1 deletion test/mock/makeMockUseExpiryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { match, P } from 'ts-pattern'

import { GetExpiryReturnType } from '@ensdomains/ensjs/public'

const mockUseExpiryTypes = ['eth', 'active', 'grace-period'] as const
const mockUseExpiryTypes = ['root', 'eth', 'active', 'grace-period'] as const
export type MockUseExpiryType = (typeof mockUseExpiryTypes)[number] | undefined

export const makeMockUseExpiryData = (type: MockUseExpiryType): GetExpiryReturnType | undefined =>
match(type)
.with('root', () => undefined)
.with('eth', () => ({
expiry: {
date: new Date('+275760-09-13T00:00:00.000Z'),
Expand Down
99 changes: 99 additions & 0 deletions test/mock/makeMockUseNameType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { NameType } from '@app/hooks/nameType/getNameType'

import { MockUseBasicNameType } from './makeMockUseBasicName'

export type MockUseNameTypeType = Exclude<
NameType,
| 'dns-emancipated-2ld'
| 'dns-locked-2ld'
| 'dns-emancipated-subname'
| 'dns-locked-subname'
| 'dns-pcc-expired-subname'
>

type MockUseNameTypeConfig = {
name: string
basicNameType: MockUseBasicNameType
}

export const mockUseNameTypeConfig: { [key in MockUseNameTypeType]: MockUseNameTypeConfig } = {
root: {
name: '[root]',
basicNameType: 'root',
},
tld: {
name: 'eth',
basicNameType: 'eth',
},
'eth-unwrapped-2ld': {
name: 'name.eth',
basicNameType: 'eth-unwrapped-2ld',
},
'eth-unwrapped-2ld:grace-period': {
name: 'name.eth',
basicNameType: 'eth-unwrapped-2ld:grace-period',
},
'eth-emancipated-2ld': {
name: 'name.eth',
basicNameType: 'eth-emancipated-2ld',
},
'eth-emancipated-2ld:grace-period': {
name: 'name.eth',
basicNameType: 'eth-emancipated-2ld:grace-period',
},
'eth-locked-2ld': {
name: 'name.eth',
basicNameType: 'eth-locked-2ld',
},
'eth-locked-2ld:grace-period': {
name: 'name.eth',
basicNameType: 'eth-locked-2ld:grace-period',
},
'eth-unwrapped-subname': {
name: 'subname.name.eth',
basicNameType: 'eth-unwrapped-subname',
},
'eth-wrapped-subname': {
name: 'subname.name.eth',
basicNameType: 'eth-wrapped-subname',
},
'eth-emancipated-subname': {
name: 'subname.name.eth',
basicNameType: 'eth-emancipated-subname',
},
'eth-locked-subname': {
name: 'subname.name.eth',
basicNameType: 'eth-locked-subname',
},
'eth-pcc-expired-subname': {
name: 'subname.name.eth',
basicNameType: 'eth-pcc-expired-subname',
},
'dns-unwrapped-2ld': {
name: 'name.com',
basicNameType: 'dns-unwrapped-2ld',
},
'dns-wrapped-2ld': {
name: 'name.com',
basicNameType: 'dns-wrapped-2ld',
},
'dns-offchain-2ld': {
name: 'name.com',
basicNameType: 'dns-offchain-2ld',
},
'dns-unwrapped-subname': {
name: 'subname.name.com',
basicNameType: 'dns-unwrapped-subname',
},
'dns-wrapped-subname': {
name: 'subname.name.com',
basicNameType: 'dns-wrapped-subname',
},
}

export const mockUseNameTypes = Object.keys(mockUseNameTypeConfig) as MockUseNameTypeType[]

export const makeMockUseNameTypeData = (type: MockUseNameTypeType) => {
return type
}
9 changes: 9 additions & 0 deletions test/mock/makeMockUseOwnerData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createAccounts } from '../../playwright/fixtures/accounts'
import { makeMockUseContractAddress } from './makeMockUseContractAddress'

const mockUseOwnerTypes = [
'root',
'eth',
'registrar',
'registrar:owner',
Expand All @@ -24,7 +25,9 @@ const mockUseOwnerTypes = [
'namewrapper:grace-period',
'registry',
'registry:unowned',
'registry:pcc-expired',
'dns',
'dns:offchain',
] as const

export type MockUseOwnerType = (typeof mockUseOwnerTypes)[number] | undefined
Expand All @@ -34,6 +37,7 @@ const user2Address = createAccounts().getAddress('user2') as Address

export const makeMockUseOwnerData = (type: MockUseOwnerType): GetOwnerReturnType | undefined =>
match(type)
.with('root', () => undefined)
.with('eth', () => ({
owner: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0' as Address,
ownershipLevel: 'registry' as const,
Expand Down Expand Up @@ -94,9 +98,14 @@ export const makeMockUseOwnerData = (type: MockUseOwnerType): GetOwnerReturnType
owner: _type.endsWith('unowned') ? user2Address : userAddress,
ownershipLevel: 'registry' as const,
}))
.with('registry:pcc-expired', () => ({
owner: makeMockUseContractAddress({ contract: 'ensNameWrapper' }),
ownershipLevel: 'registry' as const,
}))
.with('dns', () => ({
owner: '0xB32cB5677a7C971689228EC835800432B339bA2B' as Address,
ownershipLevel: 'registry' as const,
}))
.with('dns:offchain', () => null)
.with(P.nullish, () => null)
.exhaustive()
3 changes: 2 additions & 1 deletion test/mock/makeMockUsePriceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { match, P } from 'ts-pattern'

import { GetPriceReturnType } from '@ensdomains/ensjs/public'

const mockUsePriceTypes = ['tld', 'base', 'premium'] as const
const mockUsePriceTypes = ['root', 'tld', 'base', 'premium'] as const
export type MockUsePriceType = (typeof mockUsePriceTypes)[number] | undefined

export const makeMockUsePriceData = (type: MockUsePriceType): GetPriceReturnType | undefined =>
match(type)
.with('root', () => undefined)
.with('tld', () => undefined)
.with('base', () => ({ base: 3203936997786453n, premium: 0n }))
.with('premium', () => ({
Expand Down
Loading
Loading