ox 1.8.13 → 1.8.14

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 (146) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/dist/core/AbiError.d.ts +2 -2
  3. package/dist/core/AbiError.d.ts.map +1 -1
  4. package/dist/core/AbiError.js +6 -2
  5. package/dist/core/AbiError.js.map +1 -1
  6. package/dist/core/AbiEvent.d.ts +71 -1
  7. package/dist/core/AbiEvent.d.ts.map +1 -1
  8. package/dist/core/AbiEvent.js +160 -19
  9. package/dist/core/AbiEvent.js.map +1 -1
  10. package/dist/core/AbiFunction.d.ts +1 -1
  11. package/dist/core/AbiFunction.d.ts.map +1 -1
  12. package/dist/core/AbiFunction.js +3 -5
  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 +18 -8
  16. package/dist/core/AbiItem.js.map +1 -1
  17. package/dist/core/Base32.d.ts +3 -3
  18. package/dist/core/Base32.d.ts.map +1 -1
  19. package/dist/core/Base32.js +5 -2
  20. package/dist/core/Base32.js.map +1 -1
  21. package/dist/core/Bytes.d.ts.map +1 -1
  22. package/dist/core/Bytes.js +4 -3
  23. package/dist/core/Bytes.js.map +1 -1
  24. package/dist/core/Caches.d.ts +1 -2
  25. package/dist/core/Caches.d.ts.map +1 -1
  26. package/dist/core/Caches.js +1 -4
  27. package/dist/core/Caches.js.map +1 -1
  28. package/dist/core/Cbor.d.ts.map +1 -1
  29. package/dist/core/Cbor.js +16 -2
  30. package/dist/core/Cbor.js.map +1 -1
  31. package/dist/core/CompactSize.d.ts +8 -1
  32. package/dist/core/CompactSize.d.ts.map +1 -1
  33. package/dist/core/CompactSize.js +9 -0
  34. package/dist/core/CompactSize.js.map +1 -1
  35. package/dist/core/Hex.d.ts.map +1 -1
  36. package/dist/core/Hex.js +9 -4
  37. package/dist/core/Hex.js.map +1 -1
  38. package/dist/core/Rlp.d.ts +9 -2
  39. package/dist/core/Rlp.d.ts.map +1 -1
  40. package/dist/core/Rlp.js +51 -8
  41. package/dist/core/Rlp.js.map +1 -1
  42. package/dist/core/RpcTransport.d.ts.map +1 -1
  43. package/dist/core/RpcTransport.js +11 -4
  44. package/dist/core/RpcTransport.js.map +1 -1
  45. package/dist/core/Siwe.d.ts.map +1 -1
  46. package/dist/core/Siwe.js +24 -9
  47. package/dist/core/Siwe.js.map +1 -1
  48. package/dist/core/TypedData.d.ts.map +1 -1
  49. package/dist/core/TypedData.js +13 -9
  50. package/dist/core/TypedData.js.map +1 -1
  51. package/dist/core/Value.d.ts.map +1 -1
  52. package/dist/core/Value.js +5 -3
  53. package/dist/core/Value.js.map +1 -1
  54. package/dist/core/internal/abiItem.d.ts +3 -1
  55. package/dist/core/internal/abiItem.d.ts.map +1 -1
  56. package/dist/core/internal/abiItem.js +5 -3
  57. package/dist/core/internal/abiItem.js.map +1 -1
  58. package/dist/core/internal/caches.d.ts +10 -0
  59. package/dist/core/internal/caches.d.ts.map +1 -0
  60. package/dist/core/internal/caches.js +10 -0
  61. package/dist/core/internal/caches.js.map +1 -0
  62. package/dist/core/internal/frameNonce.d.ts.map +1 -1
  63. package/dist/core/internal/frameNonce.js +2 -2
  64. package/dist/core/internal/frameNonce.js.map +1 -1
  65. package/dist/erc4337/UserOperation.d.ts +15 -2
  66. package/dist/erc4337/UserOperation.d.ts.map +1 -1
  67. package/dist/erc4337/UserOperation.js +8 -2
  68. package/dist/erc4337/UserOperation.js.map +1 -1
  69. package/dist/erc8021/Attribution.d.ts.map +1 -1
  70. package/dist/erc8021/Attribution.js +12 -3
  71. package/dist/erc8021/Attribution.js.map +1 -1
  72. package/dist/tempo/AccountOperation.d.ts +8 -0
  73. package/dist/tempo/AccountOperation.d.ts.map +1 -1
  74. package/dist/tempo/AccountOperation.js +26 -6
  75. package/dist/tempo/AccountOperation.js.map +1 -1
  76. package/dist/tempo/SignatureEnvelope.d.ts +4 -4
  77. package/dist/tempo/Tick.d.ts.map +1 -1
  78. package/dist/tempo/Tick.js +4 -3
  79. package/dist/tempo/Tick.js.map +1 -1
  80. package/dist/zod/Address.d.ts.map +1 -1
  81. package/dist/zod/Address.js +2 -1
  82. package/dist/zod/Address.js.map +1 -1
  83. package/dist/zod/FrameRequest.d.ts.map +1 -1
  84. package/dist/zod/FrameRequest.js +2 -1
  85. package/dist/zod/FrameRequest.js.map +1 -1
  86. package/dist/zod/Hex.d.ts.map +1 -1
  87. package/dist/zod/Hex.js +2 -1
  88. package/dist/zod/Hex.js.map +1 -1
  89. package/dist/zod/Solidity.d.ts.map +1 -1
  90. package/dist/zod/Solidity.js +12 -7
  91. package/dist/zod/Solidity.js.map +1 -1
  92. package/dist/zod/TypedData.d.ts +12 -12
  93. package/package.json +1 -1
  94. package/src/core/AbiError.ts +33 -22
  95. package/src/core/AbiEvent.ts +177 -18
  96. package/src/core/AbiFunction.ts +10 -7
  97. package/src/core/AbiItem.ts +17 -8
  98. package/src/core/Base32.ts +8 -3
  99. package/src/core/Bytes.ts +4 -3
  100. package/src/core/Caches.ts +1 -6
  101. package/src/core/Cbor.ts +20 -2
  102. package/src/core/CompactSize.ts +13 -0
  103. package/src/core/Hex.ts +10 -5
  104. package/src/core/Rlp.ts +55 -5
  105. package/src/core/RpcTransport.ts +11 -4
  106. package/src/core/Siwe.ts +26 -11
  107. package/src/core/TypedData.ts +14 -9
  108. package/src/core/Value.ts +6 -3
  109. package/src/core/_test/AbiError.test-d.ts +10 -0
  110. package/src/core/_test/AbiError.test.ts +73 -0
  111. package/src/core/_test/AbiEvent.fuzz.ts +260 -0
  112. package/src/core/_test/AbiEvent.snap-d.ts +9 -0
  113. package/src/core/_test/AbiEvent.test.ts +421 -0
  114. package/src/core/_test/AbiFunction.snap-d.ts +25 -0
  115. package/src/core/_test/AbiFunction.test.ts +101 -18
  116. package/src/core/_test/AbiItem.snap-d.ts +11 -0
  117. package/src/core/_test/AbiItem.test.ts +155 -1
  118. package/src/core/_test/Base32.test.ts +16 -0
  119. package/src/core/_test/Bytes.test.ts +15 -0
  120. package/src/core/_test/Caches.test.ts +10 -1
  121. package/src/core/_test/Cbor.test.ts +23 -0
  122. package/src/core/_test/CompactSize.test.ts +14 -0
  123. package/src/core/_test/Hex.test.ts +25 -0
  124. package/src/core/_test/Rlp.test.ts +51 -0
  125. package/src/core/_test/RpcTransport.test.ts +47 -0
  126. package/src/core/_test/Siwe.test.ts +28 -0
  127. package/src/core/_test/TypedData.test.ts +17 -2
  128. package/src/core/internal/abiItem.ts +16 -7
  129. package/src/core/internal/caches.ts +12 -0
  130. package/src/core/internal/frameNonce.ts +3 -4
  131. package/src/erc4337/UserOperation.ts +30 -3
  132. package/src/erc4337/_test/UserOperation.test.ts +132 -35
  133. package/src/erc8021/Attribution.ts +9 -7
  134. package/src/erc8021/_test/Attribution.test.ts +37 -0
  135. package/src/tempo/AccountOperation.test.ts +116 -0
  136. package/src/tempo/AccountOperation.ts +38 -5
  137. package/src/tempo/AccountSimulation.test.ts +1 -1
  138. package/src/tempo/SignatureEnvelope.test.ts +12 -0
  139. package/src/tempo/SignatureEnvelope.ts +4 -4
  140. package/src/tempo/Tick.test.ts +10 -0
  141. package/src/tempo/Tick.ts +5 -3
  142. package/src/version.ts +1 -1
  143. package/src/zod/Address.ts +3 -3
  144. package/src/zod/FrameRequest.ts +3 -4
  145. package/src/zod/Hex.ts +3 -1
  146. package/src/zod/Solidity.ts +15 -18
