viem 2.57.18 → 2.57.20

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 (222) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/_cjs/accounts/index.js +2 -1
  3. package/_cjs/accounts/index.js.map +1 -1
  4. package/_cjs/accounts/utils/publicKeyToAddress.js +15 -0
  5. package/_cjs/accounts/utils/publicKeyToAddress.js.map +1 -1
  6. package/_cjs/actions/ens/getEnsText.js.map +1 -1
  7. package/_cjs/actions/public/call.js +19 -3
  8. package/_cjs/actions/public/call.js.map +1 -1
  9. package/_cjs/actions/public/estimateMaxPriorityFeePerGas.js +1 -1
  10. package/_cjs/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -1
  11. package/_cjs/actions/public/getDelegation.js +2 -1
  12. package/_cjs/actions/public/getDelegation.js.map +1 -1
  13. package/_cjs/actions/public/getFeeHistory.js.map +1 -1
  14. package/_cjs/actions/token/internal.js +2 -10
  15. package/_cjs/actions/token/internal.js.map +1 -1
  16. package/_cjs/actions/wallet/deployContract.js +2 -1
  17. package/_cjs/actions/wallet/deployContract.js.map +1 -1
  18. package/_cjs/actions/wallet/prepareTransactionRequest.js +99 -76
  19. package/_cjs/actions/wallet/prepareTransactionRequest.js.map +1 -1
  20. package/_cjs/actions/wallet/sendCalls.js +2 -1
  21. package/_cjs/actions/wallet/sendCalls.js.map +1 -1
  22. package/_cjs/actions/wallet/signAuthorization.js +2 -1
  23. package/_cjs/actions/wallet/signAuthorization.js.map +1 -1
  24. package/_cjs/chains/definitions/darwinia.js +1 -1
  25. package/_cjs/chains/definitions/darwinia.js.map +1 -1
  26. package/_cjs/chains/definitions/flowMainnet.js +1 -1
  27. package/_cjs/chains/definitions/flowMainnet.js.map +1 -1
  28. package/_cjs/chains/definitions/kinetiqElysium.js +30 -0
  29. package/_cjs/chains/definitions/kinetiqElysium.js.map +1 -0
  30. package/_cjs/chains/definitions/kinetiqElysiumTestnet.js +31 -0
  31. package/_cjs/chains/definitions/kinetiqElysiumTestnet.js.map +1 -0
  32. package/_cjs/chains/index.js +14 -9
  33. package/_cjs/chains/index.js.map +1 -1
  34. package/_cjs/errors/encoding.js +9 -1
  35. package/_cjs/errors/encoding.js.map +1 -1
  36. package/_cjs/errors/typedData.js +2 -0
  37. package/_cjs/errors/typedData.js.map +1 -1
  38. package/_cjs/errors/version.js +1 -1
  39. package/_cjs/index.js +6 -5
  40. package/_cjs/index.js.map +1 -1
  41. package/_cjs/tempo/actions/accounts.js +14 -4
  42. package/_cjs/tempo/actions/accounts.js.map +1 -1
  43. package/_cjs/tempo/chainConfig.js +23 -1
  44. package/_cjs/tempo/chainConfig.js.map +1 -1
  45. package/_cjs/tempo/internal/relay.js +2 -2
  46. package/_cjs/tempo/internal/relay.js.map +1 -1
  47. package/_cjs/tempo/internal/utils.js +2 -10
  48. package/_cjs/tempo/internal/utils.js.map +1 -1
  49. package/_cjs/tokens/defineToken.js +1 -1
  50. package/_cjs/tokens/defineToken.js.map +1 -1
  51. package/_cjs/tokens/definitions/ousd.js +1 -0
  52. package/_cjs/tokens/definitions/ousd.js.map +1 -1
  53. package/_cjs/utils/address/getAddress.js +7 -6
  54. package/_cjs/utils/address/getAddress.js.map +1 -1
  55. package/_cjs/utils/address/isAddress.js +8 -12
  56. package/_cjs/utils/address/isAddress.js.map +1 -1
  57. package/_cjs/utils/encoding/fromRlp.js +33 -8
  58. package/_cjs/utils/encoding/fromRlp.js.map +1 -1
  59. package/_cjs/utils/index.js +5 -4
  60. package/_cjs/utils/index.js.map +1 -1
  61. package/_cjs/utils/promise/createBatchScheduler.js +7 -2
  62. package/_cjs/utils/promise/createBatchScheduler.js.map +1 -1
  63. package/_cjs/utils/typedData.js +5 -9
  64. package/_cjs/utils/typedData.js.map +1 -1
  65. package/_esm/accounts/index.js +1 -1
  66. package/_esm/accounts/index.js.map +1 -1
  67. package/_esm/accounts/utils/publicKeyToAddress.js +20 -1
  68. package/_esm/accounts/utils/publicKeyToAddress.js.map +1 -1
  69. package/_esm/actions/ens/getEnsText.js.map +1 -1
  70. package/_esm/actions/public/call.js +19 -3
  71. package/_esm/actions/public/call.js.map +1 -1
  72. package/_esm/actions/public/estimateMaxPriorityFeePerGas.js +1 -1
  73. package/_esm/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -1
  74. package/_esm/actions/public/getDelegation.js +2 -1
  75. package/_esm/actions/public/getDelegation.js.map +1 -1
  76. package/_esm/actions/public/getFeeHistory.js.map +1 -1
  77. package/_esm/actions/token/internal.js +2 -10
  78. package/_esm/actions/token/internal.js.map +1 -1
  79. package/_esm/actions/wallet/deployContract.js +2 -1
  80. package/_esm/actions/wallet/deployContract.js.map +1 -1
  81. package/_esm/actions/wallet/prepareTransactionRequest.js +111 -79
  82. package/_esm/actions/wallet/prepareTransactionRequest.js.map +1 -1
  83. package/_esm/actions/wallet/sendCalls.js +2 -1
  84. package/_esm/actions/wallet/sendCalls.js.map +1 -1
  85. package/_esm/actions/wallet/signAuthorization.js +2 -1
  86. package/_esm/actions/wallet/signAuthorization.js.map +1 -1
  87. package/_esm/chains/definitions/darwinia.js +1 -1
  88. package/_esm/chains/definitions/darwinia.js.map +1 -1
  89. package/_esm/chains/definitions/flowMainnet.js +1 -1
  90. package/_esm/chains/definitions/flowMainnet.js.map +1 -1
  91. package/_esm/chains/definitions/kinetiqElysium.js +27 -0
  92. package/_esm/chains/definitions/kinetiqElysium.js.map +1 -0
  93. package/_esm/chains/definitions/kinetiqElysiumTestnet.js +28 -0
  94. package/_esm/chains/definitions/kinetiqElysiumTestnet.js.map +1 -0
  95. package/_esm/chains/index.js +2 -0
  96. package/_esm/chains/index.js.map +1 -1
  97. package/_esm/errors/encoding.js +8 -0
  98. package/_esm/errors/encoding.js.map +1 -1
  99. package/_esm/errors/typedData.js +2 -0
  100. package/_esm/errors/typedData.js.map +1 -1
  101. package/_esm/errors/version.js +1 -1
  102. package/_esm/index.js +1 -1
  103. package/_esm/index.js.map +1 -1
  104. package/_esm/tempo/actions/accounts.js +19 -5
  105. package/_esm/tempo/actions/accounts.js.map +1 -1
  106. package/_esm/tempo/chainConfig.js +25 -1
  107. package/_esm/tempo/chainConfig.js.map +1 -1
  108. package/_esm/tempo/internal/relay/feePayer.js +1 -1
  109. package/_esm/tempo/internal/relay/feePayer.js.map +1 -1
  110. package/_esm/tempo/internal/relay.js +8 -3
  111. package/_esm/tempo/internal/relay.js.map +1 -1
  112. package/_esm/tempo/internal/utils.js +2 -10
  113. package/_esm/tempo/internal/utils.js.map +1 -1
  114. package/_esm/tokens/defineToken.js +2 -1
  115. package/_esm/tokens/defineToken.js.map +1 -1
  116. package/_esm/tokens/definitions/ousd.js +2 -1
  117. package/_esm/tokens/definitions/ousd.js.map +1 -1
  118. package/_esm/utils/address/getAddress.js +32 -6
  119. package/_esm/utils/address/getAddress.js.map +1 -1
  120. package/_esm/utils/address/isAddress.js +32 -12
  121. package/_esm/utils/address/isAddress.js.map +1 -1
  122. package/_esm/utils/encoding/fromRlp.js +37 -9
  123. package/_esm/utils/encoding/fromRlp.js.map +1 -1
  124. package/_esm/utils/index.js +1 -1
  125. package/_esm/utils/index.js.map +1 -1
  126. package/_esm/utils/promise/createBatchScheduler.js +7 -2
  127. package/_esm/utils/promise/createBatchScheduler.js.map +1 -1
  128. package/_esm/utils/typedData.js +7 -9
  129. package/_esm/utils/typedData.js.map +1 -1
  130. package/_types/accounts/index.d.ts +1 -1
  131. package/_types/accounts/index.d.ts.map +1 -1
  132. package/_types/accounts/utils/publicKeyToAddress.d.ts +16 -2
  133. package/_types/accounts/utils/publicKeyToAddress.d.ts.map +1 -1
  134. package/_types/actions/ens/getEnsText.d.ts +2 -1
  135. package/_types/actions/ens/getEnsText.d.ts.map +1 -1
  136. package/_types/actions/public/call.d.ts +1 -1
  137. package/_types/actions/public/call.d.ts.map +1 -1
  138. package/_types/actions/public/estimateMaxPriorityFeePerGas.d.ts +1 -1
  139. package/_types/actions/public/estimateMaxPriorityFeePerGas.d.ts.map +1 -1
  140. package/_types/actions/public/getDelegation.d.ts.map +1 -1
  141. package/_types/actions/public/getFeeHistory.d.ts +2 -1
  142. package/_types/actions/public/getFeeHistory.d.ts.map +1 -1
  143. package/_types/actions/token/internal.d.ts.map +1 -1
  144. package/_types/actions/wallet/deployContract.d.ts.map +1 -1
  145. package/_types/actions/wallet/prepareTransactionRequest.d.ts +2 -1
  146. package/_types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -1
  147. package/_types/actions/wallet/sendCalls.d.ts.map +1 -1
  148. package/_types/actions/wallet/signAuthorization.d.ts.map +1 -1
  149. package/_types/chains/definitions/darwinia.d.ts +1 -1
  150. package/_types/chains/definitions/darwinia.d.ts.map +1 -1
  151. package/_types/chains/definitions/flowMainnet.d.ts +1 -1
  152. package/_types/chains/definitions/flowMainnet.d.ts.map +1 -1
  153. package/_types/chains/definitions/kinetiqElysium.d.ts +52 -0
  154. package/_types/chains/definitions/kinetiqElysium.d.ts.map +1 -0
  155. package/_types/chains/definitions/kinetiqElysiumTestnet.d.ts +52 -0
  156. package/_types/chains/definitions/kinetiqElysiumTestnet.d.ts.map +1 -0
  157. package/_types/chains/index.d.ts +2 -0
  158. package/_types/chains/index.d.ts.map +1 -1
  159. package/_types/errors/encoding.d.ts +9 -0
  160. package/_types/errors/encoding.d.ts.map +1 -1
  161. package/_types/errors/typedData.d.ts.map +1 -1
  162. package/_types/errors/version.d.ts +1 -1
  163. package/_types/index.d.ts +1 -1
  164. package/_types/index.d.ts.map +1 -1
  165. package/_types/tempo/actions/accounts.d.ts +4 -1
  166. package/_types/tempo/actions/accounts.d.ts.map +1 -1
  167. package/_types/tempo/chainConfig.d.ts.map +1 -1
  168. package/_types/tempo/internal/utils.d.ts.map +1 -1
  169. package/_types/tokens/defineToken.d.ts +5 -1
  170. package/_types/tokens/defineToken.d.ts.map +1 -1
  171. package/_types/tokens/definitions/ousd.d.ts +2 -1
  172. package/_types/tokens/definitions/ousd.d.ts.map +1 -1
  173. package/_types/tokens/sets.d.ts +3 -0
  174. package/_types/tokens/sets.d.ts.map +1 -1
  175. package/_types/utils/address/getAddress.d.ts +23 -0
  176. package/_types/utils/address/getAddress.d.ts.map +1 -1
  177. package/_types/utils/address/isAddress.d.ts +22 -0
  178. package/_types/utils/address/isAddress.d.ts.map +1 -1
  179. package/_types/utils/encoding/fromRlp.d.ts +3 -3
  180. package/_types/utils/encoding/fromRlp.d.ts.map +1 -1
  181. package/_types/utils/index.d.ts +1 -1
  182. package/_types/utils/index.d.ts.map +1 -1
  183. package/_types/utils/promise/createBatchScheduler.d.ts.map +1 -1
  184. package/_types/utils/typedData.d.ts.map +1 -1
  185. package/_types/zksync/errors/bridge.d.ts +1 -1
  186. package/_types/zksync/errors/bridge.d.ts.map +1 -1
  187. package/accounts/index.ts +2 -0
  188. package/accounts/utils/publicKeyToAddress.ts +27 -1
  189. package/actions/ens/getEnsText.ts +2 -0
  190. package/actions/public/call.ts +20 -3
  191. package/actions/public/estimateMaxPriorityFeePerGas.ts +2 -2
  192. package/actions/public/getDelegation.ts +6 -1
  193. package/actions/public/getFeeHistory.ts +2 -0
  194. package/actions/token/internal.ts +2 -10
  195. package/actions/wallet/deployContract.ts +6 -1
  196. package/actions/wallet/prepareTransactionRequest.ts +138 -107
  197. package/actions/wallet/sendCalls.ts +6 -1
  198. package/actions/wallet/signAuthorization.ts +6 -1
  199. package/chains/definitions/darwinia.ts +1 -1
  200. package/chains/definitions/flowMainnet.ts +1 -1
  201. package/chains/definitions/kinetiqElysium.ts +27 -0
  202. package/chains/definitions/kinetiqElysiumTestnet.ts +28 -0
  203. package/chains/index.ts +2 -0
  204. package/errors/encoding.ts +12 -0
  205. package/errors/typedData.ts +2 -0
  206. package/errors/version.ts +1 -1
  207. package/index.ts +2 -0
  208. package/package.json +2 -2
  209. package/tempo/actions/accounts.ts +24 -6
  210. package/tempo/chainConfig.ts +33 -5
  211. package/tempo/internal/relay/feePayer.ts +1 -1
  212. package/tempo/internal/relay.ts +8 -3
  213. package/tempo/internal/utils.ts +2 -10
  214. package/tokens/defineToken.ts +6 -2
  215. package/tokens/definitions/ousd.ts +2 -1
  216. package/utils/address/getAddress.ts +31 -6
  217. package/utils/address/isAddress.ts +31 -10
  218. package/utils/encoding/fromRlp.ts +37 -5
  219. package/utils/index.ts +2 -0
  220. package/utils/promise/createBatchScheduler.ts +10 -2
  221. package/utils/typedData.ts +7 -9
  222. package/zksync/errors/bridge.ts +1 -1
