@fleet-sdk/serializer 0.8.5 → 0.9.1
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 +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/index.js +82 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +82 -63
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
@@ -32,6 +32,22 @@ function negateAndMask(value) {
|
|
32
32
|
const mask = (1n << BigInt(len)) - 1n;
|
33
33
|
return (~val & mask) + 1n;
|
34
34
|
}
|
35
|
+
|
36
|
+
// src/coders/numRanges.ts
|
37
|
+
var MAX_U8 = 255;
|
38
|
+
var MAX_I8 = 127;
|
39
|
+
var MIN_I16 = -32768;
|
40
|
+
var MAX_I16 = 32767;
|
41
|
+
var MIN_I32 = -2147483648;
|
42
|
+
var MAX_I32 = 2147483647;
|
43
|
+
var MIN_I64 = -BigInt("0x8000000000000000");
|
44
|
+
var MAX_I64 = BigInt("0x7fffffffffffffff");
|
45
|
+
var MIN_I256 = -BigInt(
|
46
|
+
"0x8000000000000000000000000000000000000000000000000000000000000000"
|
47
|
+
);
|
48
|
+
var MAX_I256 = BigInt(
|
49
|
+
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
|
50
|
+
);
|
35
51
|
function writeVLQ(writer, value) {
|
36
52
|
if (value === 0) return writer.write(0);
|
37
53
|
if (value < 0) {
|
@@ -78,7 +94,7 @@ function readBigVLQ(reader) {
|
|
78
94
|
if (reader.isEmpty) return _0n;
|
79
95
|
let value = _0n;
|
80
96
|
let shift = _0n;
|
81
|
-
let lower7bits
|
97
|
+
let lower7bits;
|
82
98
|
do {
|
83
99
|
lower7bits = BigInt(reader.readByte());
|
84
100
|
value |= (lower7bits & _127n) << shift;
|
@@ -127,22 +143,6 @@ var zigZag64 = {
|
|
127
143
|
}
|
128
144
|
};
|
129
145
|
|
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
|
-
);
|
145
|
-
|
146
146
|
// src/coders/sigmaByteReader.ts
|
147
147
|
var SigmaByteReader = class {
|
148
148
|
#bytes;
|
@@ -214,6 +214,9 @@ var SigmaByteReader = class {
|
|
214
214
|
const len = readVLQ(this);
|
215
215
|
return hexToBigInt(hex.encode(this.readBytes(len)));
|
216
216
|
}
|
217
|
+
readRemainingBytes() {
|
218
|
+
return this.readBytes(this.#bytes.length - this.#cursor);
|
219
|
+
}
|
217
220
|
/**
|
218
221
|
* Returns bytes without advancing the cursor.
|
219
222
|
*/
|
@@ -319,7 +322,7 @@ var SigmaByteWriter = class {
|
|
319
322
|
this.writeUInt(items.length);
|
320
323
|
if (items.length === 0) return this;
|
321
324
|
for (const item of items) {
|
322
|
-
serializer(
|
325
|
+
serializer(this, item);
|
323
326
|
}
|
324
327
|
return this;
|
325
328
|
}
|
@@ -575,14 +578,8 @@ var SByte = monoProxy(SByteType, descriptors.byte);
|
|
575
578
|
var SBool = monoProxy(SBoolType, descriptors.bool);
|
576
579
|
var SShort = monoProxy(SShortType, descriptors.short);
|
577
580
|
var SInt = monoProxy(SIntType, descriptors.int);
|
578
|
-
var SLong = monoProxy(
|
579
|
-
|
580
|
-
descriptors.long
|
581
|
-
);
|
582
|
-
var SBigInt = monoProxy(
|
583
|
-
SBigIntType,
|
584
|
-
descriptors.bigInt
|
585
|
-
);
|
581
|
+
var SLong = monoProxy(SLongType, descriptors.long);
|
582
|
+
var SBigInt = monoProxy(SBigIntType, descriptors.bigInt);
|
586
583
|
var SGroupElement = monoProxy(
|
587
584
|
SGroupElementType,
|
588
585
|
descriptors.groupElement
|
@@ -630,12 +627,12 @@ function writeTokens(writer, tokens, tokenIds) {
|
|
630
627
|
if (tokenIds) {
|
631
628
|
writer.writeArray(
|
632
629
|
tokens,
|
633
|
-
(
|
630
|
+
(w, token) => w.writeUInt(tokenIds.indexOf(token.tokenId)).writeBigUInt(ensureBigInt(token.amount))
|
634
631
|
);
|
635
632
|
} else {
|
636
633
|
writer.writeArray(
|
637
634
|
tokens,
|
638
|
-
(
|
635
|
+
(w, token) => w.writeHex(token.tokenId).writeBigUInt(ensureBigInt(token.amount))
|
639
636
|
);
|
640
637
|
}
|
641
638
|
}
|
@@ -647,7 +644,7 @@ function writeRegisters(writer, registers) {
|
|
647
644
|
if (!value) continue;
|
648
645
|
values.push(value);
|
649
646
|
}
|
650
|
-
writer.writeArray(values, (
|
647
|
+
writer.writeArray(values, (w, value) => w.writeHex(value));
|
651
648
|
}
|
652
649
|
function estimateBoxSize(box, withValue) {
|
653
650
|
if (isUndefined(box.creationHeight)) {
|
@@ -816,9 +813,7 @@ var dataSerializer = {
|
|
816
813
|
}
|
817
814
|
if (type.code === descriptors.unit.code) return writer;
|
818
815
|
if (type.code === descriptors.box.code) return serializeBox(data, writer);
|
819
|
-
throw Error(
|
820
|
-
`Serialization error: '0x${type.code.toString(16)}' type not implemented.`
|
821
|
-
);
|
816
|
+
throw Error(`Serialization error: '0x${type.code.toString(16)}' type not implemented.`);
|
822
817
|
},
|
823
818
|
deserialize(type, reader) {
|
824
819
|
if (type.embeddable) {
|
@@ -864,9 +859,7 @@ var dataSerializer = {
|
|
864
859
|
}
|
865
860
|
}
|
866
861
|
case descriptors.tuple.code: {
|
867
|
-
return type.elementsType.map(
|
868
|
-
(t) => this.deserialize(t, reader)
|
869
|
-
);
|
862
|
+
return type.elementsType.map((t) => this.deserialize(t, reader));
|
870
863
|
}
|
871
864
|
case descriptors.unit.code:
|
872
865
|
return void 0;
|
@@ -891,9 +884,7 @@ var typeSerializer = {
|
|
891
884
|
} else if (isColl(type.elementsType)) {
|
892
885
|
const nestedColl = type.elementsType;
|
893
886
|
if (nestedColl.elementsType.embeddable) {
|
894
|
-
writer.write(
|
895
|
-
descriptors.coll.nestedCollTypeCode + nestedColl.elementsType.code
|
896
|
-
);
|
887
|
+
writer.write(descriptors.coll.nestedCollTypeCode + nestedColl.elementsType.code);
|
897
888
|
} else {
|
898
889
|
writer.write(descriptors.coll.simpleCollTypeCode);
|
899
890
|
this.serialize(nestedColl, writer);
|
@@ -932,10 +923,7 @@ var typeSerializer = {
|
|
932
923
|
break;
|
933
924
|
default: {
|
934
925
|
const len = type.elementsType.length;
|
935
|
-
assert(
|
936
|
-
len >= 2 && len <= 255,
|
937
|
-
"Invalid type: tuples must have between 2 and 255 items."
|
938
|
-
);
|
926
|
+
assert(len >= 2 && len <= 255, "Invalid type: tuples must have between 2 and 255 items.");
|
939
927
|
writer.write(descriptors.tuple.genericTupleTypeCode);
|
940
928
|
writer.writeUInt(len);
|
941
929
|
}
|
@@ -973,12 +961,7 @@ var typeSerializer = {
|
|
973
961
|
return new STupleType(internal);
|
974
962
|
}
|
975
963
|
case constructorCode.symmetricPair: {
|
976
|
-
const internal = embdCode === 0 ? [
|
977
|
-
this.deserialize(r),
|
978
|
-
this.deserialize(r),
|
979
|
-
this.deserialize(r),
|
980
|
-
this.deserialize(r)
|
981
|
-
] : [getPrimitiveType(embdCode), getPrimitiveType(embdCode)];
|
964
|
+
const internal = embdCode === 0 ? [this.deserialize(r), this.deserialize(r), this.deserialize(r), this.deserialize(r)] : [getPrimitiveType(embdCode), getPrimitiveType(embdCode)];
|
982
965
|
return new STupleType(internal);
|
983
966
|
}
|
984
967
|
}
|
@@ -1006,6 +989,7 @@ var MAX_CONSTANT_LENGTH = 4096;
|
|
1006
989
|
var SConstant = class _SConstant {
|
1007
990
|
#type;
|
1008
991
|
#data;
|
992
|
+
#bytes;
|
1009
993
|
constructor(type, data) {
|
1010
994
|
this.#type = type;
|
1011
995
|
this.#data = type.coerce(data);
|
@@ -1013,9 +997,14 @@ var SConstant = class _SConstant {
|
|
1013
997
|
static from(bytes) {
|
1014
998
|
const reader = bytes instanceof SigmaByteReader ? bytes : new SigmaByteReader(bytes);
|
1015
999
|
if (reader.isEmpty) throw new Error("Empty constant bytes.");
|
1000
|
+
const start = reader.cursor;
|
1016
1001
|
const type = typeSerializer.deserialize(reader);
|
1017
1002
|
const data = dataSerializer.deserialize(type, reader);
|
1018
|
-
return new _SConstant(type, data);
|
1003
|
+
return new _SConstant(type, data).#withBytes(reader.bytes.slice(start, reader.cursor));
|
1004
|
+
}
|
1005
|
+
#withBytes(bytes) {
|
1006
|
+
this.#bytes = bytes;
|
1007
|
+
return this;
|
1019
1008
|
}
|
1020
1009
|
get type() {
|
1021
1010
|
return this.#type;
|
@@ -1023,16 +1012,52 @@ var SConstant = class _SConstant {
|
|
1023
1012
|
get data() {
|
1024
1013
|
return this.#data;
|
1025
1014
|
}
|
1026
|
-
|
1027
|
-
|
1015
|
+
/**
|
1016
|
+
* Returns the serialized representation of the current instance as a `Uint8Array`.
|
1017
|
+
* If the bytes have already been computed and cached, returns the cached value.
|
1018
|
+
* Otherwise, serializes the instance and returns the resulting bytes.
|
1019
|
+
*/
|
1020
|
+
get bytes() {
|
1021
|
+
if (this.#bytes) return this.#bytes;
|
1022
|
+
return this.serialize();
|
1023
|
+
}
|
1024
|
+
/**
|
1025
|
+
* Serializes the current object into a `Uint8Array`.
|
1026
|
+
*/
|
1027
|
+
serialize() {
|
1028
|
+
const writer = new SigmaByteWriter(guessConstantBytesSize(this.type, this.data));
|
1028
1029
|
typeSerializer.serialize(this.type, writer);
|
1029
1030
|
dataSerializer.serialize(this.data, this.type, writer);
|
1030
|
-
|
1031
|
+
this.#bytes = writer.toBytes();
|
1032
|
+
return this.#bytes;
|
1033
|
+
}
|
1034
|
+
/**
|
1035
|
+
* @deprecated use `serialize` instead
|
1036
|
+
*/
|
1037
|
+
toBytes() {
|
1038
|
+
return this.serialize();
|
1031
1039
|
}
|
1032
1040
|
toHex() {
|
1033
|
-
return hex.encode(this.
|
1041
|
+
return hex.encode(this.serialize());
|
1034
1042
|
}
|
1035
1043
|
};
|
1044
|
+
function guessConstantBytesSize(type, data) {
|
1045
|
+
const dataSize = 1;
|
1046
|
+
if (type.code === descriptors.short.code) return dataSize + 8;
|
1047
|
+
if (type.code === descriptors.int.code) return dataSize + 16;
|
1048
|
+
if (type.code === descriptors.long.code) return dataSize + 32;
|
1049
|
+
if (type.code === descriptors.bigInt.code) return dataSize + 64;
|
1050
|
+
if (type.code === descriptors.bool.code) return dataSize + 1;
|
1051
|
+
if (type.code === descriptors.byte.code) return dataSize + 1;
|
1052
|
+
if (type.code === descriptors.unit.code) return dataSize + 0;
|
1053
|
+
if (type.code === descriptors.groupElement.code) return dataSize + 33;
|
1054
|
+
if (type.code === descriptors.sigmaProp.code) return dataSize + 35;
|
1055
|
+
if (isColl(type) && !isColl(type.elementsType) && !isTuple(type.elementsType)) {
|
1056
|
+
const len = data.length;
|
1057
|
+
return dataSize + estimateVLQSize(len) + guessConstantBytesSize(type.elementsType) * len;
|
1058
|
+
}
|
1059
|
+
return MAX_CONSTANT_LENGTH;
|
1060
|
+
}
|
1036
1061
|
function decode(value) {
|
1037
1062
|
if (value === void 0) return;
|
1038
1063
|
try {
|
@@ -1060,7 +1085,7 @@ function parse(constant, mode = "strict") {
|
|
1060
1085
|
}
|
1061
1086
|
function serializeTransaction(transaction) {
|
1062
1087
|
const tokenIds = getDistinctTokenIds(transaction.outputs);
|
1063
|
-
return new SigmaByteWriter(1e5).writeArray(transaction.inputs, (
|
1088
|
+
return new SigmaByteWriter(1e5).writeArray(transaction.inputs, (w, input) => writeInput(w, input)).writeArray(transaction.dataInputs, (w, dataInput) => w.writeHex(dataInput.boxId)).writeArray(tokenIds, (w, tokenId) => w.writeHex(tokenId)).writeArray(transaction.outputs, (w, output) => serializeBox(output, w, tokenIds));
|
1064
1089
|
}
|
1065
1090
|
function writeInput(writer, input) {
|
1066
1091
|
if (isSignedInput(input)) {
|
@@ -1077,10 +1102,7 @@ function writeSignedInput(writer, input) {
|
|
1077
1102
|
function writeUnsignedInput(writer, input) {
|
1078
1103
|
writer.writeHex(input.boxId);
|
1079
1104
|
writeProof(writer, null);
|
1080
|
-
writeExtension(
|
1081
|
-
writer,
|
1082
|
-
isSignedInput(input) ? input.spendingProof?.extension : input.extension
|
1083
|
-
);
|
1105
|
+
writeExtension(writer, isSignedInput(input) ? input.spendingProof?.extension : input.extension);
|
1084
1106
|
}
|
1085
1107
|
function isSignedInput(input) {
|
1086
1108
|
return input.spendingProof !== void 0;
|
@@ -1105,10 +1127,7 @@ function writeExtension(writer, extension) {
|
|
1105
1127
|
if (!value) continue;
|
1106
1128
|
values.push([key, value]);
|
1107
1129
|
}
|
1108
|
-
writer.writeArray(
|
1109
|
-
values,
|
1110
|
-
([key, value], w) => w.writeUInt(Number(key)).writeHex(value)
|
1111
|
-
);
|
1130
|
+
writer.writeArray(values, (w, [key, value]) => w.writeUInt(Number(key)).writeHex(value));
|
1112
1131
|
}
|
1113
1132
|
function getDistinctTokenIds(outputs) {
|
1114
1133
|
const tokenIds = /* @__PURE__ */ new Set();
|
@@ -1141,7 +1160,7 @@ function readInput(reader) {
|
|
1141
1160
|
return proofBytes ? { boxId, spendingProof: { proofBytes, extension } } : { boxId, extension };
|
1142
1161
|
}
|
1143
1162
|
function computeId(reader, inputs) {
|
1144
|
-
const bytes = new SigmaByteWriter(reader.bytes.length).writeArray(inputs, (
|
1163
|
+
const bytes = new SigmaByteWriter(reader.bytes.length).writeArray(inputs, (w, input) => writeUnsignedInput(w, input)).writeBytes(reader.bytes.subarray(reader.cursor)).toBytes();
|
1145
1164
|
return hex.encode(blake2b256(bytes));
|
1146
1165
|
}
|
1147
1166
|
|