@ensdomains/ensjs 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/dist/cjs/clients/decorators/public.js.map +1 -1
  2. package/dist/cjs/contracts/consts.js +3 -3
  3. package/dist/cjs/contracts/universalResolver.js +92 -59
  4. package/dist/cjs/contracts/universalResolver.js.map +1 -1
  5. package/dist/cjs/errors/version.js +1 -1
  6. package/dist/cjs/functions/public/_getAbi.js +52 -45
  7. package/dist/cjs/functions/public/_getAbi.js.map +1 -1
  8. package/dist/cjs/functions/public/_getAddr.js +30 -23
  9. package/dist/cjs/functions/public/_getAddr.js.map +1 -1
  10. package/dist/cjs/functions/public/_getContentHash.js +14 -7
  11. package/dist/cjs/functions/public/_getContentHash.js.map +1 -1
  12. package/dist/cjs/functions/public/_getText.js +15 -8
  13. package/dist/cjs/functions/public/_getText.js.map +1 -1
  14. package/dist/cjs/functions/public/getAbiRecord.js +13 -5
  15. package/dist/cjs/functions/public/getAbiRecord.js.map +1 -1
  16. package/dist/cjs/functions/public/getAddressRecord.js +13 -5
  17. package/dist/cjs/functions/public/getAddressRecord.js.map +1 -1
  18. package/dist/cjs/functions/public/getContentHashRecord.js +13 -5
  19. package/dist/cjs/functions/public/getContentHashRecord.js.map +1 -1
  20. package/dist/cjs/functions/public/getName.js +53 -35
  21. package/dist/cjs/functions/public/getName.js.map +1 -1
  22. package/dist/cjs/functions/public/getRecords.js +164 -161
  23. package/dist/cjs/functions/public/getRecords.js.map +1 -1
  24. package/dist/cjs/functions/public/getTextRecord.js +13 -5
  25. package/dist/cjs/functions/public/getTextRecord.js.map +1 -1
  26. package/dist/cjs/functions/public/universalWrapper.js +45 -37
  27. package/dist/cjs/functions/public/universalWrapper.js.map +1 -1
  28. package/dist/cjs/utils/checkSafeUniversalResolverData.js +35 -0
  29. package/dist/cjs/utils/checkSafeUniversalResolverData.js.map +1 -0
  30. package/dist/esm/clients/decorators/public.js.map +1 -1
  31. package/dist/esm/contracts/consts.js +3 -3
  32. package/dist/esm/contracts/universalResolver.js +92 -59
  33. package/dist/esm/contracts/universalResolver.js.map +1 -1
  34. package/dist/esm/errors/version.js +1 -1
  35. package/dist/esm/functions/public/_getAbi.js +56 -49
  36. package/dist/esm/functions/public/_getAbi.js.map +1 -1
  37. package/dist/esm/functions/public/_getAddr.js +30 -23
  38. package/dist/esm/functions/public/_getAddr.js.map +1 -1
  39. package/dist/esm/functions/public/_getContentHash.js +14 -7
  40. package/dist/esm/functions/public/_getContentHash.js.map +1 -1
  41. package/dist/esm/functions/public/_getText.js +15 -8
  42. package/dist/esm/functions/public/_getText.js.map +1 -1
  43. package/dist/esm/functions/public/getAbiRecord.js +13 -5
  44. package/dist/esm/functions/public/getAbiRecord.js.map +1 -1
  45. package/dist/esm/functions/public/getAddressRecord.js +13 -5
  46. package/dist/esm/functions/public/getAddressRecord.js.map +1 -1
  47. package/dist/esm/functions/public/getContentHashRecord.js +13 -5
  48. package/dist/esm/functions/public/getContentHashRecord.js.map +1 -1
  49. package/dist/esm/functions/public/getName.js +54 -36
  50. package/dist/esm/functions/public/getName.js.map +1 -1
  51. package/dist/esm/functions/public/getRecords.js +168 -165
  52. package/dist/esm/functions/public/getRecords.js.map +1 -1
  53. package/dist/esm/functions/public/getTextRecord.js +13 -5
  54. package/dist/esm/functions/public/getTextRecord.js.map +1 -1
  55. package/dist/esm/functions/public/universalWrapper.js +46 -38
  56. package/dist/esm/functions/public/universalWrapper.js.map +1 -1
  57. package/dist/esm/utils/checkSafeUniversalResolverData.js +39 -0
  58. package/dist/esm/utils/checkSafeUniversalResolverData.js.map +1 -0
  59. package/dist/types/clients/decorators/public.d.ts +6 -6
  60. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  61. package/dist/types/clients/public.d.ts +34 -8
  62. package/dist/types/clients/public.d.ts.map +1 -1
  63. package/dist/types/contracts/consts.d.ts +3 -3
  64. package/dist/types/contracts/universalResolver.d.ts +73 -0
  65. package/dist/types/contracts/universalResolver.d.ts.map +1 -1
  66. package/dist/types/errors/version.d.ts +1 -1
  67. package/dist/types/functions/public/_getAbi.d.ts +3 -1
  68. package/dist/types/functions/public/_getAbi.d.ts.map +1 -1
  69. package/dist/types/functions/public/_getAddr.d.ts +3 -1
  70. package/dist/types/functions/public/_getAddr.d.ts.map +1 -1
  71. package/dist/types/functions/public/_getContentHash.d.ts +3 -1
  72. package/dist/types/functions/public/_getContentHash.d.ts.map +1 -1
  73. package/dist/types/functions/public/_getText.d.ts +3 -1
  74. package/dist/types/functions/public/_getText.d.ts.map +1 -1
  75. package/dist/types/functions/public/getAbiRecord.d.ts +8 -5
  76. package/dist/types/functions/public/getAbiRecord.d.ts.map +1 -1
  77. package/dist/types/functions/public/getAddressRecord.d.ts +8 -5
  78. package/dist/types/functions/public/getAddressRecord.d.ts.map +1 -1
  79. package/dist/types/functions/public/getContentHashRecord.d.ts +7 -4
  80. package/dist/types/functions/public/getContentHashRecord.d.ts.map +1 -1
  81. package/dist/types/functions/public/getName.d.ts +9 -3
  82. package/dist/types/functions/public/getName.d.ts.map +1 -1
  83. package/dist/types/functions/public/getRecords.d.ts +53 -26
  84. package/dist/types/functions/public/getRecords.d.ts.map +1 -1
  85. package/dist/types/functions/public/getTextRecord.d.ts +7 -4
  86. package/dist/types/functions/public/getTextRecord.d.ts.map +1 -1
  87. package/dist/types/functions/public/universalWrapper.d.ts +4 -2
  88. package/dist/types/functions/public/universalWrapper.d.ts.map +1 -1
  89. package/dist/types/types.d.ts +2 -2
  90. package/dist/types/types.d.ts.map +1 -1
  91. package/dist/types/utils/checkSafeUniversalResolverData.d.ts +14 -0
  92. package/dist/types/utils/checkSafeUniversalResolverData.d.ts.map +1 -0
  93. package/package.json +1 -1
  94. package/src/clients/decorators/public.ts +31 -4
  95. package/src/contracts/consts.ts +3 -3
  96. package/src/contracts/universalResolver.ts +95 -59
  97. package/src/errors/version.ts +1 -1
  98. package/src/functions/public/_getAbi.ts +61 -53
  99. package/src/functions/public/_getAddr.ts +31 -24
  100. package/src/functions/public/_getContentHash.ts +15 -7
  101. package/src/functions/public/_getText.ts +16 -8
  102. package/src/functions/public/getAbiRecord.ts +21 -8
  103. package/src/functions/public/getAddressRecord.ts +25 -9
  104. package/src/functions/public/getContentHashRecord.ts +19 -7
  105. package/src/functions/public/getName.ts +58 -40
  106. package/src/functions/public/getRecords.ts +336 -235
  107. package/src/functions/public/getTextRecord.ts +19 -6
  108. package/src/functions/public/universalWrapper.ts +49 -43
  109. package/src/types.ts +4 -3
  110. package/src/utils/checkSafeUniversalResolverData.ts +62 -0
