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