@@ -5,6 +5,8 @@ import {
5
5
  AbiItem,
6
6
  AbiParameters,
7
7
  Bytes,
8
+ Caches,
9
+ Engine,
8
10
  type Hex,
9
11
  } from 'ox'
10
12
  import { describe, expect, test } from 'vp/test'
@@ -18,6 +20,7 @@ import {
18
20
  isArgOfType,
19
21
  normalizeSignature,
20
22
  } from '../internal/abiItem.js'
23
+ import { caches } from '../internal/caches.js'
21
24
 
22
25
  describe('format', () => {
23
26
  test('default', () => {
@@ -398,6 +401,19 @@ describe('fromAbi', () => {
398
401
  "inputs": [],
399
402
  "name": "foo",
400
403
  "outputs": [],
404
+ "overloads": [
405
+ {
406
+ "inputs": [
407
+ {
408
+ "type": "uint256",
409
+ },
410
+ ],
411
+ "name": "foo",
412
+ "outputs": [],
413
+ "stateMutability": "nonpayable",
414
+ "type": "function",
415
+ },
416
+ ],
401
417
  "stateMutability": "nonpayable",
402
418
  "type": "function",
403
419
  }
@@ -417,6 +433,19 @@ describe('fromAbi', () => {
417
433
  "inputs": [],
418
434
  "name": "foo",
419
435
  "outputs": [],
436
+ "overloads": [
437
+ {
438
+ "inputs": [
439
+ {
440
+ "type": "uint256",
441
+ },
442
+ ],
443
+ "name": "foo",
444
+ "outputs": [],
445
+ "stateMutability": "nonpayable",
446
+ "type": "function",
447
+ },
448
+ ],
420
449
  "stateMutability": "nonpayable",
421
450
  "type": "function",
422
451
  }
@@ -524,6 +553,30 @@ describe('fromAbi', () => {
524
553
  "inputs": [],
525
554
  "name": "mint",
526
555
  "outputs": [],
556
+ "overloads": [
557
+ {
558
+ "inputs": [
559
+ {
560
+ "type": "uint256",
561
+ },
562
+ ],
563
+ "name": "mint",
564
+ "outputs": [],
565
+ "stateMutability": "nonpayable",
566
+ "type": "function",
567
+ },
568
+ {
569
+ "inputs": [
570
+ {
571
+ "type": "string",
572
+ },
573
+ ],
574
+ "name": "mint",
575
+ "outputs": [],
576
+ "stateMutability": "nonpayable",
577
+ "type": "function",
578
+ },
579
+ ],
527
580
  "stateMutability": "nonpayable",
528
581
  "type": "function",
529
582
  }
@@ -1047,6 +1100,68 @@ describe('fromAbi', () => {
1047
1100
  `)
1048
1101
  })
1049
1102
 
1103
+ test('behavior: overloads: string vs bytes', () => {
1104
+ const results = (
1105
+ [
1106
+ ['bytes', 'string'],
1107
+ ['string', 'bytes'],
1108
+ ] as const
1109
+ ).map(([first, second]) => [
1110
+ AbiItem.format(
1111
+ AbiItem.fromAbi(
1112
+ Abi.from([`function foo(${first})`, `function foo(${second})`]),
1113
+ 'foo',
1114
+ { args: ['hello'] },
1115
+ ),
1116
+ ),
1117
+ AbiItem.format(
1118
+ AbiItem.fromAbi(
1119
+ Abi.from([`function foo(${first}[])`, `function foo(${second}[])`]),
1120
+ 'foo',
1121
+ { args: [['hello', 'world']] },
1122
+ ),
1123
+ ),
1124
+ AbiItem.format(
1125
+ AbiItem.fromAbi(
1126
+ Abi.from([`function foo((${first}))`, `function foo((${second}))`]),
1127
+ 'foo',
1128
+ { args: [['hello']] },
1129
+ ),
1130
+ ),
1131
+ ])
1132
+
1133
+ expect(results).toMatchInlineSnapshot(`
1134
+ [
1135
+ [
1136
+ "function foo(string)",
1137
+ "function foo(string[])",
1138
+ "function foo((string))",
1139
+ ],
1140
+ [
1141
+ "function foo(string)",
1142
+ "function foo(string[])",
1143
+ "function foo((string))",
1144
+ ],
1145
+ ]
1146
+ `)
1147
+
1148
+ expect(() =>
1149
+ AbiItem.fromAbi(
1150
+ Abi.from(['function foo(bytes)', 'function foo(string)']),
1151
+ 'foo',
1152
+ { args: ['0x1234'] },
1153
+ ),
1154
+ ).toThrowErrorMatchingInlineSnapshot(`
1155
+ [AbiItem.AmbiguityError: Found ambiguous types in overloaded ABI Items.
1156
+
1157
+ \`string\` in \`foo(string)\`, and
1158
+ \`bytes\` in \`foo(bytes)\`
1159
+
1160
+ These types encode differently and cannot be distinguished at runtime.
1161
+ Remove one of the ambiguous items in the ABI.]
1162
+ `)
1163
+ })
1164
+
1050
1165
  test('behavior: overloads: uint256 vs int256', () => {
1051
1166
  expect(() =>
1052
1167
  AbiItem.fromAbi(
@@ -1553,7 +1668,8 @@ describe('fromAbi', () => {
1553
1668
  { arg: false, abiParameter: { type: 'bool' }, expected: true },
1554
1669
  { arg: 'true', abiParameter: { type: 'bool' }, expected: false },
1555
1670
  // bytes
1556
- { arg: 'foo', abiParameter: { type: 'bytes' }, expected: true },
1671
+ { arg: 'foo', abiParameter: { type: 'bytes' }, expected: false },
1672
+ { arg: '0x1234', abiParameter: { type: 'bytes' }, expected: true },
1557
1673
  { arg: 'foo', abiParameter: { type: 'bytes32' }, expected: false },
1558
1674
  { arg: '0x1234', abiParameter: { type: 'bytes2' }, expected: true },
1559
1675
  { arg: '0x1234', abiParameter: { type: 'bytes4' }, expected: false },
@@ -1583,6 +1699,10 @@ describe('fromAbi', () => {
1583
1699
  { arg: 1, abiParameter: { type: 'uint' }, expected: true },
1584
1700
  { arg: 1n, abiParameter: { type: 'uint' }, expected: true },
1585
1701
  { arg: 1n, abiParameter: { type: 'uint' }, expected: true },
1702
+ { arg: 2n ** 256n - 1n, abiParameter: { type: 'uint' }, expected: true },
1703
+ { arg: 2n ** 256n, abiParameter: { type: 'uint' }, expected: false },
1704
+ { arg: -(2n ** 255n), abiParameter: { type: 'int' }, expected: true },
1705
+ { arg: 2n ** 255n, abiParameter: { type: 'int' }, expected: false },
1586
1706
  { arg: 1, abiParameter: { type: 'int256' }, expected: true },
1587
1707
  { arg: 1, abiParameter: { type: 'uint256' }, expected: true },
1588
1708
  { arg: 1, abiParameter: { type: 'int69' }, expected: false },
@@ -2486,6 +2606,40 @@ describe('getSignature', () => {
2486
2606
  })
2487
2607
 
2488
2608
  describe('getSignatureHash', () => {
2609
+ test('behavior: caches by normalized signature', () => {
2610
+ Caches.clear()
2611
+ const hash = AbiItem.getSignatureHash('function ownerOf(uint256 tokenId)')
2612
+ expect(
2613
+ AbiItem.getSignatureHash({
2614
+ inputs: [{ name: 'id', type: 'uint256' }],
2615
+ name: 'ownerOf',
2616
+ outputs: [],
2617
+ stateMutability: 'view',
2618
+ type: 'function',
2619
+ }),
2620
+ ).toBe(hash)
2621
+ expect([...caches.signatureHash]).toMatchInlineSnapshot(`
2622
+ [
2623
+ [
2624
+ "ownerOf(uint256)",
2625
+ "0x6352211e6566aa027e75ac9dbf2423197fbd9b82b9d981a3ab367d355866aa1c",
2626
+ ],
2627
+ ]
2628
+ `)
2629
+ })
2630
+
2631
+ test('behavior: recomputes after an engine change', () => {
2632
+ const hash = AbiItem.getSignatureHash('function ownerOf(uint256)')
2633
+ expect(
2634
+ Engine.with({ Hash: { keccak256: () => new Uint8Array(32) } }, () =>
2635
+ AbiItem.getSignatureHash('function ownerOf(uint256)'),
2636
+ ),
2637
+ ).toMatchInlineSnapshot(
2638
+ `"0x0000000000000000000000000000000000000000000000000000000000000000"`,
2639
+ )
2640
+ expect(AbiItem.getSignatureHash('function ownerOf(uint256)')).toBe(hash)
2641
+ })
2642
+
2489
2643
  test('hashes functions', () => {
2490
2644
  expect(AbiItem.getSignatureHash('function ownerOf(uint256)')).toEqual(
2491
2645
  '0x6352211e6566aa027e75ac9dbf2423197fbd9b82b9d981a3ab367d355866aa1c',
@@ -44,6 +44,22 @@ describe('toBytes', () => {
44
44
  `[Base32.InvalidCharacterError: Invalid bech32 base32 character: "b".]`,
45
45
  )
46
46
  })
47
+
48
+ test('error: non-zero trailing bits', () => {
49
+ // `qrlsq` encodes 0x00ff00; `qrlsp` sets a trailing bit below the byte boundary.
50
+ expect(() => Base32.toBytes('qrlsp')).toThrowErrorMatchingInlineSnapshot(
51
+ `[Base32.InvalidPaddingError: Non-canonical trailing bits in Base32 input.]`,
52
+ )
53
+ })
54
+
55
+ test('error: invalid length', () => {
56
+ expect(() => Base32.toBytes('q')).toThrowErrorMatchingInlineSnapshot(
57
+ `[Base32.InvalidPaddingError: Non-canonical trailing bits in Base32 input.]`,
58
+ )
59
+ expect(() => Base32.toBytes('qqq')).toThrowErrorMatchingInlineSnapshot(
60
+ `[Base32.InvalidPaddingError: Non-canonical trailing bits in Base32 input.]`,
61
+ )
62
+ })
47
63
  })
48
64
 
49
65
  describe('toHex', () => {
@@ -293,6 +293,21 @@ describe('fromHex', () => {
293
293
  )
294
294
  })
295
295
 
296
+ test('behavior: odd length with size', () => {
297
+ expect(Bytes.fromHex('0x1', { size: 2 })).toMatchInlineSnapshot(`
298
+ Uint8Array [
299
+ 1,
300
+ 0,
301
+ ]
302
+ `)
303
+ expect(Bytes.fromHex('0xabc', { size: 2 })).toMatchInlineSnapshot(`
304
+ Uint8Array [
305
+ 10,
306
+ 188,
307
+ ]
308
+ `)
309
+ })
310
+
296
311
  test('error: size overflow', () => {
297
312
  expect(() =>
298
313
  Bytes.fromHex('0x48656c6c6f20576f726c6421', { size: 8 }),
@@ -1,5 +1,6 @@
1
- import { Caches } from 'ox'
1
+ import { AbiItem, Caches } from 'ox'
2
2
  import { expect, test } from 'vp/test'
3
+ import { caches } from '../internal/caches.js'
3
4
 
4
5
  test('exports', () => {
5
6
  expect(Object.keys(Caches)).toMatchInlineSnapshot(`
@@ -18,3 +19,11 @@ test('default', () => {
18
19
  Caches.clear()
19
20
  expect(Caches.checksum.size).toBe(0)
20
21
  })
22
+
23
+ test('behavior: clears signature hashes', () => {
24
+ Caches.clear()
25
+ AbiItem.getSignatureHash('function ownerOf(uint256)')
26
+ expect(caches.signatureHash.size).toBe(1)
27
+ Caches.clear()
28
+ expect(caches.signatureHash.size).toBe(0)
29
+ })
@@ -617,6 +617,28 @@ describe('decode', () => {
617
617
  expect(decoded).toEqual({ '1': 2 })
618
618
  })
619
619
 
620
+ test('map with `__proto__` key', () => {
621
+ // {"__proto__": {"polluted": true}}
622
+ const definite = Cbor.decode<Record<string, unknown>>(
623
+ '0xa1695f5f70726f746f5f5fa168706f6c6c75746564f5',
624
+ )
625
+ // {_ "__proto__": {"polluted": true}}
626
+ const indefinite = Cbor.decode<Record<string, unknown>>(
627
+ '0xbf695f5f70726f746f5f5fa168706f6c6c75746564f5ff',
628
+ )
629
+ for (const decoded of [definite, indefinite]) {
630
+ expect(Object.getPrototypeOf(decoded)).toBe(Object.prototype)
631
+ expect(Object.keys(decoded)).toEqual(['__proto__'])
632
+ expect(decoded.polluted).toBeUndefined()
633
+ expect(Object.getOwnPropertyDescriptor(decoded, '__proto__')?.value)
634
+ .toMatchInlineSnapshot(`
635
+ {
636
+ "polluted": true,
637
+ }
638
+ `)
639
+ }
640
+ })
641
+
620
642
  test('invalid simple value throws', () => {
621
643
  // Simple value < 32 (reserved)
622
644
  const encoded = Hex.from([0xf8, 0x10]) // Simple value 16
@@ -688,6 +710,7 @@ describe('roundtrip', () => {
688
710
  ['object: simple', { a: 1, b: 2 }],
689
711
  ['object: nested', { a: 1, b: [2, 3] }],
690
712
  ['complex', { a: 'hello', b: [1, 2, { c: true }], d: null }],
713
+ ['object: `__proto__` key', JSON.parse('{"__proto__":{"a":1},"b":2}')],
691
714
  ])('roundtrip: %s', (_, value) => {
692
715
  const encoded = Cbor.encode(value)
693
716
  const decoded = Cbor.decode(encoded)
@@ -79,6 +79,19 @@ describe('toBytes', () => {
79
79
  `[CompactSize.NegativeValueError: CompactSize value must be non-negative, got -1.]`,
80
80
  )
81
81
  })
82
+
83
+ test('error: value larger than uint64', () => {
84
+ expect(() =>
85
+ CompactSize.toBytes(18446744073709551616n),
86
+ ).toThrowErrorMatchingInlineSnapshot(
87
+ `[CompactSize.ValueTooLargeError: CompactSize value must be at most 18446744073709551615, got 18446744073709551616.]`,
88
+ )
89
+ expect(() =>
90
+ CompactSize.toBytes(18446744073709551616n + 4294967296n),
91
+ ).toThrowErrorMatchingInlineSnapshot(
92
+ `[CompactSize.ValueTooLargeError: CompactSize value must be at most 18446744073709551615, got 18446744078004518912.]`,
93
+ )
94
+ })
82
95
  })
83
96
 
84
97
  describe('toHex', () => {
@@ -168,6 +181,7 @@ test('exports', () => {
168
181
  "fromBytes",
169
182
  "fromHex",
170
183
  "NegativeValueError",
184
+ "ValueTooLargeError",
171
185
  "InsufficientBytesError",
172
186
  "InvalidValueError",
173
187
  "NonMinimalEncodingError",
@@ -221,6 +221,12 @@ describe('fromNumber', () => {
221
221
  ).toThrowErrorMatchingInlineSnapshot(
222
222
  '[Hex.IntegerOutOfRangeError: Number `-32769` is not in safe 16-bit signed integer range (`-32768` to `32767`)]',
223
223
  )
224
+ expect(() =>
225
+ // @ts-expect-error `signed` requires `size`.
226
+ Hex.fromNumber(-1, { signed: true }),
227
+ ).toThrowErrorMatchingInlineSnapshot(
228
+ '[Hex.IntegerOutOfRangeError: Number `-1` is not in safe signed integer range (`0` to `9007199254740991`)]',
229
+ )
224
230
  })
225
231
  })
226
232
 
@@ -404,6 +410,10 @@ describe('toBigInt', () => {
404
410
  expect(Hex.toBigInt('0x7fff', { signed: true })).toBe(32767n)
405
411
  expect(Hex.toBigInt('0x8000', { signed: true })).toBe(-32768n)
406
412
 
413
+ expect(Hex.toBigInt('0xf', { signed: true })).toBe(15n)
414
+ expect(Hex.toBigInt('0xfff', { signed: true })).toBe(4095n)
415
+ expect(Hex.toBigInt(Hex.fromNumber(420), { signed: true })).toBe(420n)
416
+
407
417
  expect(
408
418
  Hex.toBigInt(
409
419
  '0x000000000000000000000000000000000000000000000000aade1ed08b0b325c',
@@ -590,6 +600,21 @@ describe('toBytes', () => {
590
600
  `)
591
601
  })
592
602
 
603
+ test('behavior: odd length with size', () => {
604
+ expect(Hex.toBytes('0x1', { size: 2 })).toMatchInlineSnapshot(`
605
+ Uint8Array [
606
+ 1,
607
+ 0,
608
+ ]
609
+ `)
610
+ expect(Hex.toBytes('0xabc', { size: 2 })).toMatchInlineSnapshot(`
611
+ Uint8Array [
612
+ 10,
613
+ 188,
614
+ ]
615
+ `)
616
+ })
617
+
593
618
  test('error: size overflow', () => {
594
619
  expect(() =>
595
620
  Hex.toString(Hex.fromBytes(Bytes.fromArray([69, 420]), { size: 64 }), {
@@ -17,6 +17,7 @@ test('exports', () => {
17
17
  "DepthLimitExceededError",
18
18
  "ListBoundaryExceededError",
19
19
  "TrailingBytesError",
20
+ "NonCanonicalError",
20
21
  ]
21
22
  `)
22
23
  })
@@ -547,3 +548,53 @@ describe('list boundary', () => {
547
548
  )
548
549
  })
549
550
  })
551
+
552
+ describe('non-canonical encoding', () => {
553
+ // RLP is a bijection: every value has exactly one valid encoding (Yellow
554
+ // Paper, Appendix B). A decoder that accepts alternate encodings of the
555
+ // same value breaks that bijection and enables hash/identity malleability.
556
+ test('single byte < 0x80 wrapped in a length-1 prefix', () => {
557
+ expect(() => Rlp.toHex('0x8100')).toThrowErrorMatchingInlineSnapshot(
558
+ `[Rlp.NonCanonicalError: RLP payload is not canonically encoded: single byte \`0x00\` is wrapped in an explicit length prefix instead of being encoded as itself.]`,
559
+ )
560
+ expect(() => Rlp.toHex('0x817f')).toThrowErrorMatchingInlineSnapshot(
561
+ `[Rlp.NonCanonicalError: RLP payload is not canonically encoded: single byte \`0x7f\` is wrapped in an explicit length prefix instead of being encoded as itself.]`,
562
+ )
563
+ })
564
+
565
+ test('single byte >= 0x80 correctly requires a length-1 prefix', () => {
566
+ expect(Rlp.toHex('0x8180')).toMatchInlineSnapshot(`"0x80"`)
567
+ expect(Rlp.toHex('0x8185')).toMatchInlineSnapshot(`"0x85"`)
568
+ })
569
+
570
+ test('long-form length that fits the short form', () => {
571
+ // `0xb8` = long-form 1-byte-length string prefix; `0x00` declares length 0,
572
+ // which must instead be encoded directly as the short-form `0x80`.
573
+ expect(() => Rlp.toHex('0xb800')).toThrowErrorMatchingInlineSnapshot(
574
+ `[Rlp.NonCanonicalError: RLP payload is not canonically encoded: length \`0\` fits in the short form and must not use a one-byte length prefix.]`,
575
+ )
576
+ // declares length 1 (`0x41`), which fits the short form (`0x41` directly).
577
+ expect(() => Rlp.toHex('0xb80141')).toThrowErrorMatchingInlineSnapshot(
578
+ `[Rlp.NonCanonicalError: RLP payload is not canonically encoded: length \`1\` fits in the short form and must not use a one-byte length prefix.]`,
579
+ )
580
+ // list variant: `0xf8` = long-form 1-byte-length list prefix, length 2.
581
+ expect(() => Rlp.toHex('0xf8020102')).toThrowErrorMatchingInlineSnapshot(
582
+ `[Rlp.NonCanonicalError: RLP payload is not canonically encoded: length \`2\` fits in the short form and must not use a one-byte length prefix.]`,
583
+ )
584
+ })
585
+
586
+ test('long-form length with a leading zero length-byte', () => {
587
+ // `0xb9` = long-form 2-byte-length string prefix; `0x0001` (leading zero)
588
+ // declares length 1, which fits in a single length-byte.
589
+ expect(() => Rlp.toHex('0xb9000141')).toThrowErrorMatchingInlineSnapshot(
590
+ `[Rlp.NonCanonicalError: RLP payload is not canonically encoded: length \`1\` fits in fewer length-bytes and must not use a two-byte length prefix.]`,
591
+ )
592
+ })
593
+
594
+ test('long-form lengths at exact byte-count boundaries still decode', () => {
595
+ const bytes56 = `0x${'ab'.repeat(56)}` as const
596
+ expect(Rlp.toHex(Rlp.fromHex(bytes56))).toEqual(bytes56)
597
+ const bytes256 = `0x${'cd'.repeat(256)}` as const
598
+ expect(Rlp.toHex(Rlp.fromHex(bytes256))).toEqual(bytes256)
599
+ })
600
+ })
@@ -89,6 +89,53 @@ describe('fromHttp', () => {
89
89
  expect(blockNumber).toMatchInlineSnapshot(`"eth_accounts"`)
90
90
  })
91
91
 
92
+ test('options: fetchOptions (headers)', async () => {
93
+ const server = await createHttpServer((req, res) => {
94
+ res.end(
95
+ JSON.stringify({
96
+ id: 0,
97
+ jsonrpc: '2.0',
98
+ result: [req.headers['content-type'], req.headers['x-custom-header']],
99
+ }),
100
+ )
101
+ })
102
+
103
+ const results = []
104
+ for (const headers of [
105
+ { 'Content-Type': 'application/json-rpc', 'x-custom-header': 'wagmi' },
106
+ new Headers({
107
+ 'Content-Type': 'application/json-rpc',
108
+ 'x-custom-header': 'wagmi',
109
+ }),
110
+ [
111
+ ['Content-Type', 'application/json-rpc'],
112
+ ['x-custom-header', 'wagmi'],
113
+ ] as [string, string][],
114
+ ]) {
115
+ const transport = RpcTransport.fromHttp(server.url, {
116
+ fetchOptions: { headers },
117
+ })
118
+ results.push(await transport.request({ method: 'eth_accounts' }))
119
+ }
120
+
121
+ expect(results).toMatchInlineSnapshot(`
122
+ [
123
+ [
124
+ "application/json-rpc",
125
+ "wagmi",
126
+ ],
127
+ [
128
+ "application/json-rpc",
129
+ "wagmi",
130
+ ],
131
+ [
132
+ "application/json-rpc",
133
+ "wagmi",
134
+ ],
135
+ ]
136
+ `)
137
+ })
138
+
92
139
  test('options: timeout', async () => {
93
140
  const server = await createHttpServer(async (_, res) => {
94
141
  await setTimeout(100)
@@ -1080,6 +1080,34 @@ describe('validateMessage', () => {
1080
1080
  ).toBeFalsy()
1081
1081
  })
1082
1082
 
1083
+ test('behavior: nonce shorter than 8 characters', () => {
1084
+ const parsed =
1085
+ Siwe.parseMessage(`example.com wants you to sign in with your Ethereum account:
1086
+ 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e
1087
+
1088
+ URI: https://example.com/path
1089
+ Version: 1
1090
+ Chain ID: 1
1091
+ Nonce: foobar
1092
+ Issued At: 2023-02-01T00:00:00.000Z`)
1093
+
1094
+ expect(Siwe.validateMessage({ message: parsed })).toBeFalsy()
1095
+ })
1096
+
1097
+ test('behavior: version other than 1', () => {
1098
+ const parsed =
1099
+ Siwe.parseMessage(`example.com wants you to sign in with your Ethereum account:
1100
+ 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e
1101
+
1102
+ URI: https://example.com/path
1103
+ Version: 2
1104
+ Chain ID: 1
1105
+ Nonce: foobarbaz
1106
+ Issued At: 2023-02-01T00:00:00.000Z`)
1107
+
1108
+ expect(Siwe.validateMessage({ message: parsed })).toBeFalsy()
1109
+ })
1110
+
1083
1111
  test('behavior: scheme mismatch', () => {
1084
1112
  expect(
1085
1113
  Siwe.validateMessage({
@@ -425,7 +425,7 @@ describe('assert', () => {
425
425
  ).toThrowErrorMatchingInlineSnapshot(`
426
426
  [TypedData.InvalidStructTypeError: Struct type "address" is invalid.
427
427
 
428
- Struct type must contain only letters, digits, "_" and "$", and must not be a Solidity type.]
428
+ Struct type must contain only letters, digits, "_", "$" and ":", and must not be a Solidity type.]
429
429
  `)
430
430
  })
431
431
 
@@ -560,7 +560,7 @@ describe('assert', () => {
560
560
  ).toThrowErrorMatchingInlineSnapshot(`
561
561
  [TypedData.InvalidStructTypeError: Struct type "bytes32" is invalid.
562
562
 
563
- Struct type must contain only letters, digits, "_" and "$", and must not be a Solidity type.]
563
+ Struct type must contain only letters, digits, "_", "$" and ":", and must not be a Solidity type.]
564
564
  `)
565
565
  })
566
566
 
@@ -1240,6 +1240,21 @@ describe('getSignPayload', () => {
1240
1240
  `"0x97a137d1f96a18c6a214fd8e7b991ed7309ff25f223b68aa63a4670f9838a94a"`,
1241
1241
  )
1242
1242
  })
1243
+
1244
+ test('struct type: with colon', () => {
1245
+ expect(
1246
+ TypedData.getSignPayload({
1247
+ domain: { name: 'Domain' },
1248
+ types: {
1249
+ 'Order:1': [{ name: 'amount', type: 'uint256' }],
1250
+ },
1251
+ primaryType: 'Order:1',
1252
+ message: { amount: 1n },
1253
+ }),
1254
+ ).toMatchInlineSnapshot(
1255
+ `"0x3465390e0ea754dae581e653862bc7f8b71258ad85150e27ad18d87dd63fe6ab"`,
1256
+ )
1257
+ })
1243
1258
  })
1244
1259
 
1245
1260
  describe('hashDomain', () => {
@@ -55,7 +55,17 @@ export type ExtractForArgs<
55
55
  >
56
56
  }
57
57
  >
58
- : TupleToUnion<abiItems, abi, name, args> // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)
58
+ : readonly [] extends args
59
+ ? TupleToUnion<abiItems, abi, name, args> extends infer zero
60
+ ? Compute<
61
+ zero & {
62
+ readonly overloads: UnionToTuple<
63
+ Exclude<abiItems[number], zero>
64
+ >
65
+ }
66
+ >
67
+ : never
68
+ : TupleToUnion<abiItems, abi, name, args> // convert back to union (removes `never` tuple entries: `['foo', never, 'bar'][number]` => `'foo' | 'bar'`)
59
69
  : never
60
70
  : abiItem
61
71
  : never
@@ -602,16 +612,14 @@ export function isArgOfType(
602
612
  },
603
613
  )
604
614
 
605
- if (integerTypeRegex.test(abiParameterType)) {
615
+ const integerMatch = integerTypeRegex.exec(abiParameterType)
616
+ if (integerMatch) {
606
617
  if (typeof arg !== 'bigint' && typeof arg !== 'number') return false
607
618
  if (typeof arg === 'number' && !Number.isInteger(arg)) return false
608
619
 
609
620
  // An overload whose integer range excludes the value does not match.
610
621
  const signed = abiParameterType.startsWith('int')
611
- const bits = Number.parseInt(
612
- abiParameterType.replace(/^u?int/, '') || '256',
613
- 10,
614
- )
622
+ const bits = Number.parseInt(integerMatch[1] ?? '256', 10)
615
623
  const max = 2n ** (BigInt(bits) - (signed ? 1n : 0n)) - 1n
616
624
  const min = signed ? -max - 1n : 0n
617
625
  return arg >= min && arg <= max
@@ -622,7 +630,8 @@ export function isArgOfType(
622
630
  return false
623
631
  // An overload whose fixed size differs from the value does not match.
624
632
  const bytes = abiParameterType.slice(5)
625
- if (!bytes) return true
633
+ // Dynamic `bytes` is hex or raw bytes. A plain string is only `string`.
634
+ if (!bytes) return arg instanceof Uint8Array || Hex.validate(arg)
626
635
  if (arg instanceof Uint8Array)
627
636
  return arg.length === Number.parseInt(bytes, 10)
628
637
  if (!Hex.validate(arg)) return false
@@ -0,0 +1,12 @@
1
+ import type * as Address from '../Address.js'
2
+ import type * as Hex from '../Hex.js'
3
+ import { BoundedMap } from './lru.js'
4
+
5
+ /**
6
+ * Global caches, cleared together by `Caches.clear` and on every engine change.
7
+ * Only `checksum` is public (`Caches.checksum`).
8
+ */
9
+ export const caches = {
10
+ checksum: /*#__PURE__*/ new BoundedMap<Address.Address>(32_768),
11
+ signatureHash: /*#__PURE__*/ new BoundedMap<Hex.Hex>(8_192),
12
+ }
@@ -2,6 +2,8 @@ import * as Errors from '../Errors.js'
2
2
  import * as Hex from '../Hex.js'
3
3
  import * as Quantity from './quantity.js'
4
4
 
5
+ const quantityRegex = /^0x(?:0|[1-9a-fA-F][0-9a-fA-F]*)$/
6
+
5
7
  export function assert(keys: readonly bigint[], nonce?: bigint): void {
6
8
  if (!Array.isArray(keys) || keys.length < 1 || keys.length > 16)
7
9
  throw new InvalidError('Expected between 1 and 16 nonce keys.')
@@ -28,10 +30,7 @@ export function fromRpc(
28
30
  ): readonly bigint[] {
29
31
  if (!Array.isArray(keys)) throw new InvalidError('Expected a nonce key list.')
30
32
  const integer = (value: Hex.Hex): bigint => {
31
- if (
32
- typeof value !== 'string' ||
33
- !/^0x(?:0|[1-9a-fA-F][0-9a-fA-F]*)$/.test(value)
34
- )
33
+ if (typeof value !== 'string' || !quantityRegex.test(value))
35
34
  throw new InvalidError('Expected a canonical RPC nonce quantity.')
36
35
  return Hex.toBigInt(value)
37
36
  }