@@ -15,25 +15,38 @@ import _getText, {
15
15
  } from './_getText.js'
16
16
  import universalWrapper from './universalWrapper.js'
17
17
 
18
- export type GetTextRecordParameters = Prettify<InternalGetTextParameters>
18
+ export type GetTextRecordParameters = Prettify<
19
+ InternalGetTextParameters & {
20
+ /** Batch gateway URLs to use for resolving CCIP-read requests. */
21
+ gatewayUrls?: string[]
22
+ }
23
+ >
19
24
 
20
25
  export type GetTextRecordReturnType = Prettify<InternalGetTextReturnType>
21
26
 
22
27
  const encode = (
23
28
  client: ClientWithEns,
24
- { name, key }: GetTextRecordParameters,
29
+ { name, key, gatewayUrls }: Omit<GetTextRecordParameters, 'strict'>,
25
30
  ): SimpleTransactionRequest => {
26
31
  const prData = _getText.encode(client, { name, key })
27
- return universalWrapper.encode(client, { name, data: prData.data })
32
+ return universalWrapper.encode(client, {
33
+ name,
34
+ data: prData.data,
35
+ gatewayUrls,
36
+ })
28
37
  }
29
38
 
30
39
  const decode = async (
31
40
  client: ClientWithEns,
32
41
  data: Hex | BaseError,
33
42
  passthrough: GenericPassthrough,
43
+ { strict }: Pick<GetTextRecordParameters, 'strict'>,
34
44
  ): Promise<GetTextRecordReturnType> => {
35
- const urData = await universalWrapper.decode(client, data, passthrough)
36
- return _getText.decode(client, urData.data)
45
+ const urData = await universalWrapper.decode(client, data, passthrough, {
46
+ strict,
47
+ })
48
+ if (!urData) return null
49
+ return _getText.decode(client, urData.data, { strict })
37
50
  }
