solidity-codecs 0.0.1-beta.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,46 +21,12 @@ var enhanceEncoder = (encoder, mapper) => dyn(encoder, (value) => encoder(mapper
21
21
  var enhanceDecoder = (decoder, mapper) => dyn(decoder, (value) => mapper(decoder(value)));
22
22
  var enhanceCodec = (codec, toFrom, fromTo) => dyn(codec, createCodec(enhanceEncoder(codec[0], toFrom), enhanceDecoder(codec[1], fromTo)));
23
23
 
24
+ // src/codecs/address.ts
25
+ import { fromHex as fromHex2, toHex } from "@unstoppablejs/utils";
26
+ import { keccak_256 } from "@noble/hashes/sha3";
27
+
24
28
  // src/internal/toInternalBytes.ts
25
- var HEX_MAP = {
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 bytes = new Uint8Array(nBytes);
54
- if (isOdd)
55
- bytes[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
- bytes[isOdd + i++] = a << 4 | b;
61
- }
62
- return bytes;
63
- }
29
+ import { fromHex } from "@unstoppablejs/utils";
64
30
  var InternalUint8Array = class extends Uint8Array {
65
31
  constructor(buffer) {
66
32
  super(buffer);
@@ -71,20 +37,27 @@ var InternalUint8Array = class extends Uint8Array {
71
37
  };
72
38
  var toInternalBytes = (fn) => (buffer) => fn(buffer instanceof InternalUint8Array ? buffer : new InternalUint8Array(buffer instanceof Uint8Array ? buffer.buffer : typeof buffer === "string" ? fromHex(buffer).buffer : buffer));
73
39
 
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
- }
40
+ // src/internal/range32.ts
41
+ var range32 = Array(32).fill(0).map((_, idx) => idx + 1);
42
+
43
+ // src/codecs/address.ts
44
+ var address = createCodec((input) => {
45
+ const result = new Uint8Array(32);
46
+ result.set(fromHex2(input), 12);
86
47
  return result;
87
- };
48
+ }, toInternalBytes((bytes33) => {
49
+ const binaryAddress = new Uint8Array(bytes33.buffer, bytes33.i + 12, 20);
50
+ bytes33.i += 32;
51
+ const nonChecksum = toHex(binaryAddress);
52
+ const hashedAddres = toHex(keccak_256(nonChecksum.slice(2)));
53
+ const result = new Array(41);
54
+ result[0] = "0x";
55
+ for (let i = 2; i < 42; i++) {
56
+ const char = nonChecksum[i];
57
+ result.push(parseInt(hashedAddres[i], 16) > 7 ? char.toUpperCase() : char);
58
+ }
59
+ return result.join("");
60
+ }));
88
61
 
89
62
  // src/codecs/Uint.ts
90
63
  var getCodec = (nBytes) => {
@@ -98,37 +71,58 @@ var getCodec = (nBytes) => {
98
71
  input >>= 64n;
99
72
  }
100
73
  return result;
101
- }, toInternalBytes((bytes) => {
74
+ }, toInternalBytes((bytes33) => {
102
75
  let result = 0n;
103
- const nextBlock = bytes.i + 32;
104
- for (let idx = bytes.i + (32 - n64 * 8); idx < nextBlock; idx += 8)
105
- result = result << 64n | bytes.v.getBigUint64(idx);
106
- bytes.i = nextBlock;
76
+ const nextBlock = bytes33.i + 32;
77
+ for (let idx = bytes33.i + (32 - n64 * 8); idx < nextBlock; idx += 8)
78
+ result = result << 64n | bytes33.v.getBigUint64(idx);
79
+ bytes33.i = nextBlock;
107
80
  return result;
108
81
  }));
109
82
  };
110
- var cache = /* @__PURE__ */ new Map();
111
- var Uint = (nBits) => {
112
- let cached = cache.get(nBits);
113
- if (cached)
114
- return cached;
115
- const nBytes = nBits / 8;
116
- cached = getCodec(nBytes);
117
- cache.set(nBits, cached);
118
- return cached;
119
- };
120
- Uint.enc = (nBits) => Uint(nBits).enc;
121
- Uint.dec = (nBits) => Uint(nBits).dec;
83
+ var [
84
+ uint8,
85
+ uint16,
86
+ uint24,
87
+ uint32,
88
+ uint40,
89
+ uint48,
90
+ uint56,
91
+ uint64,
92
+ uint72,
93
+ uint80,
94
+ uint88,
95
+ uint96,
96
+ uint104,
97
+ uint112,
98
+ uint120,
99
+ uint128,
100
+ uint136,
101
+ uint144,
102
+ uint152,
103
+ uint160,
104
+ uint168,
105
+ uint176,
106
+ uint184,
107
+ uint192,
108
+ uint200,
109
+ uint208,
110
+ uint226,
111
+ uint224,
112
+ uint232,
113
+ uint240,
114
+ uint248,
115
+ uint256
116
+ ] = range32.map(getCodec);
117
+ var uint = uint256;
122
118
 
123
119
  // src/codecs/bool.ts
124
- var bool = enhanceCodec(Uint(8), (value) => value ? 1n : 0n, Boolean);
120
+ var bool = enhanceCodec(uint8, (value) => value ? 1n : 0n, Boolean);
125
121
 
126
- // src/codecs/str.ts
127
- var uint256 = Uint(256);
128
- var textEncoder = new TextEncoder();
129
- var strEnc = (str2) => {
130
- const val = textEncoder.encode(str2);
131
- const args = [uint256.enc(BigInt(val.length)), val];
122
+ // src/codecs/bytes.ts
123
+ import { mergeUint8 } from "@unstoppablejs/utils";
124
+ var bytesEnc = (val) => {
125
+ const args = [uint[0](BigInt(val.length)), val];
132
126
  const extra = val.length % 32;
133
127
  if (extra > 0) {
134
128
  ;
@@ -136,36 +130,69 @@ var strEnc = (str2) => {
136
130
  }
137
131
  return mergeUint8(...args);
138
132
  };
139
- strEnc.dyn = true;
140
- var textDecoder = new TextDecoder();
141
- var strDec = toInternalBytes((bytes) => {
142
- let nElements = Number(uint256.dec(bytes));
143
- const dv = new DataView(bytes.buffer, bytes.i, nElements);
144
- const extra = nElements % 32;
145
- const padding = extra && 32 - extra;
146
- bytes.i += nElements + padding;
147
- return textDecoder.decode(dv);
133
+ bytesEnc.dyn = true;
134
+ var bytesDec = toInternalBytes((bytes33) => {
135
+ let nElements = Number(uint[1](bytes33));
136
+ const result = new Uint8Array(bytes33.buffer, bytes33.i, nElements);
137
+ bytes33.i += nElements + nElements % 32;
138
+ return result;
148
139
  });
149
- strDec.dyn = true;
150
- var str = createCodec(strEnc, strDec);
151
- str.dyn = true;
140
+ bytesDec.dyn = true;
141
+ var bytes = createCodec(bytesEnc, bytesDec);
142
+ bytes.dyn = true;
143
+
144
+ // src/codecs/str.ts
145
+ var textEncoder = new TextEncoder();
146
+ var textDecoder = new TextDecoder();
147
+ var str = enhanceCodec(bytes, textEncoder.encode.bind(textEncoder), textDecoder.decode.bind(textDecoder));
152
148
 
153
- // src/codecs/Bytes.ts
154
- var bytesEnc = (nBytes) => (bytes) => {
155
- if (bytes.length === 32)
156
- return bytes;
149
+ // src/codecs/BytesX.ts
150
+ var bytesEnc2 = (nBytes) => (bytes33) => {
151
+ if (bytes33.length === nBytes && nBytes === 32)
152
+ return bytes33;
157
153
  const result = new Uint8Array(32);
158
- result.set(bytes.length === nBytes ? bytes : bytes.slice(0, nBytes));
154
+ result.set(bytes33.length === nBytes ? bytes33 : bytes33.slice(0, nBytes));
159
155
  return result;
160
156
  };
161
- var bytesDec = (nBytes) => toInternalBytes((bytes) => {
162
- const result = new Uint8Array(bytes.buffer, bytes.i, nBytes);
163
- bytes.i += 32;
157
+ var bytesDec2 = (nBytes) => toInternalBytes((bytes33) => {
158
+ const result = new Uint8Array(bytes33.buffer, bytes33.i, nBytes);
159
+ bytes33.i += 32;
164
160
  return result;
165
161
  });
166
- var Bytes = (nBytes) => createCodec(bytesEnc(nBytes), bytesDec(nBytes));
167
- Bytes.enc = bytesEnc;
168
- Bytes.dec = bytesDec;
162
+ var [
163
+ bytes1,
164
+ bytes2,
165
+ bytes3,
166
+ bytes4,
167
+ bytes5,
168
+ bytes6,
169
+ bytes7,
170
+ bytes8,
171
+ bytes9,
172
+ bytes10,
173
+ bytes11,
174
+ bytes12,
175
+ bytes13,
176
+ bytes14,
177
+ bytes15,
178
+ bytes16,
179
+ bytes17,
180
+ bytes18,
181
+ bytes19,
182
+ bytes20,
183
+ bytes21,
184
+ bytes22,
185
+ bytes23,
186
+ bytes24,
187
+ bytes25,
188
+ bytes26,
189
+ bytes27,
190
+ bytes28,
191
+ bytes29,
192
+ bytes30,
193
+ bytes31,
194
+ bytes32
195
+ ] = range32.map((nBytes) => createCodec(bytesEnc2(nBytes), bytesDec2(nBytes)));
169
196
 
170
197
  // src/codecs/Int.ts
171
198
  var signGetters = {
@@ -206,62 +233,72 @@ var getCodec2 = (nBytes) => {
206
233
  }
207
234
  let idx = 32;
208
235
  for (let i = sequence.length - 1; i > 0; i--) {
209
- const [bytes2, shift, fn2] = sequence[i];
210
- idx -= bytes2;
211
- dv[usignSetters[bytes2]](idx, fn2(input));
236
+ const [bytes34, shift, fn2] = sequence[i];
237
+ idx -= bytes34;
238
+ dv[usignSetters[bytes34]](idx, fn2(input));
212
239
  input >>= shift;
213
240
  }
214
- const [bytes, , fn] = sequence[0];
215
- idx -= bytes;
216
- dv[signSetters[bytes]](idx, fn(input));
241
+ const [bytes33, , fn] = sequence[0];
242
+ idx -= bytes33;
243
+ dv[signSetters[bytes33]](idx, fn(input));
217
244
  return result;
218
245
  };
219
- const dec = toInternalBytes((bytes) => {
220
- let idx = bytes.i + 32 - nBytes;
246
+ const dec = toInternalBytes((bytes33) => {
247
+ let idx = bytes33.i + 32 - nBytes;
221
248
  const bits = sequence[0][0];
222
- let result = BigInt(bytes.v[signGetters[bits]](idx));
249
+ let result = BigInt(bytes33.v[signGetters[bits]](idx));
223
250
  idx += bits;
224
251
  for (let i = 1; i < sequence.length; i++) {
225
252
  const [bits2, shift] = sequence[i];
226
- result = result << shift | BigInt(bytes.v[usignGetters[bits2]](idx));
253
+ result = result << shift | BigInt(bytes33.v[usignGetters[bits2]](idx));
227
254
  idx += bits2;
228
255
  }
229
- bytes.i += 32;
256
+ bytes33.i += 32;
230
257
  return result;
231
258
  });
232
259
  return createCodec(enc, dec);
233
260
  };
234
- var cache2 = /* @__PURE__ */ new Map();
235
- var Int = (nBits) => {
236
- let cached = cache2.get(nBits);
237
- if (cached)
238
- return cached;
239
- const nBytes = nBits / 8;
240
- cached = getCodec2(nBytes);
241
- cache2.set(nBits, cached);
242
- return cached;
243
- };
244
- Int.enc = (nBits) => Int(nBits).enc;
245
- Int.dec = (nBits) => Int(nBits).dec;
261
+ var [
262
+ int8,
263
+ int16,
264
+ int24,
265
+ int32,
266
+ int40,
267
+ int48,
268
+ int56,
269
+ int64,
270
+ int72,
271
+ int80,
272
+ int88,
273
+ int96,
274
+ int104,
275
+ int112,
276
+ int120,
277
+ int128,
278
+ int136,
279
+ int144,
280
+ int152,
281
+ int160,
282
+ int168,
283
+ int176,
284
+ int184,
285
+ int192,
286
+ int200,
287
+ int208,
288
+ int226,
289
+ int224,
290
+ int232,
291
+ int240,
292
+ int248,
293
+ int256
294
+ ] = range32.map(getCodec2);
295
+ var int = int256;
246
296
 
247
297
  // src/codecs/Fixed.ts
248
- var creator = (codec) => {
249
- const cache3 = /* @__PURE__ */ new Map();
250
- return (nBits, decimals) => {
251
- const key = decimals << 8 | nBits;
252
- let cached = cache3.get(key);
253
- if (cached)
254
- return cached;
255
- cached = enhanceCodec(codec(nBits), (x) => x.value, (value) => ({ value, decimals }));
256
- cache3.set(key, cached);
257
- return cached;
258
- };
259
- };
260
- var Fixed = creator(Int);
261
- var Ufixed = creator(Uint);
298
+ var Fixed = (baseCodec, decimals) => enhanceCodec(baseCodec, (x) => x.value, (value) => ({ value, decimals }));
262
299
 
263
300
  // src/codecs/Tuple.ts
264
- var uint2562 = Uint(256);
301
+ import { mergeUint8 as mergeUint82 } from "@unstoppablejs/utils";
265
302
  var dynamicEnc = (...encoders) => {
266
303
  const res = (values) => {
267
304
  const mapped = values.map((value, idx) => encoders[idx](value));
@@ -278,31 +315,31 @@ var dynamicEnc = (...encoders) => {
278
315
  }
279
316
  }
280
317
  dinamics.forEach((idx) => {
281
- resultArray[idx] = uint2562.enc(len);
318
+ resultArray[idx] = uint[0](len);
282
319
  const data = mapped[idx];
283
320
  resultArray.push(data);
284
321
  len += BigInt(data.length);
285
322
  });
286
- return mergeUint8(...resultArray);
323
+ return mergeUint82(...resultArray);
287
324
  };
288
325
  res.dyn = true;
289
326
  return res;
290
327
  };
291
- var staticEnc = (...encoders) => (values) => mergeUint8(...values.map((value, idx) => encoders[idx](value)));
292
- var staticDec = (...decoders) => toInternalBytes((bytes) => decoders.map((decoder) => decoder(bytes)));
328
+ var staticEnc = (...encoders) => (values) => mergeUint82(...values.map((value, idx) => encoders[idx](value)));
329
+ var staticDec = (...decoders) => toInternalBytes((bytes33) => decoders.map((decoder) => decoder(bytes33)));
293
330
  var dynamicDec = (...decoders) => {
294
- const res = toInternalBytes((bytes) => {
331
+ const res = toInternalBytes((bytes33) => {
295
332
  const result = new Array(decoders.length);
296
- let start = bytes.i;
333
+ let start = bytes33.i;
297
334
  for (let i = 0; i < decoders.length; i++) {
298
335
  if (decoders[i].dyn) {
299
- const offset = Number(uint2562.dec(bytes));
300
- const current = bytes.i;
301
- bytes.i = start + offset;
302
- result[i] = decoders[i](bytes);
303
- bytes.i = current;
336
+ const offset = Number(uint[1](bytes33));
337
+ const current = bytes33.i;
338
+ bytes33.i = start + offset;
339
+ result[i] = decoders[i](bytes33);
340
+ bytes33.i = current;
304
341
  } else {
305
- result[i] = decoders[i](bytes);
342
+ result[i] = decoders[i](bytes33);
306
343
  }
307
344
  }
308
345
  return result;
@@ -325,53 +362,146 @@ var Struct = (codecs) => {
325
362
  };
326
363
 
327
364
  // src/codecs/Vector.ts
328
- var uint2563 = Uint(256);
365
+ import { mergeUint8 as mergeUint83 } from "@unstoppablejs/utils";
329
366
  var vectorEnc = (inner, size) => {
330
367
  if (size >= 0) {
331
- const res = (value) => mergeUint8(...value.map(inner));
332
- res.dyn = inner.dyn;
333
- return res;
368
+ const encoder2 = (value) => mergeUint83(...value.map(inner));
369
+ encoder2.dyn = inner.dyn;
370
+ return encoder2;
334
371
  }
335
- const result = (value) => mergeUint8(uint2563.enc(BigInt(value.length)), ...value.map(inner));
336
- result.dyn = true;
337
- return result;
372
+ const encoder = (value) => mergeUint83(uint[0](BigInt(value.length)), ...value.map(inner));
373
+ encoder.dyn = true;
374
+ return encoder;
338
375
  };
339
376
  var vectorDec = (getter, size) => {
340
- const result = toInternalBytes((bytes) => {
341
- const nElements = size >= 0 ? size : Number(uint2563.dec(bytes));
342
- const result2 = new Array(nElements);
377
+ const decoder = toInternalBytes((bytes33) => {
378
+ const nElements = size >= 0 ? size : Number(uint[1](bytes33));
379
+ const decoded = new Array(nElements);
343
380
  for (let i = 0; i < nElements; i++) {
344
- result2[i] = getter(bytes);
381
+ decoded[i] = getter(bytes33);
345
382
  }
346
- return result2;
383
+ return decoded;
347
384
  });
348
385
  if (size == null)
349
- result.dyn = true;
350
- return result;
386
+ decoder.dyn = true;
387
+ return decoder;
351
388
  };
352
389
  var Vector = (inner, size) => {
353
- const result = createCodec(vectorEnc(inner[0], size), vectorDec(inner[1], size));
390
+ const codec = createCodec(vectorEnc(inner[0], size), vectorDec(inner[1], size));
354
391
  if (size == null)
355
- result.dyn = true;
356
- return result;
392
+ codec.dyn = true;
393
+ return codec;
357
394
  };
358
- Vector.enc = vectorEnc;
359
- Vector.dec = vectorDec;
360
395
  export {
361
- Bytes,
362
396
  Fixed,
363
- Int,
364
397
  Struct,
365
398
  Tuple,
366
- Ufixed,
367
- Uint,
368
399
  Vector,
400
+ address,
369
401
  bool,
402
+ bytes,
403
+ bytes1,
404
+ bytes10,
405
+ bytes11,
406
+ bytes12,
407
+ bytes13,
408
+ bytes14,
409
+ bytes15,
410
+ bytes16,
411
+ bytes17,
412
+ bytes18,
413
+ bytes19,
414
+ bytes2,
415
+ bytes20,
416
+ bytes21,
417
+ bytes22,
418
+ bytes23,
419
+ bytes24,
420
+ bytes25,
421
+ bytes26,
422
+ bytes27,
423
+ bytes28,
424
+ bytes29,
425
+ bytes3,
426
+ bytes30,
427
+ bytes31,
428
+ bytes32,
429
+ bytes4,
430
+ bytes5,
431
+ bytes6,
432
+ bytes7,
433
+ bytes8,
434
+ bytes9,
370
435
  createCodec,
371
- dyn,
372
436
  enhanceCodec,
373
437
  enhanceDecoder,
374
438
  enhanceEncoder,
375
- str
439
+ int,
440
+ int104,
441
+ int112,
442
+ int120,
443
+ int128,
444
+ int136,
445
+ int144,
446
+ int152,
447
+ int16,
448
+ int160,
449
+ int168,
450
+ int176,
451
+ int184,
452
+ int192,
453
+ int200,
454
+ int208,
455
+ int224,
456
+ int226,
457
+ int232,
458
+ int24,
459
+ int240,
460
+ int248,
461
+ int256,
462
+ int32,
463
+ int40,
464
+ int48,
465
+ int56,
466
+ int64,
467
+ int72,
468
+ int8,
469
+ int80,
470
+ int88,
471
+ int96,
472
+ str,
473
+ uint,
474
+ uint104,
475
+ uint112,
476
+ uint120,
477
+ uint128,
478
+ uint136,
479
+ uint144,
480
+ uint152,
481
+ uint16,
482
+ uint160,
483
+ uint168,
484
+ uint176,
485
+ uint184,
486
+ uint192,
487
+ uint200,
488
+ uint208,
489
+ uint224,
490
+ uint226,
491
+ uint232,
492
+ uint24,
493
+ uint240,
494
+ uint248,
495
+ uint256,
496
+ uint32,
497
+ uint40,
498
+ uint48,
499
+ uint56,
500
+ uint64,
501
+ uint72,
502
+ uint8,
503
+ uint80,
504
+ uint88,
505
+ uint96
376
506
  };
377
507
  //# sourceMappingURL=solidity-codecs.js.map