ox 1.8.16 → 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 (170) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/dist/core/AbiEvent.d.ts +1 -1
  3. package/dist/core/AbiEvent.d.ts.map +1 -1
  4. package/dist/core/AbiEvent.js +27 -22
  5. package/dist/core/AbiEvent.js.map +1 -1
  6. package/dist/core/AbiItem.d.ts.map +1 -1
  7. package/dist/core/AbiItem.js +15 -5
  8. package/dist/core/AbiItem.js.map +1 -1
  9. package/dist/core/AbiParameters.d.ts +1 -1
  10. package/dist/core/AbiParameters.d.ts.map +1 -1
  11. package/dist/core/AbiParameters.js +8 -2
  12. package/dist/core/AbiParameters.js.map +1 -1
  13. package/dist/core/Base58.d.ts.map +1 -1
  14. package/dist/core/Base58.js +16 -4
  15. package/dist/core/Base58.js.map +1 -1
  16. package/dist/core/Blobs.d.ts +2 -3
  17. package/dist/core/Blobs.d.ts.map +1 -1
  18. package/dist/core/Blobs.js +38 -43
  19. package/dist/core/Blobs.js.map +1 -1
  20. package/dist/core/Bytes.d.ts.map +1 -1
  21. package/dist/core/Bytes.js +10 -2
  22. package/dist/core/Bytes.js.map +1 -1
  23. package/dist/core/Cbor.d.ts +13 -6
  24. package/dist/core/Cbor.d.ts.map +1 -1
  25. package/dist/core/Cbor.js +31 -18
  26. package/dist/core/Cbor.js.map +1 -1
  27. package/dist/core/Ens.js +2 -2
  28. package/dist/core/Ens.js.map +1 -1
  29. package/dist/core/FrameReceipt.d.ts.map +1 -1
  30. package/dist/core/FrameReceipt.js +4 -1
  31. package/dist/core/FrameReceipt.js.map +1 -1
  32. package/dist/core/Hex.d.ts.map +1 -1
  33. package/dist/core/Hex.js +34 -31
  34. package/dist/core/Hex.js.map +1 -1
  35. package/dist/core/Rlp.d.ts +1 -1
  36. package/dist/core/Rlp.d.ts.map +1 -1
  37. package/dist/core/Rlp.js +107 -0
  38. package/dist/core/Rlp.js.map +1 -1
  39. package/dist/core/RpcTransport.d.ts.map +1 -1
  40. package/dist/core/RpcTransport.js +3 -1
  41. package/dist/core/RpcTransport.js.map +1 -1
  42. package/dist/core/Transaction.d.ts.map +1 -1
  43. package/dist/core/Transaction.js +4 -2
  44. package/dist/core/Transaction.js.map +1 -1
  45. package/dist/core/TransactionReceipt.d.ts.map +1 -1
  46. package/dist/core/TransactionReceipt.js +8 -2
  47. package/dist/core/TransactionReceipt.js.map +1 -1
  48. package/dist/core/TypedData.d.ts.map +1 -1
  49. package/dist/core/TypedData.js +14 -2
  50. package/dist/core/TypedData.js.map +1 -1
  51. package/dist/core/internal/abiParameters.d.ts +3 -3
  52. package/dist/core/internal/abiParameters.d.ts.map +1 -1
  53. package/dist/core/internal/abiParameters.js +93 -68
  54. package/dist/core/internal/abiParameters.js.map +1 -1
  55. package/dist/core/internal/base58.d.ts.map +1 -1
  56. package/dist/core/internal/base58.js +9 -3
  57. package/dist/core/internal/base58.js.map +1 -1
  58. package/dist/core/internal/caches.d.ts +6 -0
  59. package/dist/core/internal/caches.d.ts.map +1 -1
  60. package/dist/core/internal/caches.js +3 -1
  61. package/dist/core/internal/caches.js.map +1 -1
  62. package/dist/core/internal/codec/hex.d.ts +13 -0
  63. package/dist/core/internal/codec/hex.d.ts.map +1 -1
  64. package/dist/core/internal/codec/hex.js +25 -2
  65. package/dist/core/internal/codec/hex.js.map +1 -1
  66. package/dist/core/internal/codec/int.d.ts.map +1 -1
  67. package/dist/core/internal/codec/int.js +48 -32
  68. package/dist/core/internal/codec/int.js.map +1 -1
  69. package/dist/core/internal/cursor.d.ts +11 -4
  70. package/dist/core/internal/cursor.d.ts.map +1 -1
  71. package/dist/core/internal/cursor.js +39 -15
  72. package/dist/core/internal/cursor.js.map +1 -1
  73. package/dist/core/internal/lru.d.ts +11 -12
  74. package/dist/core/internal/lru.d.ts.map +1 -1
  75. package/dist/core/internal/lru.js +22 -32
  76. package/dist/core/internal/lru.js.map +1 -1
  77. package/dist/core/internal/promise.d.ts.map +1 -1
  78. package/dist/core/internal/promise.js +13 -7
  79. package/dist/core/internal/promise.js.map +1 -1
  80. package/dist/erc6492/SignatureErc6492.d.ts +1 -1
  81. package/dist/erc6492/SignatureErc6492.d.ts.map +1 -1
  82. package/dist/erc6492/SignatureErc6492.js +1 -1
  83. package/dist/erc6492/SignatureErc6492.js.map +1 -1
  84. package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
  85. package/dist/tempo/SignatureEnvelope.js +7 -41
  86. package/dist/tempo/SignatureEnvelope.js.map +1 -1
  87. package/dist/tempo/TransactionReceipt.d.ts.map +1 -1
  88. package/dist/tempo/TransactionReceipt.js +4 -1
  89. package/dist/tempo/TransactionReceipt.js.map +1 -1
  90. package/dist/tempo/ZoneId.d.ts +3 -1
  91. package/dist/tempo/ZoneId.d.ts.map +1 -1
  92. package/dist/tempo/ZoneId.js +7 -1
  93. package/dist/tempo/ZoneId.js.map +1 -1
  94. package/dist/tempo/internal/virtualMasterPool.d.ts.map +1 -1
  95. package/dist/tempo/internal/virtualMasterPool.js +17 -6
  96. package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
  97. package/dist/tempo/internal/webauthnData.d.ts +23 -0
  98. package/dist/tempo/internal/webauthnData.d.ts.map +1 -0
  99. package/dist/tempo/internal/webauthnData.js +75 -0
  100. package/dist/tempo/internal/webauthnData.js.map +1 -0
  101. package/dist/zod/TypedData.d.ts +4 -4
  102. package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
  103. package/dist/zod/tempo/SignatureEnvelope.js +5 -15
  104. package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
  105. package/package.json +2 -2
  106. package/src/core/AbiEvent.bench.ts +45 -0
  107. package/src/core/AbiEvent.ts +30 -23
  108. package/src/core/AbiFunction.bench.ts +16 -0
  109. package/src/core/AbiItem.ts +13 -4
  110. package/src/core/AbiParameters.bench.ts +33 -0
  111. package/src/core/AbiParameters.ts +8 -1
  112. package/src/core/Base58.bench.ts +28 -0
  113. package/src/core/Base58.ts +15 -4
  114. package/src/core/Blobs.bench.ts +40 -0
  115. package/src/core/Blobs.ts +47 -55
  116. package/src/core/Bytes.bench.ts +34 -0
  117. package/src/core/Bytes.ts +10 -2
  118. package/src/core/Cbor.ts +36 -14
  119. package/src/core/Ens.ts +2 -2
  120. package/src/core/FrameReceipt.ts +4 -1
  121. package/src/core/Hex.bench.ts +17 -0
  122. package/src/core/Hex.ts +29 -32
  123. package/src/core/Rlp.ts +139 -1
  124. package/src/core/Rlp_tx.bench.ts +55 -0
  125. package/src/core/RpcTransport.ts +3 -1
  126. package/src/core/Transaction.ts +4 -2
  127. package/src/core/TransactionReceipt.ts +8 -2
  128. package/src/core/TypedData.bench.ts +50 -0
  129. package/src/core/TypedData.ts +14 -2
  130. package/src/core/_test/AbiEvent.test.ts +83 -0
  131. package/src/core/_test/AbiFunction.test.ts +7 -6
  132. package/src/core/_test/AbiParameters.decode.test.ts +34 -1
  133. package/src/core/_test/AbiParameters.encode.test.ts +10 -0
  134. package/src/core/_test/AbiParameters.test.ts +15 -0
  135. package/src/core/_test/Bytes.test.ts +33 -0
  136. package/src/core/_test/Caches.test.ts +20 -1
  137. package/src/core/_test/Cbor.test.ts +26 -0
  138. package/src/core/_test/FrameReceipt.test.ts +13 -0
  139. package/src/core/_test/Rlp.fuzz.ts +55 -1
  140. package/src/core/_test/Rlp.test.ts +64 -0
  141. package/src/core/_test/RpcTransport.test.ts +15 -2
  142. package/src/core/_test/Transaction.test.ts +17 -0
  143. package/src/core/_test/TransactionReceipt.test.ts +41 -0
  144. package/src/core/_test/TypedData.test.ts +19 -0
  145. package/src/core/_test/internal/cursor.test.ts +42 -6
  146. package/src/core/internal/abiParameters.ts +117 -80
  147. package/src/core/internal/base58.ts +9 -3
  148. package/src/core/internal/caches.ts +3 -1
  149. package/src/core/internal/codec/hex.ts +27 -2
  150. package/src/core/internal/codec/int.ts +45 -32
  151. package/src/core/internal/cursor.ts +52 -23
  152. package/src/core/internal/lru.ts +21 -35
  153. package/src/core/internal/promise.ts +11 -5
  154. package/src/erc6492/SignatureErc6492.ts +1 -1
  155. package/src/erc6492/_test/SignatureErc6492.test.ts +48 -1
  156. package/src/tempo/SignatureEnvelope.test.ts +89 -1
  157. package/src/tempo/SignatureEnvelope.ts +7 -43
  158. package/src/tempo/TransactionReceipt.test.ts +8 -0
  159. package/src/tempo/TransactionReceipt.ts +4 -1
  160. package/src/tempo/VirtualMaster.bench.ts +7 -0
  161. package/src/tempo/ZoneId.test.ts +11 -0
  162. package/src/tempo/ZoneId.ts +7 -1
  163. package/src/tempo/internal/virtualMasterPool.ts +20 -6
  164. package/src/tempo/internal/webauthnData.ts +84 -0
  165. package/src/version.ts +1 -1
  166. package/src/zod/tempo/SignatureEnvelope.ts +7 -16
  167. package/src/zod/tempo/_test/SignatureEnvelope.test.ts +37 -1
  168. package/src/core/_test/internal/lru.test.ts +0 -38
  169. package/src/core/_test/internal/uid.test.ts +0 -7
  170. package/src/core/internal/uid.ts +0 -15