38
51
 
39
52
  type BatchableFunctionObject = GeneratedFunction<typeof encode, typeof decode>
@@ -59,7 +72,7 @@ type BatchableFunctionObject = GeneratedFunction<typeof encode, typeof decode>
59
72
  */
60
73
  const getTextRecord = generateFunction({ encode, decode }) as ((
61
74
  client: ClientWithEns,
62
- { name, key }: GetTextRecordParameters,
75
+ { name, key, strict }: GetTextRecordParameters,
63
76
  ) => Promise<GetTextRecordReturnType>) &
64
77
  BatchableFunctionObject
65
78
 
@@ -1,9 +1,7 @@
1
1
  import {
2
2
  BaseError,
3
- decodeErrorResult,
4
3
  decodeFunctionResult,
5
4
  encodeFunctionData,
6
- getContractError,
7
5
  labelhash,
8
6
  toBytes,
9
7
  toHex,
@@ -17,25 +15,26 @@ import type {
17
15
  GenericPassthrough,
18
16
  TransactionRequestWithPassthrough,
19
17
  } from '../../types.js'
20
- import { EMPTY_ADDRESS } from '../../utils/consts.js'
18
+ import { checkSafeUniversalResolverData } from '../../utils/checkSafeUniversalResolverData.js'
21
19
  import { generateFunction } from '../../utils/generateFunction.js'
22
- import { getRevertErrorData } from '../../utils/getRevertErrorData.js'
23
20
  import { packetToBytes } from '../../utils/hexEncodedName.js'
24
21
  import { encodeLabelhash } from '../../utils/labels.js'
25
22
 
26
23
  export type UniversalWrapperParameters = {
27
24
  name: string
28
25
  data: Hex
26
+ strict?: boolean
27
+ gatewayUrls?: string[]
29
28
  }
30
29
 
31
30
  export type UniversalWrapperReturnType = {
32
31
  data: Hex
33
32
  resolver: Address
34
- }
33
+ } | null
35
34
 
36
35
  const encode = (
37
36
  client: ClientWithEns,
38
- { name, data }: UniversalWrapperParameters,
37
+ { name, data, gatewayUrls }: Omit<UniversalWrapperParameters, 'strict'>,
39
38
  ): TransactionRequestWithPassthrough => {
40
39
  const nameWithSizedLabels = name
41
40
  .split('.')
@@ -52,17 +51,32 @@ const encode = (
52
51
  contract: 'ensUniversalResolver',
53
52
  })
54
53
  const args = [toHex(packetToBytes(nameWithSizedLabels)), data] as const
54
+
55
55
  return {
56
- to: getChainContractAddress({ client, contract: 'ensUniversalResolver' }),
57
- data: encodeFunctionData({
58
- abi: universalResolverResolveSnippet,
59
- functionName: 'resolve',
60
- args,
61
- }),
62
- passthrough: {
63
- args,
64
- address: to,
65
- },
56
+ to,
57
+ ...(gatewayUrls?.length
58
+ ? {
59
+ data: encodeFunctionData({
60
+ abi: universalResolverResolveSnippet,
61
+ functionName: 'resolve',
62
+ args: [...args, gatewayUrls] as const,
63
+ }),
64
+ passthrough: {
65
+ args: [...args, gatewayUrls],
66
+ address: to,
67
+ },
68
+ }
69
+ : {
70
+ data: encodeFunctionData({
71
+ abi: universalResolverResolveSnippet,
72
+ functionName: 'resolve',
73
+ args,
74
+ }),
75
+ passthrough: {
76
+ args,
77
+ address: to,
78
+ },
79
+ }),
66
80
  }
67
81
  }
68
82
 
