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