Skip to content
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
25 changes: 25 additions & 0 deletions packages/ensjs/src/errors/public.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Address } from 'viem'
import { BaseError } from './base.js'

export class CoinFormatterNotFoundError extends BaseError {
Expand Down Expand Up @@ -29,3 +30,27 @@ export class NoRecordsSpecifiedError extends BaseError {
super('No records specified')
}
}

export class NameNotNormalisedError extends BaseError {
override name = 'NameNotNormalisedError'

address: Address
resolvedName: string
coinType: number

constructor({
address,
resolvedName,
coinType,
}: { address: Address; resolvedName: string; coinType: number }) {
super(`Name ${resolvedName} resolved from address is not normalised`, {
metaMessages: [
`- Resolved from address: ${address}`,
`Resolved for coinType: ${coinType}`,
],
})
this.address = address
this.resolvedName = resolvedName
this.coinType = coinType
}
}
60 changes: 43 additions & 17 deletions packages/ensjs/src/functions/public/getName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import {
RawContractError,
bytesToHex,
encodeErrorResult,
labelhash,
namehash,
} from 'viem'
import { writeContract } from 'viem/actions'
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import type { ClientWithEns } from '../../contracts/consts.js'
import { publicResolverSetAddrSnippet } from '../../contracts/publicResolver.js'
import { registrySetSubnodeRecordSnippet } from '../../contracts/registry.js'
import { universalResolverErrors } from '../../contracts/universalResolver.js'
import {
deploymentAddresses,
Expand All @@ -16,8 +21,6 @@ import {
walletClient,
} from '../../test/addTestContracts.js'
import { packetToBytes } from '../../utils/hexEncodedName.js'
import createSubname from '../wallet/createSubname.js'
import setAddressRecord from '../wallet/setAddressRecord.js'
import setPrimaryName from '../wallet/setPrimaryName.js'
import getName from './getName.js'

Expand Down Expand Up @@ -151,20 +154,26 @@ describe('getName', () => {
`)
})
it('should not return unnormalised name', async () => {
const tx1 = await createSubname(walletClient, {
name: 'suB.with-profile.eth',
contract: 'registry',
owner: accounts[0],
resolverAddress: deploymentAddresses.PublicResolver,
account: accounts[0],
const tx1 = await writeContract(walletClient, {
abi: registrySetSubnodeRecordSnippet,
account: accounts[2],
address: deploymentAddresses.ENSRegistry,
functionName: 'setSubnodeRecord',
args: [
namehash('with-profile.eth'),
labelhash('suB'),
accounts[0],
deploymentAddresses.PublicResolver,
0n,
],
})
await waitForTransaction(tx1)
const tx2 = await setAddressRecord(walletClient, {
name: 'suB.with-profile.eth',
coin: 'eth',
resolverAddress: deploymentAddresses.PublicResolver,
value: accounts[0],
const tx2 = await writeContract(walletClient, {
abi: publicResolverSetAddrSnippet,
account: accounts[0],
address: deploymentAddresses.PublicResolver,
functionName: 'setAddr',
args: [namehash('suB.with-profile.eth'), 60n, accounts[0]],
})
await waitForTransaction(tx2)
const tx3 = await setPrimaryName(walletClient, {
Expand All @@ -173,10 +182,27 @@ describe('getName', () => {
})
await waitForTransaction(tx3)

const result = await getName(publicClient, {
address: accounts[0],
})
// Should throw NameNotNormalisedError
await expect(
getName(publicClient, {
address: accounts[0],
strict: true,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[NameNotNormalisedError: Name suB.with-profile.eth resolved from address is not normalised

expect(result).toBeNull()
- Resolved from address: 0x82e01223d51Eb87e16A03E24687EDF0F294da6f1
Resolved for coinType: 60

Version: @ensdomains/ensjs@1.0.0-mock.0]
`)

// should return null when strict is false
await expect(
getName(publicClient, {
address: accounts[0],
strict: false,
}),
).resolves.toBeNull()
})
})
10 changes: 8 additions & 2 deletions packages/ensjs/src/functions/public/getName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
universalResolverReverseSnippet,
universalResolverReverseWithGatewaysSnippet,
} from '../../contracts/universalResolver.js'
import { NameNotNormalisedError } from '../../errors/public.js'
import type {
GenericPassthrough,
TransactionRequestWithPassthrough,
Expand Down Expand Up @@ -154,9 +155,14 @@ const decode = async (

if (!unnormalisedName) return null

const normalisedName = normalise(unnormalisedName)
if (unnormalisedName !== normalise(unnormalisedName))
throw new NameNotNormalisedError({
address: passthrough.address,
resolvedName: unnormalisedName,
coinType: passthrough.args[1] as number,
})
return {
name: normalisedName,
name: unnormalisedName,
match: true,
reverseResolverAddress,
resolverAddress,
Expand Down
36 changes: 21 additions & 15 deletions packages/ensjs/src/test/addTestContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const publicClient: PublicClient<typeof transport, typeof localhost> =
createPublicClient({
chain: localhost,
transport,
cacheTime: 0,
})

export const testClient: TestClient<
Expand All @@ -125,18 +126,23 @@ export const walletClient: WalletClient<
transport,
})

export const waitForTransaction = async (hash: Hash) =>
new Promise<TransactionReceipt>((resolveFn, reject) => {
publicClient
.getTransactionReceipt({ hash })
.then(resolveFn)
.catch((e) => {
if (e instanceof TransactionReceiptNotFoundError) {
setTimeout(() => {
waitForTransaction(hash).then(resolveFn)
}, 100)
} else {
reject(e)
}
})
})
export const waitForTransaction = async (
hash: Hash,
): Promise<TransactionReceipt> => {
const receipt = await publicClient
.getTransactionReceipt({ hash })
.catch((e) => {
if (e instanceof TransactionReceiptNotFoundError) return null
throw e
})
if (receipt === null) {
return new Promise<TransactionReceipt>((resolve, reject) => {
setTimeout(() => {
waitForTransaction(hash).then(resolve).catch(reject)
}, 100)
})
}

if (receipt.status !== 'success') throw new Error('Transaction failed')
return receipt
}
Loading