@@ -70,37 +84,29 @@ const decode = async (
70
84
  _client: ClientWithEns,
71
85
  data: Hex | BaseError,
72
86
  passthrough: GenericPassthrough,
87
+ { strict }: Pick<UniversalWrapperParameters, 'strict'>,
73
88
  ): Promise<UniversalWrapperReturnType> => {
74
- if (typeof data === 'object') {
75
- const errorData = getRevertErrorData(data)
76
- if (errorData) {
77
- const decodedError = decodeErrorResult({
78
- abi: universalResolverResolveSnippet,
79
- data: errorData,
80
- })
81
- if (
82
- decodedError.errorName === 'ResolverNotFound' ||
83
- decodedError.errorName === 'ResolverWildcardNotSupported'
84
- )
85
- return {
86
- data: '0x',
87
- resolver: EMPTY_ADDRESS,
88
- }
89
- }
90
- throw getContractError(data, {
91
- abi: universalResolverResolveSnippet,
92
- functionName: 'resolve',
93
- args: passthrough.args,
94
- address: passthrough.address,
95
- }) as BaseError
96
- }
97
-
98
- const result = decodeFunctionResult({
89
+ const isSafe = checkSafeUniversalResolverData(data, {
90
+ strict,
99
91
  abi: universalResolverResolveSnippet,
92
+ args: passthrough.args,
100
93
  functionName: 'resolve',
101
- data,
94
+ address: passthrough.address,
102
95
  })
103
- return { data: result[0], resolver: result[1] }
96
+
97
+ if (!isSafe) return null
98
+
99
+ try {
100
+ const result = decodeFunctionResult({
101
+ abi: universalResolverResolveSnippet,
102
+ functionName: 'resolve',
103
+ data,
104
+ })
105
+ return { data: result[0], resolver: result[1] }
106
+ } catch (error) {
107
+ if (strict) throw error
108
+ return null
109
+ }
104
110
  }
105
111
 
106
112
  const universalWrapper = generateFunction({ encode, decode })
package/src/types.ts CHANGED
@@ -18,9 +18,10 @@ export type SimpleTransactionRequest = {
18
18
  >
19
19
  }
20
20
 
21
- export type TransactionRequestWithPassthrough = SimpleTransactionRequest & {
22
- passthrough?: any
23
- }
21
+ export type TransactionRequestWithPassthrough<TPassthrough = any> =
22
+ SimpleTransactionRequest & {
23
+ passthrough?: TPassthrough
24
+ }
24
25
 
25
26
  export type GenericPassthrough = { args: any; address: Address }
26
27
 
@@ -0,0 +1,62 @@
1
+ import {
2
+ CallExecutionError,
3
+ decodeErrorResult,
4
+ getContractError,
5
+ type BaseError,
6
+ type Hex,
7
+ } from 'viem'
8
+ import type { Prettify } from '../types.js'
9
+ import { getRevertErrorData } from './getRevertErrorData.js'
10
+
11
+ type CheckSafeUniversalResolverDataParameters = Prettify<
12
+ {
13
+ strict: boolean | undefined
14
+ args: any[] | (() => any[])
15
+ } & Omit<Parameters<typeof getContractError>[1], 'args'>
16
+ >
17
+
18
+ /**
19
+ * Checks if the data returned from a universal resolver is safe to use, or if it is a known revert error
20
+ * @param parameters - {@link CheckSafeUniversalResolverDataParameters}
21
+ * @returns `true` if the data is safe to use, `false` if it is a known revert error, or throws if it is an unknown error
22
+ */
23
+ export const checkSafeUniversalResolverData = (
24
+ data: Hex | BaseError,
25
+ {
26
+ strict,
27
+ abi,
28
+ args,
29
+ functionName,
30
+ address,
31
+ docsPath,
32
+ sender,
33
+ }: CheckSafeUniversalResolverDataParameters,
34
+ ): data is Hex => {
35
+ if (typeof data === 'object') {
36
+ // if not strict, attempt to check if the error is a known revert error before throwing
37
+ // if it is a known revert error, return false instead of throwing
38
+ // even if strict is false, we want to throw unknown contract errors
39
+ if (!strict) {
40
+ const errorData = getRevertErrorData(data)
41
+ if (errorData) {
42
+ try {
43
+ decodeErrorResult({
44
+ abi,
45
+ data: errorData,
46
+ })
47
+ return false
48
+ } catch (error) {}
49
+ }
50
+ }
51
+ if (data instanceof CallExecutionError) throw data
52
+ throw getContractError(data, {
53
+ abi,
54
+ args: typeof args === 'function' ? args() : args,
55
+ functionName,
56
+ address,
57
+ docsPath,
58
+ sender,
59
+ }) as BaseError
60
+ }
61
+ return true
62
+ }