@@ -266,9 +266,15 @@ export function fromRpc<const receipt extends Rpc | null>(
266
266
  effectiveGasPrice: BigInt(receipt.effectiveGasPrice ?? 0n),
267
267
  gasUsed: BigInt(receipt.gasUsed ?? 0n),
268
268
  logs: receipt.logs.map((log) => Log.fromRpc(log, { pending: false })),
269
- status: fromRpcStatus[receipt.status],
269
+ // Own keys only: an inherited one such as `__proto__` or `toString` is
270
+ // not a status or type.
271
+ status: Object.hasOwn(fromRpcStatus, receipt.status)
272
+ ? fromRpcStatus[receipt.status]
273
+ : undefined,
270
274
  transactionIndex: Number(receipt.transactionIndex ?? 0),
271
- type: (fromRpcType as any)[receipt.type] || receipt.type,
275
+ type: Object.hasOwn(fromRpcType, receipt.type)
276
+ ? fromRpcType[receipt.type as keyof typeof fromRpcType]
277
+ : receipt.type,
272
278
  } as never
273
279
  }
274
280
 
@@ -0,0 +1,50 @@
1
+ import { bench, describe } from 'vp/test'
2
+ import * as TypedData from './TypedData.js'
3
+
4
+ const typedData = {
5
+ domain: {
6
+ chainId: 1,
7
+ name: 'Ether Mail',
8
+ verifyingContract: '0x0000000000000000000000000000000000000000',
9
+ version: '1',
10
+ },
11
+ message: {
12
+ contents: 'Hello, Bob!',
13
+ from: {
14
+ name: 'Cow',
15
+ wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
16
+ },
17
+ to: {
18
+ name: 'Bob',
19
+ wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
20
+ },
21
+ },
22
+ primaryType: 'Mail',
23
+ types: {
24
+ Mail: [
25
+ { name: 'from', type: 'Person' },
26
+ { name: 'to', type: 'Person' },
27
+ { name: 'contents', type: 'string' },
28
+ ],
29
+ Person: [
30
+ { name: 'name', type: 'string' },
31
+ { name: 'wallet', type: 'address' },
32
+ ],
33
+ },
34
+ } as const
35
+
36
+ describe('TypedData.getSignPayload', () => {
37
+ bench('Mail', () => {
38
+ TypedData.getSignPayload(typedData)
39
+ })
40
+ })
41
+
42
+ describe('TypedData.hashStruct', () => {
43
+ bench('Person', () => {
44
+ TypedData.hashStruct({
45
+ data: typedData.message.from,
46
+ primaryType: 'Person',
47
+ types: typedData.types,
48
+ })
49
+ })
50
+ })
@@ -5,6 +5,7 @@ import * as Bytes from './Bytes.js'
5
5
  import * as Errors from './Errors.js'
