@fleet-sdk/serializer 0.4.1 → 0.6.4

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/index.mjs CHANGED
@@ -1,62 +1,49 @@
1
- import { ensureBigInt, _0n, _128n, isEmpty, assert, first, last, isUndefined, isDefined, some, byteSizeOf, _1n, _7n, _127n, _63n } from '@fleet-sdk/common';
2
- import { hex } from '@fleet-sdk/crypto';
1
+ import { ensureBigInt, _0n, _128n, isEmpty, assert, first, last, isUndefined, isDefined, some, byteSizeOf, _7n, _1n, _63n, _127n } from '@fleet-sdk/common';
2
+ import { ensureBytes, hex, blake2b256 } from '@fleet-sdk/crypto';
3
3
 
4
- // src/coders/sigmaReader.ts
4
+ // src/coders/sigmaByteReader.ts
5
5
  function hexToBigInt(hex6) {
6
- if (hex6.length % 2) {
7
- hex6 = "0" + hex6;
8
- }
9
- const value = BigInt("0x" + hex6);
10
- const highByte = parseInt(hex6.slice(0, 2), 16);
11
- if (128 & highByte) {
12
- return -_bitNegate(value);
13
- }
6
+ const value = BigInt(hex6.length % 2 ? `0x0${hex6}` : `0x${hex6}`);
7
+ const highByte = Number.parseInt(hex6.slice(0, 2), 16);
8
+ if (128 & highByte) return -negateAndMask(value);
14
9
  return value;
15
10
  }
16
11
  function bigIntToHex(value) {
17
12
  const positive = value >= _0n;
18
- if (!positive) {
19
- value = _bitNegate(value);
20
- }
21
- let hex6 = value.toString(16);
22
- if (hex6.length % 2) {
23
- hex6 = "0" + hex6;
24
- }
25
- if (positive && 128 & parseInt(hex6.slice(0, 2), 16)) {
26
- hex6 = "00" + hex6;
13
+ let hex6 = (positive ? value : negateAndMask(value)).toString(16);
14
+ if (hex6.length % 2) hex6 = `0${hex6}`;
15
+ if (positive && 128 & Number.parseInt(hex6.slice(0, 2), 16)) {
16
+ return `00${hex6}`;
27
17
  }
28
18
  return hex6;
29
19
  }
30
- function _bitNegate(value) {
31
- const negative = value < _0n;
32
- if (negative) {
33
- value = -value;
34
- }
35
- const bits = value.toString(2);
36
- let bitLen = bits.length;
37
- const mod = bitLen % 8;
20
+ function negateAndMask(value) {
21
+ let val = value;
22
+ const negative = val < _0n;
23
+ if (negative) val = -val;
24
+ const bits = val.toString(2);
25
+ let len = bits.length;
26
+ const mod = len % 8;
38
27
  if (mod > 0) {
39
- bitLen += 8 - mod;
28
+ len += 8 - mod;
40
29
  } else if (negative && first(bits) === "1" && bits.indexOf("1", 1) !== -1) {
41
- bitLen += 8;
30
+ len += 8;
42
31
  }
43
- const mask = (1n << BigInt(bitLen)) - 1n;
44
- return (~value & mask) + 1n;
32
+ const mask = (1n << BigInt(len)) - 1n;
33
+ return (~val & mask) + 1n;
45
34
  }
46
35
  function writeVLQ(writer, value) {
47
- if (value === 0) {
48
- return writer.write(0);
49
- } else if (value < 0) {
36
+ if (value === 0) return writer.write(0);
37
+ if (value < 0) {
50
38
  throw new RangeError("Variable Length Quantity not supported for negative numbers.");
51
39
  }
40
+ let val = value;
52
41
  do {
53
- let lower7bits = value & 127;
54
- value >>= 7;
55
- if (value > 0) {
56
- lower7bits |= 128;
57
- }
42
+ let lower7bits = val & 127;
43
+ val >>= 7;
44
+ if (val > 0) lower7bits |= 128;
58
45
  writer.write(lower7bits);
59
- } while (value > 0);
46
+ } while (val > 0);
60
47
  return writer;
61
48
  }
62
49
  function readVLQ(reader) {
@@ -70,29 +57,25 @@ function readVLQ(reader) {
70
57
  lower7bits = reader.readByte();
71
58
  value |= (lower7bits & 127) << shift;
72
59
  shift += 7;
73
- } while ((lower7bits & 128) != 0);
60
+ } while ((lower7bits & 128) !== 0);
74
61
  return value;
75
62
  }
76
63
  function writeBigVLQ(writer, value) {
77
- if (value === _0n) {
78
- return writer.write(0);
79
- } else if (value < _0n) {
64
+ if (value === _0n) return writer.write(0);
65
+ if (value < _0n) {
80
66
  throw new RangeError("Variable Length Quantity not supported for negative numbers");
81
67
  }
68
+ let val = value;
82
69
  do {
83
- let lower7bits = Number(value & _127n);
84
- value >>= _7n;
85
- if (value > 0) {
86
- lower7bits |= 128;
87
- }
70
+ let lower7bits = Number(val & _127n);
71
+ val >>= _7n;
72
+ if (val > 0) lower7bits |= 128;
88
73
  writer.write(lower7bits);
89
- } while (value > 0);
74
+ } while (val > 0);
90
75
  return writer;
91
76
  }
92
77
  function readBigVLQ(reader) {
93
- if (reader.isEmpty) {
94
- return _0n;
95
- }
78
+ if (reader.isEmpty) return _0n;
96
79
  let value = _0n;
97
80
  let shift = _0n;
98
81
  let lower7bits = _0n;
@@ -100,23 +83,24 @@ function readBigVLQ(reader) {
100
83
  lower7bits = BigInt(reader.readByte());
101
84
  value |= (lower7bits & _127n) << shift;
102
85
  shift += _7n;
103
- } while ((lower7bits & _128n) != _0n);
86
+ } while ((lower7bits & _128n) !== _0n);
104
87
  return value;
105
88
  }
