ox 1.8.15 → 1.8.18

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 (292) hide show
  1. package/CHANGELOG.md +108 -0
  2. package/dist/core/AbiError.d.ts +52 -6
  3. package/dist/core/AbiError.d.ts.map +1 -1
  4. package/dist/core/AbiError.js +60 -4
  5. package/dist/core/AbiError.js.map +1 -1
  6. package/dist/core/AbiEvent.d.ts +5 -3
  7. package/dist/core/AbiEvent.d.ts.map +1 -1
  8. package/dist/core/AbiEvent.js +89 -58
  9. package/dist/core/AbiEvent.js.map +1 -1
  10. package/dist/core/AbiFunction.d.ts +48 -2
  11. package/dist/core/AbiFunction.d.ts.map +1 -1
  12. package/dist/core/AbiFunction.js +59 -3
  13. package/dist/core/AbiFunction.js.map +1 -1
  14. package/dist/core/AbiItem.d.ts.map +1 -1
  15. package/dist/core/AbiItem.js +15 -5
  16. package/dist/core/AbiItem.js.map +1 -1
  17. package/dist/core/AbiParameters.d.ts +1 -1
  18. package/dist/core/AbiParameters.d.ts.map +1 -1
  19. package/dist/core/AbiParameters.js +8 -2
  20. package/dist/core/AbiParameters.js.map +1 -1
  21. package/dist/core/Address.d.ts +17 -2
  22. package/dist/core/Address.d.ts.map +1 -1
  23. package/dist/core/Address.js +17 -1
  24. package/dist/core/Address.js.map +1 -1
  25. package/dist/core/Authorization.d.ts +24 -2
  26. package/dist/core/Authorization.d.ts.map +1 -1
  27. package/dist/core/Authorization.js +29 -2
  28. package/dist/core/Authorization.js.map +1 -1
  29. package/dist/core/Base58.d.ts.map +1 -1
  30. package/dist/core/Base58.js +16 -4
  31. package/dist/core/Base58.js.map +1 -1
  32. package/dist/core/Blobs.d.ts +2 -3
  33. package/dist/core/Blobs.d.ts.map +1 -1
  34. package/dist/core/Blobs.js +38 -43
  35. package/dist/core/Blobs.js.map +1 -1
  36. package/dist/core/Bytes.d.ts.map +1 -1
  37. package/dist/core/Bytes.js +10 -2
  38. package/dist/core/Bytes.js.map +1 -1
  39. package/dist/core/Cbor.d.ts +13 -6
  40. package/dist/core/Cbor.d.ts.map +1 -1
  41. package/dist/core/Cbor.js +31 -18
  42. package/dist/core/Cbor.js.map +1 -1
  43. package/dist/core/Ens.js +2 -2
  44. package/dist/core/Ens.js.map +1 -1
  45. package/dist/core/FrameReceipt.d.ts.map +1 -1
  46. package/dist/core/FrameReceipt.js +4 -1
  47. package/dist/core/FrameReceipt.js.map +1 -1
  48. package/dist/core/Hex.d.ts.map +1 -1
  49. package/dist/core/Hex.js +34 -31
  50. package/dist/core/Hex.js.map +1 -1
  51. package/dist/core/Keystore.d.ts +31 -2
  52. package/dist/core/Keystore.d.ts.map +1 -1
  53. package/dist/core/Keystore.js +28 -2
  54. package/dist/core/Keystore.js.map +1 -1
  55. package/dist/core/Rlp.d.ts +1 -1
  56. package/dist/core/Rlp.d.ts.map +1 -1
  57. package/dist/core/Rlp.js +107 -0
  58. package/dist/core/Rlp.js.map +1 -1
  59. package/dist/core/RpcTransport.d.ts.map +1 -1
  60. package/dist/core/RpcTransport.js +3 -1
  61. package/dist/core/RpcTransport.js.map +1 -1
  62. package/dist/core/Secp256k1.d.ts.map +1 -1
  63. package/dist/core/Secp256k1.js +4 -1
  64. package/dist/core/Secp256k1.js.map +1 -1
  65. package/dist/core/Signature.d.ts +1 -1
  66. package/dist/core/Signature.d.ts.map +1 -1
  67. package/dist/core/Signature.js +4 -2
  68. package/dist/core/Signature.js.map +1 -1
  69. package/dist/core/Siwe.d.ts +4 -0
  70. package/dist/core/Siwe.d.ts.map +1 -1
  71. package/dist/core/Siwe.js +29 -3
  72. package/dist/core/Siwe.js.map +1 -1
  73. package/dist/core/Transaction.d.ts.map +1 -1
  74. package/dist/core/Transaction.js +4 -2
  75. package/dist/core/Transaction.js.map +1 -1
  76. package/dist/core/TransactionReceipt.d.ts.map +1 -1
  77. package/dist/core/TransactionReceipt.js +8 -2
  78. package/dist/core/TransactionReceipt.js.map +1 -1
  79. package/dist/core/TxEnvelopeEip1559.d.ts.map +1 -1
  80. package/dist/core/TxEnvelopeEip1559.js +2 -1
  81. package/dist/core/TxEnvelopeEip1559.js.map +1 -1
  82. package/dist/core/TxEnvelopeEip2930.d.ts.map +1 -1
  83. package/dist/core/TxEnvelopeEip2930.js +2 -1
  84. package/dist/core/TxEnvelopeEip2930.js.map +1 -1
  85. package/dist/core/TxEnvelopeEip7702.d.ts +1 -1
  86. package/dist/core/TxEnvelopeEip7702.d.ts.map +1 -1
  87. package/dist/core/TxEnvelopeEip7702.js +6 -1
  88. package/dist/core/TxEnvelopeEip7702.js.map +1 -1
  89. package/dist/core/TxEnvelopeLegacy.d.ts.map +1 -1
  90. package/dist/core/TxEnvelopeLegacy.js +49 -7
  91. package/dist/core/TxEnvelopeLegacy.js.map +1 -1
  92. package/dist/core/TypedData.d.ts +3 -1
  93. package/dist/core/TypedData.d.ts.map +1 -1
  94. package/dist/core/TypedData.js +23 -11
  95. package/dist/core/TypedData.js.map +1 -1
  96. package/dist/core/Value.d.ts.map +1 -1
  97. package/dist/core/Value.js +14 -3
  98. package/dist/core/Value.js.map +1 -1
  99. package/dist/core/internal/abiParameters.d.ts +3 -3
  100. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  101. package/dist/core/internal/abiParameters.js +99 -69
  102. package/dist/core/internal/abiParameters.js.map +1 -1
  103. package/dist/core/internal/base58.d.ts.map +1 -1
  104. package/dist/core/internal/base58.js +9 -3
  105. package/dist/core/internal/base58.js.map +1 -1
  106. package/dist/core/internal/caches.d.ts +6 -0
  107. package/dist/core/internal/caches.d.ts.map +1 -1
  108. package/dist/core/internal/caches.js +3 -1
  109. package/dist/core/internal/caches.js.map +1 -1
  110. package/dist/core/internal/codec/hex.d.ts +13 -0
  111. package/dist/core/internal/codec/hex.d.ts.map +1 -1
  112. package/dist/core/internal/codec/hex.js +25 -2
  113. package/dist/core/internal/codec/hex.js.map +1 -1
  114. package/dist/core/internal/codec/int.d.ts.map +1 -1
  115. package/dist/core/internal/codec/int.js +48 -32
  116. package/dist/core/internal/codec/int.js.map +1 -1
  117. package/dist/core/internal/cursor.d.ts +24 -4
  118. package/dist/core/internal/cursor.d.ts.map +1 -1
  119. package/dist/core/internal/cursor.js +62 -11
  120. package/dist/core/internal/cursor.js.map +1 -1
  121. package/dist/core/internal/lru.d.ts +11 -12
  122. package/dist/core/internal/lru.d.ts.map +1 -1
  123. package/dist/core/internal/lru.js +22 -32
  124. package/dist/core/internal/lru.js.map +1 -1
  125. package/dist/core/internal/promise.d.ts.map +1 -1
  126. package/dist/core/internal/promise.js +13 -7
  127. package/dist/core/internal/promise.js.map +1 -1
  128. package/dist/erc4337/UserOperation.d.ts.map +1 -1
  129. package/dist/erc4337/UserOperation.js +1 -3
  130. package/dist/erc4337/UserOperation.js.map +1 -1
  131. package/dist/erc6492/SignatureErc6492.d.ts +1 -1
  132. package/dist/erc6492/SignatureErc6492.d.ts.map +1 -1
  133. package/dist/erc6492/SignatureErc6492.js +1 -1
  134. package/dist/erc6492/SignatureErc6492.js.map +1 -1
  135. package/dist/erc7821/Execute.d.ts +45 -0
  136. package/dist/erc7821/Execute.d.ts.map +1 -1
  137. package/dist/erc7821/Execute.js +70 -2
  138. package/dist/erc7821/Execute.js.map +1 -1
  139. package/dist/index.d.ts +1 -1
  140. package/dist/index.js +1 -1
  141. package/dist/tempo/KeyAuthorization.d.ts +9 -3
  142. package/dist/tempo/KeyAuthorization.d.ts.map +1 -1
  143. package/dist/tempo/KeyAuthorization.js +16 -0
  144. package/dist/tempo/KeyAuthorization.js.map +1 -1
  145. package/dist/tempo/SignatureEnvelope.d.ts +1 -1
  146. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  147. package/dist/tempo/SignatureEnvelope.js +53 -54
  148. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  149. package/dist/tempo/TransactionReceipt.d.ts.map +1 -1
  150. package/dist/tempo/TransactionReceipt.js +4 -1
  151. package/dist/tempo/TransactionReceipt.js.map +1 -1
  152. package/dist/tempo/TransactionRequest.d.ts +1 -1
  153. package/dist/tempo/TransactionRequest.d.ts.map +1 -1
  154. package/dist/tempo/TransactionRequest.js +5 -2
  155. package/dist/tempo/TransactionRequest.js.map +1 -1
  156. package/dist/tempo/TxEnvelopeTempo.d.ts +30 -1
  157. package/dist/tempo/TxEnvelopeTempo.d.ts.map +1 -1
  158. package/dist/tempo/TxEnvelopeTempo.js +51 -2
  159. package/dist/tempo/TxEnvelopeTempo.js.map +1 -1
  160. package/dist/tempo/ZoneId.d.ts +3 -1
  161. package/dist/tempo/ZoneId.d.ts.map +1 -1
  162. package/dist/tempo/ZoneId.js +7 -1
  163. package/dist/tempo/ZoneId.js.map +1 -1
  164. package/dist/tempo/internal/virtualMasterPool.d.ts.map +1 -1
  165. package/dist/tempo/internal/virtualMasterPool.js +17 -6
  166. package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
  167. package/dist/tempo/internal/webauthnData.d.ts +23 -0
  168. package/dist/tempo/internal/webauthnData.d.ts.map +1 -0
  169. package/dist/tempo/internal/webauthnData.js +75 -0
  170. package/dist/tempo/internal/webauthnData.js.map +1 -0
  171. package/dist/webauthn/Registration.d.ts +2 -0
  172. package/dist/webauthn/Registration.d.ts.map +1 -1
  173. package/dist/webauthn/Registration.js +2 -6
  174. package/dist/webauthn/Registration.js.map +1 -1
  175. package/dist/zod/TypedData.d.ts +4 -4
  176. package/dist/zod/tempo/KeyAuthorization.d.ts.map +1 -1
  177. package/dist/zod/tempo/KeyAuthorization.js +4 -2
  178. package/dist/zod/tempo/KeyAuthorization.js.map +1 -1
  179. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  180. package/dist/zod/tempo/SignatureEnvelope.js +29 -29
  181. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  182. package/package.json +2 -2
  183. package/src/core/AbiError.ts +82 -8
  184. package/src/core/AbiEvent.bench.ts +45 -0
  185. package/src/core/AbiEvent.ts +120 -68
  186. package/src/core/AbiFunction.bench.ts +16 -0
  187. package/src/core/AbiFunction.ts +76 -4
  188. package/src/core/AbiItem.ts +13 -4
  189. package/src/core/AbiParameters.bench.ts +33 -0
  190. package/src/core/AbiParameters.ts +8 -1
  191. package/src/core/Address.ts +18 -1
  192. package/src/core/Authorization.ts +34 -4
  193. package/src/core/Base58.bench.ts +28 -0
  194. package/src/core/Base58.ts +15 -4
  195. package/src/core/Blobs.bench.ts +40 -0
  196. package/src/core/Blobs.ts +47 -55
  197. package/src/core/Bytes.bench.ts +34 -0
  198. package/src/core/Bytes.ts +10 -2
  199. package/src/core/Cbor.ts +36 -14
  200. package/src/core/Ens.ts +2 -2
  201. package/src/core/FrameReceipt.ts +4 -1
  202. package/src/core/Hex.bench.ts +17 -0
  203. package/src/core/Hex.ts +29 -32
  204. package/src/core/Keystore.ts +40 -3
  205. package/src/core/Rlp.ts +139 -1
  206. package/src/core/Rlp_tx.bench.ts +55 -0
  207. package/src/core/RpcTransport.ts +3 -1
  208. package/src/core/Secp256k1.ts +4 -1
  209. package/src/core/Signature.ts +7 -3
  210. package/src/core/Siwe.ts +29 -3
  211. package/src/core/Transaction.ts +4 -2
  212. package/src/core/TransactionReceipt.ts +8 -2
  213. package/src/core/TxEnvelopeEip1559.ts +4 -1
  214. package/src/core/TxEnvelopeEip2930.ts +4 -1
  215. package/src/core/TxEnvelopeEip7702.ts +9 -2
  216. package/src/core/TxEnvelopeLegacy.ts +58 -7
  217. package/src/core/TypedData.bench.ts +50 -0
  218. package/src/core/TypedData.ts +24 -11
  219. package/src/core/Value.ts +14 -3
  220. package/src/core/_test/AbiError.test.ts +105 -2
  221. package/src/core/_test/AbiEvent.fuzz.ts +1 -1
  222. package/src/core/_test/AbiEvent.test-d.ts +46 -0
  223. package/src/core/_test/AbiEvent.test.ts +646 -11
  224. package/src/core/_test/AbiFunction.test.ts +79 -6
  225. package/src/core/_test/AbiParameters.adversarial.fuzz.ts +1 -0
  226. package/src/core/_test/AbiParameters.decode.test.ts +143 -0
  227. package/src/core/_test/AbiParameters.encode.test.ts +36 -1
  228. package/src/core/_test/AbiParameters.test.ts +15 -0
  229. package/src/core/_test/Address.test.ts +41 -0
  230. package/src/core/_test/Authorization.test.ts +130 -0
  231. package/src/core/_test/Bytes.test.ts +33 -0
  232. package/src/core/_test/Caches.test.ts +20 -1
  233. package/src/core/_test/Cbor.test.ts +26 -0
  234. package/src/core/_test/FrameReceipt.test.ts +13 -0
  235. package/src/core/_test/Keystore.test.ts +65 -1
  236. package/src/core/_test/Rlp.fuzz.ts +55 -1
  237. package/src/core/_test/Rlp.test.ts +64 -0
  238. package/src/core/_test/RpcTransport.test.ts +15 -2
  239. package/src/core/_test/Secp256k1.test.ts +36 -0
  240. package/src/core/_test/Signature.test.ts +102 -0
  241. package/src/core/_test/Siwe.test.ts +123 -0
  242. package/src/core/_test/Transaction.test.ts +17 -0
  243. package/src/core/_test/TransactionEnvelopeEip1559.test.ts +28 -0
  244. package/src/core/_test/TransactionEnvelopeEip2930.test.ts +28 -0
  245. package/src/core/_test/TransactionEnvelopeEip7702.test.ts +133 -0
  246. package/src/core/_test/TransactionEnvelopeLegacy.test.ts +322 -1
  247. package/src/core/_test/TransactionReceipt.test.ts +41 -0
  248. package/src/core/_test/TypedData.test.ts +197 -0
  249. package/src/core/_test/Value.test.ts +21 -0
  250. package/src/core/_test/internal/cursor.test.ts +71 -0
  251. package/src/core/internal/abiParameters.ts +123 -81
  252. package/src/core/internal/base58.ts +9 -3
  253. package/src/core/internal/caches.ts +3 -1
  254. package/src/core/internal/codec/hex.ts +27 -2
  255. package/src/core/internal/codec/int.ts +45 -32
  256. package/src/core/internal/cursor.ts +84 -19
  257. package/src/core/internal/lru.ts +21 -35
  258. package/src/core/internal/promise.ts +11 -5
  259. package/src/erc4337/UserOperation.ts +3 -4
  260. package/src/erc4337/_test/UserOperation.test.ts +59 -1
  261. package/src/erc6492/SignatureErc6492.ts +1 -1
  262. package/src/erc6492/_test/SignatureErc6492.test.ts +48 -1
  263. package/src/erc7821/Execute.ts +83 -2
  264. package/src/erc7821/_test/Execute.test.ts +217 -1
  265. package/src/index.ts +1 -1
  266. package/src/tempo/KeyAuthorization.test.ts +91 -0
  267. package/src/tempo/KeyAuthorization.ts +32 -3
  268. package/src/tempo/SignatureEnvelope.test.ts +300 -28
  269. package/src/tempo/SignatureEnvelope.ts +62 -54
  270. package/src/tempo/TransactionReceipt.test.ts +8 -0
  271. package/src/tempo/TransactionReceipt.ts +4 -1
  272. package/src/tempo/TransactionRequest.test.ts +30 -0
  273. package/src/tempo/TransactionRequest.ts +7 -2
  274. package/src/tempo/TxEnvelopeTempo.test.ts +310 -0
  275. package/src/tempo/TxEnvelopeTempo.ts +64 -1
  276. package/src/tempo/VirtualMaster.bench.ts +7 -0
  277. package/src/tempo/ZoneId.test.ts +11 -0
  278. package/src/tempo/ZoneId.ts +7 -1
  279. package/src/tempo/configurableAccounts.e2e.test.ts +1 -1
  280. package/src/tempo/internal/virtualMasterPool.ts +20 -6
  281. package/src/tempo/internal/webauthnData.ts +84 -0
  282. package/src/version.ts +1 -1
  283. package/src/webauthn/Registration.ts +4 -6
  284. package/src/webauthn/_test/Registration.browser.test.ts +14 -0
  285. package/src/webauthn/_test/Registration.test.ts +60 -0
  286. package/src/zod/tempo/KeyAuthorization.ts +12 -5
  287. package/src/zod/tempo/SignatureEnvelope.ts +38 -32
  288. package/src/zod/tempo/_test/KeyAuthorization.test.ts +36 -0
  289. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +138 -4
  290. package/src/core/_test/internal/lru.test.ts +0 -38
  291. package/src/core/_test/internal/uid.test.ts +0 -7
  292. package/src/core/internal/uid.ts +0 -15
