solidity-codecs 0.0.1-beta.4 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/codecs/Uint.d.ts +0 -1
- package/dist/codecs/address.d.ts +1 -0
- package/dist/codecs/index.d.ts +1 -0
- package/dist/solidity-codecs.cjs.development.js +35 -61
- 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 +33 -59
- package/dist/solidity-codecs.js.map +3 -3
- package/dist/solidity-codecs.mjs +33 -59
- package/dist/solidity-codecs.mjs.map +3 -3
- package/dist/utils.d.ts +6 -0
- package/package.json +7 -8
package/dist/codecs/Uint.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { Codec } from "../types";
|
|
2
2
|
export declare const uint8: Codec<bigint>, uint16: Codec<bigint>, uint24: Codec<bigint>, uint32: Codec<bigint>, uint40: Codec<bigint>, uint48: Codec<bigint>, uint56: Codec<bigint>, uint64: Codec<bigint>, uint72: Codec<bigint>, uint80: Codec<bigint>, uint88: Codec<bigint>, uint96: Codec<bigint>, uint104: Codec<bigint>, uint112: Codec<bigint>, uint120: Codec<bigint>, uint128: Codec<bigint>, uint136: Codec<bigint>, uint144: Codec<bigint>, uint152: Codec<bigint>, uint160: Codec<bigint>, uint168: Codec<bigint>, uint176: Codec<bigint>, uint184: Codec<bigint>, uint192: Codec<bigint>, uint200: Codec<bigint>, uint208: Codec<bigint>, uint226: Codec<bigint>, uint224: Codec<bigint>, uint232: Codec<bigint>, uint240: Codec<bigint>, uint248: Codec<bigint>, uint256: Codec<bigint>;
|
|
3
3
|
export declare const uint: Codec<bigint>;
|
|
4
|
-
export declare const address: Codec<bigint>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const address: import("..").Codec<string>;
|
package/dist/codecs/index.d.ts
CHANGED
|
@@ -100,6 +100,7 @@ __export(src_exports, {
|
|
|
100
100
|
int80: () => int80,
|
|
101
101
|
int88: () => int88,
|
|
102
102
|
int96: () => int96,
|
|
103
|
+
keccak: () => keccak,
|
|
103
104
|
str: () => str,
|
|
104
105
|
uint: () => uint,
|
|
105
106
|
uint104: () => uint104,
|
|
@@ -138,6 +139,8 @@ __export(src_exports, {
|
|
|
138
139
|
module.exports = __toCommonJS(src_exports);
|
|
139
140
|
|
|
140
141
|
// src/utils.ts
|
|
142
|
+
var import_sha3 = require("@noble/hashes/sha3");
|
|
143
|
+
var keccak = import_sha3.keccak_256;
|
|
141
144
|
var createCodec = (encoder, decoder) => {
|
|
142
145
|
const result = [encoder, decoder];
|
|
143
146
|
result.enc = encoder;
|
|
@@ -153,46 +156,11 @@ var enhanceEncoder = (encoder, mapper) => dyn(encoder, (value) => encoder(mapper
|
|
|
153
156
|
var enhanceDecoder = (decoder, mapper) => dyn(decoder, (value) => mapper(decoder(value)));
|
|
154
157
|
var enhanceCodec = (codec, toFrom, fromTo) => dyn(codec, createCodec(enhanceEncoder(codec[0], toFrom), enhanceDecoder(codec[1], fromTo)));
|
|
155
158
|
|
|
159
|
+
// src/codecs/address.ts
|
|
160
|
+
var import_utils2 = require("@unstoppablejs/utils");
|
|
161
|
+
|
|
156
162
|
// src/internal/toInternalBytes.ts
|
|
157
|
-
var
|
|
158
|
-
0: 0,
|
|
159
|
-
1: 1,
|
|
160
|
-
2: 2,
|
|
161
|
-
3: 3,
|
|
162
|
-
4: 4,
|
|
163
|
-
5: 5,
|
|
164
|
-
6: 6,
|
|
165
|
-
7: 7,
|
|
166
|
-
8: 8,
|
|
167
|
-
9: 9,
|
|
168
|
-
a: 10,
|
|
169
|
-
b: 11,
|
|
170
|
-
c: 12,
|
|
171
|
-
d: 13,
|
|
172
|
-
e: 14,
|
|
173
|
-
f: 15,
|
|
174
|
-
A: 10,
|
|
175
|
-
B: 11,
|
|
176
|
-
C: 12,
|
|
177
|
-
D: 13,
|
|
178
|
-
E: 14,
|
|
179
|
-
F: 15
|
|
180
|
-
};
|
|
181
|
-
function fromHex(hexString) {
|
|
182
|
-
const isOdd = hexString.length % 2;
|
|
183
|
-
const base = (hexString[1] === "x" ? 2 : 0) + isOdd;
|
|
184
|
-
const nBytes = (hexString.length - base) / 2 + isOdd;
|
|
185
|
-
const bytes33 = new Uint8Array(nBytes);
|
|
186
|
-
if (isOdd)
|
|
187
|
-
bytes33[0] = 0 | HEX_MAP[hexString[2]];
|
|
188
|
-
for (let i = 0; i < nBytes; ) {
|
|
189
|
-
const idx = base + i * 2;
|
|
190
|
-
const a = HEX_MAP[hexString[idx]];
|
|
191
|
-
const b = HEX_MAP[hexString[idx + 1]];
|
|
192
|
-
bytes33[isOdd + i++] = a << 4 | b;
|
|
193
|
-
}
|
|
194
|
-
return bytes33;
|
|
195
|
-
}
|
|
163
|
+
var import_utils = require("@unstoppablejs/utils");
|
|
196
164
|
var InternalUint8Array = class extends Uint8Array {
|
|
197
165
|
constructor(buffer) {
|
|
198
166
|
super(buffer);
|
|
@@ -201,26 +169,30 @@ var InternalUint8Array = class extends Uint8Array {
|
|
|
201
169
|
this.v = new DataView(buffer);
|
|
202
170
|
}
|
|
203
171
|
};
|
|
204
|
-
var toInternalBytes = (fn) => (buffer) => fn(buffer instanceof InternalUint8Array ? buffer : new InternalUint8Array(buffer instanceof Uint8Array ? buffer.buffer : typeof buffer === "string" ? fromHex(buffer).buffer : buffer));
|
|
205
|
-
|
|
206
|
-
// src/internal/mergeUint8.ts
|
|
207
|
-
var mergeUint8 = (...inputs) => {
|
|
208
|
-
const len = inputs.length;
|
|
209
|
-
let totalLen = 0;
|
|
210
|
-
for (let i = 0; i < len; i++)
|
|
211
|
-
totalLen += inputs[i].byteLength;
|
|
212
|
-
const result = new Uint8Array(totalLen);
|
|
213
|
-
for (let idx = 0, at = 0; idx < len; idx++) {
|
|
214
|
-
const current = inputs[idx];
|
|
215
|
-
result.set(current, at);
|
|
216
|
-
at += current.byteLength;
|
|
217
|
-
}
|
|
218
|
-
return result;
|
|
219
|
-
};
|
|
172
|
+
var toInternalBytes = (fn) => (buffer) => fn(buffer instanceof InternalUint8Array ? buffer : new InternalUint8Array(buffer instanceof Uint8Array ? buffer.buffer : typeof buffer === "string" ? (0, import_utils.fromHex)(buffer).buffer : buffer));
|
|
220
173
|
|
|
221
174
|
// src/internal/range32.ts
|
|
222
175
|
var range32 = Array(32).fill(0).map((_, idx) => idx + 1);
|
|
223
176
|
|
|
177
|
+
// src/codecs/address.ts
|
|
178
|
+
var address = createCodec((input) => {
|
|
179
|
+
const result = new Uint8Array(32);
|
|
180
|
+
result.set((0, import_utils2.fromHex)(input), 12);
|
|
181
|
+
return result;
|
|
182
|
+
}, toInternalBytes((bytes33) => {
|
|
183
|
+
const binaryAddress = new Uint8Array(bytes33.buffer, bytes33.i + 12, 20);
|
|
184
|
+
bytes33.i += 32;
|
|
185
|
+
const nonChecksum = (0, import_utils2.toHex)(binaryAddress);
|
|
186
|
+
const hashedAddres = (0, import_utils2.toHex)(keccak(nonChecksum.slice(2)));
|
|
187
|
+
const result = new Array(41);
|
|
188
|
+
result[0] = "0x";
|
|
189
|
+
for (let i = 2; i < 42; i++) {
|
|
190
|
+
const char = nonChecksum[i];
|
|
191
|
+
result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char);
|
|
192
|
+
}
|
|
193
|
+
return result.join("");
|
|
194
|
+
}));
|
|
195
|
+
|
|
224
196
|
// src/codecs/Uint.ts
|
|
225
197
|
var getCodec = (nBytes) => {
|
|
226
198
|
const n64 = Math.ceil(nBytes / 8);
|
|
@@ -277,12 +249,12 @@ var [
|
|
|
277
249
|
uint256
|
|
278
250
|
] = range32.map(getCodec);
|
|
279
251
|
var uint = uint256;
|
|
280
|
-
var address = uint160;
|
|
281
252
|
|
|
282
253
|
// src/codecs/bool.ts
|
|
283
254
|
var bool = enhanceCodec(uint8, (value) => value ? 1n : 0n, Boolean);
|
|
284
255
|
|
|
285
256
|
// src/codecs/bytes.ts
|
|
257
|
+
var import_utils5 = require("@unstoppablejs/utils");
|
|
286
258
|
var bytesEnc = (val) => {
|
|
287
259
|
const args = [uint[0](BigInt(val.length)), val];
|
|
288
260
|
const extra = val.length % 32;
|
|
@@ -290,7 +262,7 @@ var bytesEnc = (val) => {
|
|
|
290
262
|
;
|
|
291
263
|
args.push(new Uint8Array(32 - extra));
|
|
292
264
|
}
|
|
293
|
-
return mergeUint8(...args);
|
|
265
|
+
return (0, import_utils5.mergeUint8)(...args);
|
|
294
266
|
};
|
|
295
267
|
bytesEnc.dyn = true;
|
|
296
268
|
var bytesDec = toInternalBytes((bytes33) => {
|
|
@@ -460,6 +432,7 @@ var int = int256;
|
|
|
460
432
|
var Fixed = (baseCodec, decimals) => enhanceCodec(baseCodec, (x) => x.value, (value) => ({ value, decimals }));
|
|
461
433
|
|
|
462
434
|
// src/codecs/Tuple.ts
|
|
435
|
+
var import_utils8 = require("@unstoppablejs/utils");
|
|
463
436
|
var dynamicEnc = (...encoders) => {
|
|
464
437
|
const res = (values) => {
|
|
465
438
|
const mapped = values.map((value, idx) => encoders[idx](value));
|
|
@@ -481,12 +454,12 @@ var dynamicEnc = (...encoders) => {
|
|
|
481
454
|
resultArray.push(data);
|
|
482
455
|
len += BigInt(data.length);
|
|
483
456
|
});
|
|
484
|
-
return mergeUint8(...resultArray);
|
|
457
|
+
return (0, import_utils8.mergeUint8)(...resultArray);
|
|
485
458
|
};
|
|
486
459
|
res.dyn = true;
|
|
487
460
|
return res;
|
|
488
461
|
};
|
|
489
|
-
var staticEnc = (...encoders) => (values) => mergeUint8(...values.map((value, idx) => encoders[idx](value)));
|
|
462
|
+
var staticEnc = (...encoders) => (values) => (0, import_utils8.mergeUint8)(...values.map((value, idx) => encoders[idx](value)));
|
|
490
463
|
var staticDec = (...decoders) => toInternalBytes((bytes33) => decoders.map((decoder) => decoder(bytes33)));
|
|
491
464
|
var dynamicDec = (...decoders) => {
|
|
492
465
|
const res = toInternalBytes((bytes33) => {
|
|
@@ -523,13 +496,14 @@ var Struct = (codecs) => {
|
|
|
523
496
|
};
|
|
524
497
|
|
|
525
498
|
// src/codecs/Vector.ts
|
|
499
|
+
var import_utils11 = require("@unstoppablejs/utils");
|
|
526
500
|
var vectorEnc = (inner, size) => {
|
|
527
501
|
if (size >= 0) {
|
|
528
|
-
const encoder2 = (value) => mergeUint8(...value.map(inner));
|
|
502
|
+
const encoder2 = (value) => (0, import_utils11.mergeUint8)(...value.map(inner));
|
|
529
503
|
encoder2.dyn = inner.dyn;
|
|
530
504
|
return encoder2;
|
|
531
505
|
}
|
|
532
|
-
const encoder = (value) => mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner));
|
|
506
|
+
const encoder = (value) => (0, import_utils11.mergeUint8)(uint[0](BigInt(value.length)), ...value.map(inner));
|
|
533
507
|
encoder.dyn = true;
|
|
534
508
|
return encoder;
|
|
535
509
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts", "../src/utils.ts", "../src/
|
|
4
|
-
"sourcesContent": ["export * from \"./utils\"\nexport * from \"./types\"\nexport * from \"./codecs\"\n", "import type { Codec, Decoder, Encoder } from \"./types\"\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 { Decoder } from \"../types\"\n\n// https://jsben.ch/URe1X\nconst HEX_MAP: Record<string, number> = {\n 0: 0,\n 1: 1,\n 2: 2,\n 3: 3,\n 4: 4,\n 5: 5,\n 6: 6,\n 7: 7,\n 8: 8,\n 9: 9,\n a: 10,\n b: 11,\n c: 12,\n d: 13,\n e: 14,\n f: 15,\n A: 10,\n B: 11,\n C: 12,\n D: 13,\n E: 14,\n F: 15,\n}\nexport function fromHex(hexString: string): Uint8Array {\n const isOdd = hexString.length % 2\n /* istanbul ignore next */\n const base = (hexString[1] === \"x\" ? 2 : 0) + isOdd\n const nBytes = (hexString.length - base) / 2 + isOdd\n const bytes = new Uint8Array(nBytes)\n\n if (isOdd) bytes[0] = 0 | HEX_MAP[hexString[2]]\n\n for (let i = 0; i < nBytes; ) {\n const idx = base + i * 2\n const a = HEX_MAP[hexString[idx]]\n const b = HEX_MAP[hexString[idx + 1]]\n bytes[isOdd + i++] = (a << 4) | b\n }\n\n return bytes\n}\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 mergeUint8 = (...inputs: Array<Uint8Array>): Uint8Array => {\n const len = inputs.length\n let totalLen = 0\n for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength\n const result = new Uint8Array(totalLen)\n\n for (let idx = 0, at = 0; idx < len; idx++) {\n const current = inputs[idx]\n result.set(current, at)\n at += current.byteLength\n }\n\n return result\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\nexport const address = uint160\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 { createCodec, Decoder, Encoder } from \"../\"\nimport { toInternalBytes, mergeUint8 } 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 + (nElements % 32)\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 { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\nimport { mergeUint8, 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 { toInternalBytes, mergeUint8 } 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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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/utils.ts", "../src/codecs/address.ts", "../src/internal/toInternalBytes.ts", "../src/internal/range32.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, 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 + (nElements % 32)\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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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;AA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;AAM3B,IAAM,YAAY,CAAI,OAAmB,SAAqC;AAC5E,MAAI,QAAS,GAAG;AACd,UAAM,WAA6B,CAAC,UAClC,+BAAW,GAAG,MAAM,IAAI,KAAK,CAAC;AAChC,aAAQ,MAAM,MAAM;AACpB,WAAO;AAAA,EACT;AACA,QAAM,UAA6B,CAAC,UAClC,+BAAW,KAAK,GAAG,OAAO,MAAM,MAAM,CAAC,GAAG,GAAG,MAAM,IAAI,KAAK,CAAC;AAC/D,UAAQ,MAAM;AACd,SAAO;AACT;AAEA,IAAM,YAAY,CAAI,QAAoB,SAAqC;AAC7E,QAAM,UAAU,gBAAgB,CAAC,YAAU;AACzC,UAAM,YAAY,QAAS,IAAI,OAAQ,OAAO,KAAK,GAAG,OAAK,CAAC;AAC5D,UAAM,UAAU,IAAI,MAAM,SAAS;AAEnC,aAAS,IAAI,GAAG,IAAI,WAAW,KAAK;AAClC,cAAQ,KAAK,OAAO,OAAK;AAAA,IAC3B;AAEA,WAAO;AAAA,EACT,CAAC;AACD,MAAI,QAAQ;AAAM,YAAQ,MAAM;AAChC,SAAO;AACT;AAEO,IAAM,SAAS,CAAI,OAAiB,SAAmC;AAC5E,QAAM,QAAQ,YACZ,UAAU,MAAM,IAAI,IAAI,GACxB,UAAU,MAAM,IAAI,IAAI,CAC1B;AACA,MAAI,QAAQ;AAAM,UAAM,MAAM;AAC9B,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var D=Object.defineProperty;var
|
|
1
|
+
var D=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var Y=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 $=(t,e)=>{for(var n in e)D(t,n,{get:e[n],enumerable:!0})},z=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of X(e))!Y.call(t,o)&&o!==n&&D(t,o,{get:()=>e[o],enumerable:!(r=W(e,o))||r.enumerable});return t};var tt=t=>z(D({},"__esModule",{value:!0}),t);var I=(t,e,n)=>(Z(t,typeof e!="symbol"?e+"":e,n),n);var nn={};$(nn,{Fixed:()=>Xe,Struct:()=>M,Tuple:()=>C,Vector:()=>J,address:()=>S,bool:()=>N,bytes:()=>A,bytes1:()=>St,bytes10:()=>Lt,bytes11:()=>Mt,bytes12:()=>Jt,bytes13:()=>Pt,bytes14:()=>Qt,bytes15:()=>Wt,bytes16:()=>Xt,bytes17:()=>Yt,bytes18:()=>Zt,bytes19:()=>$t,bytes2:()=>Vt,bytes20:()=>zt,bytes21:()=>te,bytes22:()=>ee,bytes23:()=>ne,bytes24:()=>re,bytes25:()=>oe,bytes26:()=>ie,bytes27:()=>ce,bytes28:()=>se,bytes29:()=>de,bytes3:()=>Nt,bytes30:()=>ae,bytes31:()=>me,bytes32:()=>ue,bytes4:()=>jt,bytes5:()=>Ht,bytes6:()=>Gt,bytes7:()=>_t,bytes8:()=>qt,bytes9:()=>Ft,createCodec:()=>d,enhanceCodec:()=>y,enhanceDecoder:()=>K,enhanceEncoder:()=>v,int:()=>We,int104:()=>ke,int112:()=>Be,int120:()=>ve,int128:()=>Ke,int136:()=>Re,int144:()=>Se,int152:()=>Ve,int16:()=>Ae,int160:()=>Ne,int168:()=>je,int176:()=>He,int184:()=>Ge,int192:()=>_e,int200:()=>qe,int208:()=>Fe,int224:()=>Me,int226:()=>Le,int232:()=>Je,int24:()=>be,int240:()=>Pe,int248:()=>Qe,int256:()=>L,int32:()=>De,int40:()=>Te,int48:()=>Ce,int56:()=>Ie,int64:()=>Ue,int72:()=>Ee,int8:()=>xe,int80:()=>he,int88:()=>Oe,int96:()=>we,keccak:()=>U,str:()=>F,uint:()=>u,uint104:()=>yt,uint112:()=>pt,uint120:()=>lt,uint128:()=>gt,uint136:()=>xt,uint144:()=>At,uint152:()=>bt,uint16:()=>nt,uint160:()=>Dt,uint168:()=>Tt,uint176:()=>Ct,uint184:()=>It,uint192:()=>Ut,uint200:()=>Et,uint208:()=>ht,uint224:()=>wt,uint226:()=>Ot,uint232:()=>kt,uint24:()=>rt,uint240:()=>Bt,uint248:()=>vt,uint256:()=>V,uint32:()=>ot,uint40:()=>it,uint48:()=>ct,uint56:()=>st,uint64:()=>dt,uint72:()=>at,uint8:()=>h,uint80:()=>mt,uint88:()=>ut,uint96:()=>ft});module.exports=tt(nn);var B=require("@noble/hashes/sha3"),U=B.keccak_256,d=(t,e)=>{let n=[t,e];return n.enc=t,n.dec=e,n},E=(t,e)=>(t.dyn&&(e.dyn=!0),e),v=(t,e)=>E(t,n=>t(e(n))),K=(t,e)=>E(t,n=>e(t(n))),y=(t,e,n)=>E(t,d(v(t[0],e),K(t[1],n)));var x=require("@unstoppablejs/utils");var R=require("@unstoppablejs/utils"),T=class extends Uint8Array{constructor(n){super(n);I(this,"i",0);I(this,"v");this.v=new DataView(n)}},a=t=>e=>t(e instanceof T?e:new T(e instanceof Uint8Array?e.buffer:typeof e=="string"?(0,R.fromHex)(e).buffer:e));var g=Array(32).fill(0).map((t,e)=>e+1);var S=d(t=>{let e=new Uint8Array(32);return e.set((0,x.fromHex)(t),12),e},a(t=>{let e=new Uint8Array(t.buffer,t.i+12,20);t.i+=32;let n=(0,x.toHex)(e),r=(0,x.toHex)(U(n.slice(2))),o=new Array(41);o[0]="0x";for(let c=2;c<42;c++){let i=n[c];o.push(parseInt(r[c],16)>7?i.toUpperCase():i)}return o.join("")}));var et=t=>{let e=Math.ceil(t/8);return d(n=>{let r=new Uint8Array(32),o=new DataView(r.buffer),c=32-e*8;for(let i=24;i>=c;i-=8)o.setBigUint64(i,n),n>>=64n;return r},a(n=>{let r=0n,o=n.i+32;for(let c=n.i+(32-e*8);c<o;c+=8)r=r<<64n|n.v.getBigUint64(c);return n.i=o,r}))},[h,nt,rt,ot,it,ct,st,dt,at,mt,ut,ft,yt,pt,lt,gt,xt,At,bt,Dt,Tt,Ct,It,Ut,Et,ht,Ot,wt,kt,Bt,vt,V]=g.map(et),u=V;var N=y(h,t=>t?1n:0n,Boolean);var j=require("@unstoppablejs/utils");var H=t=>{let e=[u[0](BigInt(t.length)),t],n=t.length%32;return n>0&&e.push(new Uint8Array(32-n)),(0,j.mergeUint8)(...e)};H.dyn=!0;var G=a(t=>{let e=Number(u[1](t)),n=new Uint8Array(t.buffer,t.i,e);return t.i+=e+e%32,n});G.dyn=!0;var A=d(H,G);A.dyn=!0;var _=new TextEncoder,q=new TextDecoder,F=y(A,_.encode.bind(_),q.decode.bind(q));var Kt=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},Rt=t=>a(e=>{let n=new Uint8Array(e.buffer,e.i,t);return e.i+=32,n}),[St,Vt,Nt,jt,Ht,Gt,_t,qt,Ft,Lt,Mt,Jt,Pt,Qt,Wt,Xt,Yt,Zt,$t,zt,te,ee,ne,re,oe,ie,ce,se,de,ae,me,ue]=g.map(t=>d(Kt(t),Rt(t)));var fe={"1":"getInt8","2":"getInt16","8":"getBigInt64"},ye={"1":"setInt8","2":"setInt16","8":"setBigInt64"},pe={"1":"getUint8","2":"getUint16","8":"getBigUint64"},le={"1":"setUint8","2":"setUint16","8":"setBigUint64"},ge=t=>{let e=t/8|0,n=t%8/2|0,r=[...Array(e).fill([8,64n,i=>i]),...Array(n).fill([2,16n,i=>Number(i&65535n)])];t%2&&r.push([1,8n,i=>Number(i&255n)]);let o=i=>{let s=new Uint8Array(32),m=new DataView(s.buffer);if(i<0n)for(let f=0;f<32-t;f+=8)m.setBigInt64(f,-1n);let p=32;for(let f=r.length-1;f>0;f--){let[k,P,Q]=r[f];p-=k,m[le[k]](p,Q(i)),i>>=P}let[l,,b]=r[0];return p-=l,m[ye[l]](p,b(i)),s},c=a(i=>{let s=i.i+32-t,m=r[0][0],p=BigInt(i.v[fe[m]](s));s+=m;for(let l=1;l<r.length;l++){let[b,f]=r[l];p=p<<f|BigInt(i.v[pe[b]](s)),s+=b}return i.i+=32,p});return d(o,c)},[xe,Ae,be,De,Te,Ce,Ie,Ue,Ee,he,Oe,we,ke,Be,ve,Ke,Re,Se,Ve,Ne,je,He,Ge,_e,qe,Fe,Le,Me,Je,Pe,Qe,L]=g.map(ge),We=L;var Xe=(t,e)=>y(t,n=>n.value,n=>({value:n,decimals:e}));var O=require("@unstoppablejs/utils");var Ye=(...t)=>{let e=n=>{let r=n.map((s,m)=>t[m](s)),o=new Array(t.length),c=[],i=0n;for(let s=0;s<t.length;s++)t[s].dyn?(c.push(s),i+=32n):(o[s]=r[s],i+=BigInt(r[s].length));return c.forEach(s=>{o[s]=u[0](i);let m=r[s];o.push(m),i+=BigInt(m.length)}),(0,O.mergeUint8)(...o)};return e.dyn=!0,e},Ze=(...t)=>e=>(0,O.mergeUint8)(...e.map((n,r)=>t[r](n))),$e=(...t)=>a(e=>t.map(n=>n(e))),ze=(...t)=>{let e=a(n=>{let r=new Array(t.length),o=n.i;for(let c=0;c<t.length;c++)if(t[c].dyn){let i=Number(u[1](n)),s=n.i;n.i=o+i,r[c]=t[c](n),n.i=s}else r[c]=t[c](n);return r});return e.dyn=!0,e},C=(...t)=>{let e=t.some(c=>c.dyn),[n,r]=e?[Ye,ze]:[Ze,$e],o=d(n(...t.map(([c])=>c)),r(...t.map(([,c])=>c)));return o.dyn=e,o};var M=t=>{let e=Object.keys(t);return y(C(...Object.values(t)),n=>e.map(r=>n[r]),n=>Object.fromEntries(n.map((r,o)=>[e[o],r])))};var w=require("@unstoppablejs/utils");var tn=(t,e)=>{if(e>=0){let r=o=>(0,w.mergeUint8)(...o.map(t));return r.dyn=t.dyn,r}let n=r=>(0,w.mergeUint8)(u[0](BigInt(r.length)),...r.map(t));return n.dyn=!0,n},en=(t,e)=>{let n=a(r=>{let o=e>=0?e:Number(u[1](r)),c=new Array(o);for(let i=0;i<o;i++)c[i]=t(r);return c});return e==null&&(n.dyn=!0),n},J=(t,e)=>{let n=d(tn(t[0],e),en(t[1],e));return e==null&&(n.dyn=!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/utils.ts", "../src/
|
|
4
|
-
"sourcesContent": ["export * from \"./utils\"\nexport * from \"./types\"\nexport * from \"./codecs\"\n", "import type { Codec, Decoder, Encoder } from \"./types\"\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 { Decoder } from \"../types\"\n\n// https://jsben.ch/URe1X\nconst HEX_MAP: Record<string, number> = {\n 0: 0,\n 1: 1,\n 2: 2,\n 3: 3,\n 4: 4,\n 5: 5,\n 6: 6,\n 7: 7,\n 8: 8,\n 9: 9,\n a: 10,\n b: 11,\n c: 12,\n d: 13,\n e: 14,\n f: 15,\n A: 10,\n B: 11,\n C: 12,\n D: 13,\n E: 14,\n F: 15,\n}\nexport function fromHex(hexString: string): Uint8Array {\n const isOdd = hexString.length % 2\n /* istanbul ignore next */\n const base = (hexString[1] === \"x\" ? 2 : 0) + isOdd\n const nBytes = (hexString.length - base) / 2 + isOdd\n const bytes = new Uint8Array(nBytes)\n\n if (isOdd) bytes[0] = 0 | HEX_MAP[hexString[2]]\n\n for (let i = 0; i < nBytes; ) {\n const idx = base + i * 2\n const a = HEX_MAP[hexString[idx]]\n const b = HEX_MAP[hexString[idx + 1]]\n bytes[isOdd + i++] = (a << 4) | b\n }\n\n return bytes\n}\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 mergeUint8 = (...inputs: Array<Uint8Array>): Uint8Array => {\n const len = inputs.length\n let totalLen = 0\n for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength\n const result = new Uint8Array(totalLen)\n\n for (let idx = 0, at = 0; idx < len; idx++) {\n const current = inputs[idx]\n result.set(current, at)\n at += current.byteLength\n }\n\n return result\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\nexport const address = uint160\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 { createCodec, Decoder, Encoder } from \"../\"\nimport { toInternalBytes, mergeUint8 } 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 + (nElements % 32)\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 { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\nimport { mergeUint8, 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 { toInternalBytes, mergeUint8 } 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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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/utils.ts", "../src/codecs/address.ts", "../src/internal/toInternalBytes.ts", "../src/internal/range32.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, 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 + (nElements % 32)\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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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": "yiBAAA,slDCCA,MAA2B,8BAEd,EAAS,aAET,EAAc,CACzB,EACA,IACa,CACb,GAAM,GAAS,CAAC,EAAS,CAAO,EAChC,SAAO,IAAM,EACb,EAAO,IAAM,EACN,CACT,EAEM,EAAM,CACV,EACA,IAEI,GAAM,KAAK,GAAO,IAAM,IACrB,GAGI,EAAiB,CAC5B,EACA,IACe,EAAI,EAAU,AAAC,GAAU,EAAQ,EAAO,CAAK,CAAC,CAAgB,EAElE,EAAiB,CAC5B,EACA,IACe,EAAI,EAAU,AAAC,GAAU,EAAO,EAAQ,CAAK,CAAC,CAAgB,EAElE,EAAe,CAC1B,EACA,EACA,IAEA,EACE,EACA,EACE,EAAe,EAAM,GAAI,CAAM,EAC/B,EAAe,EAAM,GAAI,CAAM,CACjC,CACF,EC5CF,MAA+B,gCCA/B,MAAwB,gCAGxB,eAAiC,WAAW,CAI1C,YAAY,EAAqB,CAC/B,M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": []
|
|
7
7
|
}
|
package/dist/solidity-codecs.js
CHANGED
|
@@ -6,6 +6,8 @@ var __publicField = (obj, key, value) => {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
// src/utils.ts
|
|
9
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
10
|
+
var keccak = keccak_256;
|
|
9
11
|
var createCodec = (encoder, decoder) => {
|
|
10
12
|
const result = [encoder, decoder];
|
|
11
13
|
result.enc = encoder;
|
|
@@ -21,46 +23,11 @@ var enhanceEncoder = (encoder, mapper) => dyn(encoder, (value) => encoder(mapper
|
|
|
21
23
|
var enhanceDecoder = (decoder, mapper) => dyn(decoder, (value) => mapper(decoder(value)));
|
|
22
24
|
var enhanceCodec = (codec, toFrom, fromTo) => dyn(codec, createCodec(enhanceEncoder(codec[0], toFrom), enhanceDecoder(codec[1], fromTo)));
|
|
23
25
|
|
|
26
|
+
// src/codecs/address.ts
|
|
27
|
+
import { fromHex as fromHex2, toHex } from "@unstoppablejs/utils";
|
|
28
|
+
|
|
24
29
|
// src/internal/toInternalBytes.ts
|
|
25
|
-
|
|
26
|
-
0: 0,
|
|
27
|
-
1: 1,
|
|
28
|
-
2: 2,
|
|
29
|
-
3: 3,
|
|
30
|
-
4: 4,
|
|
31
|
-
5: 5,
|
|
32
|
-
6: 6,
|
|
33
|
-
7: 7,
|
|
34
|
-
8: 8,
|
|
35
|
-
9: 9,
|
|
36
|
-
a: 10,
|
|
37
|
-
b: 11,
|
|
38
|
-
c: 12,
|
|
39
|
-
d: 13,
|
|
40
|
-
e: 14,
|
|
41
|
-
f: 15,
|
|
42
|
-
A: 10,
|
|
43
|
-
B: 11,
|
|
44
|
-
C: 12,
|
|
45
|
-
D: 13,
|
|
46
|
-
E: 14,
|
|
47
|
-
F: 15
|
|
48
|
-
};
|
|
49
|
-
function fromHex(hexString) {
|
|
50
|
-
const isOdd = hexString.length % 2;
|
|
51
|
-
const base = (hexString[1] === "x" ? 2 : 0) + isOdd;
|
|
52
|
-
const nBytes = (hexString.length - base) / 2 + isOdd;
|
|
53
|
-
const bytes33 = new Uint8Array(nBytes);
|
|
54
|
-
if (isOdd)
|
|
55
|
-
bytes33[0] = 0 | HEX_MAP[hexString[2]];
|
|
56
|
-
for (let i = 0; i < nBytes; ) {
|
|
57
|
-
const idx = base + i * 2;
|
|
58
|
-
const a = HEX_MAP[hexString[idx]];
|
|
59
|
-
const b = HEX_MAP[hexString[idx + 1]];
|
|
60
|
-
bytes33[isOdd + i++] = a << 4 | b;
|
|
61
|
-
}
|
|
62
|
-
return bytes33;
|
|
63
|
-
}
|
|
30
|
+
import { fromHex } from "@unstoppablejs/utils";
|
|
64
31
|
var InternalUint8Array = class extends Uint8Array {
|
|
65
32
|
constructor(buffer) {
|
|
66
33
|
super(buffer);
|
|
@@ -71,24 +38,28 @@ var InternalUint8Array = class extends Uint8Array {
|
|
|
71
38
|
};
|
|
72
39
|
var toInternalBytes = (fn) => (buffer) => fn(buffer instanceof InternalUint8Array ? buffer : new InternalUint8Array(buffer instanceof Uint8Array ? buffer.buffer : typeof buffer === "string" ? fromHex(buffer).buffer : buffer));
|
|
73
40
|
|
|
74
|
-
// src/internal/mergeUint8.ts
|
|
75
|
-
var mergeUint8 = (...inputs) => {
|
|
76
|
-
const len = inputs.length;
|
|
77
|
-
let totalLen = 0;
|
|
78
|
-
for (let i = 0; i < len; i++)
|
|
79
|
-
totalLen += inputs[i].byteLength;
|
|
80
|
-
const result = new Uint8Array(totalLen);
|
|
81
|
-
for (let idx = 0, at = 0; idx < len; idx++) {
|
|
82
|
-
const current = inputs[idx];
|
|
83
|
-
result.set(current, at);
|
|
84
|
-
at += current.byteLength;
|
|
85
|
-
}
|
|
86
|
-
return result;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
41
|
// src/internal/range32.ts
|
|
90
42
|
var range32 = Array(32).fill(0).map((_, idx) => idx + 1);
|
|
91
43
|
|
|
44
|
+
// src/codecs/address.ts
|
|
45
|
+
var address = createCodec((input) => {
|
|
46
|
+
const result = new Uint8Array(32);
|
|
47
|
+
result.set(fromHex2(input), 12);
|
|
48
|
+
return result;
|
|
49
|
+
}, toInternalBytes((bytes33) => {
|
|
50
|
+
const binaryAddress = new Uint8Array(bytes33.buffer, bytes33.i + 12, 20);
|
|
51
|
+
bytes33.i += 32;
|
|
52
|
+
const nonChecksum = toHex(binaryAddress);
|
|
53
|
+
const hashedAddres = toHex(keccak(nonChecksum.slice(2)));
|
|
54
|
+
const result = new Array(41);
|
|
55
|
+
result[0] = "0x";
|
|
56
|
+
for (let i = 2; i < 42; i++) {
|
|
57
|
+
const char = nonChecksum[i];
|
|
58
|
+
result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char);
|
|
59
|
+
}
|
|
60
|
+
return result.join("");
|
|
61
|
+
}));
|
|
62
|
+
|
|
92
63
|
// src/codecs/Uint.ts
|
|
93
64
|
var getCodec = (nBytes) => {
|
|
94
65
|
const n64 = Math.ceil(nBytes / 8);
|
|
@@ -145,12 +116,12 @@ var [
|
|
|
145
116
|
uint256
|
|
146
117
|
] = range32.map(getCodec);
|
|
147
118
|
var uint = uint256;
|
|
148
|
-
var address = uint160;
|
|
149
119
|
|
|
150
120
|
// src/codecs/bool.ts
|
|
151
121
|
var bool = enhanceCodec(uint8, (value) => value ? 1n : 0n, Boolean);
|
|
152
122
|
|
|
153
123
|
// src/codecs/bytes.ts
|
|
124
|
+
import { mergeUint8 } from "@unstoppablejs/utils";
|
|
154
125
|
var bytesEnc = (val) => {
|
|
155
126
|
const args = [uint[0](BigInt(val.length)), val];
|
|
156
127
|
const extra = val.length % 32;
|
|
@@ -328,6 +299,7 @@ var int = int256;
|
|
|
328
299
|
var Fixed = (baseCodec, decimals) => enhanceCodec(baseCodec, (x) => x.value, (value) => ({ value, decimals }));
|
|
329
300
|
|
|
330
301
|
// src/codecs/Tuple.ts
|
|
302
|
+
import { mergeUint8 as mergeUint82 } from "@unstoppablejs/utils";
|
|
331
303
|
var dynamicEnc = (...encoders) => {
|
|
332
304
|
const res = (values) => {
|
|
333
305
|
const mapped = values.map((value, idx) => encoders[idx](value));
|
|
@@ -349,12 +321,12 @@ var dynamicEnc = (...encoders) => {
|
|
|
349
321
|
resultArray.push(data);
|
|
350
322
|
len += BigInt(data.length);
|
|
351
323
|
});
|
|
352
|
-
return
|
|
324
|
+
return mergeUint82(...resultArray);
|
|
353
325
|
};
|
|
354
326
|
res.dyn = true;
|
|
355
327
|
return res;
|
|
356
328
|
};
|
|
357
|
-
var staticEnc = (...encoders) => (values) =>
|
|
329
|
+
var staticEnc = (...encoders) => (values) => mergeUint82(...values.map((value, idx) => encoders[idx](value)));
|
|
358
330
|
var staticDec = (...decoders) => toInternalBytes((bytes33) => decoders.map((decoder) => decoder(bytes33)));
|
|
359
331
|
var dynamicDec = (...decoders) => {
|
|
360
332
|
const res = toInternalBytes((bytes33) => {
|
|
@@ -391,13 +363,14 @@ var Struct = (codecs) => {
|
|
|
391
363
|
};
|
|
392
364
|
|
|
393
365
|
// src/codecs/Vector.ts
|
|
366
|
+
import { mergeUint8 as mergeUint83 } from "@unstoppablejs/utils";
|
|
394
367
|
var vectorEnc = (inner, size) => {
|
|
395
368
|
if (size >= 0) {
|
|
396
|
-
const encoder2 = (value) =>
|
|
369
|
+
const encoder2 = (value) => mergeUint83(...value.map(inner));
|
|
397
370
|
encoder2.dyn = inner.dyn;
|
|
398
371
|
return encoder2;
|
|
399
372
|
}
|
|
400
|
-
const encoder = (value) =>
|
|
373
|
+
const encoder = (value) => mergeUint83(uint[0](BigInt(value.length)), ...value.map(inner));
|
|
401
374
|
encoder.dyn = true;
|
|
402
375
|
return encoder;
|
|
403
376
|
};
|
|
@@ -497,6 +470,7 @@ export {
|
|
|
497
470
|
int80,
|
|
498
471
|
int88,
|
|
499
472
|
int96,
|
|
473
|
+
keccak,
|
|
500
474
|
str,
|
|
501
475
|
uint,
|
|
502
476
|
uint104,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/utils.ts", "../src/
|
|
4
|
-
"sourcesContent": ["import type { Codec, Decoder, Encoder } from \"./types\"\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 { Decoder } from \"../types\"\n\n// https://jsben.ch/URe1X\nconst HEX_MAP: Record<string, number> = {\n 0: 0,\n 1: 1,\n 2: 2,\n 3: 3,\n 4: 4,\n 5: 5,\n 6: 6,\n 7: 7,\n 8: 8,\n 9: 9,\n a: 10,\n b: 11,\n c: 12,\n d: 13,\n e: 14,\n f: 15,\n A: 10,\n B: 11,\n C: 12,\n D: 13,\n E: 14,\n F: 15,\n}\nexport function fromHex(hexString: string): Uint8Array {\n const isOdd = hexString.length % 2\n /* istanbul ignore next */\n const base = (hexString[1] === \"x\" ? 2 : 0) + isOdd\n const nBytes = (hexString.length - base) / 2 + isOdd\n const bytes = new Uint8Array(nBytes)\n\n if (isOdd) bytes[0] = 0 | HEX_MAP[hexString[2]]\n\n for (let i = 0; i < nBytes; ) {\n const idx = base + i * 2\n const a = HEX_MAP[hexString[idx]]\n const b = HEX_MAP[hexString[idx + 1]]\n bytes[isOdd + i++] = (a << 4) | b\n }\n\n return bytes\n}\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 mergeUint8 = (...inputs: Array<Uint8Array>): Uint8Array => {\n const len = inputs.length\n let totalLen = 0\n for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength\n const result = new Uint8Array(totalLen)\n\n for (let idx = 0, at = 0; idx < len; idx++) {\n const current = inputs[idx]\n result.set(current, at)\n at += current.byteLength\n }\n\n return result\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\nexport const address = uint160\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 { createCodec, Decoder, Encoder } from \"../\"\nimport { toInternalBytes, mergeUint8 } 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 + (nElements % 32)\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 { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\nimport { mergeUint8, 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 { toInternalBytes, mergeUint8 } 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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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": ";;;;;;;;AAEO,IAAM,cAAc,CACzB,SACA,YACa;AACb,QAAM,SAAS,CAAC,SAAS,OAAO;AAChC,SAAO,MAAM;AACb,SAAO,MAAM;AACb,SAAO;AACT;AAEA,IAAM,MAAM,CACV,OACA,WACM;AACN,MAAI,MAAM;AAAK,WAAO,MAAM;AAC5B,SAAO;AACT;AAEO,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAAU,QAAQ,OAAO,KAAK,CAAC,CAAgB;AAExE,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAAU,OAAO,QAAQ,KAAK,CAAC,CAAgB;AAExE,IAAM,eAAe,CAC1B,OACA,QACA,WAEA,IACE,OACA,YACE,eAAe,MAAM,IAAI,MAAM,GAC/B,eAAe,MAAM,IAAI,MAAM,CACjC,CACF;;;
|
|
3
|
+
"sources": ["../src/utils.ts", "../src/codecs/address.ts", "../src/internal/toInternalBytes.ts", "../src/internal/range32.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": ["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 + (nElements % 32)\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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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": ";;;;;;;;AACA;AAEO,IAAM,SAAS;AAEf,IAAM,cAAc,CACzB,SACA,YACa;AACb,QAAM,SAAS,CAAC,SAAS,OAAO;AAChC,SAAO,MAAM;AACb,SAAO,MAAM;AACb,SAAO;AACT;AAEA,IAAM,MAAM,CACV,OACA,WACM;AACN,MAAI,MAAM;AAAK,WAAO,MAAM;AAC5B,SAAO;AACT;AAEO,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAA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AM,UAAU,IAAI,MAAM,SAAS;AAEnC,aAAS,IAAI,GAAG,IAAI,WAAW,KAAK;AAClC,cAAQ,KAAK,OAAO,OAAK;AAAA,IAC3B;AAEA,WAAO;AAAA,EACT,CAAC;AACD,MAAI,QAAQ;AAAM,YAAQ,MAAM;AAChC,SAAO;AACT;AAEO,IAAM,SAAS,CAAI,OAAiB,SAAmC;AAC5E,QAAM,QAAQ,YACZ,UAAU,MAAM,IAAI,IAAI,GACxB,UAAU,MAAM,IAAI,IAAI,CAC1B;AACA,MAAI,QAAQ;AAAM,UAAM,MAAM;AAC9B,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/solidity-codecs.mjs
CHANGED
|
@@ -6,6 +6,8 @@ var __publicField = (obj, key, value) => {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
// src/utils.ts
|
|
9
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
10
|
+
var keccak = keccak_256;
|
|
9
11
|
var createCodec = (encoder, decoder) => {
|
|
10
12
|
const result = [encoder, decoder];
|
|
11
13
|
result.enc = encoder;
|
|
@@ -21,46 +23,11 @@ var enhanceEncoder = (encoder, mapper) => dyn(encoder, (value) => encoder(mapper
|
|
|
21
23
|
var enhanceDecoder = (decoder, mapper) => dyn(decoder, (value) => mapper(decoder(value)));
|
|
22
24
|
var enhanceCodec = (codec, toFrom, fromTo) => dyn(codec, createCodec(enhanceEncoder(codec[0], toFrom), enhanceDecoder(codec[1], fromTo)));
|
|
23
25
|
|
|
26
|
+
// src/codecs/address.ts
|
|
27
|
+
import { fromHex as fromHex2, toHex } from "@unstoppablejs/utils";
|
|
28
|
+
|
|
24
29
|
// src/internal/toInternalBytes.ts
|
|
25
|
-
|
|
26
|
-
0: 0,
|
|
27
|
-
1: 1,
|
|
28
|
-
2: 2,
|
|
29
|
-
3: 3,
|
|
30
|
-
4: 4,
|
|
31
|
-
5: 5,
|
|
32
|
-
6: 6,
|
|
33
|
-
7: 7,
|
|
34
|
-
8: 8,
|
|
35
|
-
9: 9,
|
|
36
|
-
a: 10,
|
|
37
|
-
b: 11,
|
|
38
|
-
c: 12,
|
|
39
|
-
d: 13,
|
|
40
|
-
e: 14,
|
|
41
|
-
f: 15,
|
|
42
|
-
A: 10,
|
|
43
|
-
B: 11,
|
|
44
|
-
C: 12,
|
|
45
|
-
D: 13,
|
|
46
|
-
E: 14,
|
|
47
|
-
F: 15
|
|
48
|
-
};
|
|
49
|
-
function fromHex(hexString) {
|
|
50
|
-
const isOdd = hexString.length % 2;
|
|
51
|
-
const base = (hexString[1] === "x" ? 2 : 0) + isOdd;
|
|
52
|
-
const nBytes = (hexString.length - base) / 2 + isOdd;
|
|
53
|
-
const bytes33 = new Uint8Array(nBytes);
|
|
54
|
-
if (isOdd)
|
|
55
|
-
bytes33[0] = 0 | HEX_MAP[hexString[2]];
|
|
56
|
-
for (let i = 0; i < nBytes; ) {
|
|
57
|
-
const idx = base + i * 2;
|
|
58
|
-
const a = HEX_MAP[hexString[idx]];
|
|
59
|
-
const b = HEX_MAP[hexString[idx + 1]];
|
|
60
|
-
bytes33[isOdd + i++] = a << 4 | b;
|
|
61
|
-
}
|
|
62
|
-
return bytes33;
|
|
63
|
-
}
|
|
30
|
+
import { fromHex } from "@unstoppablejs/utils";
|
|
64
31
|
var InternalUint8Array = class extends Uint8Array {
|
|
65
32
|
constructor(buffer) {
|
|
66
33
|
super(buffer);
|
|
@@ -71,24 +38,28 @@ var InternalUint8Array = class extends Uint8Array {
|
|
|
71
38
|
};
|
|
72
39
|
var toInternalBytes = (fn) => (buffer) => fn(buffer instanceof InternalUint8Array ? buffer : new InternalUint8Array(buffer instanceof Uint8Array ? buffer.buffer : typeof buffer === "string" ? fromHex(buffer).buffer : buffer));
|
|
73
40
|
|
|
74
|
-
// src/internal/mergeUint8.ts
|
|
75
|
-
var mergeUint8 = (...inputs) => {
|
|
76
|
-
const len = inputs.length;
|
|
77
|
-
let totalLen = 0;
|
|
78
|
-
for (let i = 0; i < len; i++)
|
|
79
|
-
totalLen += inputs[i].byteLength;
|
|
80
|
-
const result = new Uint8Array(totalLen);
|
|
81
|
-
for (let idx = 0, at = 0; idx < len; idx++) {
|
|
82
|
-
const current = inputs[idx];
|
|
83
|
-
result.set(current, at);
|
|
84
|
-
at += current.byteLength;
|
|
85
|
-
}
|
|
86
|
-
return result;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
41
|
// src/internal/range32.ts
|
|
90
42
|
var range32 = Array(32).fill(0).map((_, idx) => idx + 1);
|
|
91
43
|
|
|
44
|
+
// src/codecs/address.ts
|
|
45
|
+
var address = createCodec((input) => {
|
|
46
|
+
const result = new Uint8Array(32);
|
|
47
|
+
result.set(fromHex2(input), 12);
|
|
48
|
+
return result;
|
|
49
|
+
}, toInternalBytes((bytes33) => {
|
|
50
|
+
const binaryAddress = new Uint8Array(bytes33.buffer, bytes33.i + 12, 20);
|
|
51
|
+
bytes33.i += 32;
|
|
52
|
+
const nonChecksum = toHex(binaryAddress);
|
|
53
|
+
const hashedAddres = toHex(keccak(nonChecksum.slice(2)));
|
|
54
|
+
const result = new Array(41);
|
|
55
|
+
result[0] = "0x";
|
|
56
|
+
for (let i = 2; i < 42; i++) {
|
|
57
|
+
const char = nonChecksum[i];
|
|
58
|
+
result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char);
|
|
59
|
+
}
|
|
60
|
+
return result.join("");
|
|
61
|
+
}));
|
|
62
|
+
|
|
92
63
|
// src/codecs/Uint.ts
|
|
93
64
|
var getCodec = (nBytes) => {
|
|
94
65
|
const n64 = Math.ceil(nBytes / 8);
|
|
@@ -145,12 +116,12 @@ var [
|
|
|
145
116
|
uint256
|
|
146
117
|
] = range32.map(getCodec);
|
|
147
118
|
var uint = uint256;
|
|
148
|
-
var address = uint160;
|
|
149
119
|
|
|
150
120
|
// src/codecs/bool.ts
|
|
151
121
|
var bool = enhanceCodec(uint8, (value) => value ? 1n : 0n, Boolean);
|
|
152
122
|
|
|
153
123
|
// src/codecs/bytes.ts
|
|
124
|
+
import { mergeUint8 } from "@unstoppablejs/utils";
|
|
154
125
|
var bytesEnc = (val) => {
|
|
155
126
|
const args = [uint[0](BigInt(val.length)), val];
|
|
156
127
|
const extra = val.length % 32;
|
|
@@ -328,6 +299,7 @@ var int = int256;
|
|
|
328
299
|
var Fixed = (baseCodec, decimals) => enhanceCodec(baseCodec, (x) => x.value, (value) => ({ value, decimals }));
|
|
329
300
|
|
|
330
301
|
// src/codecs/Tuple.ts
|
|
302
|
+
import { mergeUint8 as mergeUint82 } from "@unstoppablejs/utils";
|
|
331
303
|
var dynamicEnc = (...encoders) => {
|
|
332
304
|
const res = (values) => {
|
|
333
305
|
const mapped = values.map((value, idx) => encoders[idx](value));
|
|
@@ -349,12 +321,12 @@ var dynamicEnc = (...encoders) => {
|
|
|
349
321
|
resultArray.push(data);
|
|
350
322
|
len += BigInt(data.length);
|
|
351
323
|
});
|
|
352
|
-
return
|
|
324
|
+
return mergeUint82(...resultArray);
|
|
353
325
|
};
|
|
354
326
|
res.dyn = true;
|
|
355
327
|
return res;
|
|
356
328
|
};
|
|
357
|
-
var staticEnc = (...encoders) => (values) =>
|
|
329
|
+
var staticEnc = (...encoders) => (values) => mergeUint82(...values.map((value, idx) => encoders[idx](value)));
|
|
358
330
|
var staticDec = (...decoders) => toInternalBytes((bytes33) => decoders.map((decoder) => decoder(bytes33)));
|
|
359
331
|
var dynamicDec = (...decoders) => {
|
|
360
332
|
const res = toInternalBytes((bytes33) => {
|
|
@@ -391,13 +363,14 @@ var Struct = (codecs) => {
|
|
|
391
363
|
};
|
|
392
364
|
|
|
393
365
|
// src/codecs/Vector.ts
|
|
366
|
+
import { mergeUint8 as mergeUint83 } from "@unstoppablejs/utils";
|
|
394
367
|
var vectorEnc = (inner, size) => {
|
|
395
368
|
if (size >= 0) {
|
|
396
|
-
const encoder2 = (value) =>
|
|
369
|
+
const encoder2 = (value) => mergeUint83(...value.map(inner));
|
|
397
370
|
encoder2.dyn = inner.dyn;
|
|
398
371
|
return encoder2;
|
|
399
372
|
}
|
|
400
|
-
const encoder = (value) =>
|
|
373
|
+
const encoder = (value) => mergeUint83(uint[0](BigInt(value.length)), ...value.map(inner));
|
|
401
374
|
encoder.dyn = true;
|
|
402
375
|
return encoder;
|
|
403
376
|
};
|
|
@@ -497,6 +470,7 @@ export {
|
|
|
497
470
|
int80,
|
|
498
471
|
int88,
|
|
499
472
|
int96,
|
|
473
|
+
keccak,
|
|
500
474
|
str,
|
|
501
475
|
uint,
|
|
502
476
|
uint104,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/utils.ts", "../src/
|
|
4
|
-
"sourcesContent": ["import type { Codec, Decoder, Encoder } from \"./types\"\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 { Decoder } from \"../types\"\n\n// https://jsben.ch/URe1X\nconst HEX_MAP: Record<string, number> = {\n 0: 0,\n 1: 1,\n 2: 2,\n 3: 3,\n 4: 4,\n 5: 5,\n 6: 6,\n 7: 7,\n 8: 8,\n 9: 9,\n a: 10,\n b: 11,\n c: 12,\n d: 13,\n e: 14,\n f: 15,\n A: 10,\n B: 11,\n C: 12,\n D: 13,\n E: 14,\n F: 15,\n}\nexport function fromHex(hexString: string): Uint8Array {\n const isOdd = hexString.length % 2\n /* istanbul ignore next */\n const base = (hexString[1] === \"x\" ? 2 : 0) + isOdd\n const nBytes = (hexString.length - base) / 2 + isOdd\n const bytes = new Uint8Array(nBytes)\n\n if (isOdd) bytes[0] = 0 | HEX_MAP[hexString[2]]\n\n for (let i = 0; i < nBytes; ) {\n const idx = base + i * 2\n const a = HEX_MAP[hexString[idx]]\n const b = HEX_MAP[hexString[idx + 1]]\n bytes[isOdd + i++] = (a << 4) | b\n }\n\n return bytes\n}\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 mergeUint8 = (...inputs: Array<Uint8Array>): Uint8Array => {\n const len = inputs.length\n let totalLen = 0\n for (let i = 0; i < len; i++) totalLen += inputs[i].byteLength\n const result = new Uint8Array(totalLen)\n\n for (let idx = 0, at = 0; idx < len; idx++) {\n const current = inputs[idx]\n result.set(current, at)\n at += current.byteLength\n }\n\n return result\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\nexport const address = uint160\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 { createCodec, Decoder, Encoder } from \"../\"\nimport { toInternalBytes, mergeUint8 } 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 + (nElements % 32)\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 { Codec, Decoder, Encoder } from \"../types\"\nimport { uint } from \"./Uint\"\nimport { mergeUint8, 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 { toInternalBytes, mergeUint8 } 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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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": ";;;;;;;;AAEO,IAAM,cAAc,CACzB,SACA,YACa;AACb,QAAM,SAAS,CAAC,SAAS,OAAO;AAChC,SAAO,MAAM;AACb,SAAO,MAAM;AACb,SAAO;AACT;AAEA,IAAM,MAAM,CACV,OACA,WACM;AACN,MAAI,MAAM;AAAK,WAAO,MAAM;AAC5B,SAAO;AACT;AAEO,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAAU,QAAQ,OAAO,KAAK,CAAC,CAAgB;AAExE,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAAU,OAAO,QAAQ,KAAK,CAAC,CAAgB;AAExE,IAAM,eAAe,CAC1B,OACA,QACA,WAEA,IACE,OACA,YACE,eAAe,MAAM,IAAI,MAAM,GAC/B,eAAe,MAAM,IAAI,MAAM,CACjC,CACF;;;
|
|
3
|
+
"sources": ["../src/utils.ts", "../src/codecs/address.ts", "../src/internal/toInternalBytes.ts", "../src/internal/range32.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": ["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 + (nElements % 32)\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 if (size! >= 0) {\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(...value.map(inner))\n encoder.dyn = inner.dyn\n return encoder\n }\n const encoder: Encoder<Array<T>> = (value) =>\n mergeUint8(uint[0](BigInt(value.length)), ...value.map(inner))\n encoder.dyn = true\n return encoder\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 for (let i = 0; i < nElements; i++) {\n decoded[i] = getter(bytes)\n }\n\n return decoded\n })\n if (size == null) 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": ";;;;;;;;AACA;AAEO,IAAM,SAAS;AAEf,IAAM,cAAc,CACzB,SACA,YACa;AACb,QAAM,SAAS,CAAC,SAAS,OAAO;AAChC,SAAO,MAAM;AACb,SAAO,MAAM;AACb,SAAO;AACT;AAEA,IAAM,MAAM,CACV,OACA,WACM;AACN,MAAI,MAAM;AAAK,WAAO,MAAM;AAC5B,SAAO;AACT;AAEO,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAAU,QAAQ,OAAO,KAAK,CAAC,CAAgB;AAExE,IAAM,iBAAiB,CAC5B,SACA,WACe,IAAI,SAAU,CAAC,UAAU,OAAO,QAAQ,KAAK,CAAC,CAAgB;AAExE,IAAM,eAAe,CAC1B,OACA,QACA,WAEA,IACE,OACA,YACE,eAAe,MAAM,IAAI,MAAM,GAC/B,eAAe,MAAM,IAAI,MAAM,CACjC,CACF;;;AC5CF;;;ACAA;AAGA,uCAAiC,WAAW;AAAA,EAI1C,YAAY,QAAqB;AAC/B,UAAM,MAAM;AAJd,6BAAY;AACZ;AAIE,SAAK,IAAI,IAAI,SAAS,MAAM;AAAA,EAC9B;AACF;AAEO,IAAM,kBACX,CAAI,OACJ,CAAC,WACC,GACE,kBAAkB,qBACd,SACA,IAAI,mBACF,kBAAkB,aACd,OAAO,SACP,OAAO,WAAW,WAClB,QAAQ,MAAM,EAAE,SAChB,MACN,CACN;;;AC1BG,IAAM,UAAU,MAAM,EAAE,EAC5B,KAAK,CAAC,EACN,IAAI,CAAC,GAAG,QAAQ,MAAM,CAAC;;;AFEnB,IAAM,UAAU,YACrB,CAAC,UAAkB;AACjB,QAAM,SAAS,IAAI,WAAW,EAAE;AAChC,SAAO,IAAI,SAAQ,KAAK,GAAG,EAAE;AAC7B,SAAO;AACT,GACA,gBAAgB,CAAC,YAAU;AACzB,QAAM,g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B,CAAC,UAClC,YAAW,GAAG,MAAM,IAAI,KAAK,CAAC;AAChC,aAAQ,MAAM,MAAM;AACpB,WAAO;AAAA,EACT;AACA,QAAM,UAA6B,CAAC,UAClC,YAAW,KAAK,GAAG,OAAO,MAAM,MAAM,CAAC,GAAG,GAAG,MAAM,IAAI,KAAK,CAAC;AAC/D,UAAQ,MAAM;AACd,SAAO;AACT;AAEA,IAAM,YAAY,CAAI,QAAoB,SAAqC;AAC7E,QAAM,UAAU,gBAAgB,CAAC,YAAU;AACzC,UAAM,YAAY,QAAS,IAAI,OAAQ,OAAO,KAAK,GAAG,OAAK,CAAC;AAC5D,UAAM,UAAU,IAAI,MAAM,SAAS;AAEnC,aAAS,IAAI,GAAG,IAAI,WAAW,KAAK;AAClC,cAAQ,KAAK,OAAO,OAAK;AAAA,IAC3B;AAEA,WAAO;AAAA,EACT,CAAC;AACD,MAAI,QAAQ;AAAM,YAAQ,MAAM;AAChC,SAAO;AACT;AAEO,IAAM,SAAS,CAAI,OAAiB,SAAmC;AAC5E,QAAM,QAAQ,YACZ,UAAU,MAAM,IAAI,IAAI,GACxB,UAAU,MAAM,IAAI,IAAI,CAC1B;AACA,MAAI,QAAQ;AAAM,UAAM,MAAM;AAC9B,SAAO;AACT;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { Codec, Decoder, Encoder } from "./types";
|
|
2
|
+
export declare const keccak: {
|
|
3
|
+
(message: import("@noble/hashes/utils").Input): Uint8Array;
|
|
4
|
+
outputLen: number;
|
|
5
|
+
blockLen: number;
|
|
6
|
+
create(): import("@noble/hashes/utils").Hash<import("@noble/hashes/sha3").Keccak>;
|
|
7
|
+
};
|
|
2
8
|
export declare const createCodec: <T>(encoder: Encoder<T>, decoder: Decoder<T>) => Codec<T>;
|
|
3
9
|
export declare const enhanceEncoder: <I, O>(encoder: Encoder<I>, mapper: (value: O) => I) => Encoder<O>;
|
|
4
10
|
export declare const enhanceDecoder: <I, O>(decoder: Decoder<I>, mapper: (value: I) => O) => Decoder<O>;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.1.2",
|
|
3
3
|
"repository": {
|
|
4
4
|
"type": "git",
|
|
5
5
|
"url": "git+https://github.com/unstoppablejs/unstoppablejs.git"
|
|
@@ -25,11 +25,10 @@
|
|
|
25
25
|
],
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "npm run build:ts && npm run build:esm && npm run build:cjs:dev && npm run build:cjs:prod",
|
|
28
|
-
"build:esm": "esbuild src/index.ts --bundle --outfile=./dist/solidity-codecs.js --target=es2020 --format=esm --sourcemap && cp ./dist/solidity-codecs.js ./dist/solidity-codecs.mjs && cp ./dist/solidity-codecs.js.map ./dist/solidity-codecs.mjs.map",
|
|
29
|
-
"build:cjs:dev": "esbuild src/index.ts --bundle --outfile=./dist/solidity-codecs.cjs.development.js --target=es2020 --format=cjs --sourcemap",
|
|
30
|
-
"build:cjs:prod": "esbuild src/index.ts --bundle --outfile=./dist/solidity-codecs.cjs.production.min.js --target=es2020 --format=cjs --minify --sourcemap",
|
|
28
|
+
"build:esm": "esbuild src/index.ts --bundle --external:@unstoppablejs/utils --external:@noble/hashes --outfile=./dist/solidity-codecs.js --target=es2020 --format=esm --sourcemap && cp ./dist/solidity-codecs.js ./dist/solidity-codecs.mjs && cp ./dist/solidity-codecs.js.map ./dist/solidity-codecs.mjs.map",
|
|
29
|
+
"build:cjs:dev": "esbuild src/index.ts --bundle --external:@unstoppablejs/utils --external:@noble/hashes --outfile=./dist/solidity-codecs.cjs.development.js --target=es2020 --format=cjs --sourcemap",
|
|
30
|
+
"build:cjs:prod": "esbuild src/index.ts --bundle --external:@unstoppablejs/utils --external:@noble/hashes --outfile=./dist/solidity-codecs.cjs.production.min.js --target=es2020 --format=cjs --minify --sourcemap",
|
|
31
31
|
"build:ts": "tsc -p ./tsconfig-build.json --outDir ./dist --skipLibCheck --emitDeclarationOnly && rm -rf ./dist/internal",
|
|
32
|
-
"deno": "node ./to-deno.mjs",
|
|
33
32
|
"test": "jest --coverage",
|
|
34
33
|
"lint": "prettier --check README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\"",
|
|
35
34
|
"format": "prettier --write README.md \"src/**/*.{js,jsx,ts,tsx,json,md}\"",
|
|
@@ -42,8 +41,8 @@
|
|
|
42
41
|
},
|
|
43
42
|
"name": "solidity-codecs",
|
|
44
43
|
"author": "Josep M Sobrepere (https://github.com/josepot)",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@unstoppablejs/utils": "^1.1.0",
|
|
46
|
+
"@noble/hashes": "^1.0.0"
|
|
48
47
|
}
|
|
49
48
|
}
|