@layerzerolabs/common-encoding-utils 0.2.64 → 0.2.66

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.
@@ -3,7 +3,7 @@
3
3
  > @layerzerolabs/common-encoding-utils@0.0.0 build /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils
4
4
  > tsup
5
5
 
6
- CLI Building entry: src/index.ts
6
+ CLI Building entry: src/byte-codec.ts, src/index.ts
7
7
  CLI Using tsconfig: tsconfig.json
8
8
  CLI tsup v8.5.1
9
9
  CLI Using tsup config: /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils/tsup.config.ts
@@ -11,9 +11,17 @@
11
11
  CLI Cleaning output folder
12
12
  CJS Build start
13
13
  ESM Build start
14
- CJS dist/index.cjs 5.03 KB
15
- CJS dist/index.cjs.map 11.80 KB
16
- CJS ⚡️ Build success in 123ms
17
- ESM dist/index.js 4.64 KB
18
- ESM dist/index.js.map 11.79 KB
19
- ESM ⚡️ Build success in 123ms
14
+ CJS dist/byte-codec.cjs 267.00 B
15
+ CJS dist/index.cjs 5.61 KB
16
+ CJS dist/DF2CBFKF.cjs 7.67 KB
17
+ CJS dist/byte-codec.cjs.map 75.00 B
18
+ CJS dist/index.cjs.map 12.44 KB
19
+ CJS dist/DF2CBFKF.cjs.map 19.07 KB
20
+ CJS ⚡️ Build success in 326ms
21
+ ESM dist/byte-codec.js 120.00 B
22
+ ESM dist/index.js 4.85 KB
23
+ ESM dist/2BTCXNB3.js 7.63 KB
24
+ ESM dist/byte-codec.js.map 74.00 B
25
+ ESM dist/index.js.map 12.42 KB
26
+ ESM dist/2BTCXNB3.js.map 19.06 KB
27
+ ESM ⚡️ Build success in 326ms
@@ -2,7 +2,7 @@
2
2
  > @layerzerolabs/common-encoding-utils@0.0.0 lint /home/runner/work/monorepo-internal/monorepo-internal/packages/common/common-encoding-utils
3
3
  > eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)
4
4
 
5
- (node:58063) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/monorepo-internal/monorepo-internal/eslint.config.js?mtime=1775024037920 is not specified and it doesn't parse as CommonJS.
5
+ (node:65664) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/monorepo-internal/monorepo-internal/eslint.config.js?mtime=1775694520701 is not specified and it doesn't parse as CommonJS.
6
6
  Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
7
7
  To eliminate this warning, add "type": "module" to /home/runner/work/monorepo-internal/monorepo-internal/package.json.
8
8
  (Use `node --trace-warnings ...` to show where the warning was created)
