@fleet-sdk/serializer 0.8.0 → 0.8.3
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 +19 -0
- package/dist/index.d.mts +18 -17
- package/dist/index.d.ts +18 -17
- package/dist/index.js +119 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -62
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { isEmpty, assert, first, last, _1n, _0n, _128n, ensureBigInt, _7n, _127n, _63n, isDefined, some, isUndefined, byteSizeOf } from '@fleet-sdk/common';
|
2
|
+
import { hex, ensureBytes, blake2b256 } from '@fleet-sdk/crypto';
|
3
3
|
|
4
4
|
// src/coders/sigmaByteReader.ts
|
5
|
-
function hexToBigInt(
|
6
|
-
const value = BigInt(
|
7
|
-
const highByte = Number.parseInt(
|
5
|
+
function hexToBigInt(hex7) {
|
6
|
+
const value = BigInt(hex7.length % 2 ? `0x0${hex7}` : `0x${hex7}`);
|
7
|
+
const highByte = Number.parseInt(hex7.slice(0, 2), 16);
|
8
8
|
if (128 & highByte) return -negateAndMask(value);
|
9
9
|
return value;
|
10
10
|
}
|
11
11
|
function bigIntToHex(value) {
|
12
12
|
const positive = value >= _0n;
|
13
|
-
let
|
14
|
-
if (
|
15
|
-
if (positive && 128 & Number.parseInt(
|
16
|
-
return `00${
|
13
|
+
let hex7 = (positive ? value : negateAndMask(value)).toString(16);
|
14
|
+
if (hex7.length % 2) hex7 = `0${hex7}`;
|
15
|
+
if (positive && 128 & Number.parseInt(hex7.slice(0, 2), 16)) {
|
16
|
+
return `00${hex7}`;
|
17
17
|
}
|
18
|
-
return
|
18
|
+
return hex7;
|
19
19
|
}
|
20
20
|
function negateAndMask(value) {
|
21
21
|
let val = value;
|
@@ -103,18 +103,45 @@ function estimateVLQSize(value) {
|
|
103
103
|
} while (val > _0n);
|
104
104
|
return size;
|
105
105
|
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
106
|
+
var _31n = BigInt(31);
|
107
|
+
var u64 = (v) => BigInt.asUintN(64, v);
|
108
|
+
var i64 = (v) => BigInt.asIntN(64, v);
|
109
|
+
var i32 = (v) => BigInt.asIntN(32, v);
|
110
|
+
var u32 = (v) => BigInt.asUintN(32, v);
|
111
|
+
var zigZag32 = {
|
112
|
+
encode: (input) => {
|
113
|
+
const v = i32(BigInt(input));
|
114
|
+
return u64(i32(v << _1n) ^ i32(v >> _31n));
|
115
|
+
},
|
116
|
+
decode: (input) => {
|
117
|
+
const v = u32(input);
|
118
|
+
return Number(v >> _1n ^ -(v & _1n));
|
119
|
+
}
|
120
|
+
};
|
121
|
+
var zigZag64 = {
|
122
|
+
encode: (input) => {
|
123
|
+
return u64(input << _1n ^ input >> _63n);
|
124
|
+
},
|
125
|
+
decode: (input) => {
|
126
|
+
return i64(input >> _1n ^ -(input & _1n));
|
127
|
+
}
|
128
|
+
};
|
129
|
+
|
130
|
+
// src/coders/numRanges.ts
|
131
|
+
var MAX_U8 = 255;
|
132
|
+
var MAX_I8 = 127;
|
133
|
+
var MIN_I16 = -32768;
|
134
|
+
var MAX_I16 = 32767;
|
135
|
+
var MIN_I32 = -2147483648;
|
136
|
+
var MAX_I32 = 2147483647;
|
137
|
+
var MIN_I64 = -BigInt("0x8000000000000000");
|
138
|
+
var MAX_I64 = BigInt("0x7fffffffffffffff");
|
139
|
+
var MIN_I256 = -BigInt(
|
140
|
+
"0x8000000000000000000000000000000000000000000000000000000000000000"
|
141
|
+
);
|
142
|
+
var MAX_I256 = BigInt(
|
143
|
+
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
144
|
+
);
|
118
145
|
|
119
146
|
// src/coders/sigmaByteReader.ts
|
120
147
|
var SigmaByteReader = class {
|
@@ -127,7 +154,7 @@ var SigmaByteReader = class {
|
|
127
154
|
this.#bytes = ensureBytes(bytes);
|
128
155
|
this.#cursor = 0;
|
129
156
|
}
|
130
|
-
|
157
|
+
readBool() {
|
131
158
|
return this.readByte() === 1;
|
132
159
|
}
|
133
160
|
readBits(length) {
|
@@ -153,16 +180,20 @@ var SigmaByteReader = class {
|
|
153
180
|
readVlq() {
|
154
181
|
return readVLQ(this);
|
155
182
|
}
|
156
|
-
|
157
|
-
|
183
|
+
readI8() {
|
184
|
+
const byte = this.readByte();
|
185
|
+
return byte > MAX_I8 ? byte - (MAX_U8 + 1) : byte;
|
158
186
|
}
|
159
|
-
|
160
|
-
return
|
187
|
+
readI16() {
|
188
|
+
return zigZag32.decode(readBigVLQ(this));
|
161
189
|
}
|
162
|
-
|
163
|
-
return
|
190
|
+
readI32() {
|
191
|
+
return zigZag32.decode(readBigVLQ(this));
|
164
192
|
}
|
165
|
-
|
193
|
+
readI64() {
|
194
|
+
return zigZag64.decode(readBigVLQ(this));
|
195
|
+
}
|
196
|
+
readI256() {
|
166
197
|
const len = readVLQ(this);
|
167
198
|
return hexToBigInt(hex.encode(this.readBytes(len)));
|
168
199
|
}
|
@@ -177,7 +208,7 @@ var SigmaByteWriter = class {
|
|
177
208
|
this.#bytes = new Uint8Array(length);
|
178
209
|
this.#cursor = 0;
|
179
210
|
}
|
180
|
-
|
211
|
+
writeBool(value) {
|
181
212
|
this.write(value === true ? 1 : 0);
|
182
213
|
return this;
|
183
214
|
}
|
@@ -187,17 +218,31 @@ var SigmaByteWriter = class {
|
|
187
218
|
writeBigVLQ(value) {
|
188
219
|
return writeBigVLQ(this, value);
|
189
220
|
}
|
190
|
-
|
191
|
-
|
221
|
+
writeI16(value) {
|
222
|
+
if (value < MIN_I16 || value > MAX_I16) {
|
223
|
+
throw new RangeError(`Value ${value} is out of range for a 16-bit integer`);
|
224
|
+
}
|
225
|
+
this.writeBigVLQ(zigZag32.encode(value));
|
192
226
|
return this;
|
193
227
|
}
|
194
|
-
|
195
|
-
|
196
|
-
|
228
|
+
writeI32(value) {
|
229
|
+
if (value < MIN_I32 || value > MAX_I32) {
|
230
|
+
throw new RangeError(`Value ${value} is out of range for a 32-bit integer`);
|
231
|
+
}
|
232
|
+
return this.writeBigVLQ(zigZag32.encode(value));
|
197
233
|
}
|
198
|
-
|
199
|
-
|
200
|
-
|
234
|
+
writeI64(value) {
|
235
|
+
if (value < MIN_I64 || value > MAX_I64) {
|
236
|
+
throw new RangeError(`Value ${value} is out of range for a 64-bit integer`);
|
237
|
+
}
|
238
|
+
return this.writeBigVLQ(zigZag64.encode(value));
|
239
|
+
}
|
240
|
+
writeI256(value) {
|
241
|
+
if (value < MIN_I256 || value > MAX_I256) {
|
242
|
+
throw new RangeError(`Value ${value} is out of range for a 256-bit integer`);
|
243
|
+
}
|
244
|
+
const hex7 = bigIntToHex(value);
|
245
|
+
return this.writeVLQ(hex7.length / 2).writeHex(hex7);
|
201
246
|
}
|
202
247
|
write(byte) {
|
203
248
|
this.#bytes[this.#cursor++] = byte;
|
@@ -227,10 +272,6 @@ var SigmaByteWriter = class {
|
|
227
272
|
if (bitOffset > 0) this.#cursor++;
|
228
273
|
return this;
|
229
274
|
}
|
230
|
-
writeBigInt(value) {
|
231
|
-
const hex6 = bigIntToHex(value);
|
232
|
-
return this.writeVLQ(hex6.length / 2).writeHex(hex6);
|
233
|
-
}
|
234
275
|
writeChecksum(length = 4, hashFn = blake2b256) {
|
235
276
|
const hash = hashFn(this.toBytes());
|
236
277
|
return this.writeBytes(length ? hash.subarray(0, length) : hash);
|
@@ -520,17 +561,17 @@ var dataSerializer = {
|
|
520
561
|
if (type.embeddable) {
|
521
562
|
switch (type.code) {
|
522
563
|
case descriptors.bool.code:
|
523
|
-
return writer.
|
564
|
+
return writer.writeBool(data);
|
524
565
|
case descriptors.byte.code:
|
525
566
|
return writer.write(data);
|
526
567
|
case descriptors.short.code:
|
527
|
-
return writer.
|
568
|
+
return writer.writeI16(data);
|
528
569
|
case descriptors.int.code:
|
529
|
-
return writer.
|
570
|
+
return writer.writeI32(data);
|
530
571
|
case descriptors.long.code:
|
531
|
-
return writer.
|
572
|
+
return writer.writeI64(data);
|
532
573
|
case descriptors.bigInt.code:
|
533
|
-
return writer.
|
574
|
+
return writer.writeI256(data);
|
534
575
|
case descriptors.groupElement.code:
|
535
576
|
return writer.writeBytes(data);
|
536
577
|
case descriptors.sigmaProp.code: {
|
@@ -588,17 +629,17 @@ var dataSerializer = {
|
|
588
629
|
if (type.embeddable) {
|
589
630
|
switch (type.code) {
|
590
631
|
case descriptors.bool.code:
|
591
|
-
return reader.
|
632
|
+
return reader.readBool();
|
592
633
|
case descriptors.byte.code:
|
593
|
-
return reader.
|
634
|
+
return reader.readI8();
|
594
635
|
case descriptors.short.code:
|
595
|
-
return reader.
|
636
|
+
return reader.readI16();
|
596
637
|
case descriptors.int.code:
|
597
|
-
return reader.
|
638
|
+
return reader.readI32();
|
598
639
|
case descriptors.long.code:
|
599
|
-
return reader.
|
640
|
+
return reader.readI64();
|
600
641
|
case descriptors.bigInt.code:
|
601
|
-
return reader.
|
642
|
+
return reader.readI256();
|
602
643
|
case descriptors.groupElement.code:
|
603
644
|
return reader.readBytes(GROUP_ELEMENT_LENGTH);
|
604
645
|
case descriptors.sigmaProp.code: {
|
@@ -901,24 +942,42 @@ function serializeTransaction(transaction) {
|
|
901
942
|
}
|
902
943
|
function writeInput(writer, input) {
|
903
944
|
writer.writeHex(input.boxId);
|
904
|
-
|
945
|
+
if (isSignedInput(input)) {
|
946
|
+
writeProof(writer, input.spendingProof?.proofBytes);
|
947
|
+
writeExtension(writer, input.spendingProof?.extension);
|
948
|
+
return;
|
949
|
+
}
|
950
|
+
writeProof(writer, null);
|
905
951
|
writeExtension(writer, input.extension);
|
906
952
|
}
|
953
|
+
function isSignedInput(input) {
|
954
|
+
return input.spendingProof !== void 0;
|
955
|
+
}
|
956
|
+
function writeProof(writer, proof) {
|
957
|
+
if (!proof) {
|
958
|
+
writer.write(0);
|
959
|
+
return;
|
960
|
+
}
|
961
|
+
const bytes = hex.decode(proof);
|
962
|
+
writer.writeVLQ(bytes.length);
|
963
|
+
writer.writeBytes(bytes);
|
964
|
+
}
|
907
965
|
function writeExtension(writer, extension) {
|
966
|
+
if (!extension) {
|
967
|
+
writer.write(0);
|
968
|
+
return;
|
969
|
+
}
|
908
970
|
const keys = Object.keys(extension);
|
909
971
|
let length = 0;
|
910
972
|
for (const key of keys) {
|
911
|
-
|
912
|
-
if (isDefined(ext)) {
|
913
|
-
length++;
|
914
|
-
}
|
973
|
+
if (isDefined(extension[key])) length++;
|
915
974
|
}
|
916
975
|
writer.writeVLQ(length);
|
917
976
|
if (length === 0) return;
|
918
977
|
for (const key of keys) {
|
919
|
-
const
|
920
|
-
if (isDefined(
|
921
|
-
writer.writeVLQ(Number(key)).writeHex(
|
978
|
+
const val = extension[key];
|
979
|
+
if (isDefined(val)) {
|
980
|
+
writer.writeVLQ(Number(key)).writeHex(val);
|
922
981
|
}
|
923
982
|
}
|
924
983
|
}
|