106
89
  function estimateVLQSize(value) {
107
90
  let size = 0;
108
- if (typeof value === "number") {
91
+ let val = value;
92
+ if (typeof val === "number") {
109
93
  do {
110
94
  size++;
111
- value = Math.floor(value / 128);
112
- } while (value > 0);
95
+ val = Math.floor(val / 128);
96
+ } while (val > 0);
113
97
  return size;
114
98
  }
115
- value = ensureBigInt(value);
99
+ val = ensureBigInt(val);
116
100
  do {
117
101
  size++;
118
- value /= _128n;
119
- } while (value > _0n);
102
+ val /= _128n;
103
+ } while (val > _0n);
120
104
  return size;
121
105
  }
122
106
  function zigZagEncode(input) {
@@ -132,19 +116,15 @@ function zigZagDecodeBigInt(input) {
132
116
  return input >> _1n ^ -(input & _1n);
133
117
  }
134
118
 
135
- // src/coders/sigmaReader.ts
136
- var SigmaReader = class {
119
+ // src/coders/sigmaByteReader.ts
120
+ var SigmaByteReader = class {
137
121
  #bytes;
138
122
  #cursor;
139
123
  get isEmpty() {
140
124
  return isEmpty(this.#bytes);
141
125
  }
142
126
  constructor(bytes) {
143
- if (typeof bytes === "string") {
144
- this.#bytes = hex.decode(bytes);
145
- } else {
146
- this.#bytes = bytes;
147
- }
127
+ this.#bytes = ensureBytes(bytes);
148
128
  this.#cursor = 0;
149
129
  }
150
130
  readBoolean() {
@@ -156,14 +136,12 @@ var SigmaReader = class {
156
136
  for (let i = 0; i < length; i++) {
157
137
  const bit = this.#bytes[this.#cursor] >> bitOffset++ & 1;
158
138
  bits[i] = bit === 1;
159
- if (bitOffset == 8) {
139
+ if (bitOffset === 8) {
160
140
  bitOffset = 0;
161
141
  this.#cursor++;
162
142
  }
163
143
  }
164
- if (bitOffset > 0) {
165
- this.#cursor++;
166
- }
144
+ if (bitOffset > 0) this.#cursor++;
167
145
  return bits;
168
146
  }
169
147
  readByte() {
@@ -179,8 +157,7 @@ var SigmaReader = class {
179
157
  return Number(zigZagDecode(readVLQ(this)));
180
158
  }
181
159
  readInt() {
182
- const int = this.readLong();
183
- return Number(int);
160
+ return Number(this.readLong());
184
161
  }
185
162
  readLong() {
186
163
  return zigZagDecodeBigInt(readBigVLQ(this));
@@ -190,14 +167,14 @@ var SigmaReader = class {
190
167
  return hexToBigInt(hex.encode(this.readBytes(len)));
191
168
  }
192
169
  };
193
- var SigmaWriter = class {
170
+ var SigmaByteWriter = class {
194
171
  #bytes;
195
172
  #cursor;
196
173
  get length() {
197
174
  return this.#cursor;
198
175
  }
199
- constructor(maxLength) {
200
- this.#bytes = new Uint8Array(maxLength);
176
+ constructor(length) {
177
+ this.#bytes = new Uint8Array(length);
201
178
  this.#cursor = 0;
202
179
  }
203
180
  writeBoolean(value) {
@@ -242,26 +219,27 @@ var SigmaWriter = class {
242
219
  } else {
243
220
  this.#bytes[this.#cursor] &= ~(1 << bitOffset++);
244
221
  }
245
- if (bitOffset == 8) {
222
+ if (bitOffset === 8) {
246
223
  bitOffset = 0;
247
224
  this.#cursor++;
248
225
  }
249
226
  }
250
- if (bitOffset > 0) {
251
- this.#cursor++;
252
- }
227
+ if (bitOffset > 0) this.#cursor++;
253
228
  return this;
254
229
  }
255
230
  writeBigInt(value) {
256
231
  const hex6 = bigIntToHex(value);
257
- this.writeVLQ(hex6.length / 2);
258
- this.writeHex(hex6);
259
- return this;
232
+ return this.writeVLQ(hex6.length / 2).writeHex(hex6);
260
233
  }
261
- toHex() {
262
- return hex.encode(this.toBytes());
234
+ writeChecksum(length = 4, hashFn = blake2b256) {
235
+ const hash = hashFn(this.toBytes());
236
+ return this.writeBytes(length ? hash.subarray(0, length) : hash);
237
+ }
238
+ encode(coder) {
239
+ return coder.encode(this.toBytes());
263
240
  }
264
241
  toBytes() {
242
+ if (this.#cursor === this.#bytes.length) return this.#bytes;
265
243
  return this.#bytes.subarray(0, this.#cursor);
266
244
  }
267
245
  };
@@ -393,7 +371,7 @@ var constructorCode = Object.freeze({
393
371
  });
394
372
  var MAX_PRIMITIVE_TYPE_CODE = 11;
395
373
  var PRIMITIVE_TYPE_RANGE = MAX_PRIMITIVE_TYPE_CODE + 1;
396
- var typeCodeOf = (constructor) => PRIMITIVE_TYPE_RANGE * constructor;
374
+ var typeCodeOf = (ctor) => PRIMITIVE_TYPE_RANGE * ctor;
397
375
  var collDescriptor = Object.freeze({
398
376
  code: typeCodeOf(constructorCode.simpleColl),
399
377
  embeddable: false,
@@ -486,8 +464,7 @@ function monoProxy(ctor, cache, forceConstruction) {
486
464
  return new Proxy(ctor, {
487
465
  apply: (target, _, args) => {
488
466
  const instance = cache ?? new target();
489
- if (!forceConstruction && isEmpty(args))
490
- return instance;
467
+ if (!forceConstruction && isEmpty(args)) return instance;
491
468
  return new SConstant(instance, ...args);
492
469
  }
493
470
  });
@@ -501,8 +478,14 @@ var SByte = monoProxy(SByteType, descriptors.byte);
501
478
  var SBool = monoProxy(SBoolType, descriptors.bool);
502
479
  var SShort = monoProxy(SShortType, descriptors.short);
503
480
  var SInt = monoProxy(SIntType, descriptors.int);
504
- var SLong = monoProxy(SLongType, descriptors.long);
505
- var SBigInt = monoProxy(SBigIntType, descriptors.bigInt);
481
+ var SLong = monoProxy(
482
+ SLongType,
483
+ descriptors.long
484
+ );
485
+ var SBigInt = monoProxy(
486
+ SBigIntType,
487
+ descriptors.bigInt
488
+ );
506
489
  var SGroupElement = monoProxy(
507
490
  SGroupElementType,
508
491
  descriptors.groupElement
@@ -515,15 +498,15 @@ var SUnit = monoProxy(SUnitType, void 0, true);
515
498
  var SColl = genericProxy(SCollType, (target, _, args) => {
516
499
  const [type, elements] = args;
517
500
  const elementsType = type();
518
- if (!elements)
519
- return () => new target(elementsType);
501
+ if (!elements) return () => new target(elementsType);
520
502
  return new SConstant(new target(elementsType), elements);
521
503
  });
522
504
  var SPair = genericProxy(STupleType, (target, _, args) => {
523
505
  const [left, right] = args;
524
506
  if (typeof left === "function" && typeof right === "function") {
525
507
  return () => new target([left(), right()]);
526
- } else if (left instanceof SConstant && right instanceof SConstant) {
508
+ }
509
+ if (left instanceof SConstant && right instanceof SConstant) {
527
510
  return new SConstant(new target([left.type, right.type]), [left.data, right.data]);
528
511
  }
529
512
  throw new Error("Invalid tuple declaration.");
@@ -532,8 +515,8 @@ var SPair = genericProxy(STupleType, (target, _, args) => {
532
515
  // src/serializers/dataSerializer.ts
533
516
  var GROUP_ELEMENT_LENGTH = 33;
534
517
  var PROVE_DLOG_OP = 205;
535
- var DataSerializer = class _DataSerializer {
536
- static serialize(data, type, writer) {
518
+ var dataSerializer = {
519
+ serialize(data, type, writer) {
537
520
  if (type.embeddable) {
538
521
  switch (type.code) {
539
522
  case descriptors.bool.code:
@@ -546,25 +529,26 @@ var DataSerializer = class _DataSerializer {
546
529
  return writer.writeInt(data);
547
530
  case descriptors.long.code:
548
531
  return writer.writeLong(data);
549
- case descriptors.bigInt.code: {
532
+ case descriptors.bigInt.code:
550
533
  return writer.writeBigInt(data);
551
- }
552
534
  case descriptors.groupElement.code:
553
535
  return writer.writeBytes(data);
554
536
  case descriptors.sigmaProp.code: {
555
537
  const node = data;
556
538
  if (node.type === descriptors.groupElement) {
557
539
  writer.write(PROVE_DLOG_OP);
558
- return _DataSerializer.serialize(node.data, node.type, writer);
559
- } else {
560
- throw Error("Serialization error: SigmaProp operation not implemented.");
540
+ return dataSerializer.serialize(node.data, node.type, writer);
561
541
  }
542
+ throw Error("Serialization error: SigmaProp operation not implemented.");
562
543
  }
563
544
  }
564
- } else if (isColl(type)) {
545
+ }
546
+ if (isColl(type)) {
565
547
  if (type.elementsType.code === descriptors.byte.code) {
566
- const isUint8Array = data instanceof Uint8Array;
567
- assert(isUint8Array, `SColl[Byte] expected an UInt8Array, got ${typeof data}.`);
548
+ assert(
549
+ data instanceof Uint8Array,
550
+ `SColl[Byte] expected an UInt8Array, got ${typeof data}.`
551
+ );
568
552
  } else {
569
553
  assert(Array.isArray(data), `SColl expected an array, got ${typeof data}.`);
570
554
  }
@@ -578,27 +562,29 @@ var DataSerializer = class _DataSerializer {
578
562
  }
579
563
  default: {
580
564
  for (let i = 0; i < data.length; i++) {
581
- _DataSerializer.serialize(data[i], type.elementsType, writer);
565
+ dataSerializer.serialize(data[i], type.elementsType, writer);
582
566
  }
583
567
  return writer;
584
568
  }
585
569
  }
586
- } else if (isTuple(type)) {
570
+ }
571
+ if (isTuple(type)) {
587
572
  assert(
588
573
  Array.isArray(data),
589
574
  `STupleType serialization expected an array, got ${typeof data}.`
590
575
  );
591
576
  const len = type.elementsType.length;
592
577
  for (let i = 0; i < len; i++) {
593
- _DataSerializer.serialize(data[i], type.elementsType[i], writer);
578
+ dataSerializer.serialize(data[i], type.elementsType[i], writer);
594
579
  }
595
580
  return writer;
596
- } else if (type.code === descriptors.unit.code) {
597
- return writer;
598
581
  }
599
- throw Error(`Serialization error: '0x${type.code.toString(16)}' type not implemented.`);
600
- }
601
- static deserialize(type, reader) {
582
+ if (type.code === descriptors.unit.code) return writer;
583
+ throw Error(
584
+ `Serialization error: '0x${type.code.toString(16)}' type not implemented.`
585
+ );
586
+ },
587
+ deserialize(type, reader) {
602
588
  if (type.embeddable) {
603
589
  switch (type.code) {
604
590
  case descriptors.bool.code:
@@ -642,18 +628,19 @@ var DataSerializer = class _DataSerializer {
642
628
  }
643
629
  }
644
630
  case descriptors.tuple.code: {
645
- return type.elementsType.map((t) => this.deserialize(t, reader));
631
+ return type.elementsType.map(
632
+ (t) => this.deserialize(t, reader)
633
+ );
646
634
  }
647
- case descriptors.unit.code: {
635
+ case descriptors.unit.code:
648
636
  return void 0;
649
- }
650
637
  }
651
638
  }
652
639
  throw new Error(`Parsing error: '0x${type.code.toString(16)}' type not implemented.`);
653
640
  }
654
641
  };
655
- var TypeSerializer = class {
656
- static serialize(type, writer) {
642
+ var typeSerializer = {
643
+ serialize(type, writer) {
657
644
  if (type.embeddable) {
658
645
  writer.write(type.code);
659
646
  } else if (type.code === descriptors.unit.code) {
@@ -664,7 +651,9 @@ var TypeSerializer = class {
664
651
  } else if (isColl(type.elementsType)) {
665
652
  const nestedColl = type.elementsType;
666
653
  if (nestedColl.elementsType.embeddable) {
667
- writer.write(descriptors.coll.nestedCollTypeCode + nestedColl.elementsType.code);
654
+ writer.write(
655
+ descriptors.coll.nestedCollTypeCode + nestedColl.elementsType.code
656
+ );
668
657
  } else {
669
658
  writer.write(descriptors.coll.simpleCollTypeCode);
670
659
  this.serialize(nestedColl, writer);
@@ -703,7 +692,10 @@ var TypeSerializer = class {
703
692
  break;
704
693
  default: {
705
694
  const len = type.elementsType.length;
706
- assert(len >= 2 && len <= 255, "Invalid type: tuples must have between 2 and 255 items.");
695
+ assert(
696
+ len >= 2 && len <= 255,
697
+ "Invalid type: tuples must have between 2 and 255 items."
698
+ );
707
699
  writer.write(descriptors.tuple.genericTupleTypeCode);
708
700
  writer.writeVLQ(len);
709
701
  }
@@ -714,8 +706,8 @@ var TypeSerializer = class {
714
706
  } else {
715
707
  throw new Error("Serialization error: type not implemented.");
716
708
  }
717
- }
718
- static deserialize(r) {
709
+ },
710
+ deserialize(r) {
719
711
  const byte = r.readByte();
720
712
  assert(byte > 0, `Parsing Error: Unexpected type code '0x${byte.toString(16)}'`);
721
713
  if (byte < descriptors.tuple.genericTupleTypeCode) {
@@ -741,23 +733,27 @@ var TypeSerializer = class {
741
733
  return new STupleType(internal);
742
734
  }
743
735
  case constructorCode.symmetricPair: {
744
- const internal = embdCode === 0 ? [this.deserialize(r), this.deserialize(r), this.deserialize(r), this.deserialize(r)] : [getPrimitiveType(embdCode), getPrimitiveType(embdCode)];
736
+ const internal = embdCode === 0 ? [
737
+ this.deserialize(r),
738
+ this.deserialize(r),
739
+ this.deserialize(r),
740
+ this.deserialize(r)
741
+ ] : [getPrimitiveType(embdCode), getPrimitiveType(embdCode)];
745
742
  return new STupleType(internal);
746
743
  }
747
744
  }
748
- } else {
749
- switch (byte) {
750
- case descriptors.tuple.genericTupleTypeCode: {
751
- const len = r.readVlq();
752
- const wrapped = new Array(len);
753
- for (let i = 0; i < len; i++) {
754
- wrapped[i] = this.deserialize(r);
755
- }
756
- return new STupleType(wrapped);
757
- }
758
- case descriptors.unit.code: {
759
- return descriptors.unit;
745
+ }
746
+ switch (byte) {
747
+ case descriptors.tuple.genericTupleTypeCode: {
748
+ const len = r.readVlq();
749
+ const wrapped = new Array(len);
750
+ for (let i = 0; i < len; i++) {
751
+ wrapped[i] = this.deserialize(r);
760
752
  }
753
+ return new STupleType(wrapped);
754
+ }
755
+ case descriptors.unit.code: {
756
+ return descriptors.unit;
761
757
  }
762
758
  }
763
759
  throw new Error("Not implemented.");
@@ -775,9 +771,9 @@ var SConstant = class _SConstant {
775
771
  }
776
772
  static from(bytes) {
777
773
  assert(bytes.length > 0, "Empty constant bytes.");
778
- const reader = new SigmaReader(bytes);
779
- const type = TypeSerializer.deserialize(reader);
780
- const data = DataSerializer.deserialize(type, reader);
774
+ const reader = new SigmaByteReader(bytes);
775
+ const type = typeSerializer.deserialize(reader);
776
+ const data = dataSerializer.deserialize(type, reader);
781
777
  return new _SConstant(type, data);
782
778
  }
783
779
  get type() {
@@ -787,9 +783,9 @@ var SConstant = class _SConstant {
787
783
  return this.#data;
788
784
  }
789
785
  toBytes() {
790
- const writer = new SigmaWriter(MAX_CONSTANT_LENGTH);
791
- TypeSerializer.serialize(this.type, writer);
792
- DataSerializer.serialize(this.data, this.type, writer);
786
+ const writer = new SigmaByteWriter(MAX_CONSTANT_LENGTH);
787
+ typeSerializer.serialize(this.type, writer);
788
+ dataSerializer.serialize(this.data, this.type, writer);
793
789
  return writer.toBytes();
794
790
  }
795
791
  toHex() {
@@ -797,18 +793,14 @@ var SConstant = class _SConstant {
797
793
  }
798
794
  };
799
795
  function decode(value, coder) {
800
- if (isUndefined(value))
801
- return;
796
+ if (isUndefined(value)) return;
802
797
  const data = parse(value, "safe");
803
- if (isUndefined(data))
804
- return;
798
+ if (isUndefined(data)) return;
805
799
  return coder ? coder(data) : data;
806
800
  }
807
801
  function parse(constant, mode = "strict") {
808
- if (mode === "strict")
809
- return SConstant.from(constant ?? "").data;
810
- if (!constant)
811
- return;
802
+ if (mode === "strict") return SConstant.from(constant ?? "").data;
803
+ if (!constant) return;
812
804
  try {
813
805
  return SConstant.from(constant).data;
814
806
  } catch {
@@ -816,23 +808,15 @@ function parse(constant, mode = "strict") {
816
808
  }
817
809
  }
818
810
  var MAX_UINT16_VALUE = 65535;
819
- function serializeBox(box, writer, distinctTokenIds) {
820
- if (!writer) {
821
- writer = new SigmaWriter(5e4);
822
- }
811
+ function serializeBox(box, writer = new SigmaByteWriter(5e4), distinctTokenIds) {
823
812
  writer.writeBigVLQ(ensureBigInt(box.value));
824
813
  writer.writeHex(box.ergoTree);
825
814
  writer.writeVLQ(box.creationHeight);
826
815
  writeTokens(writer, box.assets, distinctTokenIds);
827
816
  writeRegisters(writer, box.additionalRegisters);
828
- if (isDefined(distinctTokenIds)) {
829
- return writer;
830
- } else {
831
- if (!isBox(box)) {
832
- throw new Error("Invalid box type.");
833
- }
834
- return writer.writeHex(box.transactionId).writeVLQ(box.index);
835
- }
817
+ if (isDefined(distinctTokenIds)) return writer;
818
+ if (!isBox(box)) throw new Error("Invalid box type.");
819
+ return writer.writeHex(box.transactionId).writeVLQ(box.index);
836
820
  }
837
821
  function isBox(box) {
838
822
  const castedBox = box;
@@ -849,26 +833,22 @@ function writeTokens(writer, tokens, tokenIds) {
849
833
  (token) => writer.writeVLQ(tokenIds.indexOf(token.tokenId)).writeBigVLQ(ensureBigInt(token.amount))
850
834
  );
851
835
  } else {
852
- tokens.map((token) => writer.writeHex(token.tokenId).writeBigVLQ(ensureBigInt(token.amount)));
836
+ tokens.map(
837
+ (token) => writer.writeHex(token.tokenId).writeBigVLQ(ensureBigInt(token.amount))
838
+ );
853
839
  }
854
840
  }
855
841
  function writeRegisters(writer, registers) {
856
842
  const keys = Object.keys(registers).sort();
857
843
  let length = 0;
858
844
  for (const key of keys) {
859
- if (registers[key]) {
860
- length++;
861
- }
845
+ if (registers[key]) length++;
862
846
  }
863
847
  writer.writeVLQ(length);
864
- if (length == 0) {
865
- return;
866
- }
848
+ if (length === 0) return;
867
849
  for (const key of keys) {
868
850
  const register = registers[key];
869
- if (isDefined(register)) {
870
- writer.writeHex(register);
871
- }
851
+ if (isDefined(register)) writer.writeHex(register);
872
852
  }
873
853
  }
874
854
  function estimateBoxSize(box, withValue) {
@@ -880,10 +860,9 @@ function estimateBoxSize(box, withValue) {
880
860
  size += byteSizeOf(box.ergoTree);
881
861
  size += estimateVLQSize(box.creationHeight);
882
862
  size += estimateVLQSize(box.assets.length);
883
- size += box.assets.reduce(
884
- (acc, curr) => acc += byteSizeOf(curr.tokenId) + estimateVLQSize(curr.amount),
885
- 0
886
- );
863
+ for (const asset of box.assets) {
864
+ size += byteSizeOf(asset.tokenId) + estimateVLQSize(asset.amount);
865
+ }
887
866
  let registersLength = 0;
888
867
  for (const key in box.additionalRegisters) {
889
868
  const register = box.additionalRegisters[key];
@@ -898,7 +877,7 @@ function estimateBoxSize(box, withValue) {
898
877
  return size;
899
878
  }
900
879
  function serializeTransaction(transaction) {
901
- const writer = new SigmaWriter(1e5);
880
+ const writer = new SigmaByteWriter(1e5);
902
881
  writer.writeVLQ(transaction.inputs.length);
903
882
  transaction.inputs.map((input) => writeInput(writer, input));
904
883
  writer.writeVLQ(transaction.dataInputs.length);
@@ -925,9 +904,7 @@ function writeExtension(writer, extension) {
925
904
  }
926
905
  }
927
906
  writer.writeVLQ(length);
928
- if (length == 0) {
929
- return;
930
- }
907
+ if (length === 0) return;
931
908
  for (const key of keys) {
932
909
  const ext = extension[key];
933
910
  if (isDefined(ext)) {
@@ -941,6 +918,6 @@ function getDistinctTokenIds(outputs) {
941
918
  return Array.from(tokenIds);
942
919
  }
943
920
 
944
- export { DataSerializer, SBigInt, SBigIntType, SBool, SBoolType, SByte, SByteType, SColl, SCollType, SConstant, SGenericType, SGroupElement, SGroupElementType, SInt, SIntType, SLong, SLongType, SMonomorphicType, SPair, SPrimitiveType, SShort, SShortType, SSigmaProp, SSigmaPropType, STupleType, SType, SUnit, SUnitType, TypeSerializer, decode, estimateBoxSize, estimateVLQSize, isColl, isTuple, parse, serializeBox, serializeTransaction };
945
- //# sourceMappingURL=out.js.map
921
+ export { SBigInt, SBigIntType, SBool, SBoolType, SByte, SByteType, SColl, SCollType, SConstant, SGenericType, SGroupElement, SGroupElementType, SInt, SIntType, SLong, SLongType, SMonomorphicType, SPair, SPrimitiveType, SShort, SShortType, SSigmaProp, SSigmaPropType, STupleType, SType, SUnit, SUnitType, SigmaByteReader, SigmaByteWriter, dataSerializer, decode, estimateBoxSize, estimateVLQSize, isColl, isTuple, parse, serializeBox, serializeTransaction, typeSerializer };
922
+ //# sourceMappingURL=index.mjs.map
946
923
  //# sourceMappingURL=index.mjs.map