ox 0.9.5 → 0.9.7

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 (71) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/_cjs/core/AbiConstructor.js +16 -2
  3. package/_cjs/core/AbiConstructor.js.map +1 -1
  4. package/_cjs/core/AbiError.js.map +1 -1
  5. package/_cjs/core/AbiEvent.js.map +1 -1
  6. package/_cjs/core/AbiFunction.js.map +1 -1
  7. package/_cjs/core/AbiItem.js +24 -3
  8. package/_cjs/core/AbiItem.js.map +1 -1
  9. package/_cjs/core/Bytes.js +1 -1
  10. package/_cjs/core/Bytes.js.map +1 -1
  11. package/_cjs/core/Hex.js +1 -1
  12. package/_cjs/core/Hex.js.map +1 -1
  13. package/_cjs/core/Rlp.js +4 -4
  14. package/_cjs/core/Rlp.js.map +1 -1
  15. package/_cjs/core/TransactionRequest.js +3 -1
  16. package/_cjs/core/TransactionRequest.js.map +1 -1
  17. package/_cjs/version.js +1 -1
  18. package/_esm/core/Abi.js +2 -2
  19. package/_esm/core/Abi.js.map +1 -1
  20. package/_esm/core/AbiConstructor.js +18 -56
  21. package/_esm/core/AbiConstructor.js.map +1 -1
  22. package/_esm/core/AbiError.js.map +1 -1
  23. package/_esm/core/AbiEvent.js.map +1 -1
  24. package/_esm/core/AbiFunction.js.map +1 -1
  25. package/_esm/core/AbiItem.js +27 -93
  26. package/_esm/core/AbiItem.js.map +1 -1
  27. package/_esm/core/Bytes.js +1 -1
  28. package/_esm/core/Bytes.js.map +1 -1
  29. package/_esm/core/Hex.js +1 -1
  30. package/_esm/core/Hex.js.map +1 -1
  31. package/_esm/core/Rlp.js +4 -4
  32. package/_esm/core/Rlp.js.map +1 -1
  33. package/_esm/core/TransactionRequest.js +3 -1
  34. package/_esm/core/TransactionRequest.js.map +1 -1
  35. package/_esm/erc6492/SignatureErc6492.js +2 -2
  36. package/_esm/erc6492/index.js +2 -2
  37. package/_esm/erc8010/SignatureErc8010.js +2 -2
  38. package/_esm/erc8010/index.js +2 -2
  39. package/_esm/version.js +1 -1
  40. package/_types/core/AbiConstructor.d.ts +43 -4
  41. package/_types/core/AbiConstructor.d.ts.map +1 -1
  42. package/_types/core/AbiError.d.ts +20 -29
  43. package/_types/core/AbiError.d.ts.map +1 -1
  44. package/_types/core/AbiEvent.d.ts +58 -0
  45. package/_types/core/AbiEvent.d.ts.map +1 -1
  46. package/_types/core/AbiFunction.d.ts +49 -6
  47. package/_types/core/AbiFunction.d.ts.map +1 -1
  48. package/_types/core/AbiItem.d.ts +36 -0
  49. package/_types/core/AbiItem.d.ts.map +1 -1
  50. package/_types/core/TransactionRequest.d.ts.map +1 -1
  51. package/_types/erc6492/SignatureErc6492.d.ts +2 -2
  52. package/_types/erc6492/index.d.ts +2 -2
  53. package/_types/erc8010/SignatureErc8010.d.ts +2 -2
  54. package/_types/erc8010/index.d.ts +2 -2
  55. package/_types/version.d.ts +1 -1
  56. package/core/Abi.ts +2 -2
  57. package/core/AbiConstructor.ts +96 -12
  58. package/core/AbiError.ts +20 -29
  59. package/core/AbiEvent.ts +58 -0
  60. package/core/AbiFunction.ts +49 -6
  61. package/core/AbiItem.ts +87 -3
  62. package/core/Bytes.ts +1 -1
  63. package/core/Hex.ts +1 -1
  64. package/core/Rlp.ts +4 -4
  65. package/core/TransactionRequest.ts +6 -1
  66. package/erc6492/SignatureErc6492.ts +2 -2
  67. package/erc6492/index.ts +2 -2
  68. package/erc8010/SignatureErc8010.ts +2 -2
  69. package/erc8010/index.ts +2 -2
  70. package/package.json +1 -1
  71. package/version.ts +1 -1