@@ -0,0 +1,221 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/byte-codec.ts
5
+ function uMax(bits) {
6
+ if (!Number.isInteger(bits) || bits < 0) {
7
+ throw new RangeError(`ByteCodec: invalid bit width: ${bits}`);
8
+ }
9
+ return bits === 0 ? 0n : (1n << BigInt(bits)) - 1n;
10
+ }
11
+ __name(uMax, "uMax");
12
+ var ByteCodec = class _ByteCodec {
13
+ static {
14
+ __name(this, "ByteCodec");
15
+ }
16
+ // Start with a small-ish initial capacity to avoid frequent reallocations for
17
+ // common short payloads (e.g. function selector + a few fixed-width fields),
18
+ // while still staying tiny in memory terms. Buffer grows by doubling as needed.
19
+ #buf = new ArrayBuffer(128);
20
+ #view = new DataView(this.#buf);
21
+ #cursor = 0;
22
+ static #ZERO = 0n;
23
+ static #U8_MAX = uMax(8);
24
+ static #U16_MAX = uMax(16);
25
+ static #U32_MAX = uMax(32);
26
+ static #U64_MAX = uMax(64);
27
+ static #U128_MAX = uMax(128);
28
+ static #U256_MAX = uMax(256);
29
+ static #outOfRange(targetLength, value) {
30
+ return new RangeError(`ByteCodec: value out of range for u${targetLength * 8}: ${value}`);
31
+ }
32
+ /**
33
+ * Read helpers (big-endian). These are intentionally small/low-level so other
34
+ * packages can share a single implementation for parsing encoded payloads.
35
+ */
36
+ static readU16be(buf, offset) {
37
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
38
+ return view.getUint16(offset, false);
39
+ }
40
+ static readU8(buf, offset) {
41
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
42
+ return view.getUint8(offset);
43
+ }
44
+ static readU32be(buf, offset) {
45
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
46
+ return view.getUint32(offset, false);
47
+ }
48
+ static readU128be(buf, offset) {
49
+ return _ByteCodec.readUNbe(buf, offset, 16);
50
+ }
51
+ static readUNbe(buf, offset, length) {
52
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
53
+ let out = 0n;
54
+ const end = offset + length;
55
+ let cursor = offset;
56
+ while (cursor + 8 <= end) {
57
+ out = out << 64n | view.getBigUint64(cursor, false);
58
+ cursor += 8;
59
+ }
60
+ while (cursor < end) {
61
+ out = out << 8n | BigInt(view.getUint8(cursor));
62
+ cursor += 1;
63
+ }
64
+ return out;
65
+ }
66
+ static readBytes32(buf, offset) {
67
+ if (offset + 32 > buf.length) throw new RangeError(`ByteCodec: out of bounds at ${offset}`);
68
+ return buf.slice(offset, offset + 32);
69
+ }
70
+ /**
71
+ * Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).
72
+ *
73
+ * Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.
74
+ */
75
+ static leftPad(bytes, targetLength, padByte = 0) {
76
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
77
+ throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
78
+ }
79
+ if (!Number.isInteger(padByte) || padByte < 0 || padByte > 255) {
80
+ throw new RangeError(`ByteCodec: invalid pad byte: ${padByte}`);
81
+ }
82
+ if (bytes.length > targetLength) {
83
+ throw new RangeError(`ByteCodec: bytes length ${bytes.length} exceeds target ${targetLength}`);
84
+ }
85
+ const out = new Uint8Array(targetLength);
86
+ if (padByte !== 0) out.fill(padByte);
87
+ out.set(bytes, targetLength - bytes.length);
88
+ return out;
89
+ }
90
+ /**
91
+ * Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),
92
+ * reverting on overflow.
93
+ *
94
+ * This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:
95
+ * - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero
96
+ * - returns the low `targetLength` bytes as the result
97
+ *
98
+ * Examples:
99
+ * - cast bytes32 -> u128: castUNbe(bytes32, 16)
100
+ * - cast bytesN -> u64: castUNbe(bytesN, 8)
101
+ */
102
+ static castUNbe(bytes, targetLength) {
103
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
104
+ throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
105
+ }
106
+ const len = bytes.length;
107
+ if (len <= targetLength) return _ByteCodec.readUNbe(bytes, 0, len);
108
+ const excess = len - targetLength;
109
+ for (let i = 0; i < excess; i++) {
110
+ if (bytes[i] !== 0) throw _ByteCodec.#outOfRange(targetLength, _ByteCodec.readUNbe(bytes, 0, len));
111
+ }
112
+ return _ByteCodec.readUNbe(bytes, excess, targetLength);
113
+ }
114
+ static castU8be(bytes) {
115
+ return _ByteCodec.castUNbe(bytes, 1);
116
+ }
117
+ static castU16be(bytes) {
118
+ return _ByteCodec.castUNbe(bytes, 2);
119
+ }
120
+ static castU32be(bytes) {
121
+ return _ByteCodec.castUNbe(bytes, 4);
122
+ }
123
+ static castU64be(bytes) {
124
+ return _ByteCodec.castUNbe(bytes, 8);
125
+ }
126
+ static castU128be(bytes) {
127
+ return _ByteCodec.castUNbe(bytes, 16);
128
+ }
129
+ ensureCapacity(additionalBytes) {
130
+ const needed = this.#cursor + additionalBytes;
131
+ if (needed <= this.#buf.byteLength) return;
132
+ let nextCap = this.#buf.byteLength;
133
+ while (nextCap < needed) nextCap *= 2;
134
+ const next = new ArrayBuffer(nextCap);
135
+ new Uint8Array(next).set(new Uint8Array(this.#buf, 0, this.#cursor));
136
+ this.#buf = next;
137
+ this.#view = new DataView(this.#buf);
138
+ }
139
+ bytes(b) {
140
+ this.ensureCapacity(b.length);
141
+ new Uint8Array(this.#buf, this.#cursor, b.length).set(b);
142
+ this.#cursor += b.length;
143
+ return this;
144
+ }
145
+ bytes32(b) {
146
+ return this.bytes(_ByteCodec.leftPad(b, 32));
147
+ }
148
+ bool(b) {
149
+ return this.u8(b ? 1n : _ByteCodec.#ZERO);
150
+ }
151
+ u8(v) {
152
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U8_MAX) throw new RangeError(`ByteCodec: value out of range for u8: ${v}`);
153
+ this.ensureCapacity(1);
154
+ this.#view.setUint8(this.#cursor, Number(v));
155
+ this.#cursor += 1;
156
+ return this;
157
+ }
158
+ u16be(v) {
159
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U16_MAX) throw new RangeError(`ByteCodec: value out of range for u16: ${v}`);
160
+ this.ensureCapacity(2);
161
+ this.#view.setUint16(this.#cursor, Number(v), false);
162
+ this.#cursor += 2;
163
+ return this;
164
+ }
165
+ u32be(v) {
166
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U32_MAX) {
167
+ throw new RangeError(`ByteCodec: value out of range for u32: ${v}`);
168
+ }
169
+ this.ensureCapacity(4);
170
+ this.#view.setUint32(this.#cursor, Number(v), false);
171
+ this.#cursor += 4;
172
+ return this;
173
+ }
174
+ u64be(v) {
175
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U64_MAX) {
176
+ throw new RangeError(`ByteCodec: value out of range for u64: ${v}`);
177
+ }
178
+ this.ensureCapacity(8);
179
+ this.#view.setBigUint64(this.#cursor, v, false);
180
+ this.#cursor += 8;
181
+ return this;
182
+ }
183
+ u128be(v) {
184
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U128_MAX) {
185
+ throw new RangeError(`ByteCodec: value out of range for u128: ${v}`);
186
+ }
187
+ this.ensureCapacity(16);
188
+ const hi = v >> 64n & _ByteCodec.#U64_MAX;
189
+ const lo = v & _ByteCodec.#U64_MAX;
190
+ this.#view.setBigUint64(this.#cursor, hi, false);
191
+ this.#view.setBigUint64(this.#cursor + 8, lo, false);
192
+ this.#cursor += 16;
193
+ return this;
194
+ }
195
+ u256be(v) {
196
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U256_MAX) {
197
+ throw new RangeError(`ByteCodec: value out of range for u256: ${v}`);
198
+ }
199
+ this.ensureCapacity(32);
200
+ const w0 = v >> 192n & _ByteCodec.#U64_MAX;
201
+ const w1 = v >> 128n & _ByteCodec.#U64_MAX;
202
+ const w2 = v >> 64n & _ByteCodec.#U64_MAX;
203
+ const w3 = v & _ByteCodec.#U64_MAX;
204
+ this.#view.setBigUint64(this.#cursor, w0, false);
205
+ this.#view.setBigUint64(this.#cursor + 8, w1, false);
206
+ this.#view.setBigUint64(this.#cursor + 16, w2, false);
207
+ this.#view.setBigUint64(this.#cursor + 24, w3, false);
208
+ this.#cursor += 32;
209
+ return this;
210
+ }
211
+ /**
212
+ * Returns a copy of the accumulated bytes (no shared backing buffer).
213
+ */
214
+ toBytes() {
215
+ return new Uint8Array(this.#buf, 0, this.#cursor).slice();
216
+ }
217
+ };
218
+
219
+ export { ByteCodec, __name };
220
+ //# sourceMappingURL=2BTCXNB3.js.map
221
+ //# sourceMappingURL=2BTCXNB3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/byte-codec.ts"],"names":["uMax","bits","Number","isInteger","RangeError","BigInt","ByteCodec","ArrayBuffer","DataView","targetLength","value","readU16be","buf","offset","view","buffer","byteOffset","byteLength","getUint16","readU8","getUint8","readU32be","getUint32","readU128be","readUNbe","length","out","end","cursor","getBigUint64","readBytes32","slice","leftPad","bytes","padByte","Uint8Array","fill","set","castUNbe","len","excess","i","castU8be","castU16be","castU32be","castU64be","castU128be","ensureCapacity","additionalBytes","needed","nextCap","next","b","bytes32","bool","u8","v","setUint8","u16be","setUint16","u32be","setUint32","u64be","setBigUint64","u128be","hi","lo","u256be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;AACnC,IAAA,MAAMgE,EAAAA,GAAMd,CAAAA,IAAK,GAAA,GAAOlD,UAAAA,CAAU,QAAA;AAClC,IAAA,MAAMiE,EAAAA,GAAKf,IAAIlD,UAAAA,CAAU,QAAA;AACzB,IAAA,IAAA,CAAK,KAAA,CAAMyD,YAAAA,CAAa,IAAA,CAAK,OAAA,EAASK,IAAI,KAAA,CAAA;AAC1C,IAAA,IAAA,CAAK,MAAML,YAAAA,CAAa,IAAA,CAAK,OAAA,GAAU,CAAA,EAAGM,IAAI,KAAA,CAAA;AAC9C,IAAA,IAAA,CAAK,MAAMN,YAAAA,CAAa,IAAA,CAAK,OAAA,GAAU,EAAA,EAAIO,IAAI,KAAA,CAAA;AAC/C,IAAA,IAAA,CAAK,MAAMP,YAAAA,CAAa,IAAA,CAAK,OAAA,GAAU,EAAA,EAAIQ,IAAI,KAAA,CAAA;AAC/C,IAAA,IAAA,CAAK,OAAA,IAAW,EAAA;AAChB,IAAA,OAAO,IAAA;AACX,EAAA;;;;EAIAC,OAAAA,GAAsB;AAClB,IAAA,OAAO,IAAIrC,WAAW,IAAA,CAAK,IAAA,EAAM,GAAG,IAAA,CAAK,OAAO,EAAEJ,KAAAA,EAAK;AAC3D,EAAA;AACJ","file":"2BTCXNB3.js","sourcesContent":["/**\n * Growable byte codec backed by `ArrayBuffer` + `DataView`.\n *\n * Low-level building block for constructing byte payloads deterministically.\n * Higher-level concerns (e.g. hashing, ABI encoding, function signatures) should\n * live in wrappers/subclasses.\n */\n\nfunction uMax(bits: number): bigint {\n if (!Number.isInteger(bits) || bits < 0) {\n throw new RangeError(`ByteCodec: invalid bit width: ${bits}`);\n }\n // (2^bits) - 1, as bigint\n return bits === 0 ? 0n : (1n << BigInt(bits)) - 1n;\n}\n\n// TODO: add tests for this class — currently has zero test coverage\nexport class ByteCodec {\n // Start with a small-ish initial capacity to avoid frequent reallocations for\n // common short payloads (e.g. function selector + a few fixed-width fields),\n // while still staying tiny in memory terms. Buffer grows by doubling as needed.\n #buf = new ArrayBuffer(128);\n #view = new DataView(this.#buf);\n #cursor = 0;\n\n static readonly #ZERO = 0n;\n static readonly #U8_MAX = uMax(8);\n static readonly #U16_MAX = uMax(16);\n static readonly #U32_MAX = uMax(32);\n static readonly #U64_MAX = uMax(64);\n static readonly #U128_MAX = uMax(128);\n static readonly #U256_MAX = uMax(256);\n\n static #outOfRange(targetLength: number, value: bigint): RangeError {\n return new RangeError(`ByteCodec: value out of range for u${targetLength * 8}: ${value}`);\n }\n\n /**\n * Read helpers (big-endian). These are intentionally small/low-level so other\n * packages can share a single implementation for parsing encoded payloads.\n */\n static readU16be(buf: Uint8Array, offset: number): number {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n return view.getUint16(offset, false);\n }\n\n static readU8(buf: Uint8Array, offset: number): number {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n return view.getUint8(offset);\n }\n\n static readU32be(buf: Uint8Array, offset: number): number {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n return view.getUint32(offset, false);\n }\n\n static readU128be(buf: Uint8Array, offset: number): bigint {\n return ByteCodec.readUNbe(buf, offset, 16);\n }\n\n static readUNbe(buf: Uint8Array, offset: number, length: number): bigint {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n\n let out = 0n;\n const end = offset + length;\n\n // Fast path: read 8 bytes at a time\n let cursor = offset;\n while (cursor + 8 <= end) {\n out = (out << 64n) | view.getBigUint64(cursor, false);\n cursor += 8;\n }\n\n // Tail: 0..7 bytes\n while (cursor < end) {\n out = (out << 8n) | BigInt(view.getUint8(cursor));\n cursor += 1;\n }\n\n return out;\n }\n\n static readBytes32(buf: Uint8Array, offset: number): Uint8Array {\n if (offset + 32 > buf.length) throw new RangeError(`ByteCodec: out of bounds at ${offset}`);\n return buf.slice(offset, offset + 32);\n }\n\n /**\n * Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).\n *\n * Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.\n */\n static leftPad(bytes: Uint8Array, targetLength: number, padByte = 0): Uint8Array {\n if (!Number.isInteger(targetLength) || targetLength < 0) {\n throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);\n }\n if (!Number.isInteger(padByte) || padByte < 0 || padByte > 255) {\n throw new RangeError(`ByteCodec: invalid pad byte: ${padByte}`);\n }\n if (bytes.length > targetLength) {\n throw new RangeError(\n `ByteCodec: bytes length ${bytes.length} exceeds target ${targetLength}`,\n );\n }\n\n const out = new Uint8Array(targetLength);\n if (padByte !== 0) out.fill(padByte);\n out.set(bytes, targetLength - bytes.length);\n return out;\n }\n\n /**\n * Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),\n * reverting on overflow.\n *\n * This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:\n * - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero\n * - returns the low `targetLength` bytes as the result\n *\n * Examples:\n * - cast bytes32 -> u128: castUNbe(bytes32, 16)\n * - cast bytesN -> u64: castUNbe(bytesN, 8)\n */\n static castUNbe(bytes: Uint8Array, targetLength: number): bigint {\n if (!Number.isInteger(targetLength) || targetLength < 0) {\n throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);\n }\n\n const len = bytes.length;\n if (len <= targetLength) return ByteCodec.readUNbe(bytes, 0, len);\n\n // Overflow check: any excess high bytes must be 0x00.\n const excess = len - targetLength;\n for (let i = 0; i < excess; i++) {\n if (bytes[i] !== 0)\n throw ByteCodec.#outOfRange(targetLength, ByteCodec.readUNbe(bytes, 0, len));\n }\n\n return ByteCodec.readUNbe(bytes, excess, targetLength);\n }\n\n static castU8be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 1);\n }\n\n static castU16be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 2);\n }\n\n static castU32be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 4);\n }\n\n static castU64be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 8);\n }\n\n static castU128be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 16);\n }\n\n protected ensureCapacity(additionalBytes: number): void {\n const needed = this.#cursor + additionalBytes;\n if (needed <= this.#buf.byteLength) return;\n\n let nextCap = this.#buf.byteLength;\n while (nextCap < needed) nextCap *= 2;\n\n // Grow by allocating a new ArrayBuffer and copying the written prefix\n // [0..cursor) into it. We then swap the backing buffer+view.\n const next = new ArrayBuffer(nextCap);\n new Uint8Array(next).set(new Uint8Array(this.#buf, 0, this.#cursor));\n this.#buf = next;\n this.#view = new DataView(this.#buf);\n }\n\n bytes(b: Uint8Array): this {\n this.ensureCapacity(b.length);\n new Uint8Array(this.#buf, this.#cursor, b.length).set(b);\n this.#cursor += b.length;\n return this;\n }\n\n bytes32(b: Uint8Array): this {\n return this.bytes(ByteCodec.leftPad(b, 32));\n }\n\n bool(b: boolean): this {\n return this.u8(b ? 1n : ByteCodec.#ZERO);\n }\n\n u8(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U8_MAX)\n throw new RangeError(`ByteCodec: value out of range for u8: ${v}`);\n this.ensureCapacity(1);\n this.#view.setUint8(this.#cursor, Number(v));\n this.#cursor += 1;\n return this;\n }\n\n u16be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U16_MAX)\n throw new RangeError(`ByteCodec: value out of range for u16: ${v}`);\n this.ensureCapacity(2);\n this.#view.setUint16(this.#cursor, Number(v), false);\n this.#cursor += 2;\n return this;\n }\n\n u32be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U32_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u32: ${v}`);\n }\n this.ensureCapacity(4);\n this.#view.setUint32(this.#cursor, Number(v), false);\n this.#cursor += 4;\n return this;\n }\n\n u64be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U64_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u64: ${v}`);\n }\n this.ensureCapacity(8);\n this.#view.setBigUint64(this.#cursor, v, false);\n this.#cursor += 8;\n return this;\n }\n\n u128be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U128_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u128: ${v}`);\n }\n this.ensureCapacity(16);\n const hi = (v >> 64n) & ByteCodec.#U64_MAX;\n const lo = v & ByteCodec.#U64_MAX;\n this.#view.setBigUint64(this.#cursor, hi, false);\n this.#view.setBigUint64(this.#cursor + 8, lo, false);\n this.#cursor += 16;\n return this;\n }\n\n u256be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U256_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u256: ${v}`);\n }\n this.ensureCapacity(32);\n const w0 = (v >> 192n) & ByteCodec.#U64_MAX;\n const w1 = (v >> 128n) & ByteCodec.#U64_MAX;\n const w2 = (v >> 64n) & ByteCodec.#U64_MAX;\n const w3 = v & ByteCodec.#U64_MAX;\n this.#view.setBigUint64(this.#cursor, w0, false);\n this.#view.setBigUint64(this.#cursor + 8, w1, false);\n this.#view.setBigUint64(this.#cursor + 16, w2, false);\n this.#view.setBigUint64(this.#cursor + 24, w3, false);\n this.#cursor += 32;\n return this;\n }\n /**\n * Returns a copy of the accumulated bytes (no shared backing buffer).\n */\n toBytes(): Uint8Array {\n return new Uint8Array(this.#buf, 0, this.#cursor).slice();\n }\n}\n"]}
@@ -0,0 +1,224 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
+
6
+ // src/byte-codec.ts
7
+ function uMax(bits) {
8
+ if (!Number.isInteger(bits) || bits < 0) {
9
+ throw new RangeError(`ByteCodec: invalid bit width: ${bits}`);
10
+ }
11
+ return bits === 0 ? 0n : (1n << BigInt(bits)) - 1n;
12
+ }
13
+ __name(uMax, "uMax");
14
+ var ByteCodec = class _ByteCodec {
15
+ static {
16
+ __name(this, "ByteCodec");
17
+ }
18
+ // Start with a small-ish initial capacity to avoid frequent reallocations for
19
+ // common short payloads (e.g. function selector + a few fixed-width fields),
20
+ // while still staying tiny in memory terms. Buffer grows by doubling as needed.
21
+ #buf = new ArrayBuffer(128);
22
+ #view = new DataView(this.#buf);
23
+ #cursor = 0;
24
+ static #ZERO = 0n;
25
+ static #U8_MAX = uMax(8);
26
+ static #U16_MAX = uMax(16);
27
+ static #U32_MAX = uMax(32);
28
+ static #U64_MAX = uMax(64);
29
+ static #U128_MAX = uMax(128);
30
+ static #U256_MAX = uMax(256);
31
+ static #outOfRange(targetLength, value) {
32
+ return new RangeError(`ByteCodec: value out of range for u${targetLength * 8}: ${value}`);
33
+ }
34
+ /**
35
+ * Read helpers (big-endian). These are intentionally small/low-level so other
36
+ * packages can share a single implementation for parsing encoded payloads.
37
+ */
38
+ static readU16be(buf, offset) {
39
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
40
+ return view.getUint16(offset, false);
41
+ }
42
+ static readU8(buf, offset) {
43
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
44
+ return view.getUint8(offset);
45
+ }
46
+ static readU32be(buf, offset) {
47
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
48
+ return view.getUint32(offset, false);
49
+ }
50
+ static readU128be(buf, offset) {
51
+ return _ByteCodec.readUNbe(buf, offset, 16);
52
+ }
53
+ static readUNbe(buf, offset, length) {
54
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
55
+ let out = 0n;
56
+ const end = offset + length;
57
+ let cursor = offset;
58
+ while (cursor + 8 <= end) {
59
+ out = out << 64n | view.getBigUint64(cursor, false);
60
+ cursor += 8;
61
+ }
62
+ while (cursor < end) {
63
+ out = out << 8n | BigInt(view.getUint8(cursor));
64
+ cursor += 1;
65
+ }
66
+ return out;
67
+ }
68
+ static readBytes32(buf, offset) {
69
+ if (offset + 32 > buf.length) throw new RangeError(`ByteCodec: out of bounds at ${offset}`);
70
+ return buf.slice(offset, offset + 32);
71
+ }
72
+ /**
73
+ * Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).
74
+ *
75
+ * Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.
76
+ */
77
+ static leftPad(bytes, targetLength, padByte = 0) {
78
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
79
+ throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
80
+ }
81
+ if (!Number.isInteger(padByte) || padByte < 0 || padByte > 255) {
82
+ throw new RangeError(`ByteCodec: invalid pad byte: ${padByte}`);
83
+ }
84
+ if (bytes.length > targetLength) {
85
+ throw new RangeError(`ByteCodec: bytes length ${bytes.length} exceeds target ${targetLength}`);
86
+ }
87
+ const out = new Uint8Array(targetLength);
88
+ if (padByte !== 0) out.fill(padByte);
89
+ out.set(bytes, targetLength - bytes.length);
90
+ return out;
91
+ }
92
+ /**
93
+ * Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),
94
+ * reverting on overflow.
95
+ *
96
+ * This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:
97
+ * - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero
98
+ * - returns the low `targetLength` bytes as the result
99
+ *
100
+ * Examples:
101
+ * - cast bytes32 -> u128: castUNbe(bytes32, 16)
102
+ * - cast bytesN -> u64: castUNbe(bytesN, 8)
103
+ */
104
+ static castUNbe(bytes, targetLength) {
105
+ if (!Number.isInteger(targetLength) || targetLength < 0) {
106
+ throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);
107
+ }
108
+ const len = bytes.length;
109
+ if (len <= targetLength) return _ByteCodec.readUNbe(bytes, 0, len);
110
+ const excess = len - targetLength;
111
+ for (let i = 0; i < excess; i++) {
112
+ if (bytes[i] !== 0) throw _ByteCodec.#outOfRange(targetLength, _ByteCodec.readUNbe(bytes, 0, len));
113
+ }
114
+ return _ByteCodec.readUNbe(bytes, excess, targetLength);
115
+ }
116
+ static castU8be(bytes) {
117
+ return _ByteCodec.castUNbe(bytes, 1);
118
+ }
119
+ static castU16be(bytes) {
120
+ return _ByteCodec.castUNbe(bytes, 2);
121
+ }
122
+ static castU32be(bytes) {
123
+ return _ByteCodec.castUNbe(bytes, 4);
124
+ }
125
+ static castU64be(bytes) {
126
+ return _ByteCodec.castUNbe(bytes, 8);
127
+ }
128
+ static castU128be(bytes) {
129
+ return _ByteCodec.castUNbe(bytes, 16);
130
+ }
131
+ ensureCapacity(additionalBytes) {
132
+ const needed = this.#cursor + additionalBytes;
133
+ if (needed <= this.#buf.byteLength) return;
134
+ let nextCap = this.#buf.byteLength;
135
+ while (nextCap < needed) nextCap *= 2;
136
+ const next = new ArrayBuffer(nextCap);
137
+ new Uint8Array(next).set(new Uint8Array(this.#buf, 0, this.#cursor));
138
+ this.#buf = next;
139
+ this.#view = new DataView(this.#buf);
140
+ }
141
+ bytes(b) {
142
+ this.ensureCapacity(b.length);
143
+ new Uint8Array(this.#buf, this.#cursor, b.length).set(b);
144
+ this.#cursor += b.length;
145
+ return this;
146
+ }
147
+ bytes32(b) {
148
+ return this.bytes(_ByteCodec.leftPad(b, 32));
149
+ }
150
+ bool(b) {
151
+ return this.u8(b ? 1n : _ByteCodec.#ZERO);
152
+ }
153
+ u8(v) {
154
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U8_MAX) throw new RangeError(`ByteCodec: value out of range for u8: ${v}`);
155
+ this.ensureCapacity(1);
156
+ this.#view.setUint8(this.#cursor, Number(v));
157
+ this.#cursor += 1;
158
+ return this;
159
+ }
160
+ u16be(v) {
161
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U16_MAX) throw new RangeError(`ByteCodec: value out of range for u16: ${v}`);
162
+ this.ensureCapacity(2);
163
+ this.#view.setUint16(this.#cursor, Number(v), false);
164
+ this.#cursor += 2;
165
+ return this;
166
+ }
167
+ u32be(v) {
168
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U32_MAX) {
169
+ throw new RangeError(`ByteCodec: value out of range for u32: ${v}`);
170
+ }
171
+ this.ensureCapacity(4);
172
+ this.#view.setUint32(this.#cursor, Number(v), false);
173
+ this.#cursor += 4;
174
+ return this;
175
+ }
176
+ u64be(v) {
177
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U64_MAX) {
178
+ throw new RangeError(`ByteCodec: value out of range for u64: ${v}`);
179
+ }
180
+ this.ensureCapacity(8);
181
+ this.#view.setBigUint64(this.#cursor, v, false);
182
+ this.#cursor += 8;
183
+ return this;
184
+ }
185
+ u128be(v) {
186
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U128_MAX) {
187
+ throw new RangeError(`ByteCodec: value out of range for u128: ${v}`);
188
+ }
189
+ this.ensureCapacity(16);
190
+ const hi = v >> 64n & _ByteCodec.#U64_MAX;
191
+ const lo = v & _ByteCodec.#U64_MAX;
192
+ this.#view.setBigUint64(this.#cursor, hi, false);
193
+ this.#view.setBigUint64(this.#cursor + 8, lo, false);
194
+ this.#cursor += 16;
195
+ return this;
196
+ }
197
+ u256be(v) {
198
+ if (v < _ByteCodec.#ZERO || v > _ByteCodec.#U256_MAX) {
199
+ throw new RangeError(`ByteCodec: value out of range for u256: ${v}`);
200
+ }
201
+ this.ensureCapacity(32);
202
+ const w0 = v >> 192n & _ByteCodec.#U64_MAX;
203
+ const w1 = v >> 128n & _ByteCodec.#U64_MAX;
204
+ const w2 = v >> 64n & _ByteCodec.#U64_MAX;
205
+ const w3 = v & _ByteCodec.#U64_MAX;
206
+ this.#view.setBigUint64(this.#cursor, w0, false);
207
+ this.#view.setBigUint64(this.#cursor + 8, w1, false);
208
+ this.#view.setBigUint64(this.#cursor + 16, w2, false);
209
+ this.#view.setBigUint64(this.#cursor + 24, w3, false);
210
+ this.#cursor += 32;
211
+ return this;
212
+ }
213
+ /**
214
+ * Returns a copy of the accumulated bytes (no shared backing buffer).
215
+ */
216
+ toBytes() {
217
+ return new Uint8Array(this.#buf, 0, this.#cursor).slice();
218
+ }
219
+ };
220
+
221
+ exports.ByteCodec = ByteCodec;
222
+ exports.__name = __name;
223
+ //# sourceMappingURL=DF2CBFKF.cjs.map
224
+ //# sourceMappingURL=DF2CBFKF.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/byte-codec.ts"],"names":["uMax","bits","Number","isInteger","RangeError","BigInt","ByteCodec","ArrayBuffer","DataView","targetLength","value","readU16be","buf","offset","view","buffer","byteOffset","byteLength","getUint16","readU8","getUint8","readU32be","getUint32","readU128be","readUNbe","length","out","end","cursor","getBigUint64","readBytes32","slice","leftPad","bytes","padByte","Uint8Array","fill","set","castUNbe","len","excess","i","castU8be","castU16be","castU32be","castU64be","castU128be","ensureCapacity","additionalBytes","needed","nextCap","next","b","bytes32","bool","u8","v","setUint8","u16be","setUint16","u32be","setUint32","u64be","setBigUint64","u128be","hi","lo","u256be","w0","w1","w2","w3","toBytes"],"mappings":";;;;;;AAQA,SAASA,KAAKC,IAAAA,EAAY;AACtB,EAAA,IAAI,CAACC,MAAAA,CAAOC,SAAAA,CAAUF,IAAAA,CAAAA,IAASA,OAAO,CAAA,EAAG;AACrC,IAAA,MAAM,IAAIG,UAAAA,CAAW,CAAA,8BAAA,EAAiCH,IAAAA,CAAAA,CAAM,CAAA;AAChE,EAAA;AAEA,EAAA,OAAOA,SAAS,CAAA,GAAI,EAAA,GAAA,CAAM,EAAA,IAAMI,MAAAA,CAAOJ,IAAAA,CAAAA,IAAS,EAAA;AACpD;AANSD,MAAAA,CAAAA,IAAAA,EAAAA,MAAAA,CAAAA;AASF,IAAMM,SAAAA,GAAN,MAAMA,UAAAA,CAAAA;EAjBb;;;;;;EAqBI,IAAA,GAAO,IAAIC,YAAY,GAAA,CAAA;EACvB,KAAA,GAAQ,IAAIC,QAAAA,CAAS,IAAA,CAAK,IAAI,CAAA;EAC9B,OAAA,GAAU,CAAA;AAEV,EAAA,OAAgB,KAAA,GAAQ,EAAA;EACxB,OAAgB,OAAA,GAAUR,KAAK,CAAA,CAA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EAAM,GAAG,IAAA,CAAK,OAAO,EAAEJ,KAAAA,EAAK;AAC3D,EAAA;AACJ","file":"DF2CBFKF.cjs","sourcesContent":["/**\n * Growable byte codec backed by `ArrayBuffer` + `DataView`.\n *\n * Low-level building block for constructing byte payloads deterministically.\n * Higher-level concerns (e.g. hashing, ABI encoding, function signatures) should\n * live in wrappers/subclasses.\n */\n\nfunction uMax(bits: number): bigint {\n if (!Number.isInteger(bits) || bits < 0) {\n throw new RangeError(`ByteCodec: invalid bit width: ${bits}`);\n }\n // (2^bits) - 1, as bigint\n return bits === 0 ? 0n : (1n << BigInt(bits)) - 1n;\n}\n\n// TODO: add tests for this class — currently has zero test coverage\nexport class ByteCodec {\n // Start with a small-ish initial capacity to avoid frequent reallocations for\n // common short payloads (e.g. function selector + a few fixed-width fields),\n // while still staying tiny in memory terms. Buffer grows by doubling as needed.\n #buf = new ArrayBuffer(128);\n #view = new DataView(this.#buf);\n #cursor = 0;\n\n static readonly #ZERO = 0n;\n static readonly #U8_MAX = uMax(8);\n static readonly #U16_MAX = uMax(16);\n static readonly #U32_MAX = uMax(32);\n static readonly #U64_MAX = uMax(64);\n static readonly #U128_MAX = uMax(128);\n static readonly #U256_MAX = uMax(256);\n\n static #outOfRange(targetLength: number, value: bigint): RangeError {\n return new RangeError(`ByteCodec: value out of range for u${targetLength * 8}: ${value}`);\n }\n\n /**\n * Read helpers (big-endian). These are intentionally small/low-level so other\n * packages can share a single implementation for parsing encoded payloads.\n */\n static readU16be(buf: Uint8Array, offset: number): number {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n return view.getUint16(offset, false);\n }\n\n static readU8(buf: Uint8Array, offset: number): number {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n return view.getUint8(offset);\n }\n\n static readU32be(buf: Uint8Array, offset: number): number {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n return view.getUint32(offset, false);\n }\n\n static readU128be(buf: Uint8Array, offset: number): bigint {\n return ByteCodec.readUNbe(buf, offset, 16);\n }\n\n static readUNbe(buf: Uint8Array, offset: number, length: number): bigint {\n const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);\n\n let out = 0n;\n const end = offset + length;\n\n // Fast path: read 8 bytes at a time\n let cursor = offset;\n while (cursor + 8 <= end) {\n out = (out << 64n) | view.getBigUint64(cursor, false);\n cursor += 8;\n }\n\n // Tail: 0..7 bytes\n while (cursor < end) {\n out = (out << 8n) | BigInt(view.getUint8(cursor));\n cursor += 1;\n }\n\n return out;\n }\n\n static readBytes32(buf: Uint8Array, offset: number): Uint8Array {\n if (offset + 32 > buf.length) throw new RangeError(`ByteCodec: out of bounds at ${offset}`);\n return buf.slice(offset, offset + 32);\n }\n\n /**\n * Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).\n *\n * Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.\n */\n static leftPad(bytes: Uint8Array, targetLength: number, padByte = 0): Uint8Array {\n if (!Number.isInteger(targetLength) || targetLength < 0) {\n throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);\n }\n if (!Number.isInteger(padByte) || padByte < 0 || padByte > 255) {\n throw new RangeError(`ByteCodec: invalid pad byte: ${padByte}`);\n }\n if (bytes.length > targetLength) {\n throw new RangeError(\n `ByteCodec: bytes length ${bytes.length} exceeds target ${targetLength}`,\n );\n }\n\n const out = new Uint8Array(targetLength);\n if (padByte !== 0) out.fill(padByte);\n out.set(bytes, targetLength - bytes.length);\n return out;\n }\n\n /**\n * Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),\n * reverting on overflow.\n *\n * This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:\n * - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero\n * - returns the low `targetLength` bytes as the result\n *\n * Examples:\n * - cast bytes32 -> u128: castUNbe(bytes32, 16)\n * - cast bytesN -> u64: castUNbe(bytesN, 8)\n */\n static castUNbe(bytes: Uint8Array, targetLength: number): bigint {\n if (!Number.isInteger(targetLength) || targetLength < 0) {\n throw new RangeError(`ByteCodec: invalid length: target=${targetLength}`);\n }\n\n const len = bytes.length;\n if (len <= targetLength) return ByteCodec.readUNbe(bytes, 0, len);\n\n // Overflow check: any excess high bytes must be 0x00.\n const excess = len - targetLength;\n for (let i = 0; i < excess; i++) {\n if (bytes[i] !== 0)\n throw ByteCodec.#outOfRange(targetLength, ByteCodec.readUNbe(bytes, 0, len));\n }\n\n return ByteCodec.readUNbe(bytes, excess, targetLength);\n }\n\n static castU8be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 1);\n }\n\n static castU16be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 2);\n }\n\n static castU32be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 4);\n }\n\n static castU64be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 8);\n }\n\n static castU128be(bytes: Uint8Array): bigint {\n return ByteCodec.castUNbe(bytes, 16);\n }\n\n protected ensureCapacity(additionalBytes: number): void {\n const needed = this.#cursor + additionalBytes;\n if (needed <= this.#buf.byteLength) return;\n\n let nextCap = this.#buf.byteLength;\n while (nextCap < needed) nextCap *= 2;\n\n // Grow by allocating a new ArrayBuffer and copying the written prefix\n // [0..cursor) into it. We then swap the backing buffer+view.\n const next = new ArrayBuffer(nextCap);\n new Uint8Array(next).set(new Uint8Array(this.#buf, 0, this.#cursor));\n this.#buf = next;\n this.#view = new DataView(this.#buf);\n }\n\n bytes(b: Uint8Array): this {\n this.ensureCapacity(b.length);\n new Uint8Array(this.#buf, this.#cursor, b.length).set(b);\n this.#cursor += b.length;\n return this;\n }\n\n bytes32(b: Uint8Array): this {\n return this.bytes(ByteCodec.leftPad(b, 32));\n }\n\n bool(b: boolean): this {\n return this.u8(b ? 1n : ByteCodec.#ZERO);\n }\n\n u8(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U8_MAX)\n throw new RangeError(`ByteCodec: value out of range for u8: ${v}`);\n this.ensureCapacity(1);\n this.#view.setUint8(this.#cursor, Number(v));\n this.#cursor += 1;\n return this;\n }\n\n u16be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U16_MAX)\n throw new RangeError(`ByteCodec: value out of range for u16: ${v}`);\n this.ensureCapacity(2);\n this.#view.setUint16(this.#cursor, Number(v), false);\n this.#cursor += 2;\n return this;\n }\n\n u32be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U32_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u32: ${v}`);\n }\n this.ensureCapacity(4);\n this.#view.setUint32(this.#cursor, Number(v), false);\n this.#cursor += 4;\n return this;\n }\n\n u64be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U64_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u64: ${v}`);\n }\n this.ensureCapacity(8);\n this.#view.setBigUint64(this.#cursor, v, false);\n this.#cursor += 8;\n return this;\n }\n\n u128be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U128_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u128: ${v}`);\n }\n this.ensureCapacity(16);\n const hi = (v >> 64n) & ByteCodec.#U64_MAX;\n const lo = v & ByteCodec.#U64_MAX;\n this.#view.setBigUint64(this.#cursor, hi, false);\n this.#view.setBigUint64(this.#cursor + 8, lo, false);\n this.#cursor += 16;\n return this;\n }\n\n u256be(v: bigint): this {\n if (v < ByteCodec.#ZERO || v > ByteCodec.#U256_MAX) {\n throw new RangeError(`ByteCodec: value out of range for u256: ${v}`);\n }\n this.ensureCapacity(32);\n const w0 = (v >> 192n) & ByteCodec.#U64_MAX;\n const w1 = (v >> 128n) & ByteCodec.#U64_MAX;\n const w2 = (v >> 64n) & ByteCodec.#U64_MAX;\n const w3 = v & ByteCodec.#U64_MAX;\n this.#view.setBigUint64(this.#cursor, w0, false);\n this.#view.setBigUint64(this.#cursor + 8, w1, false);\n this.#view.setBigUint64(this.#cursor + 16, w2, false);\n this.#view.setBigUint64(this.#cursor + 24, w3, false);\n this.#cursor += 32;\n return this;\n }\n /**\n * Returns a copy of the accumulated bytes (no shared backing buffer).\n */\n toBytes(): Uint8Array {\n return new Uint8Array(this.#buf, 0, this.#cursor).slice();\n }\n}\n"]}
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ var DF2CBFKF_cjs = require('./DF2CBFKF.cjs');
4
+
5
+
6
+
7
+ Object.defineProperty(exports, "ByteCodec", {
8
+ enumerable: true,
9
+ get: function () { return DF2CBFKF_cjs.ByteCodec; }
10
+ });
11
+ //# sourceMappingURL=byte-codec.cjs.map
12
+ //# sourceMappingURL=byte-codec.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"byte-codec.cjs"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Growable byte codec backed by `ArrayBuffer` + `DataView`.
3
+ *
4
+ * Low-level building block for constructing byte payloads deterministically.
5
+ * Higher-level concerns (e.g. hashing, ABI encoding, function signatures) should
6
+ * live in wrappers/subclasses.
7
+ */
8
+ export declare class ByteCodec {
9
+ #private;
10
+ /**
11
+ * Read helpers (big-endian). These are intentionally small/low-level so other
12
+ * packages can share a single implementation for parsing encoded payloads.
13
+ */
14
+ static readU16be(buf: Uint8Array, offset: number): number;
15
+ static readU8(buf: Uint8Array, offset: number): number;
16
+ static readU32be(buf: Uint8Array, offset: number): number;
17
+ static readU128be(buf: Uint8Array, offset: number): bigint;
18
+ static readUNbe(buf: Uint8Array, offset: number, length: number): bigint;
19
+ static readBytes32(buf: Uint8Array, offset: number): Uint8Array;
20
+ /**
21
+ * Left-pad `bytes` to `targetLength` using `padByte` (default 0x00).
22
+ *
23
+ * Commonly used to mimic Solidity's `bytes32(bytesN)`-style left zero padding.
24
+ */
25
+ static leftPad(bytes: Uint8Array, targetLength: number, padByte?: number): Uint8Array;
26
+ /**
27
+ * Cast an unsigned big-endian integer in `bytes` into a fixed-width uint (by bytes),
28
+ * reverting on overflow.
29
+ *
30
+ * This mirrors Solidity-style safe casts like `SafeCast.toUint128(uint256)`:
31
+ * - if `bytes.length > targetLength`, the high (bytes.length - targetLength) bytes must be all zero
32
+ * - returns the low `targetLength` bytes as the result
33
+ *
34
+ * Examples:
35
+ * - cast bytes32 -> u128: castUNbe(bytes32, 16)
36
+ * - cast bytesN -> u64: castUNbe(bytesN, 8)
37
+ */
38
+ static castUNbe(bytes: Uint8Array, targetLength: number): bigint;
39
+ static castU8be(bytes: Uint8Array): bigint;
40
+ static castU16be(bytes: Uint8Array): bigint;
41
+ static castU32be(bytes: Uint8Array): bigint;
42
+ static castU64be(bytes: Uint8Array): bigint;
43
+ static castU128be(bytes: Uint8Array): bigint;
44
+ protected ensureCapacity(additionalBytes: number): void;
45
+ bytes(b: Uint8Array): this;
46
+ bytes32(b: Uint8Array): this;
47
+ bool(b: boolean): this;
48
+ u8(v: bigint): this;
49
+ u16be(v: bigint): this;
50
+ u32be(v: bigint): this;
51
+ u64be(v: bigint): this;
52
+ u128be(v: bigint): this;
53
+ u256be(v: bigint): this;
54
+ /**
55
+ * Returns a copy of the accumulated bytes (no shared backing buffer).
56
+ */
57
+ toBytes(): Uint8Array;
58
+ }
59
+ //# sourceMappingURL=byte-codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"byte-codec.d.ts","sourceRoot":"","sources":["../src/byte-codec.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAWH,qBAAa,SAAS;;IAoBlB;;;OAGG;IACH,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAKzD,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAKtD,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM;IAKzD,MAAM,CAAC,UAA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