6
6
  import * as Hash from './Hash.js'
7
7
  import * as Hex from './Hex.js'
8
+ import { caches } from './internal/caches.js'
8
9
  import type { Compute } from './internal/types.js'
9
10
  import * as Json from './Json.js'
10
11
  import * as Solidity from './Solidity.js'
@@ -914,6 +915,9 @@ export declare namespace encodeData {
914
915
  | Errors.GlobalErrorType
915
916
  }
916
917
 
918
+ /** Longest encoded type, in characters, whose hash is kept in the cache. */
919
+ const maxCachedTypeLength = 4_096
920
+
917
921
  /** @internal */
918
922
  export function hashType(value: {
919
923
  primaryType: string
@@ -923,8 +927,16 @@ export function hashType(value: {
923
927
  const { primaryType, types, typeHashes } = value
924
928
  const cached = typeHashes?.get(primaryType)
925
929
  if (cached) return cached
926
- const encodedHashType = Hex.fromString(encodeType({ primaryType, types }))
927
- const hash = Hash.keccak256(encodedHashType)
930
+ // Keyed by the encoded type, so equal struct definitions share one hash
931
+ // across calls, whichever object their types came from.
932
+ const type = encodeType({ primaryType, types })
933
+ let hash = caches.typeHash.get(type)
934
+ if (!hash) {
935
+ hash = Hash.keccak256(Hex.fromString(type))
936
+ // Only short encoded types are cached, so the cache's memory is bounded
937
+ // by its entry count rather than by the size of the types it was given.
938
+ if (type.length <= maxCachedTypeLength) caches.typeHash.set(type, hash)
939
+ }
928
940
  typeHashes?.set(primaryType, hash)
929
941
  return hash
930
942
  }
@@ -866,6 +866,89 @@ describe('decode', () => {
866
866
  `)
867
867
  })
868
868
 
869
+ test('error: indexed topic longer than 32 bytes', () => {
870
+ const abiEvent = AbiEvent.from('event Foo(address indexed a)')
871
+
872
+ expect(() =>
873
+ AbiEvent.decode(abiEvent, {
874
+ topics: [
875
+ AbiEvent.getSelector(abiEvent),
876
+ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac00',
877
+ ],
878
+ }),
879
+ ).toThrowErrorMatchingInlineSnapshot(
880
+ `[Hex.SizeOverflowError: Size cannot exceed \`32\` bytes. Given size: \`33\` bytes.]`,
881
+ )
882
+ })
883
+
884
+ test('error: indexed `address` topic with invalid characters', () => {
885
+ const abiEvent = AbiEvent.from('event Foo(address indexed a)')
886
+ const topic =
887
+ '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678zz'
888
+
889
+ expect(() =>
890
+ AbiEvent.decode(
891
+ abiEvent,
892
+ { topics: [AbiEvent.getSelector(abiEvent), topic] },
893
+ { checksumAddress: false },
894
+ ),
895
+ ).toThrowErrorMatchingInlineSnapshot(`
896
+ [Address.InvalidAddressError: Address "0xa5cc3c03994db5b0d9a5eedd10cabab0813678zz" is invalid.
897
+
898
+ Details: Address is not a 20 byte (40 hexadecimal character) value.]
899
+ `)
900
+ expect(() =>
901
+ AbiEvent.decode(abiEvent, {
902
+ topics: [AbiEvent.getSelector(abiEvent), topic],
903
+ }),
904
+ ).toThrowErrorMatchingInlineSnapshot(`
905
+ [Address.InvalidAddressError: Address "0xa5cc3c03994db5b0d9a5eedd10cabab0813678zz" is invalid.
906
+
907
+ Details: Address is not a 20 byte (40 hexadecimal character) value.]
908
+ `)
909
+ })
910
+
911
+ test('error: non-canonical indexed `bytesN` topics', () => {
912
+ const abiEvent = AbiEvent.from('event Foo(bytes4 indexed a)')
913
+
914
+ expect(
915
+ AbiEvent.decode(abiEvent, {
916
+ topics: [
917
+ AbiEvent.getSelector(abiEvent),
918
+ '0xdeadbeef00000000000000000000000000000000000000000000000000000000',
919
+ ],
920
+ }),
921
+ ).toMatchInlineSnapshot(`
922
+ {
923
+ "a": "0xdeadbeef",
924
+ }
925
+ `)
926
+ expect(() =>
927
+ AbiEvent.decode(abiEvent, {
928
+ topics: [
929
+ AbiEvent.getSelector(abiEvent),
930
+ '0xdeadbeef00000000000000000000000000000000000000000000000000000001',
931
+ ],
932
+ }),
933
+ ).toThrowErrorMatchingInlineSnapshot(
934
+ `[AbiParameters.BytesSizeMismatchError: Size of bytes "0xdeadbeef00000000000000000000000000000000000000000000000000000001" (bytes32) does not match expected size (bytes4).]`,
935
+ )
936
+
937
+ const bytes32 = AbiEvent.from('event Foo(bytes32 indexed a)')
938
+ expect(() =>
939
+ AbiEvent.decode(bytes32, {
940
+ topics: [
941
+ AbiEvent.getSelector(bytes32),
942
+ '0xdeadbeef000000000000000000000000000000000000000000000000000000zz',
943
+ ],
944
+ }),
945
+ ).toThrowErrorMatchingInlineSnapshot(`
946
+ [Hex.InvalidHexValueError: Value \`0xdeadbeef000000000000000000000000000000000000000000000000000000zz\` is an invalid hex value.
947
+
948
+ Hex values must start with \`"0x"\` and contain only hexadecimal characters (0-9, a-f, A-F).]
949
+ `)
950
+ })
951
+
869
952
  test('error: event selector topic mismatch', () => {
870
953
  const transfer = AbiEvent.from(
871
954
  'event Transfer(address indexed from, address indexed to, bool sender)',
@@ -322,13 +322,14 @@ describe('encodeData', () => {
322
322
  )
323
323
  })
324
324
 
325
- test('behavior: malformed bytes compatibility', () => {
325
+ test('error: malformed bytes', () => {
326
326
  const abiFunction = AbiFunction.from('function foo(bytes)')
327
- expect(
328
- AbiFunction.encodeData(abiFunction, ['0xzz11']),
329
- ).toMatchInlineSnapshot(
330
- `"0x30c8d1da00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002zz11000000000000000000000000000000000000000000000000000000000000"`,
331
- )
327
+ expect(() => AbiFunction.encodeData(abiFunction, ['0xzz11']))
328
+ .toThrowErrorMatchingInlineSnapshot(`
329
+ [Hex.InvalidHexValueError: Value \`0xzz11\` is an invalid hex value.
330
+
331
+ Hex values must start with \`"0x"\` and contain only hexadecimal characters (0-9, a-f, A-F).]
332
+ `)
332
333
  })
333
334
 
334
335
  test('behavior: with overloads', () => {
@@ -136,6 +136,17 @@ describe('static', () => {
136
136
  assertType<readonly [Hex.Hex]>(result)
137
137
  expect(result).toEqual(['0x0123456789abcdef'])
138
138
  })
139
+
140
+ test('error: non-zero padding', () => {
141
+ expect(() =>
142
+ AbiParameters.decode(
143
+ AbiParameters.from('bytes8'),
144
+ '0x0123456789abcdef000000000000000000000000000000000000000000000001',
145
+ ),
146
+ ).toThrowErrorMatchingInlineSnapshot(
147
+ `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x0123456789abcdef000000000000000000000000000000000000000000000001" (bytes32) does not match expected size (bytes8).]`,
148
+ )
149
+ })
139
150
  })
140
151
 
141
152
  describe('bytes16', () => {
@@ -1890,6 +1901,28 @@ describe('read size', () => {
1890
1901
  expect(value).toEqual(Array.from({ length }, () => tail))
1891
1902
  })
1892
1903
 
1904
+ test('error: array elements sharing one large tail', () => {
1905
+ // 33 elements point at one 64 KiB tail, so the decoded value would be
1906
+ // 33 times the size of the data.
1907
+ const length = 33
1908
+ const tail = Hex.fromBytes(new Uint8Array(65_536).fill(0xab))
1909
+ const data = Hex.concat(
1910
+ Hex.fromNumber(32, { size: 32 }), // ptr
1911
+ Hex.fromNumber(length, { size: 32 }), // len
1912
+ ...Array.from({ length }, () =>
1913
+ Hex.fromNumber(32 * length, { size: 32 }),
1914
+ ),
1915
+ Hex.fromNumber(65_536, { size: 32 }), // len of tail
1916
+ tail,
1917
+ )
1918
+ expect(Hex.size(data)).toMatchInlineSnapshot(`66688`)
1919
+ expect(() =>
1920
+ AbiParameters.decode(AbiParameters.from('bytes[]'), data),
1921
+ ).toThrowErrorMatchingInlineSnapshot(
1922
+ `[Cursor.ReadSizeLimitExceededError: Read size limit of \`395520\` bytes exceeded (read size: \`459264\` bytes).]`,
1923
+ )
1924
+ })
1925
+
1893
1926
  test('error: array elements with overlapping tails', () => {
1894
1927
  // Each element starts 33 bytes after the previous one and runs to the end
1895
1928
  // of the data.
@@ -1907,7 +1940,7 @@ describe('read size', () => {
1907
1940
  expect(() =>
1908
1941
  AbiParameters.decode(AbiParameters.from('bytes[]'), data),
1909
1942
  ).toThrowErrorMatchingInlineSnapshot(
1910
- `[Cursor.ReadSizeLimitExceededError: Read size limit of \`1329152\` bytes exceeded (read size: \`1335257\` bytes).]`,
1943
+ `[Cursor.ReadSizeLimitExceededError: Read size limit of \`328832\` bytes exceeded (read size: \`332354\` bytes).]`,
1911
1944
  )
1912
1945
  })
1913
1946
  })
@@ -348,6 +348,16 @@ describe('static', () => {
348
348
  `[AbiParameters.BytesSizeMismatchError: Size of bytes "0x0000000000000000000000000000000000000000000000000000000000000000000000000123456789abcdef" (bytes44) does not match expected size (bytes8).]`,
349
349
  )
350
350
  })
351
+
352
+ test('error: invalid hex', () => {
353
+ expect(() =>
354
+ AbiParameters.encode([{ type: 'bytes8' }], ['0x0123456789abcdeg']),
355
+ ).toThrowErrorMatchingInlineSnapshot(`
356
+ [Hex.InvalidHexValueError: Value \`0x0123456789abcdeg\` is an invalid hex value.
357
+
358
+ Hex values must start with \`"0x"\` and contain only hexadecimal characters (0-9, a-f, A-F).]
359
+ `)
360
+ })
351
361
  })
352
362
 
353
363
  describe('bytes16', () => {
@@ -243,6 +243,21 @@ describe('encodePacked', () => {
243
243
  )
244
244
  })
245
245
 
246
+ test('error: odd-length bytes', () => {
247
+ expect(() => AbiParameters.encodePacked(['bytes', 'uint8'], ['0x123', 1]))
248
+ .toThrowErrorMatchingInlineSnapshot(`
249
+ [Hex.InvalidLengthError: Hex value \`"0x123"\` is an odd length (3 nibbles).
250
+
251
+ It must be an even length.]
252
+ `)
253
+ expect(() => AbiParameters.encodePacked(['bytes[]'], [['0x0123', '0x123']]))
254
+ .toThrowErrorMatchingInlineSnapshot(`
255
+ [Hex.InvalidLengthError: Hex value \`"0x123"\` is an odd length (3 nibbles).
256
+
257
+ It must be an even length.]
258
+ `)
259
+ })
260
+
246
261
  test('error: unsupported type', () => {
247
262
  expect(
248
263
  // @ts-expect-error
@@ -1371,6 +1371,39 @@ describe('toBigInt', () => {
1371
1371
  ).toMatchInlineSnapshot('4206942069420694206942069420694206942069n')
1372
1372
  })
1373
1373
 
1374
+ test('behavior: zero-padded values', () => {
1375
+ for (const value of [
1376
+ 0n,
1377
+ 2n ** 48n - 1n,
1378
+ 2n ** 48n,
1379
+ 2n ** 64n - 1n,
1380
+ 2n ** 64n,
1381
+ 2n ** 72n + 1n,
1382
+ 2n ** 200n + 12345n,
1383
+ 2n ** 256n - 1n,
1384
+ ])
1385
+ for (const size of [32, 33, 40, 47])
1386
+ expect(Bytes.toBigInt(Bytes.fromNumber(value, { size }))).toBe(value)
1387
+ })
1388
+
1389
+ test('behavior: values wider than 64 bytes', () => {
1390
+ const ones = new Uint8Array(65).fill(0xff)
1391
+ expect(Bytes.toBigInt(ones)).toEqual((1n << 520n) - 1n)
1392
+ expect(Bytes.toBigInt(ones, { signed: true })).toEqual(-1n)
1393
+
1394
+ const high = new Uint8Array(65)
1395
+ high[0] = 0x80
1396
+ expect(Bytes.toBigInt(high)).toEqual(1n << 519n)
1397
+ expect(Bytes.toBigInt(high, { signed: true })).toEqual(-(1n << 519n))
1398
+
1399
+ // Leading zeros, read from an offset into a larger buffer.
1400
+ const padded = new Uint8Array(101).fill(0x01).subarray(1)
1401
+ padded.fill(0, 0, 20)
1402
+ expect(Bytes.toBigInt(padded, { signed: true })).toEqual(
1403
+ BigInt(`0x${'01'.repeat(80)}`),
1404
+ )
1405
+ })
1406
+
1374
1407
  test('args: size', () => {
1375
1408
  expect(
1376
1409
  Bytes.toBigInt(Bytes.fromNumber(420n, { size: 32 }), {
@@ -1,4 +1,4 @@
1
- import { AbiItem, Caches } from 'ox'
1
+ import { AbiItem, Caches, Engine } from 'ox'
2
2
  import { expect, test } from 'vp/test'
3
3
  import { caches } from '../internal/caches.js'
4
4
 
@@ -27,3 +27,22 @@ test('behavior: clears signature hashes', () => {
27
27
  Caches.clear()
28
28
  expect(caches.signatureHash.size).toBe(0)
29
29
  })
30
+
31
+ test('behavior: an engine change re-derives ABI item signature hashes', () => {
32
+ const abiItem = {
33
+ inputs: [{ type: 'uint256' }],
34
+ name: 'ownerOf',
35
+ outputs: [],
36
+ stateMutability: 'view',
37
+ type: 'function',
38
+ } as const
39
+ const hash = AbiItem.getSignatureHash(abiItem)
40
+ expect(
41
+ Engine.with({ Hash: { keccak256: () => new Uint8Array(32) } }, () =>
42
+ AbiItem.getSignatureHash(abiItem),
43
+ ),
44
+ ).toMatchInlineSnapshot(
45
+ `"0x0000000000000000000000000000000000000000000000000000000000000000"`,
46
+ )
47
+ expect(AbiItem.getSignatureHash(abiItem)).toBe(hash)
48
+ })
@@ -225,6 +225,15 @@ describe('encode', () => {
225
225
  expect(() => Cbor.encode(num)).toThrow(Cbor.NumberTooLargeError)
226
226
  })
227
227
 
228
+ test('number too large error names the encodable range', () => {
229
+ expect(() => Cbor.encode(0x100000000)).toThrow(
230
+ 'Number is outside the encodable integer range (-4294967296 to 4294967295): 4294967296',
231
+ )
232
+ expect(() => Cbor.encode(-0x100000001)).toThrow(
233
+ 'Number is outside the encodable integer range (-4294967296 to 4294967295): -4294967297',
234
+ )
235
+ })
236
+
228
237
  test('bigint throws', () => {
229
238
  expect(() => Cbor.encode(BigInt(42))).toThrow(Cbor.UnsupportedBigIntError)
230
239
  })
@@ -510,6 +519,23 @@ describe('decode', () => {
510
519
  expect(() => Cbor.decode('0x123')).toThrow()
511
520
  })
512
521
 
522
+ test('error: nesting deeper than the depth limit', () => {
523
+ // `0x81` opens a one-element array.
524
+ expect(
525
+ Cbor.decode(new Uint8Array([...new Uint8Array(1_023).fill(0x81), 0x01])),
526
+ ).toBeDefined()
527
+ expect(() =>
528
+ Cbor.decode(new Uint8Array([...new Uint8Array(1_024).fill(0x81), 0x01])),
529
+ ).toThrowErrorMatchingInlineSnapshot(
530
+ `[Cbor.DepthLimitExceededError: CBOR depth limit of \`1024\` exceeded.]`,
531
+ )
532
+ expect(() =>
533
+ Cbor.decode(new Uint8Array(100_000).fill(0x81)),
534
+ ).toThrowErrorMatchingInlineSnapshot(
535
+ `[Cbor.DepthLimitExceededError: CBOR depth limit of \`1024\` exceeded.]`,
536
+ )
537
+ })
538
+
513
539
  test('large string (> 0xffff)', () => {
514
540
  const str = 'a'.repeat(0x10000)
515
541
  const encoded = Cbor.encode(str)
@@ -46,6 +46,19 @@ describe('fromRpc', () => {
46
46
  status: expected,
47
47
  })
48
48
  })
49
+
50
+ test('behavior: unknown status named like an `Object.prototype` key', () => {
51
+ for (const status of ['__proto__', 'constructor', 'toString'])
52
+ expect(
53
+ FrameReceipt.fromRpc({
54
+ executionGasUsed: '0x0',
55
+ gasUsed: '0x0',
56
+ logs: [],
57
+ stateGasUsed: '0x0',
58
+ status: status as never,
59
+ }).status,
60
+ ).toBeUndefined()
61
+ })
49
62
  })
50
63
 
51
64
  describe('toRpc', () => {
@@ -1,4 +1,4 @@
1
- import { test } from '@fast-check/vitest'
1
+ import { fc, test } from '@fast-check/vitest'
2
2
  import { type Bytes, Hex, Rlp } from 'ox'
3
3
  import { describe, expect } from 'vp/test'
4
4
 
@@ -36,8 +36,62 @@ describe('Rlp round-trip', () => {
36
36
  expect(asHex).toEqual(Hex.fromBytes(asBytes))
37
37
  },
38
38
  )
39
+
40
+ test.prop(
41
+ {
42
+ edit: fc.tuple(
43
+ fc.nat(),
44
+ fc.nat(0xff),
45
+ fc.constantFrom('cut', 'set', 'add', 'char'),
46
+ ),
47
+ tree: arbitraryRecursiveRlpHex(),
48
+ },
49
+ { numRuns },
50
+ )(
51
+ 'hex and bytes decode paths agree on mutated encodings, errors included',
52
+ ({ edit: [at, byte, kind], tree }) => {
53
+ const body = Rlp.from(tree, { as: 'Hex' }).slice(2)
54
+ const index = (at % (body.length / 2 + 1)) * 2
55
+ const pair = byte.toString(16).padStart(2, '0')
56
+ const mutated = `0x${(() => {
57
+ if (kind === 'cut') return body.slice(0, index)
58
+ if (kind === 'set')
59
+ return `${body.slice(0, index)}${pair}${body.slice(index + 2)}`
60
+ if (kind === 'add')
61
+ return `${body.slice(0, index)}${pair}${body.slice(index)}`
62
+ return `${body.slice(0, index)}g${body.slice(index + 1)}`
63
+ })()}` as Hex.Hex
64
+ expect(outcome(() => Rlp.toHex(mutated))).toEqual(
65
+ outcome(() => normalizeBytesToHex(Rlp.toBytes(mutated))),
66
+ )
67
+ },
68
+ )
39
69
  })
40
70
 
71
+ /**
72
+ * Returns a decode result or its error, so that two decode paths can be
73
+ * compared on malformed input.
74
+ *
75
+ * @internal
76
+ */
77
+ function outcome(fn: () => unknown) {
78
+ try {
79
+ return { value: fn() }
80
+ } catch (error) {
81
+ return { error: `${(error as Error).name}: ${(error as Error).message}` }
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Converts `Uint8Array` leaves to hex, as `Rlp.toHex` returns them.
87
+ *
88
+ * @internal
89
+ */
90
+ function normalizeBytesToHex(value: RecursiveArray<Bytes.Bytes>): unknown {
91
+ if (Array.isArray(value)) return value.map(normalizeBytesToHex)
92
+ return Hex.fromBytes(value as Uint8Array)
93
+ }
94
+
41
95
  /**
42
96
  * Lower-case any hex string in a recursive tree. The encoder emits
43
97
  * lowercase but the arbitrary may produce mixed casing in the future,
@@ -157,6 +157,70 @@ describe('Rlp.to', () => {
157
157
  expect(Rlp.toHex(rlpBytes)).toEqual('0x')
158
158
  })
159
159
 
160
+ test('behavior: hex leaves of a hex input', () => {
161
+ // Short and long leaves, nested, in short and long (> 4096 hex
162
+ // characters) inputs.
163
+ for (const length of [32, 4_096]) {
164
+ const value = [
165
+ '0x0a',
166
+ '0x',
167
+ `0x${'ab'.repeat(20)}`,
168
+ [[`0x${'cd'.repeat(length)}`], '0x7f', '0x80'],
169
+ `0x${'ef'.repeat(56)}`,
170
+ ] as const
171
+ const encoded = Rlp.fromHex(value as never)
172
+ expect(Rlp.toHex(encoded)).toEqual(value)
173
+ // Leaves are lowercase, as `Hex.fromBytes` writes them.
174
+ expect(Rlp.toHex(`0x${encoded.slice(2).toUpperCase()}`)).toEqual(value)
175
+ }
176
+ })
177
+
178
+ test('behavior: hex input decodes and fails like byte input', () => {
179
+ // Decoding to bytes always goes through `Bytes.fromHex` and the cursor,
180
+ // which short hex input to hex skips.
181
+ const toHex = (value: unknown): unknown =>
182
+ Array.isArray(value) ? value.map(toHex) : Hex.fromBytes(value as never)
183
+ const outcome = (fn: () => unknown) => {
184
+ try {
185
+ return { value: fn() }
186
+ } catch (error) {
187
+ return {
188
+ error: `${(error as Error).name}: ${(error as Error).message}`,
189
+ }
190
+ }
191
+ }
192
+ for (const value of [
193
+ '0x',
194
+ '0x00',
195
+ '0x7F',
196
+ '0x8180',
197
+ '0xC88363617483646F67',
198
+ '0xc0',
199
+ '0xzz',
200
+ '0x8',
201
+ '0x0\u0130',
202
+ '0x82',
203
+ '0x8201',
204
+ '0xb8',
205
+ '0xb801ff',
206
+ '0xb838',
207
+ '0xb90001',
208
+ '0xbc',
209
+ '0x8100',
210
+ '0x817f',
211
+ '0xc20102',
212
+ '0xc1c0c0',
213
+ '0xc3010203ff',
214
+ '0xc28201',
215
+ '0xff',
216
+ '0x0101',
217
+ `0x${'c1'.repeat(1_025)}`,
218
+ ] as const)
219
+ expect(outcome(() => Rlp.toHex(value as Hex.Hex))).toEqual(
220
+ outcome(() => toHex(Rlp.toBytes(value as Hex.Hex))),
221
+ )
222
+ })
223
+
160
224
  describe('prefix < 0x80', () => {
161
225
  test('bytes -> bytes', () => {
162
226
  expect(Rlp.toBytes(Rlp.fromBytes(Bytes.fromHex('0x00')))).toEqual(
@@ -224,7 +224,7 @@ describe('fromHttp', () => {
224
224
 
225
225
  Status: 400
226
226
  URL: https://oxlib.sh/rpc
227
- Body: "{\\"id\\":0,\\"method\\":\\"eth_accounts\\",\\"jsonrpc\\":\\"2.0\\"}"
227
+ Body: {"id":0,"method":"eth_accounts","jsonrpc":"2.0"}
228
228
 
229
229
  Details: Bad Request]
230
230
  `)
@@ -244,7 +244,7 @@ describe('fromHttp', () => {
244
244
 
245
245
  Status: 400
246
246
  URL: https://oxlib.sh/rpc
247
- Body: "{\\"id\\":0,\\"method\\":\\"eth_accounts\\",\\"jsonrpc\\":\\"2.0\\"}"
247
+ Body: {"id":0,"method":"eth_accounts","jsonrpc":"2.0"}
248
248
 
249
249
  Details: bad]
250
250
  `)
@@ -317,6 +317,19 @@ describe('fromHttp', () => {
317
317
  }
318
318
  })
319
319
 
320
+ test('behavior: timeout rejects when `fetchFn` ignores the signal', async () => {
321
+ const transport = RpcTransport.fromHttp('https://example.com', {
322
+ fetchFn: () => new Promise<Response>(() => {}),
323
+ timeout: 50,
324
+ })
325
+
326
+ await expect(() =>
327
+ transport.request({ method: 'eth_accounts' }),
328
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
329
+ `[Promise.TimeoutError: Operation timed out.]`,
330
+ )
331
+ })
332
+
320
333
  test('behavior: timeout covers a response body that stalls after its headers', async () => {
321
334
  const server = await createHttpServer(async (_, res) => {
322
335
  res.writeHead(200, { 'Content-Type': 'application/json' })
@@ -13,6 +13,23 @@ import { accounts } from '../../../test/constants/accounts.js'
13
13
  import { anvilMainnet } from '../../../test/prool.js'
14
14
 
15
15
  describe('fromRpc', () => {
16
+ test('behavior: unknown type named like an `Object.prototype` key', () => {
17
+ const transaction = Transaction.fromRpc({
18
+ blockHash: null,
19
+ blockNumber: null,
20
+ from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6',
21
+ gas: '0x5208',
22
+ hash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0',
23
+ input: '0x',
24
+ nonce: '0x0',
25
+ to: null,
26
+ transactionIndex: null,
27
+ type: 'constructor',
28
+ value: '0x0',
29
+ } as unknown as Transaction.Rpc)
30
+ expect(transaction.type).toMatchInlineSnapshot(`"constructor"`)
31
+ })
32
+
16
33
  describe('legacy', () => {
17
34
  test('default', () => {
18
35
  expect(