@@ -10,11 +10,6 @@ import type { IsNarrowable } from './internal/types.js'
10
10
  /** Root type for an {@link ox#AbiItem.AbiItem} with a `constructor` type. */
11
11
  export type AbiConstructor = abitype.AbiConstructor
12
12
 
13
- /** @internal */
14
- export function decode<const abiConstructor extends AbiConstructor>(
15
- abiConstructor: abiConstructor,
16
- options: decode.Options,
17
- ): decode.ReturnType<abiConstructor>
18
13
  /**
19
14
  * ABI-decodes the provided constructor input (`inputs`).
20
15
  *
@@ -37,19 +32,63 @@ export function decode<const abiConstructor extends AbiConstructor>(
37
32
  * }) // [!code focus]
38
33
  * ```
39
34
  *
35
+ * @example
36
+ * ### ABI-shorthand
37
+ *
38
+ * You can also specify an entire ABI object as a parameter to `AbiConstructor.decode`.
39
+ *
40
+ * ```ts twoslash
41
+ * // @noErrors
42
+ * import { Abi, AbiConstructor } from 'ox'
43
+ *
44
+ * const abi = Abi.from([...])
45
+ *
46
+ * const data = AbiConstructor.encode(abi, {
47
+ * bytecode: '0x...',
48
+ * args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n],
49
+ * })
50
+ *
51
+ * const decoded = AbiConstructor.decode(abi, { // [!code focus]
52
+ * bytecode: '0x...', // [!code focus]
53
+ * data, // [!code focus]
54
+ * }) // [!code focus]
55
+ * ```
56
+ *
40
57
  * @param abiConstructor - The ABI Constructor to decode.
41
58
  * @param options - Decoding options.
42
59
  * @returns The decoded constructor inputs.
43
60
  */