@@ -17,6 +17,8 @@ export type Tokens = readonly Token[]
17
17
  export type ResolvedToken = {
18
18
  /** Token contract address. */
19
19
  address: Address
20
+ /** Chain id the token was resolved for. */
21
+ chainId: number
20
22
  /** Token currency denomination. */
21
23
  currency?: string | undefined
22
24
  /** Token decimals, used to parse human-readable `amount` strings. */
@@ -57,6 +59,7 @@ export type ResolvedToken = {
57
59
  * usdc(1)
58
60
  * // {
59
61
  * // address: '0xA0b8…48',
62
+ * // chainId: 1,
60
63
  * // currency: 'USD',
61
64
  * // decimals: 6,
62
65
  * // name: 'USD Coin',
@@ -76,7 +79,7 @@ export function defineToken<const token extends defineToken.Parameters>(
76
79
  const address = (addresses as Record<number, Address>)[chainId]
77
80
  if (!address)
78
81
  throw new Error(`Token has no address for chain id "${chainId}".`)
79
- return { address, currency, decimals, name, popular, symbol }
82
+ return { address, chainId, currency, decimals, name, popular, symbol }
80
83
  }
81
84
  return Object.defineProperties(fn, {
82
85
  addresses: { enumerable: true, value: addresses },
@@ -113,13 +116,14 @@ export namespace defineToken {
113
116
 
114
117
  export type ReturnType<token extends Parameters = Parameters> = {
115
118
  /**
116
- * Resolves the token config (`address`, `currency`, `decimals`, `name`,
119
+ * Resolves the token config (`address`, `chainId`, `currency`, `decimals`, `name`,
117
120
  * `popular`, `symbol`) for a chain id.
118
121
  */
119
122
  <chainId extends keyof token['addresses']>(
120
123
  chainId: chainId,
121
124
  ): {
122
125
  address: token['addresses'][chainId]
126
+ chainId: chainId
123
127
  currency: ParameterValue<token, 'currency'>
124
128
  decimals: token['decimals']
125
129
  name: ParameterValue<token, 'name'>
@@ -2,7 +2,7 @@ import { defineToken } from '../defineToken.js'
2
2
 
3
3
  /**
4
4
  * [Open USD](https://joinopenstandard.com/) token, with canonical contract
5
- * addresses on Ethereum, Tempo, and Base.
5
+ * addresses on Ethereum, Tempo, Tempo Moderato, and Base.
6
6
  *
7
7
  * @example
8
8
  * ```ts
@@ -17,6 +17,7 @@ export const ousd = /*#__PURE__*/ defineToken({
17
17
  1: '0x9f6F3991D525015a6F8CaF062C83b62fD3AC4436', // mainnet
18
18
  4217: '0x20c0000000000000000000006a37DA5C996874BE', // tempo
19
19
  8453: '0xB2000000000000000000002fEb517dFeC7415344', // base
20
+ 42431: '0x20c0000000000000000000006a37DA5C996874BE', // tempoModerato
20
21
  },
21
22
  currency: 'USD',
22
23
  decimals: 6,
@@ -17,6 +17,12 @@ export type ChecksumAddressErrorType =
17
17
  | StringToBytesErrorType
18
18
  | ErrorType
19
19
 
20
+ /**
21
+ * Returns the [EIP-55](https://eips.ethereum.org/EIPS/eip-55) checksummed form
22
+ * of an address. The input's casing is ignored.
23
+ *
24
+ * Does not validate the input; use {@link getAddress} for untrusted strings.
25
+ */
20
26
  export function checksumAddress(
21
27
  address_: Address,
22
28
  /**
@@ -31,12 +37,14 @@ export function checksumAddress(
31
37
  */
32
38
  chainId?: number | undefined,
33
39
  ): Address {
34
- if (checksumAddressCache.has(`${address_}.${chainId}`))
35
- return checksumAddressCache.get(`${address_}.${chainId}`)!
40
+ // The checksum depends only on the lowercase address, so every casing of an
41
+ // address shares one cache entry.
42
+ const lower = address_.toLowerCase()
43
+ const cacheKey = `${lower}.${chainId}`
44
+ const cached = checksumAddressCache.get(cacheKey)
45
+ if (cached) return cached
36
46
 
37
- const hexAddress = chainId
38
- ? `${chainId}${address_.toLowerCase()}`
39
- : address_.substring(2).toLowerCase()
47
+ const hexAddress = chainId ? `${chainId}${lower}` : lower.substring(2)
40
48
  const hash = keccak256(stringToBytes(hexAddress), 'bytes')
41
49
 
42
50
  const address = (
@@ -52,7 +60,7 @@ export function checksumAddress(
52
60
  }
53
61
 
54
62
  const result = `0x${address.join('')}` as const
55
- checksumAddressCache.set(`${address_}.${chainId}`, result)
63
+ checksumAddressCache.set(cacheKey, result)
56
64
  return result
57
65
  }
58
66
 
@@ -61,6 +69,23 @@ export type GetAddressErrorType =
61
69
  | IsAddressErrorType
62
70
  | ErrorType
63
71
 
72
+ /**
73
+ * Converts an address into its [EIP-55](https://eips.ethereum.org/EIPS/eip-55)
74
+ * checksummed form. Throws if the string is not a `0x`-prefixed, 20-byte hex
75
+ * string.
76
+ *
77
+ * The input's casing is ignored, so an address with a wrong checksum is
78
+ * returned with the correct one. To reject a wrong checksum, check the input
79
+ * with {@link isAddress} first.
80
+ *
81
+ * - Docs: https://viem.sh/docs/utilities/getAddress
82
+ *
83
+ * @example
84
+ * import { getAddress } from 'viem'
85
+ *
86
+ * getAddress('0x787bc77f1700dce0f8c1581c18a12d3153d1b150')
87
+ * // '0x787bc77F1700DCE0f8c1581c18A12d3153D1B150'
88
+ */
64
89
  export function getAddress(
65
90
  address: string,
66
91
  /**
@@ -19,21 +19,42 @@ export type IsAddressOptions = {
19
19
 
20
20
  export type IsAddressErrorType = ErrorType
21
21
 
22
+ /**
23
+ * Checks if a string is a valid address.
24
+ *
25
+ * In strict mode (the default), a mixed-case address must match its
26
+ * [EIP-55](https://eips.ethereum.org/EIPS/eip-55) checksum. An all-lowercase
27
+ * address has no checksum and is valid.
28
+ *
29
+ * Use this to validate user input. {@link getAddress} does not validate the
30
+ * checksum; it returns the checksummed form of any casing.
31
+ *
32
+ * - Docs: https://viem.sh/docs/utilities/isAddress
33
+ *
34
+ * @example
35
+ * import { isAddress } from 'viem'
36
+ *
37
+ * isAddress('0x787bc77F1700DCE0f8c1581c18A12d3153D1B150')
38
+ * // true
39
+ * isAddress('0x787bc77F1700DCE0f8c1581c18A12d3153D1b150') // last `B` lowercased
40
+ * // false
41
+ * isAddress('0x787bc77f1700dce0f8c1581c18a12d3153d1b150')
42
+ * // true
43
+ */
22
44
  export function isAddress(
23
45
  address: string,
24
46
  options?: IsAddressOptions | undefined,
25
47
  ): address is Address {
26
48
  const { strict = true } = options ?? {}
27
- const cacheKey = `${address}.${strict}`
49
+ if (!addressRegex.test(address)) return false
50
+ if (!strict || address.toLowerCase() === address) return true
28
51
 
29
- if (isAddressCache.has(cacheKey)) return isAddressCache.get(cacheKey)!
30
-
31
- const result = (() => {
32
- if (!addressRegex.test(address)) return false
33
- if (address.toLowerCase() === address) return true
34
- if (strict) return checksumAddress(address as Address) === address
35
- return true
36
- })()
37
- isAddressCache.set(cacheKey, result)
52
+ // Only the checksum comparison is cached; the checks above are cheaper than a
53
+ // cache lookup.
54
+ let result = isAddressCache.get(address)
55
+ if (result === undefined) {
56
+ result = checksumAddress(address as Address) === address
57
+ isAddressCache.set(address, result)
58
+ }
38
59
  return result
39
60
  }
@@ -6,6 +6,8 @@ import {
6
6
  type RlpDepthLimitExceededErrorType,
7
7
  RlpListBoundaryExceededError,
8
8
  type RlpListBoundaryExceededErrorType,
9
+ RlpNonCanonicalError,
10
+ type RlpNonCanonicalErrorType,
9
11
  RlpTrailingBytesError,
10
12
  type RlpTrailingBytesErrorType,
11
13
  } from '../../errors/encoding.js'
@@ -35,6 +37,7 @@ export type FromRlpErrorType =
35
37
  | HexToBytesErrorType
36
38
  | InvalidHexValueErrorType
37
39
  | RlpDepthLimitExceededErrorType
40
+ | RlpNonCanonicalErrorType
38
41
  | RlpTrailingBytesErrorType
39
42
  | ErrorType
40
43
 
@@ -91,6 +94,11 @@ function fromRlpCursor<to extends To = 'hex'>(
91
94
  if (prefix < 0xc0) {
92
95
  const length = readLength(cursor, prefix, 0x80)
93
96
  const bytes = cursor.readBytes(length)
97
+ // A single byte below `0x80` is its own encoding (Yellow Paper, Appendix B).
98
+ if (prefix >= 0x80 && length === 1 && bytes[0]! < 0x80)
99
+ throw new RlpNonCanonicalError({
100
+ reason: 'single byte below `0x80` has a length prefix',
101
+ })
94
102
  return (to === 'hex' ? bytesToHex(bytes) : bytes) as FromRlpReturnType<to>
95
103
  }
96
104
 
@@ -104,18 +112,42 @@ function fromRlpCursor<to extends To = 'hex'>(
104
112
  ) as {} as FromRlpReturnType<to>
105
113
  }
106
114
 
107
- type ReadLengthErrorType = BaseErrorType | ErrorType
115
+ type ReadLengthErrorType = BaseErrorType | RlpNonCanonicalErrorType | ErrorType
108
116
 
109
117
  function readLength(cursor: Cursor, prefix: number, offset: number) {
110
118
  if (offset === 0x80 && prefix < 0x80) return 1
111
119
  if (prefix <= offset + 55) return prefix - offset
112
- if (prefix === offset + 55 + 1) return cursor.readUint8()
113
- if (prefix === offset + 55 + 2) return cursor.readUint16()
114
- if (prefix === offset + 55 + 3) return cursor.readUint24()
115
- if (prefix === offset + 55 + 4) return cursor.readUint32()
120
+ // Long-form lengths must be above 55 and have no leading zero bytes
121
+ // (Yellow Paper, Appendix B).
122
+ if (prefix === offset + 55 + 1) {
123
+ const length = cursor.readUint8()
124
+ if (length <= 55) throw nonCanonicalLengthError(length)
125
+ return length
126
+ }
127
+ if (prefix === offset + 55 + 2) {
128
+ const length = cursor.readUint16()
129
+ if (length <= 0xff) throw nonCanonicalLengthError(length)
130
+ return length
131
+ }
132
+ if (prefix === offset + 55 + 3) {
133
+ const length = cursor.readUint24()
134
+ if (length <= 0xffff) throw nonCanonicalLengthError(length)
135
+ return length
136
+ }
137
+ if (prefix === offset + 55 + 4) {
138
+ const length = cursor.readUint32()
139
+ if (length <= 0xffffff) throw nonCanonicalLengthError(length)
140
+ return length
141
+ }
116
142
  throw new BaseError('Invalid RLP prefix')
117
143
  }
118
144
 
145
+ function nonCanonicalLengthError(length: number) {
146
+ return new RlpNonCanonicalError({
147
+ reason: `length \`${length}\` does not use the shortest length prefix`,
148
+ })
149
+ }
150
+
119
151
  type ReadListErrorType = RlpListBoundaryExceededErrorType | ErrorType
120
152
 
121
153
  function readList<to extends To>(
package/utils/index.ts CHANGED
@@ -14,6 +14,8 @@ export {
14
14
  parseAccount,
15
15
  } from '../accounts/utils/parseAccount.js'
16
16
  export {
17
+ InvalidPublicKeyError,
18
+ type InvalidPublicKeyErrorType,
17
19
  type PublicKeyToAddressErrorType,
18
20
  publicKeyToAddress,
19
21
  } from '../accounts/utils/publicKeyToAddress.js'
@@ -38,6 +38,10 @@ type CreateBatchSchedulerReturnType<
38
38
  export type CreateBatchSchedulerErrorType = ErrorType
39
39
 
40
40
  const schedulerCache = /*#__PURE__*/ new Map<number | string, SchedulerItem[]>()
41
+ const timerCache = /*#__PURE__*/ new Map<
42
+ number | string,
43
+ ReturnType<typeof setTimeout>
44
+ >()
41
45
 
42
46
  /** @internal */
43
47
  export function createBatchScheduler<
@@ -78,7 +82,11 @@ export function createBatchScheduler<
78
82
  })
79
83
  }
80
84
 
81
- const flush = () => schedulerCache.delete(id)
85
+ const flush = () => {
86
+ clearTimeout(timerCache.get(id))
87
+ timerCache.delete(id)
88
+ schedulerCache.delete(id)
89
+ }
82
90
 
83
91
  const getBatchedArgs = () =>
84
92
  getScheduler().map(({ args }) => args) as parameters[]
@@ -104,7 +112,7 @@ export function createBatchScheduler<
104
112
  }
105
113
 
106
114
  setScheduler({ args, resolve, reject })
107
- setTimeout(exec, wait)
115
+ timerCache.set(id, setTimeout(exec, wait))
108
116
  return promise
109
117
  },
110
118
  } as unknown as CreateBatchSchedulerReturnType<parameters, returnType>
@@ -145,19 +145,17 @@ export function getTypesForEIP712Domain({
145
145
  }: {
146
146
  domain?: TypedDataDomain | undefined
147
147
  }): TypedDataParameter[] {
148
+ // A field is part of the schema when it is present on the domain (not
149
+ // `undefined` or `null`).
148
150
  return [
149
- typeof domain?.name === 'string' && { name: 'name', type: 'string' },
150
- typeof domain?.version === 'string' && { name: 'version', type: 'string' },
151
- (typeof domain?.chainId === 'number' ||
152
- typeof domain?.chainId === 'bigint') && {
153
- name: 'chainId',
154
- type: 'uint256',
155
- },
156
- domain?.verifyingContract && {
151
+ domain?.name != null && { name: 'name', type: 'string' },
152
+ domain?.version != null && { name: 'version', type: 'string' },
153
+ domain?.chainId != null && { name: 'chainId', type: 'uint256' },
154
+ domain?.verifyingContract != null && {
157
155
  name: 'verifyingContract',
158
156
  type: 'address',
159
157
  },
160
- domain?.salt && { name: 'salt', type: 'bytes32' },
158
+ domain?.salt != null && { name: 'salt', type: 'bytes32' },
161
159
  ].filter(Boolean) as TypedDataParameter[]
162
160
  }
163
161
 
@@ -18,7 +18,7 @@ export class BaseFeeHigherThanValueError extends BaseError {
18
18
  }
19
19
  }
20
20
 
21
- export type TxHashNotFoundInLogsErrorType = BaseFeeHigherThanValueError & {
21
+ export type TxHashNotFoundInLogsErrorType = TxHashNotFoundInLogsError & {
22
22
  name: 'TxHashNotFoundInLogsError'
23
23
  }
24
24
  export class TxHashNotFoundInLogsError extends BaseError {