-
-
Notifications
You must be signed in to change notification settings - Fork 977
/
Copy pathgetEnsText.ts
158 lines (146 loc) · 4.97 KB
/
getEnsText.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { Address } from 'abitype'
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import {
textResolverAbi,
universalResolverResolveAbi,
} from '../../constants/abis.js'
import type { Chain } from '../../types/chain.js'
import type { Prettify } from '../../types/utils.js'
import {
type DecodeFunctionResultErrorType,
decodeFunctionResult,
} from '../../utils/abi/decodeFunctionResult.js'
import {
type EncodeFunctionDataErrorType,
encodeFunctionData,
} from '../../utils/abi/encodeFunctionData.js'
import {
type GetChainContractAddressErrorType,
getChainContractAddress,
} from '../../utils/chain/getChainContractAddress.js'
import { type ToHexErrorType, toHex } from '../../utils/encoding/toHex.js'
import { isNullUniversalResolverError } from '../../utils/ens/errors.js'
import { type NamehashErrorType, namehash } from '../../utils/ens/namehash.js'
import {
type PacketToBytesErrorType,
packetToBytes,
} from '../../utils/ens/packetToBytes.js'
import { getAction } from '../../utils/getAction.js'
import {
type ReadContractErrorType,
type ReadContractParameters,
readContract,
} from '../public/readContract.js'
export type GetEnsTextParameters = Prettify<
Pick<ReadContractParameters, 'blockNumber' | 'blockTag'> & {
/** ENS name to get Text for. */
name: string
/** Universal Resolver gateway URLs to use for resolving CCIP-read requests. */
gatewayUrls?: string[] | undefined
/** Text record to retrieve. */
key: string
/** Whether or not to throw errors propagated from the ENS Universal Resolver Contract. */
strict?: boolean | undefined
/** Address of ENS Universal Resolver Contract. */
universalResolverAddress?: Address | undefined
}
>
export type GetEnsTextReturnType = string | null
export type GetEnsTextErrorType =
| GetChainContractAddressErrorType
| ReadContractErrorType
| ToHexErrorType
| PacketToBytesErrorType
| EncodeFunctionDataErrorType
| NamehashErrorType
| DecodeFunctionResultErrorType
/**
* Gets a text record for specified ENS name.
*
* - Docs: https://viem.sh/docs/ens/actions/getEnsResolver
* - Examples: https://stackblitz.com/github/wevm/viem/tree/main/examples/ens
*
* Calls `resolve(bytes, bytes)` on ENS Universal Resolver Contract.
*
* Since ENS names prohibit certain forbidden characters (e.g. underscore) and have other validation rules, you likely want to [normalize ENS names](https://docs.ens.domains/contract-api-reference/name-processing#normalising-names) with [UTS-46 normalization](https://unicode.org/reports/tr46) before passing them to `getEnsAddress`. You can use the built-in [`normalize`](https://viem.sh/docs/ens/utilities/normalize) function for this.
*
* @param client - Client to use
* @param parameters - {@link GetEnsTextParameters}
* @returns Address for ENS resolver. {@link GetEnsTextReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { mainnet } from 'viem/chains'
* import { getEnsText, normalize } from 'viem/ens'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const twitterRecord = await getEnsText(client, {
* name: normalize('wevm.eth'),
* key: 'com.twitter',
* })
* // 'wevm_dev'
*/
export async function getEnsText<chain extends Chain | undefined>(
client: Client<Transport, chain>,
{
blockNumber,
blockTag,
name,
key,
gatewayUrls,
strict,
universalResolverAddress: universalResolverAddress_,
}: GetEnsTextParameters,
): Promise<GetEnsTextReturnType> {
let universalResolverAddress = universalResolverAddress_
if (!universalResolverAddress) {
if (!client.chain)
throw new Error(
'client chain not configured. universalResolverAddress is required.',
)
universalResolverAddress = getChainContractAddress({
blockNumber,
chain: client.chain,
contract: 'ensUniversalResolver',
})
}
try {
const readContractParameters = {
address: universalResolverAddress,
abi: universalResolverResolveAbi,
functionName: 'resolve',
args: [
toHex(packetToBytes(name)),
encodeFunctionData({
abi: textResolverAbi,
functionName: 'text',
args: [namehash(name), key],
}),
],
blockNumber,
blockTag,
} as const
const readContractAction = getAction(client, readContract, 'readContract')
const res = gatewayUrls
? await readContractAction({
...readContractParameters,
args: [...readContractParameters.args, gatewayUrls],
})
: await readContractAction(readContractParameters)
if (res[0] === '0x') return null
const record = decodeFunctionResult({
abi: textResolverAbi,
functionName: 'text',
data: res[0],
})
return record === '' ? null : record
} catch (err) {
if (strict) throw err
if (isNullUniversalResolverError(err, 'resolve')) return null
throw err
}
}