44
- export function decode(
45
- abiConstructor: AbiConstructor,
61
+ export function decode<
62
+ const abi extends Abi.Abi | readonly unknown[],
63
+ abiConstructor extends
64
+ AbiConstructor = fromAbi.ReturnType<abi> extends AbiConstructor
65
+ ? fromAbi.ReturnType<abi>
66
+ : never,
67
+ >(
68
+ abi: abi | Abi.Abi | readonly unknown[],
46
69
  options: decode.Options,
47
- ): readonly unknown[] | undefined
48
- /** @internal */
49
- export function decode(
50
- abiConstructor: AbiConstructor,
70
+ ): decode.ReturnType<abiConstructor>
71
+ export function decode<const abiConstructor extends AbiConstructor>(
72
+ abiConstructor: abiConstructor | AbiConstructor,
51
73
  options: decode.Options,
74
+ ): decode.ReturnType<abiConstructor>
75
+ // eslint-disable-next-line jsdoc/require-jsdoc
76
+ export function decode(
77
+ ...parameters:
78
+ | [abi: Abi.Abi | readonly unknown[], options: decode.Options]
79
+ | [abiConstructor: AbiConstructor, options: decode.Options]
52
80
  ): decode.ReturnType {
81
+ const [abiConstructor, options] = (() => {
82
+ if (Array.isArray(parameters[0])) {
83
+ const [abi, options] = parameters as [
84
+ Abi.Abi | readonly unknown[],
85
+ decode.Options,
86
+ ]
87
+ return [fromAbi(abi), options] as [AbiConstructor, decode.Options]
88
+ }
89
+ return parameters as [AbiConstructor, decode.Options]
90
+ })()
91
+
53
92
  const { bytecode } = options
54
93
  if (abiConstructor.inputs.length === 0) return undefined
55
94
  const data = options.data.replace(bytecode, '0x') as Hex.Hex
@@ -91,6 +130,23 @@ export declare namespace decode {
91
130
  * ```
92
131
  *
93
132
  * @example
133
+ * ### ABI-shorthand
134
+ *
135
+ * You can also specify an entire ABI object as a parameter to `AbiConstructor.encode`.
136
+ *
137
+ * ```ts twoslash
138
+ * // @noErrors
139
+ * import { Abi, AbiConstructor } from 'ox'
140
+ *
141
+ * const abi = Abi.from([...])
142
+ *
143
+ * const data = AbiConstructor.encode(abi, { // [!code focus]
144
+ * bytecode: '0x...', // [!code focus]
145
+ * args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n], // [!code focus]
146
+ * }) // [!code focus]
147
+ * ```
148
+ *
149
+ * @example
94
150
  * ### End-to-end
95
151
  *
96
152
  * Below is an end-to-end example of using `AbiConstructor.encode` to encode the constructor of a contract and deploy it.
@@ -128,10 +184,38 @@ export declare namespace decode {
128
184
  * @param options - Encoding options.
129
185
  * @returns The encoded constructor.
130
186
  */
187
+ export function encode<
188
+ const abi extends Abi.Abi | readonly unknown[],
189
+ abiConstructor extends
190
+ AbiConstructor = fromAbi.ReturnType<abi> extends AbiConstructor
191
+ ? fromAbi.ReturnType<abi>
192
+ : never,
193
+ >(
194
+ abi: abi | Abi.Abi | readonly unknown[],
195
+ options: encode.Options<abiConstructor>,
196
+ ): encode.ReturnType
131
197
  export function encode<const abiConstructor extends AbiConstructor>(
132
- abiConstructor: abiConstructor,
198
+ abiConstructor: abiConstructor | AbiConstructor,
133
199
  options: encode.Options<abiConstructor>,
200
+ ): encode.ReturnType
201
+ // eslint-disable-next-line jsdoc/require-jsdoc
202
+ export function encode(
203
+ ...parameters:
204
+ | [abi: Abi.Abi | readonly unknown[], options: encode.Options]
205
+ | [abiConstructor: AbiConstructor, options: encode.Options]
134
206
  ): encode.ReturnType {
207
+ const [abiConstructor, options] = (() => {
208
+ if (Array.isArray(parameters[0])) {
209
+ const [abi, options] = parameters as [
210
+ Abi.Abi | readonly unknown[],
211
+ encode.Options,
212
+ ]
213
+ return [fromAbi(abi), options] as [AbiConstructor, encode.Options]
214
+ }
215
+
216
+ return parameters as [AbiConstructor, encode.Options]
217
+ })()
218
+
135
219
  const { bytecode, args } = options
136
220
  return Hex.concat(
137
221
  bytecode,
package/core/AbiError.ts CHANGED
@@ -59,35 +59,37 @@ export function decode<
59
59
  * ```
60
60
  *
61
61
  * @example
62
- * Or define the ABI and error name as parameters to {@link ox#AbiError.(decode:function)}:
62
+ * You can pass the error `data` to the `name` property of {@link ox#AbiError.(fromAbi:function)} to extract and infer the error by its 4-byte selector:
63
63
  *
64
64
  * ```ts twoslash
65
65
  * // @noErrors
66
66
  * import { Abi, AbiError } from 'ox'
67
67
  *
68
+ * const data = '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001'
69
+ *
68
70
  * const abi = Abi.from([...])
71
+ * const error = AbiError.fromAbi(abi, data) // [!code hl]
69
72
  *
70
- * const value = AbiError.decode(
71
- * abi, // [!code hl]
72
- * 'InvalidSignature', // [!code hl]
73
- * '0x...'
74
- * )
73
+ * const value = AbiError.decode(error, data)
75
74
  * // @log: [420n, 69n, 1]
76
75
  * ```
77
76
  *
78
77
  * @example
79
- * You can pass the error `data` to the `name` property of {@link ox#AbiError.(fromAbi:function)} to extract and infer the error by its 4-byte selector:
78
+ * ### ABI-shorthand
79
+ *
80
+ * You can also specify an entire ABI object as a parameter to {@link ox#AbiError.(decode:function)}:
80
81
  *
81
82
  * ```ts twoslash
82
83
  * // @noErrors
83
84
  * import { Abi, AbiError } from 'ox'
84
85
  *
85
- * const data = '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001'
86
- *
87
86
  * const abi = Abi.from([...])
88
- * const error = AbiError.fromAbi(abi, data) // [!code hl]
89
87
  *
90
- * const value = AbiError.decode(error, data)
88
+ * const value = AbiError.decode(
89
+ * abi, // [!code hl]
90
+ * 'InvalidSignature', // [!code hl]
91
+ * '0x...'
92
+ * )
91
93
  * // @log: [420n, 69n, 1]
92
94
  * ```
93
95
  *
@@ -281,32 +283,21 @@ export declare namespace decode {
281
283
  * ```
282
284
  *
283
285
  * @example
284
- * You can extract an ABI Error from a JSON ABI with {@link ox#AbiError.(fromAbi:function)}:
286
+ * ### ABI-shorthand
287
+ *
288
+ * You can also specify an entire ABI object and an error name as parameters to `AbiError.encode`.
285
289
  *
286
290
  * ```ts twoslash
287
291
  * // @noErrors
288
292
  * import { Abi, AbiError } from 'ox'
289
293
  *
290
- * const abi = Abi.from([ // [!code hl]
291
- * // ... // [!code hl]
292
- * { // [!code hl]
293
- * name: 'InvalidSignature', // [!code hl]
294
- * type: 'error', // [!code hl]
295
- * inputs: [ // [!code hl]
296
- * { name: 'r', type: 'uint256' }, // [!code hl]
297
- * { name: 's', type: 'uint256' }, // [!code hl]
298
- * { name: 'yParity', type: 'uint8' }, // [!code hl]
299
- * ], // [!code hl]
300
- * }, // [!code hl]
301
- * // ... // [!code hl]
302
- * ]) // [!code hl]
303
- * const error = AbiError.fromAbi(abi, 'InvalidSignature') // [!code hl]
294
+ * const abi = Abi.from([...])
304
295
  *
305
296
  * const data = AbiError.encode(
306
- * error,
307
- * ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
297
+ * abi, // [!code hl]
298
+ * 'InvalidSignature', // [!code hl]
299
+ * [1n, 2n, 0]
308
300
  * )
309
- * // @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c'
310
301
  * ```
311
302
  *
312
303
  * @param abiError - ABI Error to encode
package/core/AbiEvent.ts CHANGED
@@ -237,6 +237,38 @@ export declare namespace assertArgs {
237
237
  * ```
238
238
  *
239
239
  * @example
240
+ * ### ABI-shorthand
241
+ *
242
+ * You can also specify an entire ABI object and an event name as parameters to {@link ox#AbiEvent.(decode:function)}:
243
+ *
244
+ * ```ts twoslash
245
+ * // @noErrors
246
+ * import { Abi, AbiEvent } from 'ox'
247
+ *
248
+ * const abi = Abi.from([...])
249
+ * const log = {
250
+ * // ...
251
+ * data: '0x0000000000000000000000000000000000000000000000000000000000000001',
252
+ * topics: [
253
+ * '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
254
+ * '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
255
+ * '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac',
256
+ * ],
257
+ * } as const
258
+ *
259
+ * const decoded = AbiEvent.decode(
260
+ * abi, // [!code focus]
261
+ * 'Transfer', // [!code focus]
262
+ * log
263
+ * )
264
+ * // @log: {
265
+ * // @log: from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
266
+ * // @log: to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
267
+ * // @log: value: 1n
268
+ * // @log: }
269
+ * ```
270
+ *
271
+ * @example
240
272
  * ### End-to-end
241
273
  *
242
274
  * Below is an end-to-end example of using `AbiEvent.decode` to decode the topics of a `Transfer` event on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2).
@@ -490,6 +522,32 @@ export declare namespace decode {
490
522
  * ```
491
523
  *
492
524
  * @example
525
+ * ### ABI-shorthand
526
+ *
527
+ * You can also specify an entire ABI object and an event name as parameters to {@link ox#AbiEvent.(encode:function)}:
528
+ *
529
+ * ```ts twoslash
530
+ * // @noErrors
531
+ * import { Abi, AbiEvent } from 'ox'
532
+ *
533
+ * const abi = Abi.from([...])
534
+ *
535
+ * const { topics } = AbiEvent.encode(
536
+ * abi, // [!code focus]
537
+ * 'Transfer', // [!code focus]
538
+ * {
539
+ * from: '0xf39fd6e51aad88f6f4ce6ab882779cfffb92266', // [!code focus]
540
+ * to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
541
+ * }
542
+ * )
543
+ * // @log: [
544
+ * // @log: '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0',
545
+ * // @log: '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
546
+ * // @log: '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'
547
+ * // @log: ]
548
+ * ```
549
+ *
550
+ * @example
493
551
  * ### End-to-end
494
552
  *
495
553
  * Below is an end-to-end example of using `AbiEvent.encode` to encode the topics of a `Transfer` event and query for events matching the encoded topics on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2).
@@ -90,6 +90,26 @@ export type ExtractNames<
90
90
  * // @log: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
91
91
  * ```
92
92
  *
93
+ * @example
94
+ * ### ABI-shorthand
95
+ *
96
+ * You can also specify an entire ABI object and a function name as parameters to {@link ox#AbiFunction.(decodeData:function)}:
97
+ *
98
+ * ```ts twoslash
99
+ * // @noErrors
100
+ * import { Abi, AbiFunction } from 'ox'
101
+ *
102
+ * const abi = Abi.from([...])
103
+ * const data = '0x...
104
+ *
105
+ * const input = AbiFunction.decodeData(
106
+ * abi, // [!code focus]
107
+ * 'approve', // [!code focus]
108
+ * data
109
+ * )
110
+ * // @log: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
111
+ * ```
112
+ *
93
113
  * @param abiFunction - The ABI Item to decode.
94
114
  * @param data - The data to decode.
95
115
  */
@@ -211,7 +231,9 @@ export declare namespace decodeData {
211
231
  * ```
212
232
  *
213
233
  * @example
214
- * Or define the ABI and function name as parameters to {@link ox#AbiFunction.(decodeResult:function)}:
234
+ * ### ABI-shorthand
235
+ *
236
+ * You can also specify an entire ABI object and a function name as parameters to {@link ox#AbiFunction.(decodeResult:function)}:
215
237
  *
216
238
  * ```ts twoslash
217
239
  * // @noErrors
@@ -222,8 +244,8 @@ export declare namespace decodeData {
222
244
  * const erc20Abi = Abi.from([...])
223
245
  *
224
246
  * const output = AbiFunction.decodeResult(
225
- * erc20Abi, // [!code hl]
226
- * 'totalSupply', // [!code hl]
247
+ * erc20Abi, // [!code focus]
248
+ * 'totalSupply', // [!code focus]
227
249
  * data
228
250
  * )
229
251
  * // @log: 42n
@@ -433,7 +455,9 @@ export declare namespace decodeResult {
433
455
  * ```
434
456
  *
435
457
  * @example
436
- * Or define the ABI and function name as parameters to {@link ox#AbiFunction.(encodeData:function)}:
458
+ * ### ABI-shorthand
459
+ *
460
+ * You can specify an entire ABI object and a function name as parameters to {@link ox#AbiFunction.(encodeData:function)}:
437
461
  *
438
462
  * ```ts twoslash
439
463
  * // @noErrors
@@ -442,8 +466,8 @@ export declare namespace decodeResult {
442
466
  * const erc20Abi = Abi.from([...])
443
467
  *
444
468
  * const data = AbiFunction.encodeData(
445
- * erc20Abi, // [!code hl]
446
- * 'approve', // [!code hl]
469
+ * erc20Abi, // [!code focus]
470
+ * 'approve', // [!code focus]
447
471
  * ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n]
448
472
  * )
449
473
  * ```
@@ -603,6 +627,25 @@ export declare namespace encodeData {
603
627
  * // @log: '0x000000000000000000000000000000000000000000000000000000000000002a'
604
628
  * ```
605
629
  *
630
+ * @example
631
+ * ### ABI-shorthand
632
+ *
633
+ * You can also specify an entire ABI object and a function name as parameters to {@link ox#AbiFunction.(encodeResult:function)}:
634
+ *
635
+ * ```ts twoslash
636
+ * // @noErrors
637
+ * import { Abi, AbiFunction } from 'ox'
638
+ *
639
+ * const abi = Abi.from([...])
640
+ *
641
+ * const data = AbiFunction.encodeResult(
642
+ * abi, // [!code focus]
643
+ * 'totalSupply', // [!code focus]
644
+ * 42n
645
+ * )
646
+ * // @log: '0x000000000000000000000000000000000000000000000000000000000000002a'
647
+ * ```
648
+ *
606
649
  * @param abiFunction - The ABI item to encode the function output for.
607
650
  * @param output - The function output to encode.
608
651
  * @param options - Encoding options.
package/core/AbiItem.ts CHANGED
@@ -493,6 +493,17 @@ export declare namespace fromAbi {
493
493
  *
494
494
  * @example
495
495
  * ```ts twoslash
496
+ * // @noErrors
497
+ * import { Abi, AbiItem } from 'ox'
498
+ *
499
+ * const erc20Abi = Abi.from([...])
500
+ *
501
+ * const selector = AbiItem.getSelector(erc20Abi, 'ownerOf')
502
+ * // @log: '0x6352211e'
503
+ * ```
504
+ *
505
+ * @example
506
+ * ```ts twoslash
496
507
  * import { AbiItem } from 'ox'
497
508
  *
498
509
  * const selector = AbiItem.getSelector({
@@ -508,7 +519,24 @@ export declare namespace fromAbi {
508
519
  * @param abiItem - The ABI item to compute the selector for. Can be a signature or an ABI item for an error, event, function, etc.
509
520
  * @returns The first 4 bytes of the {@link ox#Hash.(keccak256:function)} hash of the function signature.
510
521
  */
511
- export function getSelector(abiItem: string | AbiItem): Hex.Hex {
522
+ export function getSelector<
523
+ abi extends Abi.Abi | readonly unknown[],
524
+ name extends Name<abi>,
525
+ >(abi: abi | Abi.Abi | readonly unknown[], name: name): Hex.Hex
526
+ export function getSelector(abiItem: string | AbiItem): Hex.Hex
527
+ // eslint-disable-next-line jsdoc/require-jsdoc
528
+ export function getSelector(
529
+ ...parameters:
530
+ | [abi: Abi.Abi | readonly unknown[], name: string]
531
+ | [string | AbiItem]
532
+ ): Hex.Hex {
533
+ const abiItem = (() => {
534
+ if (Array.isArray(parameters[0])) {
535
+ const [abi, name] = parameters as [Abi.Abi | readonly unknown[], string]
536
+ return fromAbi(abi, name)
537
+ }
538
+ return parameters[0] as string | AbiItem
539
+ })()
512
540
  return Hex.slice(getSignatureHash(abiItem), 0, 4)
513
541
  }
514
542
 
@@ -532,6 +560,17 @@ export declare namespace getSelector {
532
560
  *
533
561
  * @example
534
562
  * ```ts twoslash
563
+ * // @noErrors
564
+ * import { Abi, AbiItem } from 'ox'
565
+ *
566
+ * const erc20Abi = Abi.from([...])
567
+ *
568
+ * const signature = AbiItem.getSignature(erc20Abi, 'ownerOf')
569
+ * // @log: 'ownerOf(uint256)'
570
+ * ```
571
+ *
572
+ * @example
573
+ * ```ts twoslash
535
574
  * import { AbiItem } from 'ox'
536
575
  *
537
576
  * const signature = AbiItem.getSignature({
@@ -547,7 +586,24 @@ export declare namespace getSelector {
547
586
  * @param abiItem - The ABI Item to compute the signature for.
548
587
  * @returns The stringified signature of the ABI Item.
549
588
  */
550
- export function getSignature(abiItem: string | AbiItem): string {
589
+ export function getSignature<
590
+ abi extends Abi.Abi | readonly unknown[],
591
+ name extends Name<abi>,
592
+ >(abi: abi | Abi.Abi | readonly unknown[], name: name): string
593
+ export function getSignature(abiItem: string | AbiItem): string
594
+ // eslint-disable-next-line jsdoc/require-jsdoc
595
+ export function getSignature(
596
+ ...parameters:
597
+ | [abi: Abi.Abi | readonly unknown[], name: string]
598
+ | [string | AbiItem]
599
+ ): string {
600
+ const abiItem = (() => {
601
+ if (Array.isArray(parameters[0])) {
602
+ const [abi, name] = parameters as [Abi.Abi | readonly unknown[], string]
603
+ return fromAbi(abi, name)
604
+ }
605
+ return parameters[0] as string | AbiItem
606
+ })()
551
607
  const signature = (() => {
552
608
  if (typeof abiItem === 'string') return abiItem
553
609
  return abitype.formatAbiItem(abiItem)
@@ -576,6 +632,17 @@ export declare namespace getSignature {
576
632
  *
577
633
  * @example
578
634
  * ```ts twoslash
635
+ * // @noErrors
636
+ * import { Abi, AbiItem } from 'ox'
637
+ *
638
+ * const erc20Abi = Abi.from([...])
639
+ *
640
+ * const hash = AbiItem.getSignatureHash(erc20Abi, 'Transfer')
641
+ * // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
642
+ * ```
643
+ *
644
+ * @example
645
+ * ```ts twoslash
579
646
  * import { AbiItem } from 'ox'
580
647
  *
581
648
  * const hash = AbiItem.getSignatureHash({
@@ -593,7 +660,24 @@ export declare namespace getSignature {
593
660
  * @param abiItem - The ABI Item to compute the signature hash for.
594
661
  * @returns The {@link ox#Hash.(keccak256:function)} hash of the ABI item's signature.
595
662
  */
596
- export function getSignatureHash(abiItem: string | AbiItem): Hex.Hex {
663
+ export function getSignatureHash<
664
+ abi extends Abi.Abi | readonly unknown[],
665
+ name extends Name<abi>,
666
+ >(abi: abi | Abi.Abi | readonly unknown[], name: name): Hex.Hex
667
+ export function getSignatureHash(abiItem: string | AbiItem): Hex.Hex
668
+ // eslint-disable-next-line jsdoc/require-jsdoc
669
+ export function getSignatureHash(
670
+ ...parameters:
671
+ | [abi: Abi.Abi | readonly unknown[], name: string]
672
+ | [string | AbiItem]
673
+ ): Hex.Hex {
674
+ const abiItem = (() => {
675
+ if (Array.isArray(parameters[0])) {
676
+ const [abi, name] = parameters as [Abi.Abi | readonly unknown[], string]
677
+ return fromAbi(abi, name)
678
+ }
679
+ return parameters[0] as string | AbiItem
680
+ })()
597
681
  if (typeof abiItem !== 'string' && 'hash' in abiItem && abiItem.hash)
598
682
  return abiItem.hash as Hex.Hex
599
683
  return Hash.keccak256(Hex.fromString(getSignature(abiItem)))
package/core/Bytes.ts CHANGED
@@ -231,7 +231,7 @@ export function fromHex(value: Hex.Hex, options: fromHex.Options = {}): Bytes {
231
231
  `Invalid byte sequence ("${hexString[j - 2]}${hexString[j - 1]}" in "${hexString}").`,
232
232
  )
233
233
  }
234
- bytes[index] = nibbleLeft * 16 + nibbleRight
234
+ bytes[index] = (nibbleLeft << 4) | nibbleRight
235
235
  }
236
236
  return bytes
237
237
  }
package/core/Hex.ts CHANGED
@@ -256,7 +256,7 @@ export function fromNumber(
256
256
  }
257
257
 
258
258
  const stringValue = (
259
- signed && value_ < 0 ? (1n << BigInt(size * 8)) + BigInt(value_) : value_
259
+ signed && value_ < 0 ? BigInt.asUintN(size * 8, BigInt(value_)) : value_
260
260
  ).toString(16)
261
261
 
262
262
  const hex = `0x${stringValue}` as Hex
package/core/Rlp.ts CHANGED
@@ -359,9 +359,9 @@ function getEncodableBytes(bytesOrHex: Bytes.Bytes | Hex.Hex): Encodable {
359
359
  }
360
360
 
361
361
  function getSizeOfLength(length: number) {
362
- if (length < 2 ** 8) return 1
363
- if (length < 2 ** 16) return 2
364
- if (length < 2 ** 24) return 3
365
- if (length < 2 ** 32) return 4
362
+ if (length <= 0xff) return 1
363
+ if (length <= 0xff_ff) return 2
364
+ if (length <= 0xff_ff_ff) return 3
365
+ if (length <= 0xff_ff_ff_ff) return 4
366
366
  throw new Errors.BaseError('Length is too large.')
367
367
  }
@@ -4,6 +4,7 @@ import * as Authorization from './Authorization.js'
4
4
  import type * as Errors from './Errors.js'
5
5
  import * as Hex from './Hex.js'
6
6
  import type { Compute } from './internal/types.js'
7
+ import * as Transaction from './Transaction.js'
7
8
 
8
9
  /** A Transaction Request that is generic to all transaction types, as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4aca1d7a3e5aab24c8f6437131289ad386944eaa/src/schemas/transaction.yaml#L358-L423). */
9
10
  export type TransactionRequest<
@@ -132,7 +133,11 @@ export function toRpc(request: TransactionRequest): Rpc {
132
133
  if (typeof request.nonce !== 'undefined')
133
134
  request_rpc.nonce = Hex.fromNumber(request.nonce)
134
135
  if (typeof request.to !== 'undefined') request_rpc.to = request.to
135
- if (typeof request.type !== 'undefined') request_rpc.type = request.type
136
+ if (typeof request.type !== 'undefined')
137
+ request_rpc.type =
138
+ Transaction.toRpcType[
139
+ request.type as keyof typeof Transaction.toRpcType
140
+ ] || request.type
136
141
  if (typeof request.value !== 'undefined')
137
142
  request_rpc.value = Hex.fromNumber(request.value)
138
143
 
@@ -187,7 +187,7 @@ export declare namespace unwrap {
187
187
  *
188
188
  * @example
189
189
  * ```ts twoslash
190
- * import { Secp256k1 } from 'ox'
190
+ * import { Secp256k1, Signature } from 'ox'
191
191
  * import { SignatureErc6492 } from 'ox/erc6492' // [!code focus]
192
192
  *
193
193
  * const signature = Secp256k1.sign({
@@ -197,7 +197,7 @@ export declare namespace unwrap {
197
197
  *
198
198
  * const wrapped = SignatureErc6492.wrap({ // [!code focus]
199
199
  * data: '0xdeadbeef', // [!code focus]
200
- * signature, // [!code focus]
200
+ * signature: Signature.toHex(signature), // [!code focus]
201
201
  * to: '0x00000000219ab540356cBB839Cbe05303d7705Fa', // [!code focus]
202
202
  * }) // [!code focus]
203
203
  * ```
package/erc6492/index.ts CHANGED
@@ -7,7 +7,7 @@ export type {}
7
7
  *
8
8
  * @example
9
9
  * ```ts twoslash
10
- * import { Secp256k1, PersonalMessage } from 'ox'
10
+ * import { PersonalMessage, Secp256k1, Signature } from 'ox'
11
11
  * import { SignatureErc6492 } from 'ox/erc6492' // [!code focus]
12
12
  *
13
13
  * const signature = Secp256k1.sign({
@@ -17,7 +17,7 @@ export type {}
17
17
  *
18
18
  * const wrapped = SignatureErc6492.wrap({ // [!code focus]
19
19
  * data: '0xcafebabe', // [!code focus]
20
- * signature, // [!code focus]
20
+ * signature: Signature.toHex(signature), // [!code focus]
21
21
  * to: '0xcafebabecafebabecafebabecafebabecafebabe', // [!code focus]
22
22
  * }) // [!code focus]
23
23
  * // @log: '0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041fa78c5905fb0b9d6066ef531f962a62bc6ef0d5eb59ecb134056d206f75aaed7780926ff2601a935c2c79707d9e1799948c9f19dcdde1e090e903b19a07923d01c000000000000000000000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492'
@@ -146,7 +146,7 @@ export declare namespace unwrap {
146
146
  * @example
147
147
  * ```ts twoslash
148
148
  * // @noErrors
149
- * import { Secp256k1 } from 'ox'
149
+ * import { Secp256k1, Signature } from 'ox'
150
150
  * import { SignatureErc8010 } from 'ox/erc8010' // [!code focus]
151
151
  *
152
152
  * const signature = Secp256k1.sign({
@@ -157,7 +157,7 @@ export declare namespace unwrap {
157
157
  * const wrapped = SignatureErc8010.wrap({ // [!code focus]
158
158
  * authorization: { ... }, // [!code focus]
159
159
  * data: '0xdeadbeef', // [!code focus]
160
- * signature, // [!code focus]
160
+ * signature: Signature.toHex(signature), // [!code focus]
161
161
  * }) // [!code focus]
162
162
  * ```
163
163
  *
package/erc8010/index.ts CHANGED
@@ -7,7 +7,7 @@ export type {}
7
7
  *
8
8
  * @example
9
9
  * ```ts twoslash
10
- * import { Authorization, Secp256k1, PersonalMessage } from 'ox'
10
+ * import { Authorization, PersonalMessage, Secp256k1, Signature } from 'ox'
11
11
  * import { SignatureErc8010 } from 'ox/erc8010' // [!code focus]
12
12
  *
13
13
  * const authorization = Authorization.from({
@@ -33,7 +33,7 @@ export type {}
33
33
  * const wrapped = SignatureErc8010.wrap({ // [!code focus]
34
34
  * authorization: authorizationSigned, // [!code focus]
35
35
  * data: '0xcafebabe', // [!code focus]
36
- * signature, // [!code focus]
36
+ * signature: Signature.toHex(signature), // [!code focus]
37
37
  * }) // [!code focus]
38
38
  * // @log: '0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041fa78c5905fb0b9d6066ef531f962a62bc6ef0d5eb59ecb134056d206f75aaed7780926ff2601a935c2c79707d9e1799948c9f19dcdde1e090e903b19a07923d01c000000000000000000000000000000000000000000000000000000000000008010801080108010801080108010801080108010801080108010801080108010'
39
39
  * ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ox",
3
3
  "description": "Ethereum Standard Library",
4
- "version": "0.9.5",
4
+ "version": "0.9.7",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",
package/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** @internal */
2
- export const version = '0.9.5'
2
+ export const version = '0.9.7'