ox 1.8.16 → 1.8.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +42 -0
- package/dist/core/AbiEvent.d.ts +1 -1
- package/dist/core/AbiEvent.d.ts.map +1 -1
- package/dist/core/AbiEvent.js +27 -22
- package/dist/core/AbiEvent.js.map +1 -1
- package/dist/core/AbiItem.d.ts.map +1 -1
- package/dist/core/AbiItem.js +15 -5
- package/dist/core/AbiItem.js.map +1 -1
- package/dist/core/AbiParameters.d.ts +1 -1
- package/dist/core/AbiParameters.d.ts.map +1 -1
- package/dist/core/AbiParameters.js +8 -2
- package/dist/core/AbiParameters.js.map +1 -1
- package/dist/core/Base58.d.ts.map +1 -1
- package/dist/core/Base58.js +16 -4
- package/dist/core/Base58.js.map +1 -1
- package/dist/core/Blobs.d.ts +2 -3
- package/dist/core/Blobs.d.ts.map +1 -1
- package/dist/core/Blobs.js +38 -43
- package/dist/core/Blobs.js.map +1 -1
- package/dist/core/Bytes.d.ts.map +1 -1
- package/dist/core/Bytes.js +10 -2
- package/dist/core/Bytes.js.map +1 -1
- package/dist/core/Cbor.d.ts +13 -6
- package/dist/core/Cbor.d.ts.map +1 -1
- package/dist/core/Cbor.js +31 -18
- package/dist/core/Cbor.js.map +1 -1
- package/dist/core/Ens.js +2 -2
- package/dist/core/Ens.js.map +1 -1
- package/dist/core/FrameReceipt.d.ts.map +1 -1
- package/dist/core/FrameReceipt.js +4 -1
- package/dist/core/FrameReceipt.js.map +1 -1
- package/dist/core/Hex.d.ts.map +1 -1
- package/dist/core/Hex.js +34 -31
- package/dist/core/Hex.js.map +1 -1
- package/dist/core/Rlp.d.ts +1 -1
- package/dist/core/Rlp.d.ts.map +1 -1
- package/dist/core/Rlp.js +107 -0
- package/dist/core/Rlp.js.map +1 -1
- package/dist/core/RpcTransport.d.ts.map +1 -1
- package/dist/core/RpcTransport.js +3 -1
- package/dist/core/RpcTransport.js.map +1 -1
- package/dist/core/Transaction.d.ts.map +1 -1
- package/dist/core/Transaction.js +4 -2
- package/dist/core/Transaction.js.map +1 -1
- package/dist/core/TransactionReceipt.d.ts.map +1 -1
- package/dist/core/TransactionReceipt.js +8 -2
- package/dist/core/TransactionReceipt.js.map +1 -1
- package/dist/core/TypedData.d.ts.map +1 -1
- package/dist/core/TypedData.js +14 -2
- package/dist/core/TypedData.js.map +1 -1
- package/dist/core/internal/abiParameters.d.ts +3 -3
- package/dist/core/internal/abiParameters.d.ts.map +1 -1
- package/dist/core/internal/abiParameters.js +93 -68
- package/dist/core/internal/abiParameters.js.map +1 -1
- package/dist/core/internal/base58.d.ts.map +1 -1
- package/dist/core/internal/base58.js +9 -3
- package/dist/core/internal/base58.js.map +1 -1
- package/dist/core/internal/caches.d.ts +6 -0
- package/dist/core/internal/caches.d.ts.map +1 -1
- package/dist/core/internal/caches.js +3 -1
- package/dist/core/internal/caches.js.map +1 -1
- package/dist/core/internal/codec/hex.d.ts +13 -0
- package/dist/core/internal/codec/hex.d.ts.map +1 -1
- package/dist/core/internal/codec/hex.js +25 -2
- package/dist/core/internal/codec/hex.js.map +1 -1
- package/dist/core/internal/codec/int.d.ts.map +1 -1
- package/dist/core/internal/codec/int.js +48 -32
- package/dist/core/internal/codec/int.js.map +1 -1
- package/dist/core/internal/cursor.d.ts +11 -4
- package/dist/core/internal/cursor.d.ts.map +1 -1
- package/dist/core/internal/cursor.js +39 -15
- package/dist/core/internal/cursor.js.map +1 -1
- package/dist/core/internal/lru.d.ts +11 -12
- package/dist/core/internal/lru.d.ts.map +1 -1
- package/dist/core/internal/lru.js +22 -32
- package/dist/core/internal/lru.js.map +1 -1
- package/dist/core/internal/promise.d.ts.map +1 -1
- package/dist/core/internal/promise.js +13 -7
- package/dist/core/internal/promise.js.map +1 -1
- package/dist/erc6492/SignatureErc6492.d.ts +1 -1
- package/dist/erc6492/SignatureErc6492.d.ts.map +1 -1
- package/dist/erc6492/SignatureErc6492.js +1 -1
- package/dist/erc6492/SignatureErc6492.js.map +1 -1
- package/dist/tempo/SignatureEnvelope.d.ts.map +1 -1
- package/dist/tempo/SignatureEnvelope.js +7 -41
- package/dist/tempo/SignatureEnvelope.js.map +1 -1
- package/dist/tempo/TransactionReceipt.d.ts.map +1 -1
- package/dist/tempo/TransactionReceipt.js +4 -1
- package/dist/tempo/TransactionReceipt.js.map +1 -1
- package/dist/tempo/ZoneId.d.ts +3 -1
- package/dist/tempo/ZoneId.d.ts.map +1 -1
- package/dist/tempo/ZoneId.js +7 -1
- package/dist/tempo/ZoneId.js.map +1 -1
- package/dist/tempo/internal/virtualMasterPool.d.ts.map +1 -1
- package/dist/tempo/internal/virtualMasterPool.js +17 -6
- package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
- package/dist/tempo/internal/webauthnData.d.ts +23 -0
- package/dist/tempo/internal/webauthnData.d.ts.map +1 -0
- package/dist/tempo/internal/webauthnData.js +75 -0
- package/dist/tempo/internal/webauthnData.js.map +1 -0
- package/dist/zod/TypedData.d.ts +4 -4
- package/dist/zod/tempo/SignatureEnvelope.d.ts.map +1 -1
- package/dist/zod/tempo/SignatureEnvelope.js +5 -15
- package/dist/zod/tempo/SignatureEnvelope.js.map +1 -1
- package/package.json +2 -2
- package/src/core/AbiEvent.bench.ts +45 -0
- package/src/core/AbiEvent.ts +30 -23
- package/src/core/AbiFunction.bench.ts +16 -0
- package/src/core/AbiItem.ts +13 -4
- package/src/core/AbiParameters.bench.ts +33 -0
- package/src/core/AbiParameters.ts +8 -1
- package/src/core/Base58.bench.ts +28 -0
- package/src/core/Base58.ts +15 -4
- package/src/core/Blobs.bench.ts +40 -0
- package/src/core/Blobs.ts +47 -55
- package/src/core/Bytes.bench.ts +34 -0
- package/src/core/Bytes.ts +10 -2
- package/src/core/Cbor.ts +36 -14
- package/src/core/Ens.ts +2 -2
- package/src/core/FrameReceipt.ts +4 -1
- package/src/core/Hex.bench.ts +17 -0
- package/src/core/Hex.ts +29 -32
- package/src/core/Rlp.ts +139 -1
- package/src/core/Rlp_tx.bench.ts +55 -0
- package/src/core/RpcTransport.ts +3 -1
- package/src/core/Transaction.ts +4 -2
- package/src/core/TransactionReceipt.ts +8 -2
- package/src/core/TypedData.bench.ts +50 -0
- package/src/core/TypedData.ts +14 -2
- package/src/core/_test/AbiEvent.test.ts +83 -0
- package/src/core/_test/AbiFunction.test.ts +7 -6
- package/src/core/_test/AbiParameters.decode.test.ts +34 -1
- package/src/core/_test/AbiParameters.encode.test.ts +10 -0
- package/src/core/_test/AbiParameters.test.ts +15 -0
- package/src/core/_test/Bytes.test.ts +33 -0
- package/src/core/_test/Caches.test.ts +20 -1
- package/src/core/_test/Cbor.test.ts +26 -0
- package/src/core/_test/FrameReceipt.test.ts +13 -0
- package/src/core/_test/Rlp.fuzz.ts +55 -1
- package/src/core/_test/Rlp.test.ts +64 -0
- package/src/core/_test/RpcTransport.test.ts +15 -2
- package/src/core/_test/Transaction.test.ts +17 -0
- package/src/core/_test/TransactionReceipt.test.ts +41 -0
- package/src/core/_test/TypedData.test.ts +19 -0
- package/src/core/_test/internal/cursor.test.ts +42 -6
- package/src/core/internal/abiParameters.ts +117 -80
- package/src/core/internal/base58.ts +9 -3
- package/src/core/internal/caches.ts +3 -1
- package/src/core/internal/codec/hex.ts +27 -2
- package/src/core/internal/codec/int.ts +45 -32
- package/src/core/internal/cursor.ts +52 -23
- package/src/core/internal/lru.ts +21 -35
- package/src/core/internal/promise.ts +11 -5
- package/src/erc6492/SignatureErc6492.ts +1 -1
- package/src/erc6492/_test/SignatureErc6492.test.ts +48 -1
- package/src/tempo/SignatureEnvelope.test.ts +89 -1
- package/src/tempo/SignatureEnvelope.ts +7 -43
- package/src/tempo/TransactionReceipt.test.ts +8 -0
- package/src/tempo/TransactionReceipt.ts +4 -1
- package/src/tempo/VirtualMaster.bench.ts +7 -0
- package/src/tempo/ZoneId.test.ts +11 -0
- package/src/tempo/ZoneId.ts +7 -1
- package/src/tempo/internal/virtualMasterPool.ts +20 -6
- package/src/tempo/internal/webauthnData.ts +84 -0
- package/src/version.ts +1 -1
- package/src/zod/tempo/SignatureEnvelope.ts +7 -16
- package/src/zod/tempo/_test/SignatureEnvelope.test.ts +37 -1
- package/src/core/_test/internal/lru.test.ts +0 -38
- package/src/core/_test/internal/uid.test.ts +0 -7
- package/src/core/internal/uid.ts +0 -15
|
@@ -4,6 +4,47 @@ import { describe, expect, test } from 'vp/test'
|
|
|
4
4
|
import { anvilMainnet } from '../../../test/prool.js'
|
|
5
5
|
|
|
6
6
|
describe('fromRpc', () => {
|
|
7
|
+
test('behavior: unknown status and type named like `Object.prototype` keys', () => {
|
|
8
|
+
const { frameReceipts, status, type } = TransactionReceipt.fromRpc({
|
|
9
|
+
blockHash:
|
|
10
|
+
'0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b',
|
|
11
|
+
blockNumber: '0x12f296f',
|
|
12
|
+
contractAddress: null,
|
|
13
|
+
cumulativeGasUsed: '0x82515',
|
|
14
|
+
effectiveGasPrice: '0x21c2f6c09',
|
|
15
|
+
frameReceipts: [
|
|
16
|
+
{
|
|
17
|
+
executionGasUsed: '0x0',
|
|
18
|
+
gasUsed: '0x0',
|
|
19
|
+
logs: [],
|
|
20
|
+
stateGasUsed: '0x0',
|
|
21
|
+
status: 'constructor',
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6',
|
|
25
|
+
gasUsed: '0x2abba',
|
|
26
|
+
logs: [],
|
|
27
|
+
logsBloom: '0x',
|
|
28
|
+
status: '__proto__',
|
|
29
|
+
to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad',
|
|
30
|
+
transactionHash:
|
|
31
|
+
'0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0',
|
|
32
|
+
transactionIndex: '0x2',
|
|
33
|
+
type: 'toString',
|
|
34
|
+
} as unknown as TransactionReceipt.Rpc)
|
|
35
|
+
expect({
|
|
36
|
+
frameStatus: frameReceipts?.[0]?.status,
|
|
37
|
+
status,
|
|
38
|
+
type,
|
|
39
|
+
}).toMatchInlineSnapshot(`
|
|
40
|
+
{
|
|
41
|
+
"frameStatus": undefined,
|
|
42
|
+
"status": undefined,
|
|
43
|
+
"type": "toString",
|
|
44
|
+
}
|
|
45
|
+
`)
|
|
46
|
+
})
|
|
47
|
+
|
|
7
48
|
test('default', () => {
|
|
8
49
|
const receipt = TransactionReceipt.fromRpc({
|
|
9
50
|
blobGasPrice: '0x42069',
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Hex, TypedData } from 'ox'
|
|
2
2
|
import { describe, expect, test } from 'vp/test'
|
|
3
3
|
import * as typedData from '../../../test/constants/typedData.js'
|
|
4
|
+
import { caches } from '../internal/caches.js'
|
|
4
5
|
|
|
5
6
|
describe('assert', () => {
|
|
6
7
|
test('default', () => {
|
|
@@ -1486,6 +1487,24 @@ describe('hashStruct', () => {
|
|
|
1486
1487
|
}),
|
|
1487
1488
|
).toBe(hash)
|
|
1488
1489
|
})
|
|
1490
|
+
|
|
1491
|
+
test('behavior: long encoded types are not kept in the global cache', () => {
|
|
1492
|
+
caches.typeHash.clear()
|
|
1493
|
+
const fields = Array.from({ length: 300 }, (_, i) => ({
|
|
1494
|
+
name: 'field' + i,
|
|
1495
|
+
type: 'uint256',
|
|
1496
|
+
}))
|
|
1497
|
+
const data = Object.fromEntries(fields.map(({ name }) => [name, 1n]))
|
|
1498
|
+
TypedData.hashStruct({ data, primaryType: 'Long', types: { Long: fields } })
|
|
1499
|
+
expect(caches.typeHash.size).toBe(0)
|
|
1500
|
+
|
|
1501
|
+
TypedData.hashStruct({
|
|
1502
|
+
...typedData.basic,
|
|
1503
|
+
primaryType: 'Mail',
|
|
1504
|
+
data: typedData.basic.message,
|
|
1505
|
+
})
|
|
1506
|
+
expect(caches.typeHash.size).toBeGreaterThan(0)
|
|
1507
|
+
})
|
|
1489
1508
|
})
|
|
1490
1509
|
|
|
1491
1510
|
describe('serialize', () => {
|
|
@@ -614,7 +614,7 @@ test('remaining', () => {
|
|
|
614
614
|
describe('readSizeLimit', () => {
|
|
615
615
|
test('default', () => {
|
|
616
616
|
const cursor = Cursor.create(new Uint8Array(generateBytes(420)))
|
|
617
|
-
expect(cursor.readSizeLimit).toMatchInlineSnapshot(`
|
|
617
|
+
expect(cursor.readSizeLimit).toMatchInlineSnapshot(`262984`)
|
|
618
618
|
|
|
619
619
|
cursor.readBytes(420)
|
|
620
620
|
cursor.setPosition(0)
|
|
@@ -626,15 +626,15 @@ describe('readSizeLimit', () => {
|
|
|
626
626
|
const cursor = Cursor.create(new Uint8Array(generateBytes(420)), {
|
|
627
627
|
recursiveReadLimit: 2,
|
|
628
628
|
})
|
|
629
|
-
expect(cursor.readSizeLimit).toMatchInlineSnapshot(`
|
|
629
|
+
expect(cursor.readSizeLimit).toMatchInlineSnapshot(`904`)
|
|
630
630
|
|
|
631
|
-
for (let i = 0; i <
|
|
631
|
+
for (let i = 0; i < 2; i++) {
|
|
632
632
|
cursor.setPosition(i)
|
|
633
633
|
cursor.readBytes(420 - i)
|
|
634
634
|
}
|
|
635
|
-
cursor.setPosition(
|
|
636
|
-
expect(() => cursor.readBytes(
|
|
637
|
-
`[Cursor.ReadSizeLimitExceededError: Read size limit of \`
|
|
635
|
+
cursor.setPosition(2)
|
|
636
|
+
expect(() => cursor.readBytes(418)).toThrowErrorMatchingInlineSnapshot(
|
|
637
|
+
`[Cursor.ReadSizeLimitExceededError: Read size limit of \`904\` bytes exceeded (read size: \`1257\` bytes).]`,
|
|
638
638
|
)
|
|
639
639
|
})
|
|
640
640
|
|
|
@@ -681,4 +681,40 @@ describe('args: recursiveReadLimit', () => {
|
|
|
681
681
|
'[Cursor.RecursiveReadLimitExceededError: Recursive read limit of `2` exceeded (recursive read count: `3`).]',
|
|
682
682
|
)
|
|
683
683
|
})
|
|
684
|
+
|
|
685
|
+
test('behavior: repeated reads at an unaligned position', () => {
|
|
686
|
+
const cursor = Cursor.create(new Uint8Array(generateBytes(420)), {
|
|
687
|
+
recursiveReadLimit: 2,
|
|
688
|
+
})
|
|
689
|
+
|
|
690
|
+
for (let i = 0; i < 3; i++) {
|
|
691
|
+
cursor.setPosition(37)
|
|
692
|
+
cursor.readBytes(10)
|
|
693
|
+
}
|
|
694
|
+
cursor.setPosition(37)
|
|
695
|
+
expect(() => cursor.readBytes(10)).toThrowErrorMatchingInlineSnapshot(
|
|
696
|
+
`[Cursor.RecursiveReadLimitExceededError: Recursive read limit of \`2\` exceeded (recursive read count: \`3\`).]`,
|
|
697
|
+
)
|
|
698
|
+
})
|
|
699
|
+
|
|
700
|
+
test('behavior: repeated reads past the length the data had at creation', () => {
|
|
701
|
+
const buffer = new (ArrayBuffer as any)(32, { maxByteLength: 4096 })
|
|
702
|
+
const bytes = new Uint8Array(buffer)
|
|
703
|
+
const cursor = Cursor.create(bytes, { recursiveReadLimit: 2 })
|
|
704
|
+
|
|
705
|
+
cursor.readBytes(32)
|
|
706
|
+
buffer.resize(4096)
|
|
707
|
+
for (let i = 0; i < 3; i++) {
|
|
708
|
+
cursor.setPosition(2048)
|
|
709
|
+
cursor.readBytes(32)
|
|
710
|
+
}
|
|
711
|
+
cursor.setPosition(2048)
|
|
712
|
+
expect(() => cursor.readBytes(32)).toThrowError(
|
|
713
|
+
'Recursive read limit of ' +
|
|
714
|
+
String.fromCharCode(96) +
|
|
715
|
+
'2' +
|
|
716
|
+
String.fromCharCode(96) +
|
|
717
|
+
' exceeded',
|
|
718
|
+
)
|
|
719
|
+
})
|
|
684
720
|
})
|
|
@@ -10,6 +10,7 @@ import * as Bytes from '../Bytes.js'
|
|
|
10
10
|
import * as Errors from '../Errors.js'
|
|
11
11
|
import * as Hex from '../Hex.js'
|
|
12
12
|
import { bytesRegex, integerRegex } from '../Solidity.js'
|
|
13
|
+
import { nibbleTable } from './codec/hex.js'
|
|
13
14
|
import { encoder } from './codec/utf8.js'
|
|
14
15
|
import type * as Cursor from './cursor.js'
|
|
15
16
|
import type { Compute, IsNarrowable, UnionToIntersection } from './types.js'
|
|
@@ -65,14 +66,8 @@ type MeasureContext = {
|
|
|
65
66
|
checksumAddress: boolean
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
type HexPatch = {
|
|
69
|
-
offset: number
|
|
70
|
-
value: string
|
|
71
|
-
}
|
|
72
|
-
|
|
73
69
|
type WriteContext = {
|
|
74
70
|
bytes: Bytes.Bytes
|
|
75
|
-
patches: HexPatch[]
|
|
76
71
|
view: DataView
|
|
77
72
|
}
|
|
78
73
|
|
|
@@ -188,14 +183,14 @@ export function decodeAddress(
|
|
|
188
183
|
})
|
|
189
184
|
const wrap = (address: Hex.Hex) =>
|
|
190
185
|
checksum ? Address.checksum(address) : address
|
|
191
|
-
|
|
186
|
+
// A view, not a copy: it is hex-encoded at once and never escapes.
|
|
187
|
+
return [wrap(Hex.fromBytes(value.subarray(12))), 32]
|
|
192
188
|
}
|
|
193
189
|
|
|
194
190
|
export declare namespace decodeAddress {
|
|
195
191
|
type ErrorType =
|
|
196
192
|
| Address.InvalidAddressError
|
|
197
193
|
| Hex.fromBytes.ErrorType
|
|
198
|
-
| Bytes.slice.ErrorType
|
|
199
194
|
| Errors.GlobalErrorType
|
|
200
195
|
}
|
|
201
196
|
|
|
@@ -228,6 +223,7 @@ export function decodeArray(
|
|
|
228
223
|
|
|
229
224
|
// Check if the array has any dynamic children.
|
|
230
225
|
const dynamicChild = hasDynamicChild(param)
|
|
226
|
+
const decode = getDecoder(param, checksumAddress)
|
|
231
227
|
|
|
232
228
|
let consumed = 0
|
|
233
229
|
const value: unknown[] = []
|
|
@@ -235,10 +231,7 @@ export function decodeArray(
|
|
|
235
231
|
// If any of the children is dynamic, then all elements will be offset pointer, thus size of one slot (32 bytes).
|
|
236
232
|
// Otherwise, elements will be the size of their encoding (consumed bytes).
|
|
237
233
|
cursor.setPosition(startOfData + (dynamicChild ? i * 32 : consumed))
|
|
238
|
-
const [data, consumed_] =
|
|
239
|
-
checksumAddress,
|
|
240
|
-
staticPosition: startOfData,
|
|
241
|
-
})
|
|
234
|
+
const [data, consumed_] = decode(cursor, startOfData)
|
|
242
235
|
consumed += consumed_
|
|
243
236
|
value.push(data)
|
|
244
237
|
// Charge zero-width elements against the read limit to bound work
|
|
@@ -264,14 +257,12 @@ export function decodeArray(
|
|
|
264
257
|
// Start is the static position of current slot + offset.
|
|
265
258
|
const start = staticPosition + offset
|
|
266
259
|
|
|
260
|
+
const decode = getDecoder(param, checksumAddress)
|
|
267
261
|
const value: unknown[] = []
|
|
268
262
|
for (let i = 0; i < length; ++i) {
|
|
269
263
|
// Move cursor along to the next slot (next offset pointer).
|
|
270
264
|
cursor.setPosition(start + i * 32)
|
|
271
|
-
const [data] =
|
|
272
|
-
checksumAddress,
|
|
273
|
-
staticPosition: start,
|
|
274
|
-
})
|
|
265
|
+
const [data] = decode(cursor, start)
|
|
275
266
|
value.push(data)
|
|
276
267
|
}
|
|
277
268
|
|
|
@@ -285,13 +276,11 @@ export function decodeArray(
|
|
|
285
276
|
|
|
286
277
|
// If the length of the array is known in advance and the array is deeply static,
|
|
287
278
|
// then we can just decode each element in sequence.
|
|
279
|
+
const decode = getDecoder(param, checksumAddress)
|
|
288
280
|
let consumed = 0
|
|
289
281
|
const value: unknown[] = []
|
|
290
282
|
for (let i = 0; i < length; ++i) {
|
|
291
|
-
const [data, consumed_] =
|
|
292
|
-
checksumAddress,
|
|
293
|
-
staticPosition: staticPosition + consumed,
|
|
294
|
-
})
|
|
283
|
+
const [data, consumed_] = decode(cursor, staticPosition + consumed)
|
|
295
284
|
consumed += consumed_
|
|
296
285
|
value.push(data)
|
|
297
286
|
// Charge zero-width elements against the read limit to bound work
|
|
@@ -349,11 +338,21 @@ export function decodeBytes(
|
|
|
349
338
|
|
|
350
339
|
// Read the whole slot so that a slot cut short of its padding is out of bounds.
|
|
351
340
|
const word = cursor.readBytes(32)
|
|
352
|
-
|
|
341
|
+
const size_ = Number.parseInt(size, 10)
|
|
342
|
+
// The value occupies the high `size` bytes of the word; the rest must be
|
|
343
|
+
// zero padding.
|
|
344
|
+
for (let i = size_; i < 32; i++)
|
|
345
|
+
if (word[i] !== 0)
|
|
346
|
+
throw new AbiParameters.BytesSizeMismatchError({
|
|
347
|
+
expectedSize: size_,
|
|
348
|
+
value: Hex.fromBytes(word),
|
|
349
|
+
})
|
|
350
|
+
return [Hex.fromBytes(word.subarray(0, size_)), 32]
|
|
353
351
|
}
|
|
354
352
|
|
|
355
353
|
export declare namespace decodeBytes {
|
|
356
354
|
type ErrorType =
|
|
355
|
+
| AbiParameters.BytesSizeMismatchError
|
|
357
356
|
| Hex.fromBytes.ErrorType
|
|
358
357
|
| Bytes.toNumber.ErrorType
|
|
359
358
|
| Errors.GlobalErrorType
|
|
@@ -364,24 +363,7 @@ export function decodeNumber(
|
|
|
364
363
|
cursor: Cursor.Cursor,
|
|
365
364
|
param: AbiParameters.Parameter,
|
|
366
365
|
) {
|
|
367
|
-
|
|
368
|
-
const size = Number.parseInt(param.type.split('int')[1] || '256', 10)
|
|
369
|
-
const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
|
|
370
|
-
// The word must hold a value that fits the declared size (zero-padded for
|
|
371
|
-
// unsigned, sign-extended for signed types).
|
|
372
|
-
if (size < 256) {
|
|
373
|
-
const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
|
|
374
|
-
const min = signed ? -max - 1n : 0n
|
|
375
|
-
if (value > max || value < min)
|
|
376
|
-
throw new Hex.IntegerOutOfRangeError({
|
|
377
|
-
max: max.toString(),
|
|
378
|
-
min: min.toString(),
|
|
379
|
-
signed,
|
|
380
|
-
size: size / 8,
|
|
381
|
-
value: value.toString(),
|
|
382
|
-
})
|
|
383
|
-
}
|
|
384
|
-
return [size > 48 ? value : Number(value), 32]
|
|
366
|
+
return decodeInteger(cursor, getInteger(param.type))
|
|
385
367
|
}
|
|
386
368
|
|
|
387
369
|
export declare namespace decodeNumber {
|
|
@@ -391,6 +373,65 @@ export declare namespace decodeNumber {
|
|
|
391
373
|
| Errors.GlobalErrorType
|
|
392
374
|
}
|
|
393
375
|
|
|
376
|
+
type Integer = {
|
|
377
|
+
max: bigint
|
|
378
|
+
min: bigint
|
|
379
|
+
signed: boolean
|
|
380
|
+
size: number
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Parses an integer type and its bounds, once per type rather than per value. */
|
|
384
|
+
function getInteger(type: string): Integer {
|
|
385
|
+
const signed = type.startsWith('int')
|
|
386
|
+
const size = Number.parseInt(type.split('int')[1] || '256', 10)
|
|
387
|
+
if (!(size < 256)) return { max: 0n, min: 0n, signed, size }
|
|
388
|
+
const max = (1n << BigInt(signed ? size - 1 : size)) - 1n
|
|
389
|
+
return { max, min: signed ? -max - 1n : 0n, signed, size }
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function decodeInteger(cursor: Cursor.Cursor, integer: Integer) {
|
|
393
|
+
const { max, min, signed, size } = integer
|
|
394
|
+
const value = Bytes.toBigInt(cursor.readBytes(32), { signed })
|
|
395
|
+
// The word must hold a value that fits the declared size (zero-padded for
|
|
396
|
+
// unsigned, sign-extended for signed types).
|
|
397
|
+
if (size < 256 && (value > max || value < min))
|
|
398
|
+
throw new Hex.IntegerOutOfRangeError({
|
|
399
|
+
max: max.toString(),
|
|
400
|
+
min: min.toString(),
|
|
401
|
+
signed,
|
|
402
|
+
size: size / 8,
|
|
403
|
+
value: value.toString(),
|
|
404
|
+
})
|
|
405
|
+
return [size > 48 ? value : Number(value), 32]
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Resolves how to decode a parameter once, so that the elements of an array
|
|
410
|
+
* do not each re-parse its type. Types other than `address`, `bool` and
|
|
411
|
+
* integers go through `decodeParameter`.
|
|
412
|
+
*/
|
|
413
|
+
function getDecoder(
|
|
414
|
+
param: AbiParameters.Parameter,
|
|
415
|
+
checksumAddress: boolean | undefined,
|
|
416
|
+
): (
|
|
417
|
+
cursor: Cursor.Cursor,
|
|
418
|
+
staticPosition: number,
|
|
419
|
+
) => ReturnType<typeof decodeParameter> {
|
|
420
|
+
const { type } = param
|
|
421
|
+
if (type === 'address')
|
|
422
|
+
return (cursor) => decodeAddress(cursor, { checksum: checksumAddress })
|
|
423
|
+
if (type === 'bool') return decodeBool
|
|
424
|
+
if (
|
|
425
|
+
(type.startsWith('uint') || type.startsWith('int')) &&
|
|
426
|
+
!getArrayComponents(type)
|
|
427
|
+
) {
|
|
428
|
+
const integer = getInteger(type)
|
|
429
|
+
return (cursor) => decodeInteger(cursor, integer)
|
|
430
|
+
}
|
|
431
|
+
return (cursor, staticPosition) =>
|
|
432
|
+
decodeParameter(cursor, param, { checksumAddress, staticPosition })
|
|
433
|
+
}
|
|
434
|
+
|
|
394
435
|
/** @internal */
|
|
395
436
|
export type TupleAbiParameter = AbiParameters.Parameter & {
|
|
396
437
|
components: readonly AbiParameters.Parameter[]
|
|
@@ -521,21 +562,13 @@ export function encodeParameters(
|
|
|
521
562
|
const bytes = new Uint8Array(prefixSize + size)
|
|
522
563
|
const writer: WriteContext = {
|
|
523
564
|
bytes,
|
|
524
|
-
patches: [],
|
|
525
565
|
view: new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
|
526
566
|
}
|
|
527
567
|
|
|
528
568
|
if (prefix) writeHex(writer, 0, prefix)
|
|
529
569
|
writeParameters(writer, prefixSize, compiled, values)
|
|
530
570
|
|
|
531
|
-
|
|
532
|
-
for (let i = 0; i < writer.patches.length; i++) {
|
|
533
|
-
const patch = writer.patches[i]!
|
|
534
|
-
const start = 2 + patch.offset * 2
|
|
535
|
-
data =
|
|
536
|
-
`${data.slice(0, start)}${patch.value}${data.slice(start + patch.value.length)}` as Hex.Hex
|
|
537
|
-
}
|
|
538
|
-
return data
|
|
571
|
+
return Hex.fromBytes(bytes)
|
|
539
572
|
}
|
|
540
573
|
|
|
541
574
|
export declare namespace encodeParameters {
|
|
@@ -552,6 +585,7 @@ export declare namespace encodeParameters {
|
|
|
552
585
|
| AbiParameters.InvalidTypeError
|
|
553
586
|
| Address.assert.ErrorType
|
|
554
587
|
| Hex.IntegerOutOfRangeError
|
|
588
|
+
| Hex.InvalidHexValueError
|
|
555
589
|
| Hex.InvalidLengthError
|
|
556
590
|
| Hex.fromBytes.ErrorType
|
|
557
591
|
| Errors.GlobalErrorType
|
|
@@ -808,18 +842,6 @@ function normalizeString(value: unknown): string {
|
|
|
808
842
|
return `${value}`
|
|
809
843
|
}
|
|
810
844
|
|
|
811
|
-
function isWritableHex(value: Hex.Hex): boolean {
|
|
812
|
-
if (
|
|
813
|
-
typeof value !== 'string' ||
|
|
814
|
-
value.charCodeAt(0) !== 48 ||
|
|
815
|
-
value.charCodeAt(1) !== 120
|
|
816
|
-
)
|
|
817
|
-
return false
|
|
818
|
-
for (let i = 2; i < value.length; i++)
|
|
819
|
-
if (toNibble(value.charCodeAt(i)) === -1) return false
|
|
820
|
-
return true
|
|
821
|
-
}
|
|
822
|
-
|
|
823
845
|
function writeParameters(
|
|
824
846
|
context: WriteContext,
|
|
825
847
|
offset: number,
|
|
@@ -922,18 +944,36 @@ function writeHex(
|
|
|
922
944
|
offset: number,
|
|
923
945
|
value: Hex.Hex,
|
|
924
946
|
): number {
|
|
925
|
-
if (
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
947
|
+
if (
|
|
948
|
+
typeof value !== 'string' ||
|
|
949
|
+
value.charCodeAt(0) !== 48 ||
|
|
950
|
+
value.charCodeAt(1) !== 120
|
|
951
|
+
)
|
|
952
|
+
throw new Hex.InvalidHexValueError(value)
|
|
953
|
+
|
|
954
|
+
// Validate and write in one pass. A valid nibble is at most `0x0f`, so only
|
|
955
|
+
// the `0xff` sentinel sets bit 7; one check after the loop covers every
|
|
956
|
+
// character. `nibbleTable` only spans Latin-1, so a code unit above it reads
|
|
957
|
+
// `undefined` and is mapped to the sentinel.
|
|
958
|
+
const { bytes } = context
|
|
959
|
+
// A local reference: some module transforms turn every read of an imported
|
|
960
|
+
// binding into a property lookup, which would land in the hot loop.
|
|
961
|
+
const table = nibbleTable
|
|
962
|
+
const length = value.length
|
|
963
|
+
let invalid = 0
|
|
931
964
|
let i = 2
|
|
932
|
-
for (; i + 1 <
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
965
|
+
for (; i + 1 < length; i += 2) {
|
|
966
|
+
const hi = table[value.charCodeAt(i)] ?? 0xff
|
|
967
|
+
const lo = table[value.charCodeAt(i + 1)] ?? 0xff
|
|
968
|
+
invalid |= hi | lo
|
|
969
|
+
bytes[offset++] = (hi << 4) | lo
|
|
970
|
+
}
|
|
971
|
+
if (i < length) {
|
|
972
|
+
const hi = table[value.charCodeAt(i)] ?? 0xff
|
|
973
|
+
invalid |= hi
|
|
974
|
+
bytes[offset++] = hi << 4
|
|
975
|
+
}
|
|
976
|
+
if (invalid & 0x80) throw new Hex.InvalidHexValueError(value)
|
|
937
977
|
return offset
|
|
938
978
|
}
|
|
939
979
|
|
|
@@ -952,25 +992,22 @@ function writeWord(
|
|
|
952
992
|
}
|
|
953
993
|
|
|
954
994
|
const hex = value_.toString(16)
|
|
995
|
+
const table = nibbleTable
|
|
955
996
|
let cursor = offset + 32 - Math.ceil(hex.length / 2)
|
|
956
997
|
let i = hex.length % 2
|
|
957
|
-
if (i === 1) context.bytes[cursor++] =
|
|
998
|
+
if (i === 1) context.bytes[cursor++] = table[hex.charCodeAt(0)]!
|
|
958
999
|
for (; i < hex.length; i += 2)
|
|
959
1000
|
context.bytes[cursor++] =
|
|
960
|
-
(
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
function toNibble(code: number): number {
|
|
964
|
-
if (code >= 48 && code <= 57) return code - 48
|
|
965
|
-
if (code >= 65 && code <= 70) return code - 55
|
|
966
|
-
if (code >= 97 && code <= 102) return code - 87
|
|
967
|
-
return -1
|
|
1001
|
+
(table[hex.charCodeAt(i)]! << 4) | table[hex.charCodeAt(i + 1)]!
|
|
968
1002
|
}
|
|
969
1003
|
|
|
970
1004
|
/** @internal */
|
|
971
1005
|
export function getArrayComponents(
|
|
972
1006
|
type: string,
|
|
973
1007
|
): [length: number | null, innerType: string] | undefined {
|
|
1008
|
+
// Only a type ending in `]` can match, so skip the regex for the element
|
|
1009
|
+
// types (`uint256`, `address`, ...) that decoding checks once per value.
|
|
1010
|
+
if (type.charCodeAt(type.length - 1) !== 93 /* ] */) return undefined
|
|
974
1011
|
const matches = arraySuffixRegex.exec(type)
|
|
975
1012
|
return matches
|
|
976
1013
|
? // Return `null` if the array is dynamic.
|
|
@@ -84,11 +84,17 @@ export function from(value: Hex.Hex | Bytes.Bytes) {
|
|
|
84
84
|
? BigInt(Hex.fromBytes(bytes))
|
|
85
85
|
: BigInt(value as string)
|
|
86
86
|
|
|
87
|
+
// Divide by 58^9, the largest power of 58 below 2^53, so each BigInt
|
|
88
|
+
// division yields nine digits that number arithmetic can split.
|
|
87
89
|
let result = ''
|
|
88
90
|
while (integer > 0n) {
|
|
89
|
-
|
|
90
|
-
integer
|
|
91
|
-
|
|
91
|
+
let remainder = Number(integer % 7_427_658_739_644_928n)
|
|
92
|
+
integer /= 7_427_658_739_644_928n
|
|
93
|
+
// Every chunk but the most significant one is zero-padded to nine digits.
|
|
94
|
+
for (let i = 0; i < 9 && (remainder > 0 || integer > 0n); i++) {
|
|
95
|
+
result = integerToAlphabet[remainder % 58] + result
|
|
96
|
+
remainder = Math.floor(remainder / 58)
|
|
97
|
+
}
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
return '1'.repeat(pad) + result
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type * as Address from '../Address.js'
|
|
2
2
|
import type * as Hex from '../Hex.js'
|
|
3
|
-
import { BoundedMap } from './lru.js'
|
|
3
|
+
import { BoundedMap, weakCache } from './lru.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Global caches, cleared together by `Caches.clear` and on every engine change.
|
|
@@ -9,4 +9,6 @@ import { BoundedMap } from './lru.js'
|
|
|
9
9
|
export const caches = {
|
|
10
10
|
checksum: /*#__PURE__*/ new BoundedMap<Address.Address>(32_768),
|
|
11
11
|
signatureHash: /*#__PURE__*/ new BoundedMap<Hex.Hex>(8_192),
|
|
12
|
+
typeHash: /*#__PURE__*/ new BoundedMap<Hex.Hex>(1_024),
|
|
13
|
+
signatureHashByItem: /*#__PURE__*/ weakCache<object, Hex.Hex>(),
|
|
12
14
|
}
|
|
@@ -8,8 +8,12 @@ const hexes = /*#__PURE__*/ Array.from({ length: 256 }, (_v, i) =>
|
|
|
8
8
|
i.toString(16).padStart(2, '0'),
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Char-code → 4-bit nibble lookup table. Sentinel `0xff` = invalid.
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export const nibbleTable = /*#__PURE__*/ (() => {
|
|
13
17
|
const table = new Uint8Array(256).fill(0xff)
|
|
14
18
|
for (let i = 0; i < 10; i++) table[48 + i] = i // '0'-'9'
|
|
15
19
|
for (let i = 0; i < 6; i++) {
|
|
@@ -179,6 +183,27 @@ export function hexToBytesLoop(value: string, length: number): Uint8Array {
|
|
|
179
183
|
return out
|
|
180
184
|
}
|
|
181
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Throws exactly what {@link hexToBytes} throws for `value`, without decoding
|
|
188
|
+
* it, for callers that read the hex string directly.
|
|
189
|
+
*
|
|
190
|
+
* @internal
|
|
191
|
+
*/
|
|
192
|
+
export function assertHexBytes(value: string): void {
|
|
193
|
+
if (
|
|
194
|
+
typeof value !== 'string' ||
|
|
195
|
+
value.length < 2 ||
|
|
196
|
+
value.charCodeAt(0) !== 48 /* '0' */ ||
|
|
197
|
+
value.charCodeAt(1) !== 120 /* 'x' */
|
|
198
|
+
)
|
|
199
|
+
throw new InvalidHexValueError(value)
|
|
200
|
+
if (((value.length - 2) & 1) !== 0) throw new InvalidLengthError(value as Hex)
|
|
201
|
+
// A regular expression scans about twice as fast as a lookup-table loop.
|
|
202
|
+
if (!hexRegex.test(value)) throw new InvalidHexValueError(value)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const hexRegex = /^0x[0-9a-fA-F]*$/
|
|
206
|
+
|
|
182
207
|
/** @internal */
|
|
183
208
|
export function charCodeToBase16(char: number): number | undefined {
|
|
184
209
|
const v = nibbleTable[char] ?? 0xff
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import * as Errors from '../../Errors.js'
|
|
2
2
|
import { bytesToHex } from './hex.js'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Significant byte count up to which {@link bytesToBigInt} shifts 8-byte chunks
|
|
6
|
+
* into the result. Each shift copies the result so far, so the loop is
|
|
7
|
+
* quadratic, and parsing hex overtakes it past 64 bytes on Node 24 and Bun.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
const shiftMaxBytes = 64
|
|
12
|
+
|
|
4
13
|
/**
|
|
5
14
|
* Decodes a big-endian unsigned `Uint8Array` into a `bigint`. When `signed` is
|
|
6
15
|
* true, applies twos-complement on the high bit.
|
|
@@ -10,36 +19,35 @@ import { bytesToHex } from './hex.js'
|
|
|
10
19
|
export function bytesToBigInt(value: Uint8Array, signed?: boolean): bigint {
|
|
11
20
|
const len = value.length
|
|
12
21
|
if (len === 0) return 0n
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
// Two's complement of the unsigned value, without the hex round-trip.
|
|
23
|
+
if (signed) return BigInt.asIntN(len * 8, bytesToBigInt(value))
|
|
24
|
+
|
|
25
|
+
// Skip leading zeros first: ABI words are 32 bytes but usually hold small
|
|
26
|
+
// values, which then fit a Number and skip the BigInt arithmetic.
|
|
27
|
+
let i = 0
|
|
28
|
+
while (i < len && value[i] === 0) i++
|
|
29
|
+
if (len - i <= 6) {
|
|
30
|
+
let n = 0
|
|
31
|
+
for (; i < len; i++) n = n * 256 + value[i]!
|
|
32
|
+
return BigInt(n)
|
|
33
|
+
}
|
|
34
|
+
if (len - i > shiftMaxBytes) return BigInt(bytesToHex(value))
|
|
35
|
+
|
|
36
|
+
// Unsigned big-endian read using DataView for 8-byte chunks.
|
|
37
|
+
let val = 0n
|
|
38
|
+
const fullEnd = len - ((len - i) & 7)
|
|
39
|
+
if (i < fullEnd) {
|
|
40
|
+
const view = new DataView(value.buffer, value.byteOffset, len)
|
|
41
|
+
while (i < fullEnd) {
|
|
42
|
+
val = (val << 64n) | view.getBigUint64(i, false)
|
|
43
|
+
i += 8
|
|
31
44
|
}
|
|
32
|
-
return val
|
|
33
45
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const max_unsigned = 1n << (BigInt(len) * 8n)
|
|
40
|
-
const max_signed = max_unsigned >> 1n
|
|
41
|
-
if (n < max_signed) return n
|
|
42
|
-
return n - max_unsigned
|
|
46
|
+
while (i < len) {
|
|
47
|
+
val = (val << 8n) | BigInt(value[i]!)
|
|
48
|
+
i++
|
|
49
|
+
}
|
|
50
|
+
return val
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
/**
|
|
@@ -49,11 +57,16 @@ export function bytesToBigInt(value: Uint8Array, signed?: boolean): bigint {
|
|
|
49
57
|
* @internal
|
|
50
58
|
*/
|
|
51
59
|
export function bytesToSafeNumber(value: Uint8Array, signed?: boolean): number {
|
|
52
|
-
if (!signed
|
|
60
|
+
if (!signed) {
|
|
53
61
|
// Small unsigned values fit in a number directly without BigInt round-trip.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
const length = value.length
|
|
63
|
+
let i = 0
|
|
64
|
+
while (i < length && value[i] === 0) i++
|
|
65
|
+
if (length - i <= 6) {
|
|
66
|
+
let n = 0
|
|
67
|
+
for (; i < length; i++) n = n * 256 + value[i]!
|
|
68
|
+
return n
|
|
69
|
+
}
|
|
57
70
|
}
|
|
58
71
|
const big = bytesToBigInt(value, signed)
|
|
59
72
|
const num = Number(big)
|