ox 0.14.57 → 0.14.58

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.
package/core/Hex.ts CHANGED
@@ -34,17 +34,24 @@ export function assert(
34
34
  value: unknown,
35
35
  options: assert.Options = {},
36
36
  ): asserts value is Hex {
37
- const { strict = false } = options
37
+ const { even = false, strict = false } = options
38
38
  if (!value) throw new InvalidHexTypeError(value)
39
39
  if (typeof value !== 'string') throw new InvalidHexTypeError(value)
40
40
  if (strict) {
41
41
  if (!/^0x[0-9a-fA-F]*$/.test(value)) throw new InvalidHexValueError(value)
42
42
  }
43
43
  if (!value.startsWith('0x')) throw new InvalidHexValueError(value)
44
+ if (even && value.length % 2 !== 0) throw new InvalidLengthError(value as Hex)
44
45
  }
45
46
 
46
47
  export declare namespace assert {
47
48
  type Options = {
49
+ /**
50
+ * Checks if the {@link ox#Hex.Hex} value has an even number of hexadecimal characters (whole bytes).
51
+ * Keep disabled for RPC quantities (e.g. `0x0`, `0x400`).
52
+ * @default false
53
+ */
54
+ even?: boolean | undefined
48
55
  /** Checks if the {@link ox#Hex.Hex} value contains invalid hexadecimal characters. @default false */
49
56
  strict?: boolean | undefined
50
57
  }
@@ -52,6 +59,7 @@ export declare namespace assert {
52
59
  type ErrorType =
53
60
  | InvalidHexTypeError
54
61
  | InvalidHexValueError
62
+ | InvalidLengthError
55
63
  | Errors.GlobalErrorType
56
64
  }
57
65
 
@@ -741,9 +749,9 @@ export function validate(
741
749
  value: unknown,
742
750
  options: validate.Options = {},
743
751
  ): value is Hex {
744
- const { strict = false } = options
752
+ const { even = false, strict = false } = options
745
753
  try {
746
- assert(value, { strict })
754
+ assert(value, { even, strict })
747
755
  return true
748
756
  } catch {
749
757
  return false
@@ -752,6 +760,12 @@ export function validate(
752
760
 
753
761
  export declare namespace validate {
754
762
  type Options = {
763
+ /**
764
+ * Checks if the {@link ox#Hex.Hex} value has an even number of hexadecimal characters (whole bytes).
765
+ * Keep disabled for RPC quantities (e.g. `0x0`, `0x400`).
766
+ * @default false
767
+ */
768
+ even?: boolean | undefined
755
769
  /** Checks if the {@link ox#Hex.Hex} value contains invalid hexadecimal characters. @default false */
756
770
  strict?: boolean | undefined
757
771
  }
@@ -95,8 +95,14 @@ export function fromHttp<
95
95
  ? await fetchOptions_(body_)
96
96
  : fetchOptions_
97
97
 
98
- const response = await promise.withTimeout(
99
- ({ signal }) => {
98
+ const { data, response } = await promise.withTimeout(
99
+ async ({ signal }) => {
100
+ // Abort on whichever signal fires first, so that a caller's
101
+ // `signal` does not disable the timeout.
102
+ const signals = [
103
+ fetchOptions?.signal,
104
+ timeout > 0 ? signal : null,
105
+ ].filter((s): s is AbortSignal => Boolean(s))
100
106
  const init: RequestInit = {
101
107
  ...fetchOptions,
102
108
  body,
@@ -105,10 +111,40 @@ export function fromHttp<
105
111
  ...fetchOptions?.headers,
106
112
  },
107
113
  method: fetchOptions?.method ?? 'POST',
108
- signal: fetchOptions?.signal ?? (timeout > 0 ? signal : null),
114
+ signal: (() => {
115
+ if (signals.length < 2) return signals[0] ?? null
116
+ // Runtimes without `AbortSignal.any` (e.g. Node < 18.17) keep
117
+ // only the caller's signal, as before.
118
+ if (typeof AbortSignal.any !== 'function') return signals[0]!
119
+ return AbortSignal.any(signals)
120
+ })(),
109
121
  }
110
122
  const request = new Request(url, init)
111
- return fetchFn(request)
123
+ const response = await fetchFn(request)
124
+
125
+ // Read the body within the timeout too, so a response that stalls
126
+ // after its headers cannot hang the request.
127
+ const data = await (async () => {
128
+ if (
129
+ response.headers
130
+ .get('Content-Type')
131
+ ?.startsWith('application/json')
132
+ )
133
+ return response.json()
134
+ return response.text().then((data) => {
135
+ try {
136
+ return JSON.parse(data || '{}')
137
+ } catch (_err) {
138
+ if (response.ok)
139
+ throw new MalformedResponseError({
140
+ response: data,
141
+ })
142
+ return { error: data }
143
+ }
144
+ })
145
+ })()
146
+
147
+ return { data, response }
112
148
  },
113
149
  {
114
150
  timeout,
@@ -116,24 +152,6 @@ export function fromHttp<
116
152
  },
117
153
  )
118
154
 
119
- const data = await (async () => {
120
- if (
121
- response.headers.get('Content-Type')?.startsWith('application/json')
122
- )
123
- return response.json()
124
- return response.text().then((data) => {
125
- try {
126
- return JSON.parse(data || '{}')
127
- } catch (_err) {
128
- if (response.ok)
129
- throw new MalformedResponseError({
130
- response: data,
131
- })
132
- return { error: data }
133
- }
134
- })
135
- })()
136
-
137
155
  if (!response.ok)
138
156
  throw new HttpError({
139
157
  body,
package/core/TypedData.ts CHANGED
@@ -393,7 +393,7 @@ export function extractEip712DomainTypes(
393
393
  ): Parameter[] {
394
394
  return [
395
395
  typeof domain?.name === 'string' && { name: 'name', type: 'string' },
396
- domain?.version && { name: 'version', type: 'string' },
396
+ typeof domain?.version === 'string' && { name: 'version', type: 'string' },
397
397
  (typeof domain?.chainId === 'number' ||
398
398
  typeof domain?.chainId === 'bigint') && {
399
399
  name: 'chainId',
@@ -4,6 +4,7 @@ import type * as AbiItem from '../AbiItem.js'
4
4
  import type * as AbiParameters from '../AbiParameters.js'
5
5
  import * as Address from '../Address.js'
6
6
  import * as Errors from '../Errors.js'
7
+ import { getArrayComponents } from './abiParameters.js'
7
8
  import type {
8
9
  Compute,
9
10
  IsNever,
@@ -466,8 +467,9 @@ export function normalizeSignature(signature: string): string {
466
467
 
467
468
  // Ignore spaces
468
469
  if (char === ' ') {
469
- // If the previous character is a separator, and the current section isn't empty, we want to deactivate.
470
- if (signature[i - 1] !== ',' && current !== ',' && current !== ',(') {
470
+ // A space directly after a `(` or `,` separator is leading whitespace.
471
+ // Otherwise, it ends the type, so we want to deactivate.
472
+ if (!current.endsWith(',') && !current.endsWith('(')) {
471
473
  current = ''
472
474
  active = false
473
475
  }
@@ -508,9 +510,12 @@ export function isArgOfType(
508
510
  if (abiParameterType === 'tuple' && 'components' in abiParameter)
509
511
  return Object.values(abiParameter.components).every(
510
512
  (component, index) => {
511
- return isArgOfType(
512
- Object.values(arg as unknown[] | Record<string, unknown>)[index],
513
- component as AbiParameters.Parameter,
513
+ return (
514
+ argType === 'object' &&
515
+ isArgOfType(
516
+ getComponentValue(arg, component, index),
517
+ component as AbiParameters.Parameter,
518
+ )
514
519
  )
515
520
  },
516
521
  )
@@ -530,15 +535,16 @@ export function isArgOfType(
530
535
  return argType === 'string' || arg instanceof Uint8Array
531
536
 
532
537
  // fixed-length (`<type>[M]`) and dynamic (`<type>[]`) arrays
533
- // https://regexr.com/6va6i
534
- if (/[a-z]+[1-9]{0,3}(\[[0-9]{0,}\])+$/.test(abiParameterType)) {
538
+ const arrayComponents = getArrayComponents(abiParameterType)
539
+ if (arrayComponents) {
540
+ const [length, type] = arrayComponents
535
541
  return (
536
542
  Array.isArray(arg) &&
543
+ (length === null || arg.length === length) &&
537
544
  arg.every((x: unknown) =>
538
545
  isArgOfType(x, {
539
546
  ...abiParameter,
540
- // Pop off `[]` or `[M]` from end of type
541
- type: abiParameterType.replace(/(\[[0-9]{0,}\])$/, ''),
547
+ type,
542
548
  } as AbiParameters.Parameter),
543
549
  )
544
550
  )
@@ -557,32 +563,65 @@ export function getAmbiguousTypes(
557
563
  ): AbiParameters.Parameter['type'][] | undefined {
558
564
  for (const parameterIndex in sourceParameters) {
559
565
  const sourceParameter = sourceParameters[parameterIndex]!
560
- const targetParameter = targetParameters[parameterIndex]!
566
+ const targetParameter = targetParameters[parameterIndex]
567
+ if (!targetParameter) continue
561
568
 
562
569
  if (
563
570
  sourceParameter.type === 'tuple' &&
564
571
  targetParameter.type === 'tuple' &&
565
572
  'components' in sourceParameter &&
566
573
  'components' in targetParameter
567
- )
568
- return getAmbiguousTypes(
574
+ ) {
575
+ const value = args[parameterIndex]
576
+ const types = getAmbiguousTypes(
569
577
  sourceParameter.components,
570
578
  targetParameter.components,
571
- (args as any)[parameterIndex],
579
+ sourceParameter.components.map((component, index) =>
580
+ getComponentValue(value, component, index),
581
+ ),
572
582
  )
583
+ if (types) return types
584
+ continue
585
+ }
586
+
587
+ const sourceArray = getArrayComponents(sourceParameter.type)
588
+ const targetArray = getArrayComponents(targetParameter.type)
589
+ if (sourceArray && targetArray) {
590
+ // Every item matches both element types, so the first item (or `undefined` for an empty array) decides.
591
+ const types = getAmbiguousTypes(
592
+ [
593
+ {
594
+ ...sourceParameter,
595
+ type: sourceArray[1],
596
+ } as AbiParameters.Parameter,
597
+ ],
598
+ [
599
+ {
600
+ ...targetParameter,
601
+ type: targetArray[1],
602
+ } as AbiParameters.Parameter,
603
+ ],
604
+ [(args[parameterIndex] as readonly unknown[] | undefined)?.[0]],
605
+ )
606
+ if (types) return types
607
+ continue
608
+ }
573
609
 
574
610
  const types = [sourceParameter.type, targetParameter.type]
575
611
 
612
+ const value = args[parameterIndex]
613
+
576
614
  const ambiguous = (() => {
577
615
  if (types.includes('address') && types.includes('bytes20')) return true
578
- if (types.includes('address') && types.includes('string'))
579
- return Address.validate(args[parameterIndex] as Address.Address, {
580
- strict: false,
581
- })
582
- if (types.includes('address') && types.includes('bytes'))
583
- return Address.validate(args[parameterIndex] as Address.Address, {
584
- strict: false,
585
- })
616
+ // An `undefined` value comes from an empty array, which matches both types.
617
+ if (
618
+ types.includes('address') &&
619
+ (types.includes('string') || types.includes('bytes'))
620
+ )
621
+ return (
622
+ value === undefined ||
623
+ Address.validate(value as Address.Address, { strict: false })
624
+ )
586
625
  return false
587
626
  })()
588
627
 
@@ -591,3 +630,13 @@ export function getAmbiguousTypes(
591
630
 
592
631
  return
593
632
  }
633
+
634
+ // Reads a tuple component's value by index from arrays and by name from objects, like `AbiParameters.encode`.
635
+ function getComponentValue(
636
+ arg: unknown,
637
+ component: AbiParameters.Parameter,
638
+ index: number,
639
+ ): unknown {
640
+ if (Array.isArray(arg)) return arg[index]
641
+ return (arg as Record<string, unknown> | undefined)?.[component.name!]
642
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ox",
3
3
  "description": "Ethereum Standard Library",
4
- "version": "0.14.57",
4
+ "version": "0.14.58",
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.14.57'
2
+ export const version = '0.14.58'