solidity-codecs 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/codecs/BytesX.d.ts +1 -1
- package/dist/codecs/Tuple.d.ts +1 -1
- package/dist/codecs/bool.d.ts +1 -1
- package/dist/codecs/str.d.ts +1 -1
- package/dist/solidity-codecs.cjs.development.js +55 -42
- package/dist/solidity-codecs.cjs.development.js.map +3 -3
- package/dist/solidity-codecs.cjs.production.min.js +1 -1
- package/dist/solidity-codecs.cjs.production.min.js.map +3 -3
- package/dist/solidity-codecs.js +53 -40
- package/dist/solidity-codecs.js.map +3 -3
- package/dist/solidity-codecs.mjs +53 -40
- package/dist/solidity-codecs.mjs.map +3 -3
- package/dist/types.d.ts +6 -3
- package/dist/utils.d.ts +0 -1
- package/package.json +1 -1
- package/dist/codecs/Bytes.d.ts +0 -6
package/dist/codecs/BytesX.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Codec } from "../types";
|
1
|
+
import type { Codec } from "../types";
|
2
2
|
export declare const bytes1: Codec<Uint8Array>, bytes2: Codec<Uint8Array>, bytes3: Codec<Uint8Array>, bytes4: Codec<Uint8Array>, bytes5: Codec<Uint8Array>, bytes6: Codec<Uint8Array>, bytes7: Codec<Uint8Array>, bytes8: Codec<Uint8Array>, bytes9: Codec<Uint8Array>, bytes10: Codec<Uint8Array>, bytes11: Codec<Uint8Array>, bytes12: Codec<Uint8Array>, bytes13: Codec<Uint8Array>, bytes14: Codec<Uint8Array>, bytes15: Codec<Uint8Array>, bytes16: Codec<Uint8Array>, bytes17: Codec<Uint8Array>, bytes18: Codec<Uint8Array>, bytes19: Codec<Uint8Array>, bytes20: Codec<Uint8Array>, bytes21: Codec<Uint8Array>, bytes22: Codec<Uint8Array>, bytes23: Codec<Uint8Array>, bytes24: Codec<Uint8Array>, bytes25: Codec<Uint8Array>, bytes26: Codec<Uint8Array>, bytes27: Codec<Uint8Array>, bytes28: Codec<Uint8Array>, bytes29: Codec<Uint8Array>, bytes30: Codec<Uint8Array>, bytes31: Codec<Uint8Array>, bytes32: Codec<Uint8Array>;
|
package/dist/codecs/Tuple.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Codec } from "../types";
|
1
|
+
import type { Codec } from "../types";
|
2
2
|
export declare const Tuple: <A extends Codec<any>[], OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown; }>(...codecs: A) => Codec<[...OT]>;
|
package/dist/codecs/bool.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Codec } from "../types";
|
1
|
+
import type { Codec } from "../types";
|
2
2
|
export declare const bool: Codec<boolean>;
|
package/dist/codecs/str.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export declare const str: import("
|
1
|
+
export declare const str: import("..").Codec<string>;
|
@@ -63,7 +63,6 @@ __export(src_exports, {
|
|
63
63
|
bytes7: () => bytes7,
|
64
64
|
bytes8: () => bytes8,
|
65
65
|
bytes9: () => bytes9,
|
66
|
-
createCodec: () => createCodec,
|
67
66
|
enhanceCodec: () => enhanceCodec,
|
68
67
|
enhanceDecoder: () => enhanceDecoder,
|
69
68
|
enhanceEncoder: () => enhanceEncoder,
|
@@ -138,26 +137,17 @@ __export(src_exports, {
|
|
138
137
|
});
|
139
138
|
module.exports = __toCommonJS(src_exports);
|
140
139
|
|
141
|
-
// src/
|
142
|
-
var
|
143
|
-
|
144
|
-
|
140
|
+
// src/internal/createCodec.ts
|
141
|
+
var createCodec = (encoder, decoder, selector) => {
|
142
|
+
;
|
143
|
+
encoder.s = selector;
|
144
|
+
decoder.s = selector;
|
145
145
|
const result = [encoder, decoder];
|
146
146
|
result.enc = encoder;
|
147
147
|
result.dec = decoder;
|
148
|
+
result.s = selector;
|
148
149
|
return result;
|
149
150
|
};
|
150
|
-
var dyn = (input, output) => {
|
151
|
-
if (input.dyn)
|
152
|
-
output.dyn = true;
|
153
|
-
return output;
|
154
|
-
};
|
155
|
-
var enhanceEncoder = (encoder, mapper) => dyn(encoder, (value) => encoder(mapper(value)));
|
156
|
-
var enhanceDecoder = (decoder, mapper) => dyn(decoder, (value) => mapper(decoder(value)));
|
157
|
-
var enhanceCodec = (codec, toFrom, fromTo) => dyn(codec, createCodec(enhanceEncoder(codec[0], toFrom), enhanceDecoder(codec[1], fromTo)));
|
158
|
-
|
159
|
-
// src/codecs/address.ts
|
160
|
-
var import_utils2 = require("@unstoppablejs/utils");
|
161
151
|
|
162
152
|
// src/internal/toInternalBytes.ts
|
163
153
|
var import_utils = require("@unstoppablejs/utils");
|
@@ -174,7 +164,21 @@ var toInternalBytes = (fn) => (buffer) => fn(buffer instanceof InternalUint8Arra
|
|
174
164
|
// src/internal/range32.ts
|
175
165
|
var range32 = Array(32).fill(0).map((_, idx) => idx + 1);
|
176
166
|
|
167
|
+
// src/utils.ts
|
168
|
+
var import_sha3 = require("@noble/hashes/sha3");
|
169
|
+
var keccak = import_sha3.keccak_256;
|
170
|
+
var dyn = (input, output) => {
|
171
|
+
if (input.d)
|
172
|
+
output.d = true;
|
173
|
+
output.s = input.s;
|
174
|
+
return output;
|
175
|
+
};
|
176
|
+
var enhanceEncoder = (encoder, mapper) => dyn(encoder, (value) => encoder(mapper(value)));
|
177
|
+
var enhanceDecoder = (decoder, mapper) => dyn(decoder, (value) => mapper(decoder(value)));
|
178
|
+
var enhanceCodec = (codec, toFrom, fromTo) => dyn(codec, createCodec(enhanceEncoder(codec[0], toFrom), enhanceDecoder(codec[1], fromTo), codec.s));
|
179
|
+
|
177
180
|
// src/codecs/address.ts
|
181
|
+
var import_utils2 = require("@unstoppablejs/utils");
|
178
182
|
var address = createCodec((input) => {
|
179
183
|
const result = new Uint8Array(32);
|
180
184
|
result.set((0, import_utils2.fromHex)(input), 12);
|
@@ -191,7 +195,7 @@ var address = createCodec((input) => {
|
|
191
195
|
result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char);
|
192
196
|
}
|
193
197
|
return result.join("");
|
194
|
-
}));
|
198
|
+
}), "address");
|
195
199
|
|
196
200
|
// src/codecs/Uint.ts
|
197
201
|
var getCodec = (nBytes) => {
|
@@ -212,7 +216,7 @@ var getCodec = (nBytes) => {
|
|
212
216
|
result = result << 64n | bytes33.v.getBigUint64(idx);
|
213
217
|
bytes33.i = nextBlock;
|
214
218
|
return result;
|
215
|
-
}));
|
219
|
+
}), "uint" + nBytes);
|
216
220
|
};
|
217
221
|
var [
|
218
222
|
uint8,
|
@@ -251,7 +255,9 @@ var [
|
|
251
255
|
var uint = uint256;
|
252
256
|
|
253
257
|
// src/codecs/bool.ts
|
254
|
-
var
|
258
|
+
var base = Object.assign([], uint8);
|
259
|
+
base.s = "bool";
|
260
|
+
var bool = enhanceCodec(base, (value) => value ? 1n : 0n, Boolean);
|
255
261
|
|
256
262
|
// src/codecs/bytes.ts
|
257
263
|
var import_utils5 = require("@unstoppablejs/utils");
|
@@ -264,7 +270,7 @@ var bytesEnc = (val) => {
|
|
264
270
|
}
|
265
271
|
return (0, import_utils5.mergeUint8)(...args);
|
266
272
|
};
|
267
|
-
bytesEnc.
|
273
|
+
bytesEnc.d = true;
|
268
274
|
var bytesDec = toInternalBytes((bytes33) => {
|
269
275
|
let nElements = Number(uint[1](bytes33));
|
270
276
|
const result = new Uint8Array(bytes33.buffer, bytes33.i, nElements);
|
@@ -274,14 +280,16 @@ var bytesDec = toInternalBytes((bytes33) => {
|
|
274
280
|
bytes33.i += 32 - extra;
|
275
281
|
return result;
|
276
282
|
});
|
277
|
-
bytesDec.
|
278
|
-
var bytes = createCodec(bytesEnc, bytesDec);
|
279
|
-
bytes.
|
283
|
+
bytesDec.d = true;
|
284
|
+
var bytes = createCodec(bytesEnc, bytesDec, "bytes");
|
285
|
+
bytes.d = true;
|
280
286
|
|
281
287
|
// src/codecs/str.ts
|
282
288
|
var textEncoder = new TextEncoder();
|
283
289
|
var textDecoder = new TextDecoder();
|
284
|
-
var
|
290
|
+
var base2 = Object.assign([], bytes);
|
291
|
+
base2.s = "string";
|
292
|
+
var str = enhanceCodec(base2, textEncoder.encode.bind(textEncoder), textDecoder.decode.bind(textDecoder));
|
285
293
|
|
286
294
|
// src/codecs/BytesX.ts
|
287
295
|
var bytesEnc2 = (nBytes) => (bytes33) => {
|
@@ -329,7 +337,7 @@ var [
|
|
329
337
|
bytes30,
|
330
338
|
bytes31,
|
331
339
|
bytes32
|
332
|
-
] = range32.map((nBytes) => createCodec(bytesEnc2(nBytes), bytesDec2(nBytes)));
|
340
|
+
] = range32.map((nBytes) => createCodec(bytesEnc2(nBytes), bytesDec2(nBytes), "bytes" + nBytes));
|
333
341
|
|
334
342
|
// src/codecs/Int.ts
|
335
343
|
var signGetters = {
|
@@ -393,7 +401,7 @@ var getCodec2 = (nBytes) => {
|
|
393
401
|
bytes33.i += 32;
|
394
402
|
return result;
|
395
403
|
});
|
396
|
-
return createCodec(enc, dec);
|
404
|
+
return createCodec(enc, dec, "int" + nBytes);
|
397
405
|
};
|
398
406
|
var [
|
399
407
|
int8,
|
@@ -432,7 +440,12 @@ var [
|
|
432
440
|
var int = int256;
|
433
441
|
|
434
442
|
// src/codecs/Fixed.ts
|
435
|
-
var Fixed = (baseCodec, decimals) =>
|
443
|
+
var Fixed = (baseCodec, decimals) => {
|
444
|
+
const baseSelector = baseCodec.s;
|
445
|
+
const eBaseCodec = Object.assign([], baseCodec);
|
446
|
+
eBaseCodec.s = (baseSelector[0] === "u" ? "ufixed" + baseSelector.slice(4) : "fixed" + baseSelector.slice(3)) + "x" + decimals;
|
447
|
+
return enhanceCodec(eBaseCodec, (x) => x.value, (value) => ({ value, decimals }));
|
448
|
+
};
|
436
449
|
|
437
450
|
// src/codecs/Tuple.ts
|
438
451
|
var import_utils8 = require("@unstoppablejs/utils");
|
@@ -443,7 +456,7 @@ var dynamicEnc = (...encoders) => {
|
|
443
456
|
const dinamics = [];
|
444
457
|
let len = 0n;
|
445
458
|
for (let i = 0; i < encoders.length; i++) {
|
446
|
-
if (encoders[i].
|
459
|
+
if (encoders[i].d) {
|
447
460
|
dinamics.push(i);
|
448
461
|
len += 32n;
|
449
462
|
} else {
|
@@ -459,7 +472,7 @@ var dynamicEnc = (...encoders) => {
|
|
459
472
|
});
|
460
473
|
return (0, import_utils8.mergeUint8)(...resultArray);
|
461
474
|
};
|
462
|
-
res.
|
475
|
+
res.d = true;
|
463
476
|
return res;
|
464
477
|
};
|
465
478
|
var staticEnc = (...encoders) => (values) => (0, import_utils8.mergeUint8)(...values.map((value, idx) => encoders[idx](value)));
|
@@ -469,7 +482,7 @@ var dynamicDec = (...decoders) => {
|
|
469
482
|
const result = new Array(decoders.length);
|
470
483
|
let start = bytes33.i;
|
471
484
|
for (let i = 0; i < decoders.length; i++) {
|
472
|
-
if (decoders[i].
|
485
|
+
if (decoders[i].d) {
|
473
486
|
const offset = Number(uint[1](bytes33));
|
474
487
|
const current = bytes33.i;
|
475
488
|
bytes33.i = start + offset;
|
@@ -481,14 +494,14 @@ var dynamicDec = (...decoders) => {
|
|
481
494
|
}
|
482
495
|
return result;
|
483
496
|
});
|
484
|
-
res.
|
497
|
+
res.d = true;
|
485
498
|
return res;
|
486
499
|
};
|
487
500
|
var Tuple = (...codecs) => {
|
488
|
-
const isDyn = codecs.some((c) => c.
|
501
|
+
const isDyn = codecs.some((c) => c.d);
|
489
502
|
const [enc, dec] = isDyn ? [dynamicEnc, dynamicDec] : [staticEnc, staticDec];
|
490
|
-
const res = createCodec(enc(...codecs.map(([encoder]) => encoder)), dec(...codecs.map(([, decoder]) => decoder)));
|
491
|
-
res.
|
503
|
+
const res = createCodec(enc(...codecs.map(([encoder]) => encoder)), dec(...codecs.map(([, decoder]) => decoder)), `(${codecs.map((c) => c.s).join(",")})`);
|
504
|
+
res.d = isDyn;
|
492
505
|
return res;
|
493
506
|
};
|
494
507
|
|
@@ -499,13 +512,13 @@ var Struct = (codecs) => {
|
|
499
512
|
};
|
500
513
|
|
501
514
|
// src/codecs/Vector.ts
|
502
|
-
var
|
515
|
+
var import_utils10 = require("@unstoppablejs/utils");
|
503
516
|
var vectorEnc = (inner, size) => {
|
504
517
|
const result = (value) => {
|
505
518
|
const isNotFixed = size == null ? 1 : 0;
|
506
519
|
const actualSize = isNotFixed ? value.length : size;
|
507
520
|
let data;
|
508
|
-
if (inner.
|
521
|
+
if (inner.d) {
|
509
522
|
data = new Array(actualSize * 2);
|
510
523
|
let offset = actualSize * 32;
|
511
524
|
for (let i = 0; i < actualSize; i++) {
|
@@ -521,16 +534,16 @@ var vectorEnc = (inner, size) => {
|
|
521
534
|
}
|
522
535
|
if (isNotFixed)
|
523
536
|
data.unshift(uint.enc(BigInt(value.length)));
|
524
|
-
return (0,
|
537
|
+
return (0, import_utils10.mergeUint8)(...data);
|
525
538
|
};
|
526
|
-
result.
|
539
|
+
result.d = true;
|
527
540
|
return result;
|
528
541
|
};
|
529
542
|
var vectorDec = (getter, size) => {
|
530
543
|
const decoder = toInternalBytes((bytes33) => {
|
531
544
|
const nElements = size >= 0 ? size : Number(uint[1](bytes33));
|
532
545
|
const decoded = new Array(nElements);
|
533
|
-
if (getter.
|
546
|
+
if (getter.d) {
|
534
547
|
const init = bytes33.i;
|
535
548
|
let current = init;
|
536
549
|
for (let i = 0; i < nElements; i++) {
|
@@ -547,13 +560,13 @@ var vectorDec = (getter, size) => {
|
|
547
560
|
}
|
548
561
|
return decoded;
|
549
562
|
});
|
550
|
-
decoder.
|
563
|
+
decoder.d = true;
|
551
564
|
return decoder;
|
552
565
|
};
|
553
566
|
var Vector = (inner, size) => {
|
554
|
-
const codec = createCodec(vectorEnc(inner[0], size), vectorDec(inner[1], size));
|
567
|
+
const codec = createCodec(vectorEnc(inner[0], size), vectorDec(inner[1], size), inner.s + `[${size == null ? "" : size}]`);
|
555
568
|
if (size == null)
|
556
|
-
codec.
|
569
|
+
codec.d = true;
|
557
570
|
return codec;
|
558
571
|
};
|
559
572
|
//# sourceMappingURL=solidity-codecs.cjs.development.js.map
|
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"version": 3,
|
3
|
-
"sources": ["../src/index.ts", "../src/
|
4
|
-
"sourcesContent": ["export * from \"./utils\"\nexport * from \"./types\"\nexport * from \"./codecs\"\n", "import type { Codec, Decoder, Encoder } from \"./types\"\nimport { keccak_256 } from \"@noble/hashes/sha3\"\n\nexport const keccak = keccak_256\n\nexport const createCodec = <T>(\n encoder: Encoder<T>,\n decoder: Decoder<T>,\n): Codec<T> => {\n const result = [encoder, decoder] as any\n result.enc = encoder\n result.dec = decoder\n return result\n}\n\nconst dyn = <T extends { dyn?: boolean }>(\n input: { dyn?: boolean },\n output: T,\n): T => {\n if (input.dyn) output.dyn = true\n return output\n}\n\nexport const enhanceEncoder = <I, O>(\n encoder: Encoder<I>,\n mapper: (value: O) => I,\n): Encoder<O> => dyn(encoder, ((value) => encoder(mapper(value))) as Encoder<O>)\n\nexport const enhanceDecoder = <I, O>(\n decoder: Decoder<I>,\n mapper: (value: I) => O,\n): Decoder<O> => dyn(decoder, ((value) => mapper(decoder(value))) as Decoder<O>)\n\nexport const enhanceCodec = <I, O>(\n codec: Codec<I>,\n toFrom: (value: O) => I,\n fromTo: (value: I) => O,\n): Codec<O> =>\n dyn(\n codec,\n createCodec(\n enhanceEncoder(codec[0], toFrom),\n enhanceDecoder(codec[1], fromTo),\n ),\n )\n", "import { fromHex, toHex } from \"@unstoppablejs/utils\"\nimport { toInternalBytes } from \"../internal\"\nimport { createCodec, keccak } from \"../utils\"\n\nexport const address = createCodec(\n (input: string) => {\n const result = new Uint8Array(32)\n result.set(fromHex(input), 12)\n return result\n },\n toInternalBytes((bytes) => {\n const binaryAddress = new Uint8Array(bytes.buffer, bytes.i + 12, 20)\n bytes.i += 32\n const nonChecksum = toHex(binaryAddress)\n const hashedAddres = toHex(keccak(nonChecksum.slice(2)))\n\n const result = new Array(41)\n result[0] = \"0x\"\n for (let i = 2; i < 42; i++) {\n const char = nonChecksum[i]\n result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char)\n }\n\n return result.join(\"\")\n }),\n)\n", "import { fromHex } from \"@unstoppablejs/utils\"\nimport { Decoder } from \"../types\"\n\nclass InternalUint8Array extends Uint8Array {\n i: number = 0\n v: DataView\n\n constructor(buffer: ArrayBuffer) {\n super(buffer)\n this.v = new DataView(buffer)\n }\n}\n\nexport const toInternalBytes =\n <T>(fn: (input: InternalUint8Array) => T): Decoder<T> =>\n (buffer: string | ArrayBuffer | Uint8Array | InternalUint8Array) =>\n fn(\n buffer instanceof InternalUint8Array\n ? buffer\n : new InternalUint8Array(\n buffer instanceof Uint8Array\n ? buffer.buffer\n : typeof buffer === \"string\"\n ? fromHex(buffer).buffer\n : buffer,\n ),\n )\n", "export const range32 = Array(32)\n .fill(0)\n .map((_, idx) => idx + 1)\n", "import { Codec } from \"../types\"\nimport { range32, toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = Math.ceil(nBytes / 8)\n return createCodec(\n (input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n const idxLimit = 32 - n64 * 8\n for (let idx = 24; idx >= idxLimit; idx -= 8) {\n dv.setBigUint64(idx, input)\n input >>= 64n\n }\n\n return result\n },\n toInternalBytes((bytes) => {\n let result = 0n\n\n const nextBlock = bytes.i + 32\n for (let idx = bytes.i + (32 - n64 * 8); idx < nextBlock; idx += 8)\n result = (result << 64n) | bytes.v.getBigUint64(idx)\n\n bytes.i = nextBlock\n return result\n }),\n )\n}\n\nexport const [\n uint8,\n uint16,\n uint24,\n uint32,\n uint40,\n uint48,\n uint56,\n uint64,\n uint72,\n uint80,\n uint88,\n uint96,\n uint104,\n uint112,\n uint120,\n uint128,\n uint136,\n uint144,\n uint152,\n uint160,\n uint168,\n uint176,\n uint184,\n uint192,\n uint200,\n uint208,\n uint226,\n uint224,\n uint232,\n uint240,\n uint248,\n uint256,\n] = range32.map(getCodec)\nexport const uint = uint256\n", "import { Codec } from \"../types\"\nimport { enhanceCodec } from \"../\"\nimport { uint8 } from \"./Uint\"\n\nexport const bool: Codec<boolean> = enhanceCodec(\n uint8,\n (value: boolean) => (value ? 1n : 0n),\n Boolean,\n)\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { createCodec, Decoder, Encoder } from \"../\"\nimport { toInternalBytes } from \"../internal\"\nimport { uint } from \"./Uint\"\n\nconst bytesEnc: Encoder<Uint8Array> = (val) => {\n const args = [uint[0](BigInt(val.length)), val] as const\n const extra = val.length % 32\n if (extra > 0) {\n ;(args as any).push(new Uint8Array(32 - extra))\n }\n return mergeUint8(...args)\n}\nbytesEnc.dyn = true\n\nconst bytesDec: Decoder<Uint8Array> = toInternalBytes((bytes) => {\n let nElements = Number(uint[1](bytes))\n const result = new Uint8Array(bytes.buffer, bytes.i, nElements)\n bytes.i += nElements\n const extra = nElements % 32\n if (extra > 0) bytes.i += 32 - extra\n return result\n})\nbytesDec.dyn = true\n\nexport const bytes = createCodec(bytesEnc, bytesDec)\nbytes.dyn = true\n", "import { enhanceCodec } from \"../\"\nimport { bytes } from \"./bytes\"\n\nconst textEncoder = new TextEncoder()\nconst textDecoder = new TextDecoder()\n\nexport const str = enhanceCodec<Uint8Array, string>(\n bytes,\n textEncoder.encode.bind(textEncoder),\n textDecoder.decode.bind(textDecoder),\n)\n", "import { Encoder, Decoder, Codec } from \"../types\"\nimport { createCodec } from \"../\"\nimport { range32, toInternalBytes } from \"../internal\"\n\nconst bytesEnc =\n (nBytes: number): Encoder<Uint8Array> =>\n (bytes) => {\n if (bytes.length === nBytes && nBytes === 32) return bytes\n const result = new Uint8Array(32)\n result.set(bytes.length === nBytes ? bytes : bytes.slice(0, nBytes))\n return result\n }\n\nconst bytesDec = (nBytes: number): Decoder<Uint8Array> =>\n toInternalBytes((bytes) => {\n const result = new Uint8Array(bytes.buffer, bytes.i, nBytes)\n bytes.i += 32\n return result\n })\n\nexport const [\n bytes1,\n bytes2,\n bytes3,\n bytes4,\n bytes5,\n bytes6,\n bytes7,\n bytes8,\n bytes9,\n bytes10,\n bytes11,\n bytes12,\n bytes13,\n bytes14,\n bytes15,\n bytes16,\n bytes17,\n bytes18,\n bytes19,\n bytes20,\n bytes21,\n bytes22,\n bytes23,\n bytes24,\n bytes25,\n bytes26,\n bytes27,\n bytes28,\n bytes29,\n bytes30,\n bytes31,\n bytes32,\n] = range32.map(\n (nBytes: number): Codec<Uint8Array> =>\n createCodec(bytesEnc(nBytes), bytesDec(nBytes)),\n)\n", "import { Encoder, Codec } from \"../types\"\nimport { range32, toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\n\nconst signGetters: Record<1 | 2 | 8, \"getBigInt64\" | \"getInt16\" | \"getInt8\"> = {\n \"1\": \"getInt8\",\n \"2\": \"getInt16\",\n \"8\": \"getBigInt64\",\n}\n\nconst signSetters: Record<1 | 2 | 8, \"setBigInt64\" | \"setInt16\" | \"setInt8\"> = {\n \"1\": \"setInt8\",\n \"2\": \"setInt16\",\n \"8\": \"setBigInt64\",\n}\n\nconst usignGetters: Record<\n 1 | 2 | 8,\n \"getBigUint64\" | \"getUint16\" | \"getUint8\"\n> = {\n \"1\": \"getUint8\",\n \"2\": \"getUint16\",\n \"8\": \"getBigUint64\",\n}\n\nconst usignSetters: Record<\n 1 | 2 | 8,\n \"setBigUint64\" | \"setUint16\" | \"setUint8\"\n> = {\n \"1\": \"setUint8\",\n \"2\": \"setUint16\",\n \"8\": \"setBigUint64\",\n}\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = (nBytes / 8) | 0\n const n16 = ((nBytes % 8) / 2) | 0\n const sequence = [\n ...Array(n64).fill([8, 64n, (x: bigint) => x]),\n ...Array(n16).fill([2, 16n, (x: bigint) => Number(x & 65535n)]),\n ]\n if (nBytes % 2) sequence.push([1, 8n, (x: bigint) => Number(x & 255n)])\n\n const enc: Encoder<bigint> = (input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n if (input < 0n) {\n for (let i = 0; i < 32 - nBytes; i += 8) dv.setBigInt64(i, -1n)\n }\n\n let idx = 32\n for (let i = sequence.length - 1; i > 0; i--) {\n const [bytes, shift, fn] = sequence[i] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[usignSetters[bytes]](idx, fn(input) as never)\n input >>= shift\n }\n const [bytes, , fn] = sequence[0] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[signSetters[bytes]](idx, fn(input) as never)\n\n return result\n }\n\n const dec = toInternalBytes((bytes) => {\n let idx = bytes.i + 32 - nBytes\n\n const bits = sequence[0][0] as 8\n let result = BigInt(bytes.v[signGetters[bits]](idx))\n idx += bits\n\n for (let i = 1; i < sequence.length; i++) {\n const [bits, shift] = sequence[i] as [1, 8n]\n result = (result << shift) | BigInt(bytes.v[usignGetters[bits]](idx))\n idx += bits\n }\n\n bytes.i += 32\n return result\n })\n\n return createCodec(enc, dec)\n}\n\nexport const [\n int8,\n int16,\n int24,\n int32,\n int40,\n int48,\n int56,\n int64,\n int72,\n int80,\n int88,\n int96,\n int104,\n int112,\n int120,\n int128,\n int136,\n int144,\n int152,\n int160,\n int168,\n int176,\n int184,\n int192,\n int200,\n int208,\n int226,\n int224,\n int232,\n int240,\n int248,\n int256,\n] = range32.map(getCodec)\nexport const int = int256\n", "import { Codec } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\n\nexport interface Decimal<T extends number = number> {\n value: bigint\n decimals: T\n}\n\nexport const Fixed = <D extends number>(\n baseCodec: Codec<bigint>,\n decimals: D,\n) =>\n enhanceCodec<bigint, Decimal<D>>(\n baseCodec,\n (x) => x.value,\n (value) => ({ value, decimals }),\n )\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\nimport { toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\n\nconst dynamicEnc = <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n>(\n ...encoders: A\n): Encoder<[...OT]> => {\n const res: Encoder<[...OT]> = (values) => {\n const mapped = values.map((value, idx) => encoders[idx](value))\n const resultArray = new Array<Uint8Array>(encoders.length)\n const dinamics = []\n let len = 0n\n for (let i = 0; i < encoders.length; i++) {\n if (encoders[i].dyn) {\n dinamics.push(i)\n len += 32n\n } else {\n resultArray[i] = mapped[i]\n len += BigInt(mapped[i].length)\n }\n }\n\n dinamics.forEach((idx) => {\n resultArray[idx] = uint[0](len)\n const data = mapped[idx]\n resultArray.push(data)\n len += BigInt(data.length)\n })\n\n return mergeUint8(...resultArray)\n }\n\n res.dyn = true\n return res\n}\n\nconst staticEnc =\n <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n >(\n ...encoders: A\n ): Encoder<[...OT]> =>\n (values) =>\n mergeUint8(...values.map((value, idx) => encoders[idx](value)))\n\nconst staticDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> =>\n toInternalBytes(\n (bytes) => decoders.map((decoder) => decoder(bytes)) as [...OT],\n )\nconst dynamicDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> => {\n const res: Decoder<[...OT]> = toInternalBytes((bytes) => {\n const result = new Array(decoders.length) as [...OT]\n let start = bytes.i\n for (let i = 0; i < decoders.length; i++) {\n if (decoders[i].dyn) {\n const offset = Number(uint[1](bytes))\n const current = bytes.i\n bytes.i = start + offset\n result[i] = decoders[i](bytes)\n bytes.i = current\n } else {\n result[i] = decoders[i](bytes)\n }\n }\n return result\n })\n res.dyn = true\n return res\n}\n\nexport const Tuple = <\n A extends Array<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n ...codecs: A\n): Codec<[...OT]> => {\n const isDyn = codecs.some((c) => c.dyn)\n const [enc, dec] = isDyn\n ? ([dynamicEnc, dynamicDec] as const)\n : ([staticEnc, staticDec] as const)\n\n const res: Codec<[...OT]> = createCodec(\n enc(...codecs.map(([encoder]) => encoder)),\n dec(...codecs.map(([, decoder]) => decoder)),\n )\n res.dyn = isDyn\n return res\n}\n", "import { Codec, StringRecord } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\nimport { Tuple } from \"./Tuple\"\n\nexport const Struct = <\n A extends StringRecord<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n codecs: A,\n): Codec<OT> => {\n const keys = Object.keys(codecs)\n return enhanceCodec(\n Tuple(...Object.values(codecs)),\n (input: OT) => keys.map((k) => input[k]),\n (tuple: Array<any>) =>\n Object.fromEntries(tuple.map((value, idx) => [keys[idx], value])) as OT,\n )\n}\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\nimport { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\n\nconst vectorEnc = <T>(inner: Encoder<T>, size?: number): Encoder<Array<T>> => {\n const result: Encoder<Array<T>> = (value) => {\n const isNotFixed = size == null ? 1 : 0\n const actualSize = isNotFixed ? value.length : size!\n let data: Array<Uint8Array>\n if (inner.dyn) {\n data = new Array<Uint8Array>(actualSize * 2)\n let offset = actualSize * 32\n for (let i = 0; i < actualSize; i++) {\n const encoded = inner(value[i])\n data[i] = uint.enc(BigInt(offset))\n offset += encoded.byteLength\n data[i + actualSize] = encoded\n }\n } else {\n data = new Array<Uint8Array>(actualSize)\n for (let i = 0; i < actualSize; i++) data[i] = inner(value[i])\n }\n if (isNotFixed) data!.unshift(uint.enc(BigInt(value.length)))\n return mergeUint8(...data)\n }\n result.dyn = true\n return result\n}\n\nconst vectorDec = <T>(getter: Decoder<T>, size?: number): Decoder<Array<T>> => {\n const decoder = toInternalBytes((bytes) => {\n const nElements = size! >= 0 ? size! : Number(uint[1](bytes))\n const decoded = new Array(nElements)\n\n if (getter.dyn) {\n const init = bytes.i\n let current = init\n for (let i = 0; i < nElements; i++) {\n bytes.i = current\n const offset = Number(uint.dec(bytes))\n current = bytes.i\n bytes.i = init + offset\n decoded[i] = getter(bytes)\n }\n } else {\n for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n }\n\n return decoded\n })\n decoder.dyn = true\n return decoder\n}\n\nexport const Vector = <T>(inner: Codec<T>, size?: number): Codec<Array<T>> => {\n const codec = createCodec(\n vectorEnc(inner[0], size),\n vectorDec(inner[1], size),\n )\n if (size == null) codec.dyn = true\n return codec\n}\n"],
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA
|
3
|
+
"sources": ["../src/index.ts", "../src/internal/createCodec.ts", "../src/internal/toInternalBytes.ts", "../src/internal/range32.ts", "../src/utils.ts", "../src/codecs/address.ts", "../src/codecs/Uint.ts", "../src/codecs/bool.ts", "../src/codecs/bytes.ts", "../src/codecs/str.ts", "../src/codecs/BytesX.ts", "../src/codecs/Int.ts", "../src/codecs/Fixed.ts", "../src/codecs/Tuple.ts", "../src/codecs/Struct.ts", "../src/codecs/Vector.ts"],
|
4
|
+
"sourcesContent": ["export * from \"./utils\"\nexport * from \"./types\"\nexport * from \"./codecs\"\n", "import type { Codec } from \"../types\"\n\nexport const createCodec = <T>(\n encoder: (value: T) => Uint8Array,\n decoder: (value: Uint8Array) => T,\n selector: string,\n): Codec<T> => {\n ;(encoder as any).s = selector\n ;(decoder as any).s = selector\n const result = [encoder, decoder] as any\n result.enc = encoder\n result.dec = decoder\n result.s = selector\n return result\n}\n", "import { fromHex } from \"@unstoppablejs/utils\"\nimport { Decoder } from \"../types\"\n\nclass InternalUint8Array extends Uint8Array {\n i: number = 0\n v: DataView\n\n constructor(buffer: ArrayBuffer) {\n super(buffer)\n this.v = new DataView(buffer)\n }\n}\n\nexport const toInternalBytes = <T>(\n fn: (input: InternalUint8Array) => T,\n): Decoder<T> =>\n ((buffer: string | ArrayBuffer | Uint8Array | InternalUint8Array) =>\n fn(\n buffer instanceof InternalUint8Array\n ? buffer\n : new InternalUint8Array(\n buffer instanceof Uint8Array\n ? buffer.buffer\n : typeof buffer === \"string\"\n ? fromHex(buffer).buffer\n : buffer,\n ),\n )) as Decoder<T>\n", "export const range32 = Array(32)\n .fill(0)\n .map((_, idx) => idx + 1)\n", "import type { Codec, Decoder, Encoder } from \"./types\"\nimport { createCodec } from \"./internal\"\nimport { keccak_256 } from \"@noble/hashes/sha3\"\n\nexport const keccak = keccak_256\n\nconst dyn = <T extends { d?: boolean; s: string }>(\n input: { d?: boolean; s: string },\n output: T,\n): T => {\n if (input.d) output.d = true\n output.s = input.s\n return output\n}\n\nexport const enhanceEncoder = <I, O>(\n encoder: Encoder<I>,\n mapper: (value: O) => I,\n): Encoder<O> => dyn(encoder, ((value) => encoder(mapper(value))) as Encoder<O>)\n\nexport const enhanceDecoder = <I, O>(\n decoder: Decoder<I>,\n mapper: (value: I) => O,\n): Decoder<O> => dyn(decoder, ((value) => mapper(decoder(value))) as Decoder<O>)\n\nexport const enhanceCodec = <I, O>(\n codec: Codec<I>,\n toFrom: (value: O) => I,\n fromTo: (value: I) => O,\n): Codec<O> =>\n dyn(\n codec,\n createCodec(\n enhanceEncoder(codec[0], toFrom),\n enhanceDecoder(codec[1], fromTo),\n codec.s,\n ),\n )\n", "import { fromHex, toHex } from \"@unstoppablejs/utils\"\nimport { createCodec, toInternalBytes } from \"../internal\"\nimport { keccak } from \"../utils\"\n\nexport const address = createCodec<string>(\n (input: string) => {\n const result = new Uint8Array(32)\n result.set(fromHex(input), 12)\n return result\n },\n toInternalBytes((bytes) => {\n const binaryAddress = new Uint8Array(bytes.buffer, bytes.i + 12, 20)\n bytes.i += 32\n const nonChecksum = toHex(binaryAddress)\n const hashedAddres = toHex(keccak(nonChecksum.slice(2)))\n\n const result = new Array(41)\n result[0] = \"0x\"\n for (let i = 2; i < 42; i++) {\n const char = nonChecksum[i]\n result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char)\n }\n\n return result.join(\"\")\n }),\n \"address\",\n)\n", "import { Codec } from \"../types\"\nimport { range32, toInternalBytes, createCodec } from \"../internal\"\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = Math.ceil(nBytes / 8)\n return createCodec(\n (input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n const idxLimit = 32 - n64 * 8\n for (let idx = 24; idx >= idxLimit; idx -= 8) {\n dv.setBigUint64(idx, input)\n input >>= 64n\n }\n\n return result\n },\n toInternalBytes((bytes) => {\n let result = 0n\n\n const nextBlock = bytes.i + 32\n for (let idx = bytes.i + (32 - n64 * 8); idx < nextBlock; idx += 8)\n result = (result << 64n) | bytes.v.getBigUint64(idx)\n\n bytes.i = nextBlock\n return result\n }),\n \"uint\" + nBytes,\n )\n}\n\nexport const [\n uint8,\n uint16,\n uint24,\n uint32,\n uint40,\n uint48,\n uint56,\n uint64,\n uint72,\n uint80,\n uint88,\n uint96,\n uint104,\n uint112,\n uint120,\n uint128,\n uint136,\n uint144,\n uint152,\n uint160,\n uint168,\n uint176,\n uint184,\n uint192,\n uint200,\n uint208,\n uint226,\n uint224,\n uint232,\n uint240,\n uint248,\n uint256,\n] = range32.map(getCodec)\nexport const uint = uint256\n", "import type { Codec } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\nimport { uint8 } from \"./Uint\"\n\nconst base = Object.assign([], uint8)\nbase.s = \"bool\"\n\nexport const bool: Codec<boolean> = enhanceCodec(\n base,\n (value: boolean) => (value ? 1n : 0n),\n Boolean,\n)\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport type { Decoder, Encoder } from \"../types\"\nimport { toInternalBytes, createCodec } from \"../internal\"\nimport { uint } from \"./Uint\"\n\nconst bytesEnc = ((val) => {\n const args = [uint[0](BigInt(val.length)), val] as const\n const extra = val.length % 32\n if (extra > 0) {\n ;(args as any).push(new Uint8Array(32 - extra))\n }\n return mergeUint8(...args)\n}) as Encoder<Uint8Array>\nbytesEnc.d = true\n\nconst bytesDec: Decoder<Uint8Array> = toInternalBytes((bytes) => {\n let nElements = Number(uint[1](bytes))\n const result = new Uint8Array(bytes.buffer, bytes.i, nElements)\n bytes.i += nElements\n const extra = nElements % 32\n if (extra > 0) bytes.i += 32 - extra\n return result\n})\nbytesDec.d = true\n\nexport const bytes = createCodec(bytesEnc, bytesDec, \"bytes\")\nbytes.d = true\n", "import { enhanceCodec } from \"../utils\"\nimport { bytes } from \"./bytes\"\n\nconst textEncoder = new TextEncoder()\nconst textDecoder = new TextDecoder()\n\nconst base = Object.assign([], bytes)\nbase.s = \"string\"\n\nexport const str = enhanceCodec<Uint8Array, string>(\n base,\n textEncoder.encode.bind(textEncoder),\n textDecoder.decode.bind(textDecoder),\n)\n", "import type { Encoder, Decoder, Codec } from \"../types\"\nimport { range32, toInternalBytes, createCodec } from \"../internal\"\n\nconst bytesEnc = (nBytes: number): Encoder<Uint8Array> =>\n ((bytes) => {\n if (bytes.length === nBytes && nBytes === 32) return bytes\n const result = new Uint8Array(32)\n result.set(bytes.length === nBytes ? bytes : bytes.slice(0, nBytes))\n return result\n }) as Encoder<Uint8Array>\n\nconst bytesDec = (nBytes: number): Decoder<Uint8Array> =>\n toInternalBytes((bytes) => {\n const result = new Uint8Array(bytes.buffer, bytes.i, nBytes)\n bytes.i += 32\n return result\n })\n\nexport const [\n bytes1,\n bytes2,\n bytes3,\n bytes4,\n bytes5,\n bytes6,\n bytes7,\n bytes8,\n bytes9,\n bytes10,\n bytes11,\n bytes12,\n bytes13,\n bytes14,\n bytes15,\n bytes16,\n bytes17,\n bytes18,\n bytes19,\n bytes20,\n bytes21,\n bytes22,\n bytes23,\n bytes24,\n bytes25,\n bytes26,\n bytes27,\n bytes28,\n bytes29,\n bytes30,\n bytes31,\n bytes32,\n] = range32.map(\n (nBytes: number): Codec<Uint8Array> =>\n createCodec(bytesEnc(nBytes), bytesDec(nBytes), \"bytes\" + nBytes),\n)\n", "import { Encoder, Codec } from \"../types\"\nimport { range32, toInternalBytes, createCodec } from \"../internal\"\n\nconst signGetters: Record<1 | 2 | 8, \"getBigInt64\" | \"getInt16\" | \"getInt8\"> = {\n \"1\": \"getInt8\",\n \"2\": \"getInt16\",\n \"8\": \"getBigInt64\",\n}\n\nconst signSetters: Record<1 | 2 | 8, \"setBigInt64\" | \"setInt16\" | \"setInt8\"> = {\n \"1\": \"setInt8\",\n \"2\": \"setInt16\",\n \"8\": \"setBigInt64\",\n}\n\nconst usignGetters: Record<\n 1 | 2 | 8,\n \"getBigUint64\" | \"getUint16\" | \"getUint8\"\n> = {\n \"1\": \"getUint8\",\n \"2\": \"getUint16\",\n \"8\": \"getBigUint64\",\n}\n\nconst usignSetters: Record<\n 1 | 2 | 8,\n \"setBigUint64\" | \"setUint16\" | \"setUint8\"\n> = {\n \"1\": \"setUint8\",\n \"2\": \"setUint16\",\n \"8\": \"setBigUint64\",\n}\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = (nBytes / 8) | 0\n const n16 = ((nBytes % 8) / 2) | 0\n const sequence = [\n ...Array(n64).fill([8, 64n, (x: bigint) => x]),\n ...Array(n16).fill([2, 16n, (x: bigint) => Number(x & 65535n)]),\n ]\n if (nBytes % 2) sequence.push([1, 8n, (x: bigint) => Number(x & 255n)])\n\n const enc = ((input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n if (input < 0n) {\n for (let i = 0; i < 32 - nBytes; i += 8) dv.setBigInt64(i, -1n)\n }\n\n let idx = 32\n for (let i = sequence.length - 1; i > 0; i--) {\n const [bytes, shift, fn] = sequence[i] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[usignSetters[bytes]](idx, fn(input) as never)\n input >>= shift\n }\n const [bytes, , fn] = sequence[0] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[signSetters[bytes]](idx, fn(input) as never)\n\n return result\n }) as Encoder<bigint>\n\n const dec = toInternalBytes((bytes) => {\n let idx = bytes.i + 32 - nBytes\n\n const bits = sequence[0][0] as 8\n let result = BigInt(bytes.v[signGetters[bits]](idx))\n idx += bits\n\n for (let i = 1; i < sequence.length; i++) {\n const [bits, shift] = sequence[i] as [1, 8n]\n result = (result << shift) | BigInt(bytes.v[usignGetters[bits]](idx))\n idx += bits\n }\n\n bytes.i += 32\n return result\n })\n\n return createCodec(enc, dec, \"int\" + nBytes)\n}\n\nexport const [\n int8,\n int16,\n int24,\n int32,\n int40,\n int48,\n int56,\n int64,\n int72,\n int80,\n int88,\n int96,\n int104,\n int112,\n int120,\n int128,\n int136,\n int144,\n int152,\n int160,\n int168,\n int176,\n int184,\n int192,\n int200,\n int208,\n int226,\n int224,\n int232,\n int240,\n int248,\n int256,\n] = range32.map(getCodec)\nexport const int = int256\n", "import { Codec } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\n\nexport interface Decimal<T extends number = number> {\n value: bigint\n decimals: T\n}\n\nexport const Fixed = <D extends number>(\n baseCodec: Codec<bigint>,\n decimals: D,\n) => {\n const baseSelector = baseCodec.s\n const eBaseCodec = Object.assign([], baseCodec)\n eBaseCodec.s =\n (baseSelector[0] === \"u\"\n ? \"ufixed\" + baseSelector.slice(4)\n : \"fixed\" + baseSelector.slice(3)) +\n \"x\" +\n decimals\n return enhanceCodec<bigint, Decimal<D>>(\n eBaseCodec,\n (x) => x.value,\n (value) => ({ value, decimals }),\n )\n}\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport type { Codec, Decoder, Encoder } from \"../types\"\nimport { toInternalBytes, createCodec } from \"../internal\"\nimport { uint } from \"./Uint\"\n\nconst dynamicEnc = <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n>(\n ...encoders: A\n): Encoder<[...OT]> => {\n const res = ((values) => {\n const mapped = values.map((value, idx) => encoders[idx](value))\n const resultArray = new Array<Uint8Array>(encoders.length)\n const dinamics = []\n let len = 0n\n for (let i = 0; i < encoders.length; i++) {\n if (encoders[i].d) {\n dinamics.push(i)\n len += 32n\n } else {\n resultArray[i] = mapped[i]\n len += BigInt(mapped[i].length)\n }\n }\n\n dinamics.forEach((idx) => {\n resultArray[idx] = uint[0](len)\n const data = mapped[idx]\n resultArray.push(data)\n len += BigInt(data.length)\n })\n\n return mergeUint8(...resultArray)\n }) as Encoder<[...OT]>\n\n res.d = true\n return res\n}\n\nconst staticEnc = <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n>(\n ...encoders: A\n) =>\n ((values) =>\n mergeUint8(...values.map((value, idx) => encoders[idx](value)))) as Encoder<\n [...OT]\n >\n\nconst staticDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> =>\n toInternalBytes(\n (bytes) => decoders.map((decoder) => decoder(bytes)) as [...OT],\n )\nconst dynamicDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> => {\n const res: Decoder<[...OT]> = toInternalBytes((bytes) => {\n const result = new Array(decoders.length) as [...OT]\n let start = bytes.i\n for (let i = 0; i < decoders.length; i++) {\n if (decoders[i].d) {\n const offset = Number(uint[1](bytes))\n const current = bytes.i\n bytes.i = start + offset\n result[i] = decoders[i](bytes)\n bytes.i = current\n } else {\n result[i] = decoders[i](bytes)\n }\n }\n return result\n })\n res.d = true\n return res\n}\n\nexport const Tuple = <\n A extends Array<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n ...codecs: A\n): Codec<[...OT]> => {\n const isDyn = codecs.some((c) => c.d)\n const [enc, dec] = isDyn\n ? ([dynamicEnc, dynamicDec] as const)\n : ([staticEnc, staticDec] as const)\n\n const res: Codec<[...OT]> = createCodec(\n enc(...codecs.map(([encoder]) => encoder)),\n dec(...codecs.map(([, decoder]) => decoder)),\n `(${codecs.map((c) => c.s).join(\",\")})`,\n )\n res.d = isDyn\n return res\n}\n", "import { Codec, StringRecord } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\nimport { Tuple } from \"./Tuple\"\n\nexport const Struct = <\n A extends StringRecord<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n codecs: A,\n): Codec<OT> => {\n const keys = Object.keys(codecs)\n return enhanceCodec(\n Tuple(...Object.values(codecs)),\n (input: OT) => keys.map((k) => input[k]),\n (tuple: Array<any>) =>\n Object.fromEntries(tuple.map((value, idx) => [keys[idx], value])) as OT,\n )\n}\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { toInternalBytes, createCodec } from \"../internal\"\nimport { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\n\nconst vectorEnc = <T>(inner: Encoder<T>, size?: number): Encoder<Array<T>> => {\n const result = ((value) => {\n const isNotFixed = size == null ? 1 : 0\n const actualSize = isNotFixed ? value.length : size!\n let data: Array<Uint8Array>\n if (inner.d) {\n data = new Array<Uint8Array>(actualSize * 2)\n let offset = actualSize * 32\n for (let i = 0; i < actualSize; i++) {\n const encoded = inner(value[i])\n data[i] = uint.enc(BigInt(offset))\n offset += encoded.byteLength\n data[i + actualSize] = encoded\n }\n } else {\n data = new Array<Uint8Array>(actualSize)\n for (let i = 0; i < actualSize; i++) data[i] = inner(value[i])\n }\n if (isNotFixed) data!.unshift(uint.enc(BigInt(value.length)))\n return mergeUint8(...data)\n }) as Encoder<Array<T>>\n result.d = true\n return result\n}\n\nconst vectorDec = <T>(getter: Decoder<T>, size?: number): Decoder<Array<T>> => {\n const decoder = toInternalBytes((bytes) => {\n const nElements = size! >= 0 ? size! : Number(uint[1](bytes))\n const decoded = new Array(nElements)\n\n if (getter.d) {\n const init = bytes.i\n let current = init\n for (let i = 0; i < nElements; i++) {\n bytes.i = current\n const offset = Number(uint.dec(bytes))\n current = bytes.i\n bytes.i = init + offset\n decoded[i] = getter(bytes)\n }\n } else {\n for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n }\n\n return decoded\n })\n decoder.d = true\n return decoder\n}\n\nexport const Vector = <T>(inner: Codec<T>, size?: number): Codec<Array<T>> => {\n const codec = createCodec(\n vectorEnc(inner[0], size),\n vectorDec(inner[1], size),\n inner.s + `[${size == null ? \"\" : size}]`,\n )\n if (size == null) codec.d = true\n return codec\n}\n"],
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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AK,OACpC;AACA,MAAI,QAAQ;AAAM,UAAM,IAAI;AAC5B,SAAO;AACT;",
|
6
6
|
"names": []
|
7
7
|
}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
var D=Object.defineProperty;var
|
1
|
+
var D=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.prototype.hasOwnProperty;var z=(t,e,n)=>e in t?D(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var tt=(t,e)=>{for(var n in e)D(t,n,{get:e[n],enumerable:!0})},et=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Y(e))!Z.call(t,s)&&s!==n&&D(t,s,{get:()=>e[s],enumerable:!(r=X(e,s))||r.enumerable});return t};var nt=t=>et(D({},"__esModule",{value:!0}),t);var U=(t,e,n)=>(z(t,typeof e!="symbol"?e+"":e,n),n);var on={};tt(on,{Fixed:()=>Ye,Struct:()=>M,Tuple:()=>C,Vector:()=>P,address:()=>j,bool:()=>R,bytes:()=>A,bytes1:()=>Rt,bytes10:()=>Mt,bytes11:()=>Jt,bytes12:()=>Pt,bytes13:()=>Qt,bytes14:()=>Wt,bytes15:()=>Xt,bytes16:()=>Yt,bytes17:()=>Zt,bytes18:()=>zt,bytes19:()=>te,bytes2:()=>Vt,bytes20:()=>ee,bytes21:()=>ne,bytes22:()=>re,bytes23:()=>oe,bytes24:()=>ie,bytes25:()=>se,bytes26:()=>ce,bytes27:()=>ae,bytes28:()=>de,bytes29:()=>me,bytes3:()=>Ht,bytes30:()=>ue,bytes31:()=>fe,bytes32:()=>pe,bytes4:()=>Ft,bytes5:()=>Gt,bytes6:()=>Lt,bytes7:()=>_t,bytes8:()=>$t,bytes9:()=>qt,enhanceCodec:()=>l,enhanceDecoder:()=>K,enhanceEncoder:()=>v,int:()=>Xe,int104:()=>ve,int112:()=>Ke,int120:()=>je,int128:()=>Se,int136:()=>Ne,int144:()=>Re,int152:()=>Ve,int16:()=>De,int160:()=>He,int168:()=>Fe,int176:()=>Ge,int184:()=>Le,int192:()=>_e,int200:()=>$e,int208:()=>qe,int224:()=>Je,int226:()=>Me,int232:()=>Pe,int24:()=>Te,int240:()=>Qe,int248:()=>We,int256:()=>q,int32:()=>Ce,int40:()=>Ue,int48:()=>Ie,int56:()=>he,int64:()=>Oe,int72:()=>Ee,int8:()=>be,int80:()=>we,int88:()=>ke,int96:()=>Be,keccak:()=>I,str:()=>$,uint:()=>f,uint104:()=>yt,uint112:()=>gt,uint120:()=>xt,uint128:()=>At,uint136:()=>bt,uint144:()=>Dt,uint152:()=>Tt,uint16:()=>ot,uint160:()=>Ct,uint168:()=>Ut,uint176:()=>It,uint184:()=>ht,uint192:()=>Ot,uint200:()=>Et,uint208:()=>wt,uint224:()=>Bt,uint226:()=>kt,uint232:()=>vt,uint24:()=>it,uint240:()=>Kt,uint248:()=>jt,uint256:()=>S,uint32:()=>st,uint40:()=>ct,uint48:()=>at,uint56:()=>dt,uint64:()=>mt,uint72:()=>ut,uint8:()=>O,uint80:()=>ft,uint88:()=>pt,uint96:()=>lt});module.exports=nt(on);var d=(t,e,n)=>{t.s=n,e.s=n;let r=[t,e];return r.enc=t,r.dec=e,r.s=n,r};var k=require("@unstoppablejs/utils"),T=class extends Uint8Array{constructor(n){super(n);U(this,"i",0);U(this,"v");this.v=new DataView(n)}},m=t=>e=>t(e instanceof T?e:new T(e instanceof Uint8Array?e.buffer:typeof e=="string"?(0,k.fromHex)(e).buffer:e));var g=Array(32).fill(0).map((t,e)=>e+1);var B=require("@noble/hashes/sha3"),I=B.keccak_256,h=(t,e)=>(t.d&&(e.d=!0),e.s=t.s,e),v=(t,e)=>h(t,n=>t(e(n))),K=(t,e)=>h(t,n=>e(t(n))),l=(t,e,n)=>h(t,d(v(t[0],e),K(t[1],n),t.s));var x=require("@unstoppablejs/utils");var j=d(t=>{let e=new Uint8Array(32);return e.set((0,x.fromHex)(t),12),e},m(t=>{let e=new Uint8Array(t.buffer,t.i+12,20);t.i+=32;let n=(0,x.toHex)(e),r=(0,x.toHex)(I(n.slice(2))),s=new Array(41);s[0]="0x";for(let i=2;i<42;i++){let o=n[i];s.push(parseInt(r[i],16)>7?o.toUpperCase():o)}return s.join("")}),"address");var rt=t=>{let e=Math.ceil(t/8);return d(n=>{let r=new Uint8Array(32),s=new DataView(r.buffer),i=32-e*8;for(let o=24;o>=i;o-=8)s.setBigUint64(o,n),n>>=64n;return r},m(n=>{let r=0n,s=n.i+32;for(let i=n.i+(32-e*8);i<s;i+=8)r=r<<64n|n.v.getBigUint64(i);return n.i=s,r}),"uint"+t)},[O,ot,it,st,ct,at,dt,mt,ut,ft,pt,lt,yt,gt,xt,At,bt,Dt,Tt,Ct,Ut,It,ht,Ot,Et,wt,kt,Bt,vt,Kt,jt,S]=g.map(rt),f=S;var N=Object.assign([],O);N.s="bool";var R=l(N,t=>t?1n:0n,Boolean);var V=require("@unstoppablejs/utils");var H=t=>{let e=[f[0](BigInt(t.length)),t],n=t.length%32;return n>0&&e.push(new Uint8Array(32-n)),(0,V.mergeUint8)(...e)};H.d=!0;var F=m(t=>{let e=Number(f[1](t)),n=new Uint8Array(t.buffer,t.i,e);t.i+=e;let r=e%32;return r>0&&(t.i+=32-r),n});F.d=!0;var A=d(H,F,"bytes");A.d=!0;var G=new TextEncoder,L=new TextDecoder,_=Object.assign([],A);_.s="string";var $=l(_,G.encode.bind(G),L.decode.bind(L));var St=t=>e=>{if(e.length===t&&t===32)return e;let n=new Uint8Array(32);return n.set(e.length===t?e:e.slice(0,t)),n},Nt=t=>m(e=>{let n=new Uint8Array(e.buffer,e.i,t);return e.i+=32,n}),[Rt,Vt,Ht,Ft,Gt,Lt,_t,$t,qt,Mt,Jt,Pt,Qt,Wt,Xt,Yt,Zt,zt,te,ee,ne,re,oe,ie,se,ce,ae,de,me,ue,fe,pe]=g.map(t=>d(St(t),Nt(t),"bytes"+t));var le={"1":"getInt8","2":"getInt16","8":"getBigInt64"},ye={"1":"setInt8","2":"setInt16","8":"setBigInt64"},ge={"1":"getUint8","2":"getUint16","8":"getBigUint64"},xe={"1":"setUint8","2":"setUint16","8":"setBigUint64"},Ae=t=>{let e=t/8|0,n=t%8/2|0,r=[...Array(e).fill([8,64n,o=>o]),...Array(n).fill([2,16n,o=>Number(o&65535n)])];t%2&&r.push([1,8n,o=>Number(o&255n)]);let s=o=>{let c=new Uint8Array(32),a=new DataView(c.buffer);if(o<0n)for(let p=0;p<32-t;p+=8)a.setBigInt64(p,-1n);let u=32;for(let p=r.length-1;p>0;p--){let[w,Q,W]=r[p];u-=w,a[xe[w]](u,W(o)),o>>=Q}let[y,,b]=r[0];return u-=y,a[ye[y]](u,b(o)),c},i=m(o=>{let c=o.i+32-t,a=r[0][0],u=BigInt(o.v[le[a]](c));c+=a;for(let y=1;y<r.length;y++){let[b,p]=r[y];u=u<<p|BigInt(o.v[ge[b]](c)),c+=b}return o.i+=32,u});return d(s,i,"int"+t)},[be,De,Te,Ce,Ue,Ie,he,Oe,Ee,we,ke,Be,ve,Ke,je,Se,Ne,Re,Ve,He,Fe,Ge,Le,_e,$e,qe,Me,Je,Pe,Qe,We,q]=g.map(Ae),Xe=q;var Ye=(t,e)=>{let n=t.s,r=Object.assign([],t);return r.s=(n[0]==="u"?"ufixed"+n.slice(4):"fixed"+n.slice(3))+"x"+e,l(r,s=>s.value,s=>({value:s,decimals:e}))};var E=require("@unstoppablejs/utils");var Ze=(...t)=>{let e=n=>{let r=n.map((c,a)=>t[a](c)),s=new Array(t.length),i=[],o=0n;for(let c=0;c<t.length;c++)t[c].d?(i.push(c),o+=32n):(s[c]=r[c],o+=BigInt(r[c].length));return i.forEach(c=>{s[c]=f[0](o);let a=r[c];s.push(a),o+=BigInt(a.length)}),(0,E.mergeUint8)(...s)};return e.d=!0,e},ze=(...t)=>e=>(0,E.mergeUint8)(...e.map((n,r)=>t[r](n))),tn=(...t)=>m(e=>t.map(n=>n(e))),en=(...t)=>{let e=m(n=>{let r=new Array(t.length),s=n.i;for(let i=0;i<t.length;i++)if(t[i].d){let o=Number(f[1](n)),c=n.i;n.i=s+o,r[i]=t[i](n),n.i=c}else r[i]=t[i](n);return r});return e.d=!0,e},C=(...t)=>{let e=t.some(i=>i.d),[n,r]=e?[Ze,en]:[ze,tn],s=d(n(...t.map(([i])=>i)),r(...t.map(([,i])=>i)),`(${t.map(i=>i.s).join(",")})`);return s.d=e,s};var M=t=>{let e=Object.keys(t);return l(C(...Object.values(t)),n=>e.map(r=>n[r]),n=>Object.fromEntries(n.map((r,s)=>[e[s],r])))};var J=require("@unstoppablejs/utils");var nn=(t,e)=>{let n=r=>{let s=e==null?1:0,i=s?r.length:e,o;if(t.d){o=new Array(i*2);let c=i*32;for(let a=0;a<i;a++){let u=t(r[a]);o[a]=f.enc(BigInt(c)),c+=u.byteLength,o[a+i]=u}}else{o=new Array(i);for(let c=0;c<i;c++)o[c]=t(r[c])}return s&&o.unshift(f.enc(BigInt(r.length))),(0,J.mergeUint8)(...o)};return n.d=!0,n},rn=(t,e)=>{let n=m(r=>{let s=e>=0?e:Number(f[1](r)),i=new Array(s);if(t.d){let o=r.i,c=o;for(let a=0;a<s;a++){r.i=c;let u=Number(f.dec(r));c=r.i,r.i=o+u,i[a]=t(r)}}else for(let o=0;o<s;o++)i[o]=t(r);return i});return n.d=!0,n},P=(t,e)=>{let n=d(nn(t[0],e),rn(t[1],e),t.s+`[${e??""}]`);return e==null&&(n.d=!0),n};
|
2
2
|
//# sourceMappingURL=solidity-codecs.cjs.production.min.js.map
|
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"version": 3,
|
3
|
-
"sources": ["../src/index.ts", "../src/
|
4
|
-
"sourcesContent": ["export * from \"./utils\"\nexport * from \"./types\"\nexport * from \"./codecs\"\n", "import type { Codec, Decoder, Encoder } from \"./types\"\nimport { keccak_256 } from \"@noble/hashes/sha3\"\n\nexport const keccak = keccak_256\n\nexport const createCodec = <T>(\n encoder: Encoder<T>,\n decoder: Decoder<T>,\n): Codec<T> => {\n const result = [encoder, decoder] as any\n result.enc = encoder\n result.dec = decoder\n return result\n}\n\nconst dyn = <T extends { dyn?: boolean }>(\n input: { dyn?: boolean },\n output: T,\n): T => {\n if (input.dyn) output.dyn = true\n return output\n}\n\nexport const enhanceEncoder = <I, O>(\n encoder: Encoder<I>,\n mapper: (value: O) => I,\n): Encoder<O> => dyn(encoder, ((value) => encoder(mapper(value))) as Encoder<O>)\n\nexport const enhanceDecoder = <I, O>(\n decoder: Decoder<I>,\n mapper: (value: I) => O,\n): Decoder<O> => dyn(decoder, ((value) => mapper(decoder(value))) as Decoder<O>)\n\nexport const enhanceCodec = <I, O>(\n codec: Codec<I>,\n toFrom: (value: O) => I,\n fromTo: (value: I) => O,\n): Codec<O> =>\n dyn(\n codec,\n createCodec(\n enhanceEncoder(codec[0], toFrom),\n enhanceDecoder(codec[1], fromTo),\n ),\n )\n", "import { fromHex, toHex } from \"@unstoppablejs/utils\"\nimport { toInternalBytes } from \"../internal\"\nimport { createCodec, keccak } from \"../utils\"\n\nexport const address = createCodec(\n (input: string) => {\n const result = new Uint8Array(32)\n result.set(fromHex(input), 12)\n return result\n },\n toInternalBytes((bytes) => {\n const binaryAddress = new Uint8Array(bytes.buffer, bytes.i + 12, 20)\n bytes.i += 32\n const nonChecksum = toHex(binaryAddress)\n const hashedAddres = toHex(keccak(nonChecksum.slice(2)))\n\n const result = new Array(41)\n result[0] = \"0x\"\n for (let i = 2; i < 42; i++) {\n const char = nonChecksum[i]\n result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char)\n }\n\n return result.join(\"\")\n }),\n)\n", "import { fromHex } from \"@unstoppablejs/utils\"\nimport { Decoder } from \"../types\"\n\nclass InternalUint8Array extends Uint8Array {\n i: number = 0\n v: DataView\n\n constructor(buffer: ArrayBuffer) {\n super(buffer)\n this.v = new DataView(buffer)\n }\n}\n\nexport const toInternalBytes =\n <T>(fn: (input: InternalUint8Array) => T): Decoder<T> =>\n (buffer: string | ArrayBuffer | Uint8Array | InternalUint8Array) =>\n fn(\n buffer instanceof InternalUint8Array\n ? buffer\n : new InternalUint8Array(\n buffer instanceof Uint8Array\n ? buffer.buffer\n : typeof buffer === \"string\"\n ? fromHex(buffer).buffer\n : buffer,\n ),\n )\n", "export const range32 = Array(32)\n .fill(0)\n .map((_, idx) => idx + 1)\n", "import { Codec } from \"../types\"\nimport { range32, toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = Math.ceil(nBytes / 8)\n return createCodec(\n (input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n const idxLimit = 32 - n64 * 8\n for (let idx = 24; idx >= idxLimit; idx -= 8) {\n dv.setBigUint64(idx, input)\n input >>= 64n\n }\n\n return result\n },\n toInternalBytes((bytes) => {\n let result = 0n\n\n const nextBlock = bytes.i + 32\n for (let idx = bytes.i + (32 - n64 * 8); idx < nextBlock; idx += 8)\n result = (result << 64n) | bytes.v.getBigUint64(idx)\n\n bytes.i = nextBlock\n return result\n }),\n )\n}\n\nexport const [\n uint8,\n uint16,\n uint24,\n uint32,\n uint40,\n uint48,\n uint56,\n uint64,\n uint72,\n uint80,\n uint88,\n uint96,\n uint104,\n uint112,\n uint120,\n uint128,\n uint136,\n uint144,\n uint152,\n uint160,\n uint168,\n uint176,\n uint184,\n uint192,\n uint200,\n uint208,\n uint226,\n uint224,\n uint232,\n uint240,\n uint248,\n uint256,\n] = range32.map(getCodec)\nexport const uint = uint256\n", "import { Codec } from \"../types\"\nimport { enhanceCodec } from \"../\"\nimport { uint8 } from \"./Uint\"\n\nexport const bool: Codec<boolean> = enhanceCodec(\n uint8,\n (value: boolean) => (value ? 1n : 0n),\n Boolean,\n)\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { createCodec, Decoder, Encoder } from \"../\"\nimport { toInternalBytes } from \"../internal\"\nimport { uint } from \"./Uint\"\n\nconst bytesEnc: Encoder<Uint8Array> = (val) => {\n const args = [uint[0](BigInt(val.length)), val] as const\n const extra = val.length % 32\n if (extra > 0) {\n ;(args as any).push(new Uint8Array(32 - extra))\n }\n return mergeUint8(...args)\n}\nbytesEnc.dyn = true\n\nconst bytesDec: Decoder<Uint8Array> = toInternalBytes((bytes) => {\n let nElements = Number(uint[1](bytes))\n const result = new Uint8Array(bytes.buffer, bytes.i, nElements)\n bytes.i += nElements\n const extra = nElements % 32\n if (extra > 0) bytes.i += 32 - extra\n return result\n})\nbytesDec.dyn = true\n\nexport const bytes = createCodec(bytesEnc, bytesDec)\nbytes.dyn = true\n", "import { enhanceCodec } from \"../\"\nimport { bytes } from \"./bytes\"\n\nconst textEncoder = new TextEncoder()\nconst textDecoder = new TextDecoder()\n\nexport const str = enhanceCodec<Uint8Array, string>(\n bytes,\n textEncoder.encode.bind(textEncoder),\n textDecoder.decode.bind(textDecoder),\n)\n", "import { Encoder, Decoder, Codec } from \"../types\"\nimport { createCodec } from \"../\"\nimport { range32, toInternalBytes } from \"../internal\"\n\nconst bytesEnc =\n (nBytes: number): Encoder<Uint8Array> =>\n (bytes) => {\n if (bytes.length === nBytes && nBytes === 32) return bytes\n const result = new Uint8Array(32)\n result.set(bytes.length === nBytes ? bytes : bytes.slice(0, nBytes))\n return result\n }\n\nconst bytesDec = (nBytes: number): Decoder<Uint8Array> =>\n toInternalBytes((bytes) => {\n const result = new Uint8Array(bytes.buffer, bytes.i, nBytes)\n bytes.i += 32\n return result\n })\n\nexport const [\n bytes1,\n bytes2,\n bytes3,\n bytes4,\n bytes5,\n bytes6,\n bytes7,\n bytes8,\n bytes9,\n bytes10,\n bytes11,\n bytes12,\n bytes13,\n bytes14,\n bytes15,\n bytes16,\n bytes17,\n bytes18,\n bytes19,\n bytes20,\n bytes21,\n bytes22,\n bytes23,\n bytes24,\n bytes25,\n bytes26,\n bytes27,\n bytes28,\n bytes29,\n bytes30,\n bytes31,\n bytes32,\n] = range32.map(\n (nBytes: number): Codec<Uint8Array> =>\n createCodec(bytesEnc(nBytes), bytesDec(nBytes)),\n)\n", "import { Encoder, Codec } from \"../types\"\nimport { range32, toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\n\nconst signGetters: Record<1 | 2 | 8, \"getBigInt64\" | \"getInt16\" | \"getInt8\"> = {\n \"1\": \"getInt8\",\n \"2\": \"getInt16\",\n \"8\": \"getBigInt64\",\n}\n\nconst signSetters: Record<1 | 2 | 8, \"setBigInt64\" | \"setInt16\" | \"setInt8\"> = {\n \"1\": \"setInt8\",\n \"2\": \"setInt16\",\n \"8\": \"setBigInt64\",\n}\n\nconst usignGetters: Record<\n 1 | 2 | 8,\n \"getBigUint64\" | \"getUint16\" | \"getUint8\"\n> = {\n \"1\": \"getUint8\",\n \"2\": \"getUint16\",\n \"8\": \"getBigUint64\",\n}\n\nconst usignSetters: Record<\n 1 | 2 | 8,\n \"setBigUint64\" | \"setUint16\" | \"setUint8\"\n> = {\n \"1\": \"setUint8\",\n \"2\": \"setUint16\",\n \"8\": \"setBigUint64\",\n}\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = (nBytes / 8) | 0\n const n16 = ((nBytes % 8) / 2) | 0\n const sequence = [\n ...Array(n64).fill([8, 64n, (x: bigint) => x]),\n ...Array(n16).fill([2, 16n, (x: bigint) => Number(x & 65535n)]),\n ]\n if (nBytes % 2) sequence.push([1, 8n, (x: bigint) => Number(x & 255n)])\n\n const enc: Encoder<bigint> = (input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n if (input < 0n) {\n for (let i = 0; i < 32 - nBytes; i += 8) dv.setBigInt64(i, -1n)\n }\n\n let idx = 32\n for (let i = sequence.length - 1; i > 0; i--) {\n const [bytes, shift, fn] = sequence[i] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[usignSetters[bytes]](idx, fn(input) as never)\n input >>= shift\n }\n const [bytes, , fn] = sequence[0] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[signSetters[bytes]](idx, fn(input) as never)\n\n return result\n }\n\n const dec = toInternalBytes((bytes) => {\n let idx = bytes.i + 32 - nBytes\n\n const bits = sequence[0][0] as 8\n let result = BigInt(bytes.v[signGetters[bits]](idx))\n idx += bits\n\n for (let i = 1; i < sequence.length; i++) {\n const [bits, shift] = sequence[i] as [1, 8n]\n result = (result << shift) | BigInt(bytes.v[usignGetters[bits]](idx))\n idx += bits\n }\n\n bytes.i += 32\n return result\n })\n\n return createCodec(enc, dec)\n}\n\nexport const [\n int8,\n int16,\n int24,\n int32,\n int40,\n int48,\n int56,\n int64,\n int72,\n int80,\n int88,\n int96,\n int104,\n int112,\n int120,\n int128,\n int136,\n int144,\n int152,\n int160,\n int168,\n int176,\n int184,\n int192,\n int200,\n int208,\n int226,\n int224,\n int232,\n int240,\n int248,\n int256,\n] = range32.map(getCodec)\nexport const int = int256\n", "import { Codec } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\n\nexport interface Decimal<T extends number = number> {\n value: bigint\n decimals: T\n}\n\nexport const Fixed = <D extends number>(\n baseCodec: Codec<bigint>,\n decimals: D,\n) =>\n enhanceCodec<bigint, Decimal<D>>(\n baseCodec,\n (x) => x.value,\n (value) => ({ value, decimals }),\n )\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\nimport { toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\n\nconst dynamicEnc = <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n>(\n ...encoders: A\n): Encoder<[...OT]> => {\n const res: Encoder<[...OT]> = (values) => {\n const mapped = values.map((value, idx) => encoders[idx](value))\n const resultArray = new Array<Uint8Array>(encoders.length)\n const dinamics = []\n let len = 0n\n for (let i = 0; i < encoders.length; i++) {\n if (encoders[i].dyn) {\n dinamics.push(i)\n len += 32n\n } else {\n resultArray[i] = mapped[i]\n len += BigInt(mapped[i].length)\n }\n }\n\n dinamics.forEach((idx) => {\n resultArray[idx] = uint[0](len)\n const data = mapped[idx]\n resultArray.push(data)\n len += BigInt(data.length)\n })\n\n return mergeUint8(...resultArray)\n }\n\n res.dyn = true\n return res\n}\n\nconst staticEnc =\n <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n >(\n ...encoders: A\n ): Encoder<[...OT]> =>\n (values) =>\n mergeUint8(...values.map((value, idx) => encoders[idx](value)))\n\nconst staticDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> =>\n toInternalBytes(\n (bytes) => decoders.map((decoder) => decoder(bytes)) as [...OT],\n )\nconst dynamicDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> => {\n const res: Decoder<[...OT]> = toInternalBytes((bytes) => {\n const result = new Array(decoders.length) as [...OT]\n let start = bytes.i\n for (let i = 0; i < decoders.length; i++) {\n if (decoders[i].dyn) {\n const offset = Number(uint[1](bytes))\n const current = bytes.i\n bytes.i = start + offset\n result[i] = decoders[i](bytes)\n bytes.i = current\n } else {\n result[i] = decoders[i](bytes)\n }\n }\n return result\n })\n res.dyn = true\n return res\n}\n\nexport const Tuple = <\n A extends Array<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n ...codecs: A\n): Codec<[...OT]> => {\n const isDyn = codecs.some((c) => c.dyn)\n const [enc, dec] = isDyn\n ? ([dynamicEnc, dynamicDec] as const)\n : ([staticEnc, staticDec] as const)\n\n const res: Codec<[...OT]> = createCodec(\n enc(...codecs.map(([encoder]) => encoder)),\n dec(...codecs.map(([, decoder]) => decoder)),\n )\n res.dyn = isDyn\n return res\n}\n", "import { Codec, StringRecord } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\nimport { Tuple } from \"./Tuple\"\n\nexport const Struct = <\n A extends StringRecord<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n codecs: A,\n): Codec<OT> => {\n const keys = Object.keys(codecs)\n return enhanceCodec(\n Tuple(...Object.values(codecs)),\n (input: OT) => keys.map((k) => input[k]),\n (tuple: Array<any>) =>\n Object.fromEntries(tuple.map((value, idx) => [keys[idx], value])) as OT,\n )\n}\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { toInternalBytes } from \"../internal\"\nimport { createCodec } from \"../utils\"\nimport { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\n\nconst vectorEnc = <T>(inner: Encoder<T>, size?: number): Encoder<Array<T>> => {\n const result: Encoder<Array<T>> = (value) => {\n const isNotFixed = size == null ? 1 : 0\n const actualSize = isNotFixed ? value.length : size!\n let data: Array<Uint8Array>\n if (inner.dyn) {\n data = new Array<Uint8Array>(actualSize * 2)\n let offset = actualSize * 32\n for (let i = 0; i < actualSize; i++) {\n const encoded = inner(value[i])\n data[i] = uint.enc(BigInt(offset))\n offset += encoded.byteLength\n data[i + actualSize] = encoded\n }\n } else {\n data = new Array<Uint8Array>(actualSize)\n for (let i = 0; i < actualSize; i++) data[i] = inner(value[i])\n }\n if (isNotFixed) data!.unshift(uint.enc(BigInt(value.length)))\n return mergeUint8(...data)\n }\n result.dyn = true\n return result\n}\n\nconst vectorDec = <T>(getter: Decoder<T>, size?: number): Decoder<Array<T>> => {\n const decoder = toInternalBytes((bytes) => {\n const nElements = size! >= 0 ? size! : Number(uint[1](bytes))\n const decoded = new Array(nElements)\n\n if (getter.dyn) {\n const init = bytes.i\n let current = init\n for (let i = 0; i < nElements; i++) {\n bytes.i = current\n const offset = Number(uint.dec(bytes))\n current = bytes.i\n bytes.i = init + offset\n decoded[i] = getter(bytes)\n }\n } else {\n for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n }\n\n return decoded\n })\n decoder.dyn = true\n return decoder\n}\n\nexport const Vector = <T>(inner: Codec<T>, size?: number): Codec<Array<T>> => {\n const codec = createCodec(\n vectorEnc(inner[0], size),\n vectorDec(inner[1], size),\n )\n if (size == null) codec.dyn = true\n return codec\n}\n"],
|
5
|
-
"mappings": "
|
3
|
+
"sources": ["../src/index.ts", "../src/internal/createCodec.ts", "../src/internal/toInternalBytes.ts", "../src/internal/range32.ts", "../src/utils.ts", "../src/codecs/address.ts", "../src/codecs/Uint.ts", "../src/codecs/bool.ts", "../src/codecs/bytes.ts", "../src/codecs/str.ts", "../src/codecs/BytesX.ts", "../src/codecs/Int.ts", "../src/codecs/Fixed.ts", "../src/codecs/Tuple.ts", "../src/codecs/Struct.ts", "../src/codecs/Vector.ts"],
|
4
|
+
"sourcesContent": ["export * from \"./utils\"\nexport * from \"./types\"\nexport * from \"./codecs\"\n", "import type { Codec } from \"../types\"\n\nexport const createCodec = <T>(\n encoder: (value: T) => Uint8Array,\n decoder: (value: Uint8Array) => T,\n selector: string,\n): Codec<T> => {\n ;(encoder as any).s = selector\n ;(decoder as any).s = selector\n const result = [encoder, decoder] as any\n result.enc = encoder\n result.dec = decoder\n result.s = selector\n return result\n}\n", "import { fromHex } from \"@unstoppablejs/utils\"\nimport { Decoder } from \"../types\"\n\nclass InternalUint8Array extends Uint8Array {\n i: number = 0\n v: DataView\n\n constructor(buffer: ArrayBuffer) {\n super(buffer)\n this.v = new DataView(buffer)\n }\n}\n\nexport const toInternalBytes = <T>(\n fn: (input: InternalUint8Array) => T,\n): Decoder<T> =>\n ((buffer: string | ArrayBuffer | Uint8Array | InternalUint8Array) =>\n fn(\n buffer instanceof InternalUint8Array\n ? buffer\n : new InternalUint8Array(\n buffer instanceof Uint8Array\n ? buffer.buffer\n : typeof buffer === \"string\"\n ? fromHex(buffer).buffer\n : buffer,\n ),\n )) as Decoder<T>\n", "export const range32 = Array(32)\n .fill(0)\n .map((_, idx) => idx + 1)\n", "import type { Codec, Decoder, Encoder } from \"./types\"\nimport { createCodec } from \"./internal\"\nimport { keccak_256 } from \"@noble/hashes/sha3\"\n\nexport const keccak = keccak_256\n\nconst dyn = <T extends { d?: boolean; s: string }>(\n input: { d?: boolean; s: string },\n output: T,\n): T => {\n if (input.d) output.d = true\n output.s = input.s\n return output\n}\n\nexport const enhanceEncoder = <I, O>(\n encoder: Encoder<I>,\n mapper: (value: O) => I,\n): Encoder<O> => dyn(encoder, ((value) => encoder(mapper(value))) as Encoder<O>)\n\nexport const enhanceDecoder = <I, O>(\n decoder: Decoder<I>,\n mapper: (value: I) => O,\n): Decoder<O> => dyn(decoder, ((value) => mapper(decoder(value))) as Decoder<O>)\n\nexport const enhanceCodec = <I, O>(\n codec: Codec<I>,\n toFrom: (value: O) => I,\n fromTo: (value: I) => O,\n): Codec<O> =>\n dyn(\n codec,\n createCodec(\n enhanceEncoder(codec[0], toFrom),\n enhanceDecoder(codec[1], fromTo),\n codec.s,\n ),\n )\n", "import { fromHex, toHex } from \"@unstoppablejs/utils\"\nimport { createCodec, toInternalBytes } from \"../internal\"\nimport { keccak } from \"../utils\"\n\nexport const address = createCodec<string>(\n (input: string) => {\n const result = new Uint8Array(32)\n result.set(fromHex(input), 12)\n return result\n },\n toInternalBytes((bytes) => {\n const binaryAddress = new Uint8Array(bytes.buffer, bytes.i + 12, 20)\n bytes.i += 32\n const nonChecksum = toHex(binaryAddress)\n const hashedAddres = toHex(keccak(nonChecksum.slice(2)))\n\n const result = new Array(41)\n result[0] = \"0x\"\n for (let i = 2; i < 42; i++) {\n const char = nonChecksum[i]\n result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char)\n }\n\n return result.join(\"\")\n }),\n \"address\",\n)\n", "import { Codec } from \"../types\"\nimport { range32, toInternalBytes, createCodec } from \"../internal\"\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = Math.ceil(nBytes / 8)\n return createCodec(\n (input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n const idxLimit = 32 - n64 * 8\n for (let idx = 24; idx >= idxLimit; idx -= 8) {\n dv.setBigUint64(idx, input)\n input >>= 64n\n }\n\n return result\n },\n toInternalBytes((bytes) => {\n let result = 0n\n\n const nextBlock = bytes.i + 32\n for (let idx = bytes.i + (32 - n64 * 8); idx < nextBlock; idx += 8)\n result = (result << 64n) | bytes.v.getBigUint64(idx)\n\n bytes.i = nextBlock\n return result\n }),\n \"uint\" + nBytes,\n )\n}\n\nexport const [\n uint8,\n uint16,\n uint24,\n uint32,\n uint40,\n uint48,\n uint56,\n uint64,\n uint72,\n uint80,\n uint88,\n uint96,\n uint104,\n uint112,\n uint120,\n uint128,\n uint136,\n uint144,\n uint152,\n uint160,\n uint168,\n uint176,\n uint184,\n uint192,\n uint200,\n uint208,\n uint226,\n uint224,\n uint232,\n uint240,\n uint248,\n uint256,\n] = range32.map(getCodec)\nexport const uint = uint256\n", "import type { Codec } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\nimport { uint8 } from \"./Uint\"\n\nconst base = Object.assign([], uint8)\nbase.s = \"bool\"\n\nexport const bool: Codec<boolean> = enhanceCodec(\n base,\n (value: boolean) => (value ? 1n : 0n),\n Boolean,\n)\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport type { Decoder, Encoder } from \"../types\"\nimport { toInternalBytes, createCodec } from \"../internal\"\nimport { uint } from \"./Uint\"\n\nconst bytesEnc = ((val) => {\n const args = [uint[0](BigInt(val.length)), val] as const\n const extra = val.length % 32\n if (extra > 0) {\n ;(args as any).push(new Uint8Array(32 - extra))\n }\n return mergeUint8(...args)\n}) as Encoder<Uint8Array>\nbytesEnc.d = true\n\nconst bytesDec: Decoder<Uint8Array> = toInternalBytes((bytes) => {\n let nElements = Number(uint[1](bytes))\n const result = new Uint8Array(bytes.buffer, bytes.i, nElements)\n bytes.i += nElements\n const extra = nElements % 32\n if (extra > 0) bytes.i += 32 - extra\n return result\n})\nbytesDec.d = true\n\nexport const bytes = createCodec(bytesEnc, bytesDec, \"bytes\")\nbytes.d = true\n", "import { enhanceCodec } from \"../utils\"\nimport { bytes } from \"./bytes\"\n\nconst textEncoder = new TextEncoder()\nconst textDecoder = new TextDecoder()\n\nconst base = Object.assign([], bytes)\nbase.s = \"string\"\n\nexport const str = enhanceCodec<Uint8Array, string>(\n base,\n textEncoder.encode.bind(textEncoder),\n textDecoder.decode.bind(textDecoder),\n)\n", "import type { Encoder, Decoder, Codec } from \"../types\"\nimport { range32, toInternalBytes, createCodec } from \"../internal\"\n\nconst bytesEnc = (nBytes: number): Encoder<Uint8Array> =>\n ((bytes) => {\n if (bytes.length === nBytes && nBytes === 32) return bytes\n const result = new Uint8Array(32)\n result.set(bytes.length === nBytes ? bytes : bytes.slice(0, nBytes))\n return result\n }) as Encoder<Uint8Array>\n\nconst bytesDec = (nBytes: number): Decoder<Uint8Array> =>\n toInternalBytes((bytes) => {\n const result = new Uint8Array(bytes.buffer, bytes.i, nBytes)\n bytes.i += 32\n return result\n })\n\nexport const [\n bytes1,\n bytes2,\n bytes3,\n bytes4,\n bytes5,\n bytes6,\n bytes7,\n bytes8,\n bytes9,\n bytes10,\n bytes11,\n bytes12,\n bytes13,\n bytes14,\n bytes15,\n bytes16,\n bytes17,\n bytes18,\n bytes19,\n bytes20,\n bytes21,\n bytes22,\n bytes23,\n bytes24,\n bytes25,\n bytes26,\n bytes27,\n bytes28,\n bytes29,\n bytes30,\n bytes31,\n bytes32,\n] = range32.map(\n (nBytes: number): Codec<Uint8Array> =>\n createCodec(bytesEnc(nBytes), bytesDec(nBytes), \"bytes\" + nBytes),\n)\n", "import { Encoder, Codec } from \"../types\"\nimport { range32, toInternalBytes, createCodec } from \"../internal\"\n\nconst signGetters: Record<1 | 2 | 8, \"getBigInt64\" | \"getInt16\" | \"getInt8\"> = {\n \"1\": \"getInt8\",\n \"2\": \"getInt16\",\n \"8\": \"getBigInt64\",\n}\n\nconst signSetters: Record<1 | 2 | 8, \"setBigInt64\" | \"setInt16\" | \"setInt8\"> = {\n \"1\": \"setInt8\",\n \"2\": \"setInt16\",\n \"8\": \"setBigInt64\",\n}\n\nconst usignGetters: Record<\n 1 | 2 | 8,\n \"getBigUint64\" | \"getUint16\" | \"getUint8\"\n> = {\n \"1\": \"getUint8\",\n \"2\": \"getUint16\",\n \"8\": \"getBigUint64\",\n}\n\nconst usignSetters: Record<\n 1 | 2 | 8,\n \"setBigUint64\" | \"setUint16\" | \"setUint8\"\n> = {\n \"1\": \"setUint8\",\n \"2\": \"setUint16\",\n \"8\": \"setBigUint64\",\n}\n\nconst getCodec = (nBytes: number): Codec<bigint> => {\n const n64 = (nBytes / 8) | 0\n const n16 = ((nBytes % 8) / 2) | 0\n const sequence = [\n ...Array(n64).fill([8, 64n, (x: bigint) => x]),\n ...Array(n16).fill([2, 16n, (x: bigint) => Number(x & 65535n)]),\n ]\n if (nBytes % 2) sequence.push([1, 8n, (x: bigint) => Number(x & 255n)])\n\n const enc = ((input) => {\n const result = new Uint8Array(32)\n const dv = new DataView(result.buffer)\n\n if (input < 0n) {\n for (let i = 0; i < 32 - nBytes; i += 8) dv.setBigInt64(i, -1n)\n }\n\n let idx = 32\n for (let i = sequence.length - 1; i > 0; i--) {\n const [bytes, shift, fn] = sequence[i] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[usignSetters[bytes]](idx, fn(input) as never)\n input >>= shift\n }\n const [bytes, , fn] = sequence[0] as [1, 8n, (x: bigint) => any]\n idx -= bytes\n dv[signSetters[bytes]](idx, fn(input) as never)\n\n return result\n }) as Encoder<bigint>\n\n const dec = toInternalBytes((bytes) => {\n let idx = bytes.i + 32 - nBytes\n\n const bits = sequence[0][0] as 8\n let result = BigInt(bytes.v[signGetters[bits]](idx))\n idx += bits\n\n for (let i = 1; i < sequence.length; i++) {\n const [bits, shift] = sequence[i] as [1, 8n]\n result = (result << shift) | BigInt(bytes.v[usignGetters[bits]](idx))\n idx += bits\n }\n\n bytes.i += 32\n return result\n })\n\n return createCodec(enc, dec, \"int\" + nBytes)\n}\n\nexport const [\n int8,\n int16,\n int24,\n int32,\n int40,\n int48,\n int56,\n int64,\n int72,\n int80,\n int88,\n int96,\n int104,\n int112,\n int120,\n int128,\n int136,\n int144,\n int152,\n int160,\n int168,\n int176,\n int184,\n int192,\n int200,\n int208,\n int226,\n int224,\n int232,\n int240,\n int248,\n int256,\n] = range32.map(getCodec)\nexport const int = int256\n", "import { Codec } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\n\nexport interface Decimal<T extends number = number> {\n value: bigint\n decimals: T\n}\n\nexport const Fixed = <D extends number>(\n baseCodec: Codec<bigint>,\n decimals: D,\n) => {\n const baseSelector = baseCodec.s\n const eBaseCodec = Object.assign([], baseCodec)\n eBaseCodec.s =\n (baseSelector[0] === \"u\"\n ? \"ufixed\" + baseSelector.slice(4)\n : \"fixed\" + baseSelector.slice(3)) +\n \"x\" +\n decimals\n return enhanceCodec<bigint, Decimal<D>>(\n eBaseCodec,\n (x) => x.value,\n (value) => ({ value, decimals }),\n )\n}\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport type { Codec, Decoder, Encoder } from \"../types\"\nimport { toInternalBytes, createCodec } from \"../internal\"\nimport { uint } from \"./Uint\"\n\nconst dynamicEnc = <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n>(\n ...encoders: A\n): Encoder<[...OT]> => {\n const res = ((values) => {\n const mapped = values.map((value, idx) => encoders[idx](value))\n const resultArray = new Array<Uint8Array>(encoders.length)\n const dinamics = []\n let len = 0n\n for (let i = 0; i < encoders.length; i++) {\n if (encoders[i].d) {\n dinamics.push(i)\n len += 32n\n } else {\n resultArray[i] = mapped[i]\n len += BigInt(mapped[i].length)\n }\n }\n\n dinamics.forEach((idx) => {\n resultArray[idx] = uint[0](len)\n const data = mapped[idx]\n resultArray.push(data)\n len += BigInt(data.length)\n })\n\n return mergeUint8(...resultArray)\n }) as Encoder<[...OT]>\n\n res.d = true\n return res\n}\n\nconst staticEnc = <\n A extends Array<Encoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Encoder<infer D> ? D : unknown },\n>(\n ...encoders: A\n) =>\n ((values) =>\n mergeUint8(...values.map((value, idx) => encoders[idx](value)))) as Encoder<\n [...OT]\n >\n\nconst staticDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> =>\n toInternalBytes(\n (bytes) => decoders.map((decoder) => decoder(bytes)) as [...OT],\n )\nconst dynamicDec = <\n A extends Array<Decoder<any>>,\n OT extends { [K in keyof A]: A[K] extends Decoder<infer D> ? D : unknown },\n>(\n ...decoders: A\n): Decoder<[...OT]> => {\n const res: Decoder<[...OT]> = toInternalBytes((bytes) => {\n const result = new Array(decoders.length) as [...OT]\n let start = bytes.i\n for (let i = 0; i < decoders.length; i++) {\n if (decoders[i].d) {\n const offset = Number(uint[1](bytes))\n const current = bytes.i\n bytes.i = start + offset\n result[i] = decoders[i](bytes)\n bytes.i = current\n } else {\n result[i] = decoders[i](bytes)\n }\n }\n return result\n })\n res.d = true\n return res\n}\n\nexport const Tuple = <\n A extends Array<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n ...codecs: A\n): Codec<[...OT]> => {\n const isDyn = codecs.some((c) => c.d)\n const [enc, dec] = isDyn\n ? ([dynamicEnc, dynamicDec] as const)\n : ([staticEnc, staticDec] as const)\n\n const res: Codec<[...OT]> = createCodec(\n enc(...codecs.map(([encoder]) => encoder)),\n dec(...codecs.map(([, decoder]) => decoder)),\n `(${codecs.map((c) => c.s).join(\",\")})`,\n )\n res.d = isDyn\n return res\n}\n", "import { Codec, StringRecord } from \"../types\"\nimport { enhanceCodec } from \"../utils\"\nimport { Tuple } from \"./Tuple\"\n\nexport const Struct = <\n A extends StringRecord<Codec<any>>,\n OT extends { [K in keyof A]: A[K] extends Codec<infer D> ? D : unknown },\n>(\n codecs: A,\n): Codec<OT> => {\n const keys = Object.keys(codecs)\n return enhanceCodec(\n Tuple(...Object.values(codecs)),\n (input: OT) => keys.map((k) => input[k]),\n (tuple: Array<any>) =>\n Object.fromEntries(tuple.map((value, idx) => [keys[idx], value])) as OT,\n )\n}\n", "import { mergeUint8 } from \"@unstoppablejs/utils\"\nimport { toInternalBytes, createCodec } from \"../internal\"\nimport { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\n\nconst vectorEnc = <T>(inner: Encoder<T>, size?: number): Encoder<Array<T>> => {\n const result = ((value) => {\n const isNotFixed = size == null ? 1 : 0\n const actualSize = isNotFixed ? value.length : size!\n let data: Array<Uint8Array>\n if (inner.d) {\n data = new Array<Uint8Array>(actualSize * 2)\n let offset = actualSize * 32\n for (let i = 0; i < actualSize; i++) {\n const encoded = inner(value[i])\n data[i] = uint.enc(BigInt(offset))\n offset += encoded.byteLength\n data[i + actualSize] = encoded\n }\n } else {\n data = new Array<Uint8Array>(actualSize)\n for (let i = 0; i < actualSize; i++) data[i] = inner(value[i])\n }\n if (isNotFixed) data!.unshift(uint.enc(BigInt(value.length)))\n return mergeUint8(...data)\n }) as Encoder<Array<T>>\n result.d = true\n return result\n}\n\nconst vectorDec = <T>(getter: Decoder<T>, size?: number): Decoder<Array<T>> => {\n const decoder = toInternalBytes((bytes) => {\n const nElements = size! >= 0 ? size! : Number(uint[1](bytes))\n const decoded = new Array(nElements)\n\n if (getter.d) {\n const init = bytes.i\n let current = init\n for (let i = 0; i < nElements; i++) {\n bytes.i = current\n const offset = Number(uint.dec(bytes))\n current = bytes.i\n bytes.i = init + offset\n decoded[i] = getter(bytes)\n }\n } else {\n for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n }\n\n return decoded\n })\n decoder.d = true\n return decoder\n}\n\nexport const Vector = <T>(inner: Codec<T>, size?: number): Codec<Array<T>> => {\n const codec = createCodec(\n vectorEnc(inner[0], size),\n vectorDec(inner[1], size),\n inner.s + `[${size == null ? \"\"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