@@ -1,6 +1,7 @@
1
1
  import { Hex, TypedData } from 'ox'
2
2
  import { describe, expect, test } from 'vp/test'
3
3
  import * as typedData from '../../../test/constants/typedData.js'
4
+ import { caches } from '../internal/caches.js'
4
5
 
5
6
  describe('assert', () => {
6
7
  test('default', () => {
@@ -200,6 +201,23 @@ describe('assert', () => {
200
201
  )
201
202
  })
202
203
 
204
+ test('bytes odd length', () => {
205
+ expect(() =>
206
+ TypedData.assert({
207
+ message: { hash: '0x123' },
208
+ primaryType: 'Foo',
209
+ types: {
210
+ EIP712Domain: [],
211
+ Foo: [{ name: 'hash', type: 'bytes2' }],
212
+ },
213
+ }),
214
+ ).toThrowErrorMatchingInlineSnapshot(`
215
+ [Hex.InvalidLengthError: Hex value \`"0x123"\` is an odd length (3 nibbles).
216
+
217
+ It must be an even length.]
218
+ `)
219
+ })
220
+
203
221
  test('domain: invalid', () => {
204
222
  expect(() =>
205
223
  TypedData.assert({
@@ -663,6 +681,24 @@ describe('domainSeparator', () => {
663
681
  `"0x2fdf3441fcaf4f30c7e16292b258a5d7054a4e2e00dbd7b7d2f467f2b8fb9413"`,
664
682
  )
665
683
  })
684
+
685
+ test('behavior: string chainId', () => {
686
+ const domain = {
687
+ name: 'Ether Mail',
688
+ verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
689
+ } as const
690
+
691
+ expect(
692
+ TypedData.domainSeparator({ ...domain, chainId: 1 }),
693
+ ).toMatchInlineSnapshot(
694
+ `"0xcbea6135f3930521627c49608c80b1e98ac5e9966ebf71d0d52f39fa5acc7a58"`,
695
+ )
696
+ expect(
697
+ TypedData.domainSeparator({ ...domain, chainId: '1' } as never),
698
+ ).toMatchInlineSnapshot(
699
+ `"0xcbea6135f3930521627c49608c80b1e98ac5e9966ebf71d0d52f39fa5acc7a58"`,
700
+ )
701
+ })
666
702
  })
667
703
 
668
704
  describe('encode', () => {
@@ -963,6 +999,58 @@ describe('extractEip712DomainTypes', () => {
963
999
  ]
964
1000
  `)
965
1001
  })
1002
+
1003
+ test('behavior: string chainId', () => {
1004
+ expect(
1005
+ TypedData.extractEip712DomainTypes({
1006
+ chainId: '1',
1007
+ name: 'Ether Mail',
1008
+ } as never),
1009
+ ).toMatchInlineSnapshot(`
1010
+ [
1011
+ {
1012
+ "name": "name",
1013
+ "type": "string",
1014
+ },
1015
+ {
1016
+ "name": "chainId",
1017
+ "type": "uint256",
1018
+ },
1019
+ ]
1020
+ `)
1021
+ })
1022
+
1023
+ test('behavior: empty verifyingContract and salt', () => {
1024
+ expect(
1025
+ TypedData.extractEip712DomainTypes({
1026
+ salt: '',
1027
+ verifyingContract: '',
1028
+ } as never),
1029
+ ).toMatchInlineSnapshot(`
1030
+ [
1031
+ {
1032
+ "name": "verifyingContract",
1033
+ "type": "address",
1034
+ },
1035
+ {
1036
+ "name": "salt",
1037
+ "type": "bytes32",
1038
+ },
1039
+ ]
1040
+ `)
1041
+ })
1042
+
1043
+ test('behavior: null and undefined fields are omitted', () => {
1044
+ expect(
1045
+ TypedData.extractEip712DomainTypes({
1046
+ chainId: undefined,
1047
+ name: null,
1048
+ salt: undefined,
1049
+ verifyingContract: null,
1050
+ version: undefined,
1051
+ } as never),
1052
+ ).toMatchInlineSnapshot(`[]`)
1053
+ })
966
1054
  })
967
1055
 
968
1056
  describe('getSignPayload', () => {
@@ -1064,6 +1152,97 @@ describe('getSignPayload', () => {
1064
1152
  expect(hashWithNumber).toBe(hashWithBigint)
1065
1153
  })
1066
1154
 
1155
+ test('domain: string chainId produces same hash as number chainId', () => {
1156
+ const value = {
1157
+ domain: {
1158
+ chainId: 1,
1159
+ name: 'Ether Mail',
1160
+ verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
1161
+ },
1162
+ message: { contents: 'Hello, Bob!' },
1163
+ primaryType: 'Mail',
1164
+ types: { Mail: [{ name: 'contents', type: 'string' }] },
1165
+ } as const
1166
+ const { chainId: _, ...domain } = value.domain
1167
+
1168
+ expect(TypedData.getSignPayload(value)).toMatchInlineSnapshot(
1169
+ `"0x653cec21fbc0c9034d94bc61d4d36850d0455272dcec0d1655a008c81193701f"`,
1170
+ )
1171
+ expect(
1172
+ TypedData.getSignPayload({
1173
+ ...value,
1174
+ domain: { ...domain, chainId: '1' },
1175
+ } as never),
1176
+ ).toMatchInlineSnapshot(
1177
+ `"0x653cec21fbc0c9034d94bc61d4d36850d0455272dcec0d1655a008c81193701f"`,
1178
+ )
1179
+ expect(
1180
+ TypedData.getSignPayload({
1181
+ ...value,
1182
+ domain: { ...domain, chainId: '0x1' },
1183
+ } as never),
1184
+ ).toMatchInlineSnapshot(
1185
+ `"0x653cec21fbc0c9034d94bc61d4d36850d0455272dcec0d1655a008c81193701f"`,
1186
+ )
1187
+ expect(
1188
+ TypedData.getSignPayload({ ...value, domain }),
1189
+ ).toMatchInlineSnapshot(
1190
+ `"0xa892aed0abd1aabdd57495f612097d467c47042ae2dbe1a98be9c5291eee51fb"`,
1191
+ )
1192
+ })
1193
+
1194
+ test('domain: string chainId survives a serialize round trip', () => {
1195
+ const value = {
1196
+ domain: { chainId: 1n, name: 'Ether Mail' },
1197
+ message: { contents: 'Hello, Bob!' },
1198
+ primaryType: 'Mail',
1199
+ types: { Mail: [{ name: 'contents', type: 'string' }] },
1200
+ } as const
1201
+
1202
+ expect(
1203
+ TypedData.getSignPayload(JSON.parse(TypedData.serialize(value))),
1204
+ ).toBe(TypedData.getSignPayload(value))
1205
+ })
1206
+
1207
+ test('domain: non-numeric string chainId', () => {
1208
+ expect(() =>
1209
+ TypedData.getSignPayload({
1210
+ domain: { chainId: 'abc', name: 'Ether Mail' },
1211
+ message: { contents: 'Hello, Bob!' },
1212
+ primaryType: 'Mail',
1213
+ types: { Mail: [{ name: 'contents', type: 'string' }] },
1214
+ } as never),
1215
+ ).toThrow(SyntaxError)
1216
+ })
1217
+
1218
+ test('domain: empty verifyingContract', () => {
1219
+ expect(() =>
1220
+ TypedData.getSignPayload({
1221
+ domain: { name: 'Ether Mail', verifyingContract: '' },
1222
+ message: { contents: 'Hello, Bob!' },
1223
+ primaryType: 'Mail',
1224
+ types: { Mail: [{ name: 'contents', type: 'string' }] },
1225
+ } as never),
1226
+ ).toThrowErrorMatchingInlineSnapshot(`
1227
+ [Address.InvalidAddressError: Address "" is invalid.
1228
+
1229
+ Details: Address is not a 20 byte (40 hexadecimal character) value.]
1230
+ `)
1231
+ })
1232
+
1233
+ test('domain: empty salt', () => {
1234
+ expect(() =>
1235
+ TypedData.getSignPayload({
1236
+ domain: { name: 'Ether Mail', salt: '' },
1237
+ message: { contents: 'Hello, Bob!' },
1238
+ primaryType: 'Mail',
1239
+ types: { Mail: [{ name: 'contents', type: 'string' }] },
1240
+ } as never),
1241
+ ).toThrowErrorMatchingInlineSnapshot(
1242
+ `[TypedData.BytesSizeMismatchError: Expected bytes32, got bytes-1.]`,
1243
+ )
1244
+ })
1245
+
1067
1246
  test('minimal valid typed message', () => {
1068
1247
  const payload = TypedData.getSignPayload({
1069
1248
  types: {
@@ -1308,6 +1487,24 @@ describe('hashStruct', () => {
1308
1487
  }),
1309
1488
  ).toBe(hash)
1310
1489
  })
1490
+
1491
+ test('behavior: long encoded types are not kept in the global cache', () => {
1492
+ caches.typeHash.clear()
1493
+ const fields = Array.from({ length: 300 }, (_, i) => ({
1494
+ name: 'field' + i,
1495
+ type: 'uint256',
1496
+ }))
1497
+ const data = Object.fromEntries(fields.map(({ name }) => [name, 1n]))
1498
+ TypedData.hashStruct({ data, primaryType: 'Long', types: { Long: fields } })
1499
+ expect(caches.typeHash.size).toBe(0)
1500
+
1501
+ TypedData.hashStruct({
1502
+ ...typedData.basic,
1503
+ primaryType: 'Mail',
1504
+ data: typedData.basic.message,
1505
+ })
1506
+ expect(caches.typeHash.size).toBeGreaterThan(0)
1507
+ })
1311
1508
  })
1312
1509
 
1313
1510
  describe('serialize', () => {
@@ -44,6 +44,14 @@ describe('format', () => {
44
44
  ),
45
45
  ).toMatchInlineSnapshot('"-6942123123123069420.1234544444678912345"')
46
46
  })
47
+
48
+ test(
49
+ 'behavior: long run of zeros before a non-zero digit',
50
+ { timeout: 5_000 },
51
+ () => {
52
+ expect(Value.format(1n, 200_001)).toBe(`0.${'0'.repeat(200_000)}1`)
53
+ },
54
+ )
47
55
  })
48
56
 
49
57
  describe('formatEther', () => {
@@ -402,6 +410,19 @@ describe('from', () => {
402
410
  )
403
411
  })
404
412
 
413
+ test(
414
+ 'behavior: long run of zeros before a non-zero digit',
415
+ { timeout: 5_000 },
416
+ () => {
417
+ expect(Value.from(`0.${'0'.repeat(200_000)}1`, 18)).toMatchInlineSnapshot(
418
+ '0n',
419
+ )
420
+ expect(Value.from(`1.5${'0'.repeat(200_000)}`, 18)).toMatchInlineSnapshot(
421
+ '1500000000000000000n',
422
+ )
423
+ },
424
+ )
425
+
405
426
  test('error: invalid decimal number', () => {
406
427
  expect(() =>
407
428
  Value.from('123.456.789', 9),
@@ -611,6 +611,41 @@ test('remaining', () => {
611
611
  expect(cursor.remaining).toBe(410)
612
612
  })
613
613
 
614
+ describe('readSizeLimit', () => {
615
+ test('default', () => {
616
+ const cursor = Cursor.create(new Uint8Array(generateBytes(420)))
617
+ expect(cursor.readSizeLimit).toMatchInlineSnapshot(`262984`)
618
+
619
+ cursor.readBytes(420)
620
+ cursor.setPosition(0)
621
+ cursor.readUint32()
622
+ expect(cursor.readSize).toMatchInlineSnapshot(`424`)
623
+ })
624
+
625
+ test('behavior: reads past the limit', () => {
626
+ const cursor = Cursor.create(new Uint8Array(generateBytes(420)), {
627
+ recursiveReadLimit: 2,
628
+ })
629
+ expect(cursor.readSizeLimit).toMatchInlineSnapshot(`904`)
630
+
631
+ for (let i = 0; i < 2; i++) {
632
+ cursor.setPosition(i)
633
+ cursor.readBytes(420 - i)
634
+ }
635
+ cursor.setPosition(2)
636
+ expect(() => cursor.readBytes(418)).toThrowErrorMatchingInlineSnapshot(
637
+ `[Cursor.ReadSizeLimitExceededError: Read size limit of \`904\` bytes exceeded (read size: \`1257\` bytes).]`,
638
+ )
639
+ })
640
+
641
+ test('behavior: no recursiveReadLimit', () => {
642
+ const cursor = Cursor.create(new Uint8Array(generateBytes(420)), {
643
+ recursiveReadLimit: Number.POSITIVE_INFINITY,
644
+ })
645
+ expect(cursor.readSizeLimit).toMatchInlineSnapshot(`Infinity`)
646
+ })
647
+ })
648
+
614
649
  describe('args: recursiveReadLimit', () => {
615
650
  test('default', () => {
616
651
  const cursor = Cursor.create(new Uint8Array(generateBytes(420)), {
@@ -646,4 +681,40 @@ describe('args: recursiveReadLimit', () => {
646
681
  '[Cursor.RecursiveReadLimitExceededError: Recursive read limit of `2` exceeded (recursive read count: `3`).]',
647
682
  )
648
683
  })
684
+
685
+ test('behavior: repeated reads at an unaligned position', () => {
686
+ const cursor = Cursor.create(new Uint8Array(generateBytes(420)), {
687
+ recursiveReadLimit: 2,
688
+ })
689
+
690
+ for (let i = 0; i < 3; i++) {
691
+ cursor.setPosition(37)
692
+ cursor.readBytes(10)
693
+ }
694
+ cursor.setPosition(37)
695
+ expect(() => cursor.readBytes(10)).toThrowErrorMatchingInlineSnapshot(
696
+ `[Cursor.RecursiveReadLimitExceededError: Recursive read limit of \`2\` exceeded (recursive read count: \`3\`).]`,
697
+ )
698
+ })
699
+
700
+ test('behavior: repeated reads past the length the data had at creation', () => {
701
+ const buffer = new (ArrayBuffer as any)(32, { maxByteLength: 4096 })
702
+ const bytes = new Uint8Array(buffer)
703
+ const cursor = Cursor.create(bytes, { recursiveReadLimit: 2 })
704
+
705
+ cursor.readBytes(32)
706
+ buffer.resize(4096)
707
+ for (let i = 0; i < 3; i++) {
708
+ cursor.setPosition(2048)
709
+ cursor.readBytes(32)
710
+ }
711
+ cursor.setPosition(2048)
712
+ expect(() => cursor.readBytes(32)).toThrowError(
713
+ 'Recursive read limit of ' +
714
+ String.fromCharCode(96) +
715
+ '2' +
716
+ String.fromCharCode(96) +
717
+ ' exceeded',
718
+ )
719
+ })
649
720
  })
@@ -10,6 +10,7 @@ import * as Bytes from '../Bytes.js'
10
10
  import * as Errors from '../Errors.js'
11
11
  import * as Hex from '../Hex.js'
12
12
  import { bytesRegex, integerRegex } from '../Solidity.js'
13
+ import { nibbleTable } from './codec/hex.js'
13
14
  import { encoder } from './codec/utf8.js'
14
15
  import type * as Cursor from './cursor.js'
15
16
  import type { Compute, IsNarrowable, UnionToIntersection } from './types.js'
@@ -65,14 +66,8 @@ type MeasureContext = {
65
66
  checksumAddress: boolean
66
67
  }
67
68
 
68
- type HexPatch = {
69
- offset: number
70
- value: string
71
- }
72
-
73
69
  type WriteContext = {
74
70
  bytes: Bytes.Bytes
75
- patches: HexPatch[]
76
71
  view: DataView
77
72
  }
78
73
 
@@ -188,14 +183,14 @@ export function decodeAddress(
188
183
  })
189
184
  const wrap = (address: Hex.Hex) =>
190
185
  checksum ? Address.checksum(address) : address
191
- return [wrap(Hex.fromBytes(Bytes.slice(value, -20))), 32]
186
+ // A view, not a copy: it is hex-encoded at once and never escapes.
187
+ return [wrap(Hex.fromBytes(value.subarray(12))), 32]
192
188
  }
193
189
 
194
190
  export declare namespace decodeAddress {
195
191
  type ErrorType =
196
192
  | Address.InvalidAddressError
197
193
  | Hex.fromBytes.ErrorType
198
- | Bytes.slice.ErrorType
199
194
  | Errors.GlobalErrorType
200
195
  }
201
196
 
@@ -228,6 +223,7 @@ export function decodeArray(
228
223
 
229
224
  // Check if the array has any dynamic children.
230
225
  const dynamicChild = hasDynamicChild(param)
226
+ const decode = getDecoder(param, checksumAddress)
231
227
 
232
228
  let consumed = 0
233
229
  const value: unknown[] = []
@@ -235,10 +231,7 @@ export function decodeArray(
235
231
  // If any of the children is dynamic, then all elements will be offset pointer, thus size of one slot (32 bytes).
236
232
  // Otherwise, elements will be the size of their encoding (consumed bytes).
237
233
  cursor.setPosition(startOfData + (dynamicChild ? i * 32 : consumed))
238
- const [data, consumed_] = decodeParameter(cursor, param, {
239
- checksumAddress,
240
- staticPosition: startOfData,
241
- })
234
+ const [data, consumed_] = decode(cursor, startOfData)
242
235
  consumed += consumed_
243
236
  value.push(data)
244
237
  // Charge zero-width elements against the read limit to bound work
@@ -264,14 +257,12 @@ export function decodeArray(
264
257
  // Start is the static position of current slot + offset.
265
258
  const start = staticPosition + offset
266
259
 
260
+ const decode = getDecoder(param, checksumAddress)
267
261
  const value: unknown[] = []
268
262
  for (let i = 0; i < length; ++i) {
269
263
  // Move cursor along to the next slot (next offset pointer).
270
264
  cursor.setPosition(start + i * 32)
271
- const [data] = decodeParameter(cursor, param, {
272
- checksumAddress,
273
- staticPosition: start,
274
- })
265
+ const [data] = decode(cursor, start)
275
266
  value.push(data)
276
267
  }
277
268
 
@@ -285,13 +276,11 @@ export function decodeArray(
285
276
 
286
277
  // If the length of the array is known in advance and the array is deeply static,
287
278
  // then we can just decode each element in sequence.
279
+ const decode = getDecoder(param, checksumAddress)
288
280
  let consumed = 0
289
281
  const value: unknown[] = []
290
282
  for (let i = 0; i < length; ++i) {
291
- const [data, consumed_] = decodeParameter(cursor, param, {
292
- checksumAddress,
293
- staticPosition: staticPosition + consumed,
294
- })
283
+ const [data, consumed_] = decode(cursor, staticPosition + consumed)
295
284
  consumed += consumed_
296
285
  value.push(data)
297
286
  // Charge zero-width elements against the read limit to bound work
@@ -347,12 +336,23 @@ export function decodeBytes(
347
336
  return [Hex.fromBytes(data), 32]
348
337
  }
349
338
 
350
- const value = Hex.fromBytes(cursor.readBytes(Number.parseInt(size, 10), 32))
351
- return [value, 32]
339
+ // Read the whole slot so that a slot cut short of its padding is out of bounds.
340
+ const word = cursor.readBytes(32)
341
+ const size_ = Number.parseInt(size, 10)
342
+ // The value occupies the high `size` bytes of the word; the rest must be
343
+ // zero padding.
344
+ for (let i = size_; i < 32; i++)
345
+ if (word[i] !== 0)
346
+ throw new AbiParameters.BytesSizeMismatchError({
347
+ expectedSize: size_,
348
+ value: Hex.fromBytes(word),
349
+ })
350
+ return [Hex.fromBytes(word.subarray(0, size_)), 32]
352
351
  }
353
352
 
354
353
  export declare namespace decodeBytes {
355
354
  type ErrorType =
355
+ | AbiParameters.BytesSizeMismatchError
356
356
  | Hex.fromBytes.ErrorType
357
357
  | Bytes.toNumber.ErrorType
358
358
  | Errors.GlobalErrorType
@@ -363,24 +363,7 @@ export function decodeNumber(
363
363
  cursor: Cursor.Cursor,
364
364
  param: AbiParameters.Parameter,
365
365
  ) {
366
- const signed = param.type.startsWith('int')
367
- const size = Number.parseInt(param.type.split('int')[1] || '256', 10)
368
- const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
369
- // The word must hold a value that fits the declared size (zero-padded for
370
- // unsigned, sign-extended for signed types).
371
- if (size < 256) {
372
- const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
373
- const min = signed ? -max - 1n : 0n
374
- if (value > max || value < min)
375
- throw new Hex.IntegerOutOfRangeError({
376
- max: max.toString(),
377
- min: min.toString(),
378
- signed,
379
- size: size / 8,
380
- value: value.toString(),
381
- })
382
- }
383
- return [size > 48 ? value : Number(value), 32]
366
+ return decodeInteger(cursor, getInteger(param.type))
384
367
  }
385
368
 
386
369
  export declare namespace decodeNumber {
@@ -390,6 +373,65 @@ export declare namespace decodeNumber {
390
373
  | Errors.GlobalErrorType
391
374
  }
392
375
 
376
+ type Integer = {
377
+ max: bigint
378
+ min: bigint
379
+ signed: boolean
380
+ size: number
381
+ }
382
+
383
+ /** Parses an integer type and its bounds, once per type rather than per value. */
384
+ function getInteger(type: string): Integer {
385
+ const signed = type.startsWith('int')
386
+ const size = Number.parseInt(type.split('int')[1] || '256', 10)
387
+ if (!(size < 256)) return { max: 0n, min: 0n, signed, size }
388
+ const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
389
+ return { max, min: signed ? -max - 1n : 0n, signed, size }
390
+ }
391
+
392
+ function decodeInteger(cursor: Cursor.Cursor, integer: Integer) {
393
+ const { max, min, signed, size } = integer
394
+ const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
395
+ // The word must hold a value that fits the declared size (zero-padded for
396
+ // unsigned, sign-extended for signed types).
397
+ if (size < 256 && (value > max || value < min))
398
+ throw new Hex.IntegerOutOfRangeError({
399
+ max: max.toString(),
400
+ min: min.toString(),
401
+ signed,
402
+ size: size / 8,
403
+ value: value.toString(),
404
+ })
405
+ return [size > 48 ? value : Number(value), 32]
406
+ }
407
+
408
+ /**
409
+ * Resolves how to decode a parameter once, so that the elements of an array
410
+ * do not each re-parse its type. Types other than `address`, `bool` and
411
+ * integers go through `decodeParameter`.
412
+ */
413
+ function getDecoder(
414
+ param: AbiParameters.Parameter,
415
+ checksumAddress: boolean | undefined,
416
+ ): (
417
+ cursor: Cursor.Cursor,
418
+ staticPosition: number,
419
+ ) => ReturnType<typeof decodeParameter> {
420
+ const { type } = param
421
+ if (type === 'address')
422
+ return (cursor) => decodeAddress(cursor, { checksum: checksumAddress })
423
+ if (type === 'bool') return decodeBool
424
+ if (
425
+ (type.startsWith('uint') || type.startsWith('int')) &&
426
+ !getArrayComponents(type)
427
+ ) {
428
+ const integer = getInteger(type)
429
+ return (cursor) => decodeInteger(cursor, integer)
430
+ }
431
+ return (cursor, staticPosition) =>
432
+ decodeParameter(cursor, param, { checksumAddress, staticPosition })
433
+ }
434
+
393
435
  /** @internal */
394
436
  export type TupleAbiParameter = AbiParameters.Parameter & {
395
437
  components: readonly AbiParameters.Parameter[]
@@ -520,21 +562,13 @@ export function encodeParameters(
520
562
  const bytes = new Uint8Array(prefixSize + size)
521
563
  const writer: WriteContext = {
522
564
  bytes,
523
- patches: [],
524
565
  view: new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength),
525
566
  }
526
567
 
527
568
  if (prefix) writeHex(writer, 0, prefix)
528
569
  writeParameters(writer, prefixSize, compiled, values)
529
570
 
530
- let data = Hex.fromBytes(bytes)
531
- for (let i = 0; i < writer.patches.length; i++) {
532
- const patch = writer.patches[i]!
533
- const start = 2 + patch.offset * 2
534
- data =
535
- `${data.slice(0, start)}${patch.value}${data.slice(start + patch.value.length)}` as Hex.Hex
536
- }
537
- return data
571
+ return Hex.fromBytes(bytes)
538
572
  }
539
573
 
540
574
  export declare namespace encodeParameters {
@@ -551,6 +585,8 @@ export declare namespace encodeParameters {
551
585
  | AbiParameters.InvalidTypeError
552
586
  | Address.assert.ErrorType
553
587
  | Hex.IntegerOutOfRangeError
588
+ | Hex.InvalidHexValueError
589
+ | Hex.InvalidLengthError
554
590
  | Hex.fromBytes.ErrorType
555
591
  | Errors.GlobalErrorType
556
592
  }
@@ -749,6 +785,9 @@ function measureParameter(
749
785
  expectedSize: parameter.size,
750
786
  value: value_,
751
787
  })
788
+ // An unpaired nibble has no byte to belong to, so reject it instead of
789
+ // guessing which half of a byte it is.
790
+ if (value_.length % 2 !== 0) throw new Hex.InvalidLengthError(value_)
752
791
  if (parameter.size !== null) return 32
753
792
  return 32 + Math.ceil(size / 32) * 32
754
793
  }
@@ -803,18 +842,6 @@ function normalizeString(value: unknown): string {
803
842
  return `${value}`
804
843
  }
805
844
 
806
- function isWritableHex(value: Hex.Hex): boolean {
807
- if (
808
- typeof value !== 'string' ||
809
- value.charCodeAt(0) !== 48 ||
810
- value.charCodeAt(1) !== 120
811
- )
812
- return false
813
- for (let i = 2; i < value.length; i++)
814
- if (toNibble(value.charCodeAt(i)) === -1) return false
815
- return true
816
- }
817
-
818
845
  function writeParameters(
819
846
  context: WriteContext,
820
847
  offset: number,
@@ -917,18 +944,36 @@ function writeHex(
917
944
  offset: number,
918
945
  value: Hex.Hex,
919
946
  ): number {
920
- if (!isWritableHex(value)) {
921
- const patch = value.slice(2)
922
- context.patches.push({ offset, value: patch })
923
- return offset + Math.ceil(patch.length / 2)
924
- }
925
-
947
+ if (
948
+ typeof value !== 'string' ||
949
+ value.charCodeAt(0) !== 48 ||
950
+ value.charCodeAt(1) !== 120
951
+ )
952
+ throw new Hex.InvalidHexValueError(value)
953
+
954
+ // Validate and write in one pass. A valid nibble is at most `0x0f`, so only
955
+ // the `0xff` sentinel sets bit 7; one check after the loop covers every
956
+ // character. `nibbleTable` only spans Latin-1, so a code unit above it reads
957
+ // `undefined` and is mapped to the sentinel.
958
+ const { bytes } = context
959
+ // A local reference: some module transforms turn every read of an imported
960
+ // binding into a property lookup, which would land in the hot loop.
961
+ const table = nibbleTable
962
+ const length = value.length
963
+ let invalid = 0
926
964
  let i = 2
927
- for (; i + 1 < value.length; i += 2)
928
- context.bytes[offset++] =
929
- (toNibble(value.charCodeAt(i)) << 4) | toNibble(value.charCodeAt(i + 1))
930
- if (i < value.length)
931
- context.bytes[offset++] = toNibble(value.charCodeAt(i)) << 4
965
+ for (; i + 1 < length; i += 2) {
966
+ const hi = table[value.charCodeAt(i)] ?? 0xff
967
+ const lo = table[value.charCodeAt(i + 1)] ?? 0xff
968
+ invalid |= hi | lo
969
+ bytes[offset++] = (hi << 4) | lo
970
+ }
971
+ if (i < length) {
972
+ const hi = table[value.charCodeAt(i)] ?? 0xff
973
+ invalid |= hi
974
+ bytes[offset++] = hi << 4
975
+ }
976
+ if (invalid & 0x80) throw new Hex.InvalidHexValueError(value)
932
977
  return offset
933
978
  }
934
979
 
@@ -947,25 +992,22 @@ function writeWord(
947
992
  }
948
993
 
949
994
  const hex = value_.toString(16)
995
+ const table = nibbleTable
950
996
  let cursor = offset + 32 - Math.ceil(hex.length / 2)
951
997
  let i = hex.length % 2
952
- if (i === 1) context.bytes[cursor++] = toNibble(hex.charCodeAt(0))
998
+ if (i === 1) context.bytes[cursor++] = table[hex.charCodeAt(0)]!
953
999
  for (; i < hex.length; i += 2)
954
1000
  context.bytes[cursor++] =
955
- (toNibble(hex.charCodeAt(i)) << 4) | toNibble(hex.charCodeAt(i + 1))
956
- }
957
-
958
- function toNibble(code: number): number {
959
- if (code >= 48 && code <= 57) return code - 48
960
- if (code >= 65 && code <= 70) return code - 55
961
- if (code >= 97 && code <= 102) return code - 87
962
- return -1
1001
+ (table[hex.charCodeAt(i)]! << 4) | table[hex.charCodeAt(i + 1)]!
963
1002
  }
964
1003
 
965
1004
  /** @internal */
966
1005
  export function getArrayComponents(
967
1006
  type: string,
968
1007
  ): [length: number | null, innerType: string] | undefined {
1008
+ // Only a type ending in `]` can match, so skip the regex for the element
1009
+ // types (`uint256`, `address`, ...) that decoding checks once per value.
1010
+ if (type.charCodeAt(type.length - 1) !== 93 /* ] */) return undefined
969
1011
  const matches = arraySuffixRegex.exec(type)
970
1012
  return matches
971
1013
  ? // Return `null` if the array is dynamic.