ox 0.14.67 → 0.14.69
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/CHANGELOG.md +62 -0
- package/_cjs/core/AbiEvent.js +45 -1
- package/_cjs/core/AbiEvent.js.map +1 -1
- package/_cjs/core/AbiParameters.js +6 -2
- package/_cjs/core/AbiParameters.js.map +1 -1
- package/_cjs/core/Address.js +24 -21
- package/_cjs/core/Address.js.map +1 -1
- package/_cjs/core/Authorization.js +2 -0
- package/_cjs/core/Authorization.js.map +1 -1
- package/_cjs/core/Base58.js +14 -4
- package/_cjs/core/Base58.js.map +1 -1
- package/_cjs/core/BinaryStateTree.js +1 -1
- package/_cjs/core/BinaryStateTree.js.map +1 -1
- package/_cjs/core/Blobs.js +26 -34
- package/_cjs/core/Blobs.js.map +1 -1
- package/_cjs/core/Block.js +1 -0
- package/_cjs/core/Block.js.map +1 -1
- package/_cjs/core/BlockOverrides.js +1 -1
- package/_cjs/core/BlockOverrides.js.map +1 -1
- package/_cjs/core/Bytes.js +56 -13
- package/_cjs/core/Bytes.js.map +1 -1
- package/_cjs/core/Cbor.js +33 -18
- package/_cjs/core/Cbor.js.map +1 -1
- package/_cjs/core/FrameReceipt.js +6 -2
- package/_cjs/core/FrameReceipt.js.map +1 -1
- package/_cjs/core/Hex.js +54 -21
- package/_cjs/core/Hex.js.map +1 -1
- package/_cjs/core/Json.js +15 -2
- package/_cjs/core/Json.js.map +1 -1
- package/_cjs/core/Rlp.js +138 -94
- package/_cjs/core/Rlp.js.map +1 -1
- package/_cjs/core/RpcTransport.js +3 -1
- package/_cjs/core/RpcTransport.js.map +1 -1
- package/_cjs/core/Siwe.js +35 -9
- package/_cjs/core/Siwe.js.map +1 -1
- package/_cjs/core/Transaction.js +10 -4
- package/_cjs/core/Transaction.js.map +1 -1
- package/_cjs/core/TransactionReceipt.js +12 -4
- package/_cjs/core/TransactionReceipt.js.map +1 -1
- package/_cjs/core/TransactionRequest.js +6 -2
- package/_cjs/core/TransactionRequest.js.map +1 -1
- package/_cjs/core/TxEnvelopeEip4844.js +1 -1
- package/_cjs/core/TxEnvelopeEip4844.js.map +1 -1
- package/_cjs/core/TypedData.js +19 -8
- package/_cjs/core/TypedData.js.map +1 -1
- package/_cjs/core/internal/abiParameters.js +59 -18
- package/_cjs/core/internal/abiParameters.js.map +1 -1
- package/_cjs/core/internal/address.js +32 -0
- package/_cjs/core/internal/address.js.map +1 -0
- package/_cjs/core/internal/base58.js +11 -9
- package/_cjs/core/internal/base58.js.map +1 -1
- package/_cjs/core/internal/bytes.js +13 -22
- package/_cjs/core/internal/bytes.js.map +1 -1
- package/_cjs/core/internal/caches.js +1 -0
- package/_cjs/core/internal/caches.js.map +1 -1
- package/_cjs/core/internal/cursor.js +32 -10
- package/_cjs/core/internal/cursor.js.map +1 -1
- package/_cjs/core/internal/hex.js +3 -3
- package/_cjs/core/internal/hex.js.map +1 -1
- package/_cjs/core/internal/lru.js +6 -5
- package/_cjs/core/internal/lru.js.map +1 -1
- package/_cjs/core/internal/promise.js +3 -3
- package/_cjs/core/internal/promise.js.map +1 -1
- package/_cjs/erc6492/SignatureErc6492.js +1 -1
- package/_cjs/erc6492/SignatureErc6492.js.map +1 -1
- package/_cjs/tempo/Transaction.js +13 -7
- package/_cjs/tempo/Transaction.js.map +1 -1
- package/_cjs/tempo/TransactionRequest.js +23 -19
- package/_cjs/tempo/TransactionRequest.js.map +1 -1
- package/_cjs/tempo/TxEnvelopeTempo.js +23 -4
- package/_cjs/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_cjs/tempo/ZoneId.js +3 -0
- package/_cjs/tempo/ZoneId.js.map +1 -1
- package/_cjs/tempo/internal/virtualMasterPool.js +12 -3
- package/_cjs/tempo/internal/virtualMasterPool.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/core/AbiError.js +2 -2
- package/_esm/core/AbiEvent.js +71 -2
- package/_esm/core/AbiEvent.js.map +1 -1
- package/_esm/core/AbiParameters.js +8 -2
- package/_esm/core/AbiParameters.js.map +1 -1
- package/_esm/core/Address.js +27 -21
- package/_esm/core/Address.js.map +1 -1
- package/_esm/core/Authorization.js +3 -0
- package/_esm/core/Authorization.js.map +1 -1
- package/_esm/core/Base58.js +16 -4
- package/_esm/core/Base58.js.map +1 -1
- package/_esm/core/BinaryStateTree.js +3 -1
- package/_esm/core/BinaryStateTree.js.map +1 -1
- package/_esm/core/Blobs.js +40 -41
- package/_esm/core/Blobs.js.map +1 -1
- package/_esm/core/Block.js +1 -0
- package/_esm/core/Block.js.map +1 -1
- package/_esm/core/BlockOverrides.js +3 -2
- package/_esm/core/BlockOverrides.js.map +1 -1
- package/_esm/core/Bytes.js +66 -13
- package/_esm/core/Bytes.js.map +1 -1
- package/_esm/core/Cbor.js +33 -17
- package/_esm/core/Cbor.js.map +1 -1
- package/_esm/core/Ens.js +1 -1
- package/_esm/core/FrameReceipt.js +6 -2
- package/_esm/core/FrameReceipt.js.map +1 -1
- package/_esm/core/Hash.js +1 -1
- package/_esm/core/Hex.js +70 -21
- package/_esm/core/Hex.js.map +1 -1
- package/_esm/core/Json.js +21 -2
- package/_esm/core/Json.js.map +1 -1
- package/_esm/core/Rlp.js +146 -97
- package/_esm/core/Rlp.js.map +1 -1
- package/_esm/core/RpcTransport.js +4 -1
- package/_esm/core/RpcTransport.js.map +1 -1
- package/_esm/core/Siwe.js +40 -10
- package/_esm/core/Siwe.js.map +1 -1
- package/_esm/core/Transaction.js +11 -4
- package/_esm/core/Transaction.js.map +1 -1
- package/_esm/core/TransactionReceipt.js +12 -4
- package/_esm/core/TransactionReceipt.js.map +1 -1
- package/_esm/core/TransactionRequest.js +6 -2
- package/_esm/core/TransactionRequest.js.map +1 -1
- package/_esm/core/TxEnvelopeEip4844.js +1 -1
- package/_esm/core/TxEnvelopeEip4844.js.map +1 -1
- package/_esm/core/TypedData.js +26 -10
- package/_esm/core/TypedData.js.map +1 -1
- package/_esm/core/internal/abiParameters.js +78 -18
- package/_esm/core/internal/abiParameters.js.map +1 -1
- package/_esm/core/internal/address.js +41 -0
- package/_esm/core/internal/address.js.map +1 -0
- package/_esm/core/internal/base58.js +15 -9
- package/_esm/core/internal/base58.js.map +1 -1
- package/_esm/core/internal/bytes.js +14 -22
- package/_esm/core/internal/bytes.js.map +1 -1
- package/_esm/core/internal/caches.js +1 -0
- package/_esm/core/internal/caches.js.map +1 -1
- package/_esm/core/internal/cursor.js +37 -10
- package/_esm/core/internal/cursor.js.map +1 -1
- package/_esm/core/internal/hex.js +5 -3
- package/_esm/core/internal/hex.js.map +1 -1
- package/_esm/core/internal/lru.js +7 -5
- package/_esm/core/internal/lru.js.map +1 -1
- package/_esm/core/internal/promise.js +4 -3
- package/_esm/core/internal/promise.js.map +1 -1
- package/_esm/erc6492/SignatureErc6492.js +1 -1
- package/_esm/erc6492/SignatureErc6492.js.map +1 -1
- package/_esm/tempo/Transaction.js +15 -7
- package/_esm/tempo/Transaction.js.map +1 -1
- package/_esm/tempo/TransactionRequest.js +25 -19
- package/_esm/tempo/TransactionRequest.js.map +1 -1
- package/_esm/tempo/TxEnvelopeTempo.js +30 -4
- package/_esm/tempo/TxEnvelopeTempo.js.map +1 -1
- package/_esm/tempo/ZoneId.js +7 -1
- package/_esm/tempo/ZoneId.js.map +1 -1
- package/_esm/tempo/internal/virtualMasterPool.js +16 -3
- package/_esm/tempo/internal/virtualMasterPool.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/core/AbiError.d.ts +3 -3
- package/_types/core/AbiEvent.d.ts +38 -3
- package/_types/core/AbiEvent.d.ts.map +1 -1
- package/_types/core/AbiParameters.d.ts +1 -1
- package/_types/core/AbiParameters.d.ts.map +1 -1
- package/_types/core/Address.d.ts.map +1 -1
- package/_types/core/Authorization.d.ts.map +1 -1
- package/_types/core/Base58.d.ts.map +1 -1
- package/_types/core/Blobs.d.ts +4 -5
- package/_types/core/Blobs.d.ts.map +1 -1
- package/_types/core/Block.d.ts +2 -0
- package/_types/core/Block.d.ts.map +1 -1
- package/_types/core/BlockOverrides.d.ts +2 -1
- package/_types/core/BlockOverrides.d.ts.map +1 -1
- package/_types/core/Bytes.d.ts +2 -0
- package/_types/core/Bytes.d.ts.map +1 -1
- package/_types/core/Cbor.d.ts +13 -6
- package/_types/core/Cbor.d.ts.map +1 -1
- package/_types/core/Ens.d.ts +1 -1
- package/_types/core/FrameReceipt.d.ts.map +1 -1
- package/_types/core/Hash.d.ts +1 -1
- package/_types/core/Hex.d.ts +3 -1
- package/_types/core/Hex.d.ts.map +1 -1
- package/_types/core/Json.d.ts.map +1 -1
- package/_types/core/Rlp.d.ts +38 -9
- package/_types/core/Rlp.d.ts.map +1 -1
- package/_types/core/RpcTransport.d.ts.map +1 -1
- package/_types/core/Siwe.d.ts +1 -0
- package/_types/core/Siwe.d.ts.map +1 -1
- package/_types/core/Transaction.d.ts.map +1 -1
- package/_types/core/TransactionReceipt.d.ts.map +1 -1
- package/_types/core/TransactionRequest.d.ts.map +1 -1
- package/_types/core/TypedData.d.ts +2 -2
- package/_types/core/TypedData.d.ts.map +1 -1
- package/_types/core/internal/abiParameters.d.ts +5 -5
- package/_types/core/internal/abiParameters.d.ts.map +1 -1
- package/_types/core/internal/address.d.ts +14 -0
- package/_types/core/internal/address.d.ts.map +1 -0
- package/_types/core/internal/base58.d.ts.map +1 -1
- package/_types/core/internal/bytes.d.ts +1 -10
- package/_types/core/internal/bytes.d.ts.map +1 -1
- package/_types/core/internal/caches.d.ts +1 -0
- package/_types/core/internal/caches.d.ts.map +1 -1
- package/_types/core/internal/cursor.d.ts +7 -2
- package/_types/core/internal/cursor.d.ts.map +1 -1
- package/_types/core/internal/hex.d.ts.map +1 -1
- package/_types/core/internal/lru.d.ts.map +1 -1
- package/_types/core/internal/promise.d.ts.map +1 -1
- package/_types/erc6492/SignatureErc6492.d.ts +1 -1
- package/_types/erc6492/SignatureErc6492.d.ts.map +1 -1
- package/_types/tempo/Transaction.d.ts.map +1 -1
- package/_types/tempo/TransactionRequest.d.ts.map +1 -1
- package/_types/tempo/TxEnvelopeTempo.d.ts.map +1 -1
- package/_types/tempo/ZoneId.d.ts +3 -1
- package/_types/tempo/ZoneId.d.ts.map +1 -1
- package/_types/tempo/internal/virtualMasterPool.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/core/AbiError.ts +3 -3
- package/core/AbiEvent.ts +74 -2
- package/core/AbiParameters.ts +8 -1
- package/core/Address.ts +25 -20
- package/core/Authorization.ts +3 -0
- package/core/Base58.ts +15 -4
- package/core/BinaryStateTree.ts +4 -1
- package/core/Blobs.ts +49 -53
- package/core/Block.ts +3 -0
- package/core/BlockOverrides.ts +3 -2
- package/core/Bytes.ts +68 -17
- package/core/Cbor.ts +33 -13
- package/core/Ens.ts +1 -1
- package/core/FrameReceipt.ts +6 -2
- package/core/Hash.ts +1 -1
- package/core/Hex.ts +79 -22
- package/core/Json.ts +23 -2
- package/core/Rlp.ts +184 -94
- package/core/RpcTransport.ts +4 -1
- package/core/Siwe.ts +41 -10
- package/core/Transaction.ts +11 -4
- package/core/TransactionReceipt.ts +13 -4
- package/core/TransactionRequest.ts +10 -6
- package/core/TxEnvelopeEip4844.ts +1 -1
- package/core/TypedData.ts +28 -10
- package/core/internal/abiParameters.ts +82 -23
- package/core/internal/address.ts +38 -0
- package/core/internal/base58.ts +14 -9
- package/core/internal/bytes.ts +13 -23
- package/core/internal/caches.ts +1 -0
- package/core/internal/cursor.ts +44 -16
- package/core/internal/hex.ts +5 -3
- package/core/internal/lru.ts +6 -7
- package/core/internal/promise.ts +5 -3
- package/erc6492/SignatureErc6492.ts +1 -1
- package/package.json +1 -1
- package/tempo/AccountSimulation.test.ts +1 -1
- package/tempo/SignaturePreAuthorized.test.ts +1 -1
- package/tempo/Transaction.ts +15 -9
- package/tempo/TransactionRequest.ts +31 -24
- package/tempo/TxEnvelopeTempo.test.ts +52 -0
- package/tempo/TxEnvelopeTempo.ts +32 -6
- package/tempo/ZoneId.test.ts +11 -0
- package/tempo/ZoneId.ts +7 -1
- package/tempo/internal/virtualMasterPool.ts +18 -4
- package/version.ts +1 -1
- package/_cjs/core/internal/uid.js +0 -17
- package/_cjs/core/internal/uid.js.map +0 -1
- package/_esm/core/internal/uid.js +0 -15
- package/_esm/core/internal/uid.js.map +0 -1
- package/_types/core/internal/uid.d.ts +0 -3
- package/_types/core/internal/uid.d.ts.map +0 -1
- package/core/internal/uid.ts +0 -15
package/core/Bytes.ts
CHANGED
|
@@ -211,29 +211,44 @@ export declare namespace fromBoolean {
|
|
|
211
211
|
*/
|
|
212
212
|
export function fromHex(value: Hex.Hex, options: fromHex.Options = {}): Bytes {
|
|
213
213
|
const { size } = options
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if
|
|
218
|
-
|
|
219
|
-
|
|
214
|
+
if (size) internal_hex.assertSize(value, size)
|
|
215
|
+
|
|
216
|
+
// Read nibbles straight from `value`, without slicing or padding a copy of
|
|
217
|
+
// it. An odd number of nibbles reads as if it had a leading zero.
|
|
218
|
+
const odd = value.length % 2
|
|
219
|
+
const length = Math.max(0, value.length - 2 + odd) / 2
|
|
220
|
+
const bytes = new Uint8Array(Math.max(size || 0, length))
|
|
221
|
+
let index = 0
|
|
222
|
+
let j = 2
|
|
223
|
+
if (odd && length) {
|
|
224
|
+
const nibble = internal.charCodeToBase16(value.charCodeAt(j++))
|
|
225
|
+
if (nibble < 0) throw invalidByteSequence(value, size, index)
|
|
226
|
+
bytes[index++] = nibble
|
|
220
227
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const nibbleRight = internal.charCodeToBase16(hexString.charCodeAt(j++))
|
|
227
|
-
if (nibbleLeft === undefined || nibbleRight === undefined) {
|
|
228
|
-
throw new Errors.BaseError(
|
|
229
|
-
`Invalid byte sequence ("${hexString[j - 2]}${hexString[j - 1]}" in "${hexString}").`,
|
|
230
|
-
)
|
|
231
|
-
}
|
|
228
|
+
for (; index < length; index++) {
|
|
229
|
+
const nibbleLeft = internal.charCodeToBase16(value.charCodeAt(j++))
|
|
230
|
+
const nibbleRight = internal.charCodeToBase16(value.charCodeAt(j++))
|
|
231
|
+
if ((nibbleLeft | nibbleRight) < 0)
|
|
232
|
+
throw invalidByteSequence(value, size, index)
|
|
232
233
|
bytes[index] = (nibbleLeft << 4) | nibbleRight
|
|
233
234
|
}
|
|
234
235
|
return bytes
|
|
235
236
|
}
|
|
236
237
|
|
|
238
|
+
// Builds the error for an invalid hex digit in the byte at `index`.
|
|
239
|
+
function invalidByteSequence(
|
|
240
|
+
value: Hex.Hex,
|
|
241
|
+
size: number | undefined,
|
|
242
|
+
index: number,
|
|
243
|
+
) {
|
|
244
|
+
let hexString = value.slice(2)
|
|
245
|
+
if (hexString.length % 2) hexString = `0${hexString}`
|
|
246
|
+
if (size) hexString = hexString.padEnd(size * 2, '0')
|
|
247
|
+
return new Errors.BaseError(
|
|
248
|
+
`Invalid byte sequence ("${hexString.slice(index * 2, index * 2 + 2)}" in "${hexString}").`,
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
237
252
|
export declare namespace fromHex {
|
|
238
253
|
type Options = {
|
|
239
254
|
/** Size of the output bytes. */
|
|
@@ -523,6 +538,27 @@ export declare namespace slice {
|
|
|
523
538
|
export function toBigInt(bytes: Bytes, options: toBigInt.Options = {}): bigint {
|
|
524
539
|
const { size } = options
|
|
525
540
|
if (typeof size !== 'undefined') internal.assertSize(bytes, size)
|
|
541
|
+
// An unsigned value of up to 6 significant bytes (most ABI words hold small
|
|
542
|
+
// values) is built as a number and converted once, skipping the hex string.
|
|
543
|
+
if (!options.signed && typeof size === 'undefined' && bytes.length > 0) {
|
|
544
|
+
let i = 0
|
|
545
|
+
while (i < bytes.length && bytes[i] === 0) i++
|
|
546
|
+
if (bytes.length - i <= 6) {
|
|
547
|
+
let value = 0
|
|
548
|
+
for (; i < bytes.length; i++) value = value * 256 + bytes[i]!
|
|
549
|
+
return BigInt(value)
|
|
550
|
+
}
|
|
551
|
+
// A full word reads as four 64-bit halves, skipping the hex string.
|
|
552
|
+
if (bytes.length === 32 && bytes instanceof Uint8Array) {
|
|
553
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, 32)
|
|
554
|
+
return (
|
|
555
|
+
(view.getBigUint64(0) << 192n) |
|
|
556
|
+
(view.getBigUint64(8) << 128n) |
|
|
557
|
+
(view.getBigUint64(16) << 64n) |
|
|
558
|
+
view.getBigUint64(24)
|
|
559
|
+
)
|
|
560
|
+
}
|
|
561
|
+
}
|
|
526
562
|
const hex = Hex.fromBytes(bytes, options)
|
|
527
563
|
return Hex.toBigInt(hex, options)
|
|
528
564
|
}
|
|
@@ -614,6 +650,8 @@ export declare namespace toHex {
|
|
|
614
650
|
/**
|
|
615
651
|
* Decodes a {@link ox#Bytes.Bytes} into a number.
|
|
616
652
|
*
|
|
653
|
+
* Throws `Hex.IntegerOutOfRangeError` when the value is not a safe integer. Use `toBigInt` for larger values.
|
|
654
|
+
*
|
|
617
655
|
* @example
|
|
618
656
|
* ```ts twoslash
|
|
619
657
|
* import { Bytes } from 'ox'
|
|
@@ -625,6 +663,17 @@ export declare namespace toHex {
|
|
|
625
663
|
export function toNumber(bytes: Bytes, options: toNumber.Options = {}): number {
|
|
626
664
|
const { size } = options
|
|
627
665
|
if (typeof size !== 'undefined') internal.assertSize(bytes, size)
|
|
666
|
+
if (!options.signed && size === undefined && bytes.length > 0) {
|
|
667
|
+
const length = bytes.length
|
|
668
|
+
let i = 0
|
|
669
|
+
while (i < length && bytes[i] === 0) i++
|
|
670
|
+
// Up to 6 bytes (48 bits) accumulate exactly as a number.
|
|
671
|
+
if (length - i <= 6) {
|
|
672
|
+
let value = 0
|
|
673
|
+
for (; i < length; i++) value = value * 256 + bytes[i]!
|
|
674
|
+
return value
|
|
675
|
+
}
|
|
676
|
+
}
|
|
628
677
|
const hex = Hex.fromBytes(bytes, options)
|
|
629
678
|
return Hex.toNumber(hex, options)
|
|
630
679
|
}
|
|
@@ -743,6 +792,8 @@ export declare namespace trimRight {
|
|
|
743
792
|
* @returns `true` if the value is {@link ox#Bytes.Bytes}, otherwise `false`.
|
|
744
793
|
*/
|
|
745
794
|
export function validate(value: unknown): value is Bytes {
|
|
795
|
+
// Reject non-objects up front, without constructing and catching an error.
|
|
796
|
+
if (!value || typeof value !== 'object') return false
|
|
746
797
|
try {
|
|
747
798
|
assert(value)
|
|
748
799
|
return true
|
package/core/Cbor.ts
CHANGED
|
@@ -3,6 +3,9 @@ import * as Errors from './Errors.js'
|
|
|
3
3
|
import * as Hex from './Hex.js'
|
|
4
4
|
import * as Cursor from './internal/cursor.js'
|
|
5
5
|
|
|
6
|
+
/** Maximum nesting depth permitted when decoding a CBOR value. */
|
|
7
|
+
const depthLimit = 1_024
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
* Encodes a value into CBOR (Concise Binary Object Representation) format.
|
|
8
11
|
*
|
|
@@ -106,6 +109,15 @@ export declare namespace decode {
|
|
|
106
109
|
| Errors.GlobalErrorType
|
|
107
110
|
}
|
|
108
111
|
|
|
112
|
+
/** Thrown when a CBOR value nests deeper than the decode depth limit. */
|
|
113
|
+
export class DepthLimitExceededError extends Errors.BaseError {
|
|
114
|
+
override readonly name = 'Cbor.DepthLimitExceededError'
|
|
115
|
+
|
|
116
|
+
constructor({ limit }: { limit: number }) {
|
|
117
|
+
super(`CBOR depth limit of \`${limit}\` exceeded.`)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
109
121
|
export class InvalidMajorTypeError extends Errors.BaseError {
|
|
110
122
|
override readonly name = 'Cbor.InvalidMajorTypeError'
|
|
111
123
|
|
|
@@ -617,7 +629,10 @@ namespace getEncodable {
|
|
|
617
629
|
|
|
618
630
|
/** @internal */
|
|
619
631
|
// biome-ignore lint/correctness/noUnusedVariables: _
|
|
620
|
-
function decodeCursor(cursor: Cursor.Cursor): unknown {
|
|
632
|
+
function decodeCursor(cursor: Cursor.Cursor, depth = 0): unknown {
|
|
633
|
+
if (depth >= depthLimit)
|
|
634
|
+
throw new DepthLimitExceededError({ limit: depthLimit })
|
|
635
|
+
|
|
621
636
|
const initialByte = cursor.readUint8()
|
|
622
637
|
const majorType = initialByte >> 5
|
|
623
638
|
const additionalInfo = initialByte & 0b00011111
|
|
@@ -633,19 +648,19 @@ function decodeCursor(cursor: Cursor.Cursor): unknown {
|
|
|
633
648
|
|
|
634
649
|
// Major type 2: Byte string
|
|
635
650
|
case 2:
|
|
636
|
-
return decodeCursor.readByteString(cursor, additionalInfo)
|
|
651
|
+
return decodeCursor.readByteString(cursor, additionalInfo, depth + 1)
|
|
637
652
|
|
|
638
653
|
// Major type 3: Text string
|
|
639
654
|
case 3:
|
|
640
|
-
return decodeCursor.readTextString(cursor, additionalInfo)
|
|
655
|
+
return decodeCursor.readTextString(cursor, additionalInfo, depth + 1)
|
|
641
656
|
|
|
642
657
|
// Major type 4: Array
|
|
643
658
|
case 4:
|
|
644
|
-
return decodeCursor.readArray(cursor, additionalInfo)
|
|
659
|
+
return decodeCursor.readArray(cursor, additionalInfo, depth + 1)
|
|
645
660
|
|
|
646
661
|
// Major type 5: Map
|
|
647
662
|
case 5:
|
|
648
|
-
return decodeCursor.readMap(cursor, additionalInfo)
|
|
663
|
+
return decodeCursor.readMap(cursor, additionalInfo, depth + 1)
|
|
649
664
|
|
|
650
665
|
// Major type 6: Tagged data (not yet supported)
|
|
651
666
|
case 6:
|
|
@@ -663,6 +678,7 @@ function decodeCursor(cursor: Cursor.Cursor): unknown {
|
|
|
663
678
|
/** @internal */
|
|
664
679
|
namespace decodeCursor {
|
|
665
680
|
export type ErrorType =
|
|
681
|
+
| DepthLimitExceededError
|
|
666
682
|
| readUnsignedInteger.ErrorType
|
|
667
683
|
| readNegativeInteger.ErrorType
|
|
668
684
|
| readByteString.ErrorType
|
|
@@ -724,6 +740,7 @@ namespace decodeCursor {
|
|
|
724
740
|
export function readByteString(
|
|
725
741
|
cursor: Cursor.Cursor,
|
|
726
742
|
additionalInfo: number,
|
|
743
|
+
depth = 0,
|
|
727
744
|
): Bytes.Bytes {
|
|
728
745
|
// Indefinite-length byte string
|
|
729
746
|
if (additionalInfo === 31) {
|
|
@@ -736,7 +753,7 @@ namespace decodeCursor {
|
|
|
736
753
|
cursor.readUint8() // consume the break byte
|
|
737
754
|
break
|
|
738
755
|
}
|
|
739
|
-
const chunk = decodeCursor(cursor) as Bytes.Bytes
|
|
756
|
+
const chunk = decodeCursor(cursor, depth) as Bytes.Bytes
|
|
740
757
|
if (!(chunk instanceof Uint8Array))
|
|
741
758
|
throw new InvalidIndefiniteLengthChunkError({ type: 'byte string' })
|
|
742
759
|
chunks.push(chunk)
|
|
@@ -769,6 +786,7 @@ namespace decodeCursor {
|
|
|
769
786
|
export function readTextString(
|
|
770
787
|
cursor: Cursor.Cursor,
|
|
771
788
|
additionalInfo: number,
|
|
789
|
+
depth = 0,
|
|
772
790
|
): string {
|
|
773
791
|
// Indefinite-length text string
|
|
774
792
|
if (additionalInfo === 31) {
|
|
@@ -780,7 +798,7 @@ namespace decodeCursor {
|
|
|
780
798
|
cursor.readUint8() // consume the break byte
|
|
781
799
|
break
|
|
782
800
|
}
|
|
783
|
-
const chunk = decodeCursor(cursor)
|
|
801
|
+
const chunk = decodeCursor(cursor, depth)
|
|
784
802
|
if (typeof chunk !== 'string')
|
|
785
803
|
throw new InvalidIndefiniteLengthChunkError({ type: 'text string' })
|
|
786
804
|
chunks.push(chunk)
|
|
@@ -807,6 +825,7 @@ namespace decodeCursor {
|
|
|
807
825
|
export function readArray(
|
|
808
826
|
cursor: Cursor.Cursor,
|
|
809
827
|
additionalInfo: number,
|
|
828
|
+
depth = 0,
|
|
810
829
|
): unknown[] {
|
|
811
830
|
// Indefinite-length array
|
|
812
831
|
if (additionalInfo === 31) {
|
|
@@ -818,7 +837,7 @@ namespace decodeCursor {
|
|
|
818
837
|
cursor.readUint8() // consume the break byte
|
|
819
838
|
break
|
|
820
839
|
}
|
|
821
|
-
result.push(decodeCursor(cursor))
|
|
840
|
+
result.push(decodeCursor(cursor, depth))
|
|
822
841
|
}
|
|
823
842
|
|
|
824
843
|
return result
|
|
@@ -828,7 +847,7 @@ namespace decodeCursor {
|
|
|
828
847
|
const result: unknown[] = []
|
|
829
848
|
|
|
830
849
|
for (let i = 0; i < length; i++) {
|
|
831
|
-
result.push(decodeCursor(cursor))
|
|
850
|
+
result.push(decodeCursor(cursor, depth))
|
|
832
851
|
}
|
|
833
852
|
|
|
834
853
|
return result
|
|
@@ -843,6 +862,7 @@ namespace decodeCursor {
|
|
|
843
862
|
export function readMap(
|
|
844
863
|
cursor: Cursor.Cursor,
|
|
845
864
|
additionalInfo: number,
|
|
865
|
+
depth = 0,
|
|
846
866
|
): Record<string, unknown> {
|
|
847
867
|
// Indefinite-length map
|
|
848
868
|
if (additionalInfo === 31) {
|
|
@@ -854,7 +874,7 @@ namespace decodeCursor {
|
|
|
854
874
|
cursor.readUint8() // consume the break byte
|
|
855
875
|
break
|
|
856
876
|
}
|
|
857
|
-
const key = decodeCursor(cursor)
|
|
877
|
+
const key = decodeCursor(cursor, depth)
|
|
858
878
|
// Support both string and number keys (for COSE_Key with integer keys)
|
|
859
879
|
const keyStr =
|
|
860
880
|
typeof key === 'string'
|
|
@@ -862,7 +882,7 @@ namespace decodeCursor {
|
|
|
862
882
|
: typeof key === 'number'
|
|
863
883
|
? String(key)
|
|
864
884
|
: String(key)
|
|
865
|
-
const value = decodeCursor(cursor)
|
|
885
|
+
const value = decodeCursor(cursor, depth)
|
|
866
886
|
setEntry(result, keyStr, value)
|
|
867
887
|
}
|
|
868
888
|
|
|
@@ -873,7 +893,7 @@ namespace decodeCursor {
|
|
|
873
893
|
const result: Record<string, unknown> = {}
|
|
874
894
|
|
|
875
895
|
for (let i = 0; i < length; i++) {
|
|
876
|
-
const key = decodeCursor(cursor)
|
|
896
|
+
const key = decodeCursor(cursor, depth)
|
|
877
897
|
// Support both string and number keys (for COSE_Key with integer keys)
|
|
878
898
|
const keyStr =
|
|
879
899
|
typeof key === 'string'
|
|
@@ -881,7 +901,7 @@ namespace decodeCursor {
|
|
|
881
901
|
: typeof key === 'number'
|
|
882
902
|
? String(key)
|
|
883
903
|
: String(key)
|
|
884
|
-
const value = decodeCursor(cursor)
|
|
904
|
+
const value = decodeCursor(cursor, depth)
|
|
885
905
|
setEntry(result, keyStr, value)
|
|
886
906
|
}
|
|
887
907
|
|
package/core/Ens.ts
CHANGED
|
@@ -46,7 +46,7 @@ export declare namespace labelhash {
|
|
|
46
46
|
* ```ts twoslash
|
|
47
47
|
* import { Ens } from 'ox'
|
|
48
48
|
* Ens.namehash('wevm.eth')
|
|
49
|
-
* // @log: '
|
|
49
|
+
* // @log: '0x08c85f2f4059e930c45a6aeff9dcd3bd95dc3c5c1cddef6a0626b31152248560'
|
|
50
50
|
* ```
|
|
51
51
|
*
|
|
52
52
|
* @param name - ENS name.
|
package/core/FrameReceipt.ts
CHANGED
|
@@ -78,7 +78,9 @@ export function fromRpc(receipt: Rpc): FrameReceipt {
|
|
|
78
78
|
gasUsed: Hex.toBigInt(receipt.gasUsed),
|
|
79
79
|
logs: receipt.logs,
|
|
80
80
|
stateGasUsed: Hex.toBigInt(receipt.stateGasUsed),
|
|
81
|
-
status: fromRpcStatus
|
|
81
|
+
status: Object.hasOwn(fromRpcStatus, receipt.status)
|
|
82
|
+
? fromRpcStatus[receipt.status]
|
|
83
|
+
: (undefined as never),
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
|
|
@@ -114,7 +116,9 @@ export function toRpc(receipt: toRpc.Input): Rpc {
|
|
|
114
116
|
gasUsed: Quantity.fromNumberish(receipt.gasUsed),
|
|
115
117
|
logs: receipt.logs,
|
|
116
118
|
stateGasUsed: Quantity.fromNumberish(receipt.stateGasUsed),
|
|
117
|
-
status: toRpcStatus
|
|
119
|
+
status: Object.hasOwn(toRpcStatus, receipt.status)
|
|
120
|
+
? toRpcStatus[receipt.status]
|
|
121
|
+
: (undefined as never),
|
|
118
122
|
}
|
|
119
123
|
}
|
|
120
124
|
|
package/core/Hash.ts
CHANGED
|
@@ -26,7 +26,7 @@ import * as Hex from './Hex.js'
|
|
|
26
26
|
* import { Hash, Hex } from 'ox'
|
|
27
27
|
*
|
|
28
28
|
* Hash.keccak256(Hex.fromString('hello world'))
|
|
29
|
-
* // @log: '
|
|
29
|
+
* // @log: '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
|
|
30
30
|
* ```
|
|
31
31
|
*
|
|
32
32
|
* @example
|
package/core/Hex.ts
CHANGED
|
@@ -5,12 +5,17 @@ import * as internal_bytes from './internal/bytes.js'
|
|
|
5
5
|
import * as internal from './internal/hex.js'
|
|
6
6
|
import * as Json from './Json.js'
|
|
7
7
|
|
|
8
|
+
const decoder = /*#__PURE__*/ new TextDecoder()
|
|
8
9
|
const encoder = /*#__PURE__*/ new TextEncoder()
|
|
9
10
|
|
|
10
11
|
const hexes = /*#__PURE__*/ Array.from({ length: 256 }, (_v, i) =>
|
|
11
12
|
i.toString(16).padStart(2, '0'),
|
|
12
13
|
)
|
|
13
14
|
|
|
15
|
+
// Taken from the prototype once, and called on the value, so that an instance
|
|
16
|
+
// or subclass defining its own `toHex` can't change the result.
|
|
17
|
+
const nativeToHex = (Uint8Array.prototype as { toHex?: () => string }).toHex
|
|
18
|
+
|
|
14
19
|
/** Root type for a Hex string. */
|
|
15
20
|
export type Hex = `0x${string}`
|
|
16
21
|
|
|
@@ -80,7 +85,14 @@ export declare namespace assert {
|
|
|
80
85
|
* @returns The concatenated {@link ox#Hex.Hex} value.
|
|
81
86
|
*/
|
|
82
87
|
export function concat(...values: readonly Hex[]): Hex {
|
|
83
|
-
|
|
88
|
+
let hex = '0x'
|
|
89
|
+
for (let i = 0; i < values.length; i++) {
|
|
90
|
+
const value = values[i]!
|
|
91
|
+
// Same result as `value.replace('0x', '')`, without a search when the
|
|
92
|
+
// prefix is present.
|
|
93
|
+
hex += value.startsWith('0x') ? value.slice(2) : value.replace('0x', '')
|
|
94
|
+
}
|
|
95
|
+
return hex as Hex
|
|
84
96
|
}
|
|
85
97
|
|
|
86
98
|
export declare namespace concat {
|
|
@@ -196,7 +208,13 @@ export function fromBytes(
|
|
|
196
208
|
options: fromBytes.Options = {},
|
|
197
209
|
): Hex {
|
|
198
210
|
let string = ''
|
|
199
|
-
|
|
211
|
+
// Native `Uint8Array.prototype.toHex` is several times faster, and returns a
|
|
212
|
+
// flat string rather than a rope of concatenated pairs.
|
|
213
|
+
// Empty values (including detached or out-of-bounds views, whose length is
|
|
214
|
+
// 0) skip it, since it throws on those.
|
|
215
|
+
if (typeof nativeToHex === 'function' && value instanceof Uint8Array) {
|
|
216
|
+
if (value.length > 0) string = nativeToHex.call(value)
|
|
217
|
+
} else for (let i = 0; i < value.length; i++) string += hexes[value[i]!]
|
|
200
218
|
const hex = `0x${string}` as const
|
|
201
219
|
|
|
202
220
|
if (typeof options.size === 'number') {
|
|
@@ -242,6 +260,37 @@ export function fromNumber(
|
|
|
242
260
|
): Hex {
|
|
243
261
|
const { signed, size } = options
|
|
244
262
|
|
|
263
|
+
// A non-negative `bigint` that fits `size` skips the range check too.
|
|
264
|
+
if (typeof value === 'bigint' && !signed && value >= 0n) {
|
|
265
|
+
const hex = value.toString(16)
|
|
266
|
+
if (!size) return `0x${hex}`
|
|
267
|
+
if (Number.isSafeInteger(size) && size > 0 && hex.length <= size * 2)
|
|
268
|
+
return `0x${hex.padStart(size * 2, '0')}`
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// An unsigned safe integer is in range when there is no `size` or the size
|
|
272
|
+
// exceeds 6 bytes, so it skips the `bigint` range check.
|
|
273
|
+
if (
|
|
274
|
+
typeof value === 'number' &&
|
|
275
|
+
!signed &&
|
|
276
|
+
Number.isSafeInteger(value) &&
|
|
277
|
+
value >= 0 &&
|
|
278
|
+
(!size || size > 6)
|
|
279
|
+
) {
|
|
280
|
+
const hex = `0x${value.toString(16)}` as Hex
|
|
281
|
+
return size ? padLeft(hex, size) : hex
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return fromNumberChecked(value, signed, size)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// The range-checked path of `fromNumber`, kept separate so that `fromNumber`
|
|
288
|
+
// stays small enough to inline.
|
|
289
|
+
function fromNumberChecked(
|
|
290
|
+
value: number | bigint,
|
|
291
|
+
signed: boolean | undefined,
|
|
292
|
+
size: number | undefined,
|
|
293
|
+
): Hex {
|
|
245
294
|
const value_ = BigInt(value)
|
|
246
295
|
|
|
247
296
|
let maxValue: bigint | number | undefined
|
|
@@ -268,7 +317,7 @@ export function fromNumber(
|
|
|
268
317
|
}
|
|
269
318
|
|
|
270
319
|
const stringValue = (
|
|
271
|
-
signed && value_ < 0 ? BigInt.asUintN(size * 8, BigInt(value_)) : value_
|
|
320
|
+
signed && value_ < 0 ? BigInt.asUintN(size! * 8, BigInt(value_)) : value_
|
|
272
321
|
).toString(16)
|
|
273
322
|
|
|
274
323
|
const hex = `0x${stringValue}` as Hex
|
|
@@ -455,9 +504,9 @@ export function slice(
|
|
|
455
504
|
): Hex {
|
|
456
505
|
const { strict } = options
|
|
457
506
|
internal.assertStartOffset(value, start)
|
|
458
|
-
const value_ = `0x${
|
|
459
|
-
.replace('0x', '')
|
|
460
|
-
|
|
507
|
+
const value_ = `0x${(
|
|
508
|
+
value.startsWith('0x') ? value.slice(2) : value.replace('0x', '')
|
|
509
|
+
).slice((start ?? 0) * 2, (end ?? value.length) * 2)}` as const
|
|
461
510
|
if (strict) internal.assertEndOffset(value_, start, end)
|
|
462
511
|
return value_
|
|
463
512
|
}
|
|
@@ -569,12 +618,7 @@ export function toBigInt(hex: Hex, options: toBigInt.Options = {}): bigint {
|
|
|
569
618
|
|
|
570
619
|
const value = BigInt(hex)
|
|
571
620
|
if (!signed) return value
|
|
572
|
-
|
|
573
|
-
const max_unsigned = (1n << (BigInt(size(hex)) * 8n)) - 1n
|
|
574
|
-
const max_signed = max_unsigned >> 1n
|
|
575
|
-
|
|
576
|
-
if (value <= max_signed) return value
|
|
577
|
-
return value - max_unsigned - 1n
|
|
621
|
+
return BigInt.asIntN(size(hex) * 8, value)
|
|
578
622
|
}
|
|
579
623
|
|
|
580
624
|
export declare namespace toBigInt {
|
|
@@ -658,6 +702,8 @@ export declare namespace toBytes {
|
|
|
658
702
|
/**
|
|
659
703
|
* Decodes a {@link ox#Hex.Hex} value into a number.
|
|
660
704
|
*
|
|
705
|
+
* Throws `Hex.IntegerOutOfRangeError` when the value is not a safe integer. Use `toBigInt` for larger values.
|
|
706
|
+
*
|
|
661
707
|
* @example
|
|
662
708
|
* ```ts twoslash
|
|
663
709
|
* import { Hex } from 'ox'
|
|
@@ -675,14 +721,25 @@ export declare namespace toBytes {
|
|
|
675
721
|
*/
|
|
676
722
|
export function toNumber(hex: Hex, options: toNumber.Options = {}): number {
|
|
677
723
|
const { signed, size } = options
|
|
678
|
-
|
|
679
|
-
|
|
724
|
+
const value = !signed && !size ? Number(hex) : Number(toBigInt(hex, options))
|
|
725
|
+
if (!Number.isSafeInteger(value))
|
|
726
|
+
throw new IntegerOutOfRangeError({
|
|
727
|
+
max: `${Number.MAX_SAFE_INTEGER}`,
|
|
728
|
+
min: signed ? `${Number.MIN_SAFE_INTEGER}` : '0',
|
|
729
|
+
signed,
|
|
730
|
+
size,
|
|
731
|
+
value: `${value}`,
|
|
732
|
+
})
|
|
733
|
+
return value
|
|
680
734
|
}
|
|
681
735
|
|
|
682
736
|
export declare namespace toNumber {
|
|
683
737
|
type Options = toBigInt.Options
|
|
684
738
|
|
|
685
|
-
type ErrorType =
|
|
739
|
+
type ErrorType =
|
|
740
|
+
| toBigInt.ErrorType
|
|
741
|
+
| IntegerOutOfRangeError
|
|
742
|
+
| Errors.GlobalErrorType
|
|
686
743
|
}
|
|
687
744
|
|
|
688
745
|
/**
|
|
@@ -713,7 +770,7 @@ export function toString(hex: Hex, options: toString.Options = {}): string {
|
|
|
713
770
|
internal_bytes.assertSize(bytes, size)
|
|
714
771
|
bytes = Bytes.trimRight(bytes)
|
|
715
772
|
}
|
|
716
|
-
return
|
|
773
|
+
return decoder.decode(bytes)
|
|
717
774
|
}
|
|
718
775
|
|
|
719
776
|
export declare namespace toString {
|
|
@@ -752,12 +809,12 @@ export function validate(
|
|
|
752
809
|
options: validate.Options = {},
|
|
753
810
|
): value is Hex {
|
|
754
811
|
const { even = false, strict = false } = options
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
812
|
+
// Mirrors `assert` without constructing and catching an error.
|
|
813
|
+
if (!value || typeof value !== 'string') return false
|
|
814
|
+
if (strict && !hexRegex.test(value)) return false
|
|
815
|
+
if (!value.startsWith('0x')) return false
|
|
816
|
+
if (even && value.length % 2 !== 0) return false
|
|
817
|
+
return true
|
|
761
818
|
}
|
|
762
819
|
|
|
763
820
|
export declare namespace validate {
|
package/core/Json.ts
CHANGED
|
@@ -2,6 +2,9 @@ import type * as Errors from './Errors.js'
|
|
|
2
2
|
|
|
3
3
|
const bigIntSuffix = '#__bigint'
|
|
4
4
|
|
|
5
|
+
/** Text `BigInt` accepts once surrounding whitespace is trimmed. */
|
|
6
|
+
const bigIntPattern = /^(?:[+-]?\d+|0[bB][01]+|0[oO][0-7]+|0[xX][\da-fA-F]+)?$/
|
|
7
|
+
|
|
5
8
|
/**
|
|
6
9
|
* Serializes a value to a canonical JSON string as defined by
|
|
7
10
|
* [RFC 8785 (JSON Canonicalization Scheme)](https://www.rfc-editor.org/rfc/rfc8785).
|
|
@@ -81,10 +84,28 @@ export function parse(
|
|
|
81
84
|
string: string,
|
|
82
85
|
reviver?: ((this: any, key: string, value: any) => any) | undefined,
|
|
83
86
|
) {
|
|
87
|
+
// Without a reviver, text that cannot hold the bigint marker (neither
|
|
88
|
+
// literally nor through a `\u` escape) parses without the reviver wrapper.
|
|
89
|
+
if (
|
|
90
|
+
typeof reviver !== 'function' &&
|
|
91
|
+
typeof string === 'string' &&
|
|
92
|
+
!string.includes(bigIntSuffix) &&
|
|
93
|
+
!string.includes('\\u')
|
|
94
|
+
)
|
|
95
|
+
return JSON.parse(string)
|
|
84
96
|
return JSON.parse(string, (key, value_) => {
|
|
85
97
|
const value = value_
|
|
86
|
-
if (typeof value === 'string' && value.endsWith(bigIntSuffix))
|
|
87
|
-
|
|
98
|
+
if (typeof value === 'string' && value.endsWith(bigIntSuffix)) {
|
|
99
|
+
// Check the text before calling `BigInt`, so that a string that is not
|
|
100
|
+
// a bigint (e.g. untrusted text) is kept without throwing and catching.
|
|
101
|
+
const text = value.slice(0, -bigIntSuffix.length).trim()
|
|
102
|
+
if (bigIntPattern.test(text))
|
|
103
|
+
try {
|
|
104
|
+
return BigInt(text)
|
|
105
|
+
} catch {
|
|
106
|
+
// Too large for a bigint, so keep the string.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
88
109
|
return typeof reviver === 'function' ? reviver(key, value) : value
|
|
89
110
|
})
|
|
90
111
|
}
|