viem 0.0.0-main.20240115T201519 → 0.0.0-main.20240115T205034
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/_cjs/errors/cursor.js +13 -1
- package/_cjs/errors/cursor.js.map +1 -1
- package/_cjs/errors/version.js +1 -1
- package/_cjs/utils/abi/decodeAbiParameters.js +117 -126
- package/_cjs/utils/abi/decodeAbiParameters.js.map +1 -1
- package/_cjs/utils/cursor.js +38 -3
- package/_cjs/utils/cursor.js.map +1 -1
- package/_cjs/utils/encoding/fromBytes.js +2 -2
- package/_cjs/utils/encoding/fromBytes.js.map +1 -1
- package/_esm/errors/cursor.js +11 -0
- package/_esm/errors/cursor.js.map +1 -1
- package/_esm/errors/version.js +1 -1
- package/_esm/utils/abi/decodeAbiParameters.js +145 -140
- package/_esm/utils/abi/decodeAbiParameters.js.map +1 -1
- package/_esm/utils/cursor.js +39 -4
- package/_esm/utils/cursor.js.map +1 -1
- package/_esm/utils/encoding/fromBytes.js +2 -2
- package/_esm/utils/encoding/fromBytes.js.map +1 -1
- package/_types/errors/cursor.d.ts +10 -0
- package/_types/errors/cursor.d.ts.map +1 -1
- package/_types/errors/version.d.ts +1 -1
- package/_types/utils/abi/decodeAbiParameters.d.ts +18 -19
- package/_types/utils/abi/decodeAbiParameters.d.ts.map +1 -1
- package/_types/utils/cursor.d.ts +13 -3
- package/_types/utils/cursor.d.ts.map +1 -1
- package/errors/cursor.ts +13 -0
- package/errors/version.ts +1 -1
- package/package.json +1 -1
- package/utils/abi/decodeAbiParameters.ts +226 -249
- package/utils/cursor.ts +52 -6
- package/utils/encoding/fromBytes.ts +2 -2
package/utils/cursor.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import {
|
2
2
|
NegativeOffsetError,
|
3
3
|
PositionOutOfBoundsError,
|
4
|
+
RecursiveReadLimitExceededError,
|
4
5
|
} from '../errors/cursor.js'
|
5
6
|
import type { ErrorType } from '../errors/utils.js'
|
6
7
|
import type { ByteArray } from '../types/misc.js'
|
@@ -9,8 +10,13 @@ export type Cursor = {
|
|
9
10
|
bytes: ByteArray
|
10
11
|
dataView: DataView
|
11
12
|
position: number
|
13
|
+
positionReadCount: Map<number, number>
|
14
|
+
recursiveReadCount: number
|
15
|
+
recursiveReadLimit: number
|
16
|
+
assertReadLimit(position?: number): void
|
12
17
|
assertPosition(position: number): void
|
13
18
|
decrementPosition(offset: number): void
|
19
|
+
getReadCount(position?: number): number
|
14
20
|
incrementPosition(offset: number): void
|
15
21
|
inspectByte(position?: number): ByteArray[number]
|
16
22
|
inspectBytes(length: number, position?: number): ByteArray
|
@@ -25,12 +31,13 @@ export type Cursor = {
|
|
25
31
|
pushUint24(value: number): void
|
26
32
|
pushUint32(value: number): void
|
27
33
|
readByte(): ByteArray[number]
|
28
|
-
readBytes(length: number): ByteArray
|
34
|
+
readBytes(length: number, size?: number): ByteArray
|
29
35
|
readUint8(): number
|
30
36
|
readUint16(): number
|
31
37
|
readUint24(): number
|
32
38
|
readUint32(): number
|
33
|
-
setPosition(position: number): void
|
39
|
+
setPosition(position: number): () => void
|
40
|
+
_touch(): void
|
34
41
|
}
|
35
42
|
|
36
43
|
export type CreateCursorErrorType = ErrorType
|
@@ -51,6 +58,16 @@ const staticCursor: Cursor = {
|
|
51
58
|
bytes: new Uint8Array(),
|
52
59
|
dataView: new DataView(new ArrayBuffer(0)),
|
53
60
|
position: 0,
|
61
|
+
positionReadCount: new Map(),
|
62
|
+
recursiveReadCount: 0,
|
63
|
+
recursiveReadLimit: Infinity,
|
64
|
+
assertReadLimit() {
|
65
|
+
if (this.recursiveReadCount >= this.recursiveReadLimit)
|
66
|
+
throw new RecursiveReadLimitExceededError({
|
67
|
+
count: this.recursiveReadCount + 1,
|
68
|
+
limit: this.recursiveReadLimit,
|
69
|
+
})
|
70
|
+
},
|
54
71
|
assertPosition(position) {
|
55
72
|
if (position < 0 || position > this.bytes.length - 1)
|
56
73
|
throw new PositionOutOfBoundsError({
|
@@ -64,6 +81,9 @@ const staticCursor: Cursor = {
|
|
64
81
|
this.assertPosition(position)
|
65
82
|
this.position = position
|
66
83
|
},
|
84
|
+
getReadCount(position) {
|
85
|
+
return this.positionReadCount.get(position || this.position) || 0
|
86
|
+
},
|
67
87
|
incrementPosition(offset) {
|
68
88
|
if (offset < 0) throw new NegativeOffsetError({ offset })
|
69
89
|
const position = this.position + offset
|
@@ -135,48 +155,74 @@ const staticCursor: Cursor = {
|
|
135
155
|
this.position += 4
|
136
156
|
},
|
137
157
|
readByte() {
|
158
|
+
this.assertReadLimit()
|
159
|
+
this._touch()
|
138
160
|
const value = this.inspectByte()
|
139
161
|
this.position++
|
140
162
|
return value
|
141
163
|
},
|
142
|
-
readBytes(length) {
|
164
|
+
readBytes(length, size) {
|
165
|
+
this.assertReadLimit()
|
166
|
+
this._touch()
|
143
167
|
const value = this.inspectBytes(length)
|
144
|
-
this.position += length
|
168
|
+
this.position += size ?? length
|
145
169
|
return value
|
146
170
|
},
|
147
171
|
readUint8() {
|
172
|
+
this.assertReadLimit()
|
173
|
+
this._touch()
|
148
174
|
const value = this.inspectUint8()
|
149
175
|
this.position += 1
|
150
176
|
return value
|
151
177
|
},
|
152
178
|
readUint16() {
|
179
|
+
this.assertReadLimit()
|
180
|
+
this._touch()
|
153
181
|
const value = this.inspectUint16()
|
154
182
|
this.position += 2
|
155
183
|
return value
|
156
184
|
},
|
157
185
|
readUint24() {
|
186
|
+
this.assertReadLimit()
|
187
|
+
this._touch()
|
158
188
|
const value = this.inspectUint24()
|
159
189
|
this.position += 3
|
160
190
|
return value
|
161
191
|
},
|
162
192
|
readUint32() {
|
193
|
+
this.assertReadLimit()
|
194
|
+
this._touch()
|
163
195
|
const value = this.inspectUint32()
|
164
196
|
this.position += 4
|
165
197
|
return value
|
166
198
|
},
|
167
199
|
setPosition(position) {
|
200
|
+
const oldPosition = this.position
|
168
201
|
this.assertPosition(position)
|
169
202
|
this.position = position
|
203
|
+
return () => (this.position = oldPosition)
|
204
|
+
},
|
205
|
+
_touch() {
|
206
|
+
const count = this.getReadCount()
|
207
|
+
this.positionReadCount.set(this.position, count + 1)
|
208
|
+
if (count > 0) this.recursiveReadCount++
|
170
209
|
},
|
171
210
|
}
|
172
211
|
|
173
|
-
|
174
|
-
|
212
|
+
type CursorConfig = { recursiveReadLimit?: number }
|
213
|
+
|
214
|
+
export function createCursor(
|
215
|
+
bytes: ByteArray,
|
216
|
+
{ recursiveReadLimit = 8_192 }: CursorConfig = {},
|
217
|
+
): Cursor {
|
218
|
+
const cursor: Cursor = Object.create(staticCursor)
|
175
219
|
cursor.bytes = bytes
|
176
220
|
cursor.dataView = new DataView(
|
177
221
|
bytes.buffer,
|
178
222
|
bytes.byteOffset,
|
179
223
|
bytes.byteLength,
|
180
224
|
)
|
225
|
+
cursor.positionReadCount = new Map()
|
226
|
+
cursor.recursiveReadLimit = recursiveReadLimit
|
181
227
|
return cursor
|
182
228
|
}
|
@@ -119,7 +119,7 @@ export function bytesToBigInt(
|
|
119
119
|
): bigint {
|
120
120
|
if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
|
121
121
|
const hex = bytesToHex(bytes, opts)
|
122
|
-
return hexToBigInt(hex)
|
122
|
+
return hexToBigInt(hex, opts)
|
123
123
|
}
|
124
124
|
|
125
125
|
export type BytesToBoolOpts = {
|
@@ -187,7 +187,7 @@ export function bytesToNumber(
|
|
187
187
|
): number {
|
188
188
|
if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
|
189
189
|
const hex = bytesToHex(bytes, opts)
|
190
|
-
return hexToNumber(hex)
|
190
|
+
return hexToNumber(hex, opts)
|
191
191
|
}
|
192
192
|
|
193
193
|
export type BytesToStringOpts = {
|