@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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # @fleet-sdk/serializer
2
2
 
3
+ ## 0.9.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 59018ee: No relevant changes
8
+ - Updated dependencies [59018ee]
9
+ - @fleet-sdk/common@0.9.1
10
+ - @fleet-sdk/crypto@0.9.1
11
+
12
+ ## 0.9.0
13
+
14
+ ### Patch Changes
15
+
16
+ - c558227: Memoize `SConstant` bytes for efficient re-encoding
17
+ - ed7e44c: Add `SigmaByteReader#readRemainingBytes` method
18
+
3
19
  ## 0.8.5
4
20
 
5
21
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -19,6 +19,7 @@ declare class SigmaByteReader {
19
19
  readI32(): number;
20
20
  readI64(): bigint;
21
21
  readI256(): bigint;
22
+ readRemainingBytes(): Uint8Array;
22
23
  /**
23
24
  * Returns bytes without advancing the cursor.
24
25
  */
@@ -54,7 +55,7 @@ declare class SigmaByteWriter {
54
55
  * @param serializer - A function that serializes each item and writes it using the provided SigmaByteWriter.
55
56
  * @returns The current instance of SigmaByteWriter for method chaining.
56
57
  */
57
- writeArray<T>(items: T[], serializer: (item: T, writer: SigmaByteWriter) => void): SigmaByteWriter;
58
+ writeArray<T>(items: T[], serializer: (writer: SigmaByteWriter, item: T) => void): SigmaByteWriter;
58
59
  encode<T>(coder: Coder<Uint8Array, T>): T;
59
60
  toBytes(): Uint8Array;
60
61
  }
@@ -186,6 +187,19 @@ declare class SConstant<D = unknown, T extends SType = SType> {
186
187
  static from<D, T extends SType = SType>(bytes: ByteInput$1 | SigmaByteReader): SConstant<D, T>;
187
188
  get type(): T;
188
189
  get data(): D;
190
+ /**
191
+ * Returns the serialized representation of the current instance as a `Uint8Array`.
192
+ * If the bytes have already been computed and cached, returns the cached value.
193
+ * Otherwise, serializes the instance and returns the resulting bytes.
194
+ */
195
+ get bytes(): Uint8Array;
196
+ /**
197
+ * Serializes the current object into a `Uint8Array`.
198
+ */
199
+ serialize(): Uint8Array;
200
+ /**
201
+ * @deprecated use `serialize` instead
202
+ */
189
203
  toBytes(): Uint8Array;
190
204
  toHex(): string;
191
205
  }
package/dist/index.d.ts CHANGED
@@ -19,6 +19,7 @@ declare class SigmaByteReader {
19
19
  readI32(): number;
20
20
  readI64(): bigint;
21
21
  readI256(): bigint;
22
+ readRemainingBytes(): Uint8Array;
22
23
  /**
23
24
  * Returns bytes without advancing the cursor.
24
25
  */
@@ -54,7 +55,7 @@ declare class SigmaByteWriter {
54
55
  * @param serializer - A function that serializes each item and writes it using the provided SigmaByteWriter.
55
56
  * @returns The current instance of SigmaByteWriter for method chaining.
56
57
  */
57
- writeArray<T>(items: T[], serializer: (item: T, writer: SigmaByteWriter) => void): SigmaByteWriter;
58
+ writeArray<T>(items: T[], serializer: (writer: SigmaByteWriter, item: T) => void): SigmaByteWriter;
58
59
  encode<T>(coder: Coder<Uint8Array, T>): T;
59
60
  toBytes(): Uint8Array;
60
61
  }
@@ -186,6 +187,19 @@ declare class SConstant<D = unknown, T extends SType = SType> {
186
187
  static from<D, T extends SType = SType>(bytes: ByteInput$1 | SigmaByteReader): SConstant<D, T>;
187
188
  get type(): T;
188
189
  get data(): D;
190
+ /**
191
+ * Returns the serialized representation of the current instance as a `Uint8Array`.
192
+ * If the bytes have already been computed and cached, returns the cached value.
193
+ * Otherwise, serializes the instance and returns the resulting bytes.
194
+ */
195
+ get bytes(): Uint8Array;
196
+ /**
197
+ * Serializes the current object into a `Uint8Array`.
198
+ */
199
+ serialize(): Uint8Array;
200
+ /**
201
+ * @deprecated use `serialize` instead
202
+ */
189
203
  toBytes(): Uint8Array;
190
204
  toHex(): string;
191
205
  }
package/dist/index.js CHANGED
@@ -34,6 +34,22 @@ function negateAndMask(value) {
34
34
  const mask = (1n << BigInt(len)) - 1n;
35
35
  return (~val & mask) + 1n;
36
36
  }
37
+
38
+ // src/coders/numRanges.ts
39
+ var MAX_U8 = 255;
40
+ var MAX_I8 = 127;
41
+ var MIN_I16 = -32768;
42
+ var MAX_I16 = 32767;
43
+ var MIN_I32 = -2147483648;
44
+ var MAX_I32 = 2147483647;
45
+ var MIN_I64 = -BigInt("0x8000000000000000");
46
+ var MAX_I64 = BigInt("0x7fffffffffffffff");
47
+ var MIN_I256 = -BigInt(
48
+ "0x8000000000000000000000000000000000000000000000000000000000000000"
49
+ );
50
+ var MAX_I256 = BigInt(
51
+ "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
52
+ );
37
53
  function writeVLQ(writer, value) {
38
54
  if (value === 0) return writer.write(0);
39
55
  if (value < 0) {
@@ -80,7 +96,7 @@ function readBigVLQ(reader) {
80
96
  if (reader.isEmpty) return common._0n;
81
97
  let value = common._0n;
82
98
  let shift = common._0n;
83
- let lower7bits = common._0n;
99
+ let lower7bits;
84
100
  do {
85
101
  lower7bits = BigInt(reader.readByte());
86
102
  value |= (lower7bits & common._127n) << shift;
@@ -129,22 +145,6 @@ var zigZag64 = {
129
145
  }
130
146
  };
131
147
 
132
- // src/coders/numRanges.ts
133
- var MAX_U8 = 255;
134
- var MAX_I8 = 127;
135
- var MIN_I16 = -32768;
136
- var MAX_I16 = 32767;
137
- var MIN_I32 = -2147483648;
138
- var MAX_I32 = 2147483647;
139
- var MIN_I64 = -BigInt("0x8000000000000000");
140
- var MAX_I64 = BigInt("0x7fffffffffffffff");
141
- var MIN_I256 = -BigInt(
142
- "0x8000000000000000000000000000000000000000000000000000000000000000"
143
- );
144
- var MAX_I256 = BigInt(
145
- "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
146
- );
147
-
148
148
  // src/coders/sigmaByteReader.ts
149
149
  var SigmaByteReader = class {
150
150
  #bytes;
@@ -216,6 +216,9 @@ var SigmaByteReader = class {
216
216
  const len = readVLQ(this);
217
217
  return hexToBigInt(crypto.hex.encode(this.readBytes(len)));
218
218
  }
219
+ readRemainingBytes() {
220
+ return this.readBytes(this.#bytes.length - this.#cursor);
221
+ }
219
222
  /**
220
223
  * Returns bytes without advancing the cursor.
221
224
  */
@@ -321,7 +324,7 @@ var SigmaByteWriter = class {
321
324
  this.writeUInt(items.length);
322
325
  if (items.length === 0) return this;
323
326
  for (const item of items) {
324
- serializer(item, this);
327
+ serializer(this, item);
325
328
  }
326
329
  return this;
327
330
  }
@@ -577,14 +580,8 @@ var SByte = monoProxy(SByteType, descriptors.byte);
577
580
  var SBool = monoProxy(SBoolType, descriptors.bool);
578
581
  var SShort = monoProxy(SShortType, descriptors.short);
579
582
  var SInt = monoProxy(SIntType, descriptors.int);
580
- var SLong = monoProxy(
581
- SLongType,
582
- descriptors.long
583
- );
584
- var SBigInt = monoProxy(
585
- SBigIntType,
586
- descriptors.bigInt
587
- );
583
+ var SLong = monoProxy(SLongType, descriptors.long);
584
+ var SBigInt = monoProxy(SBigIntType, descriptors.bigInt);
588
585
  var SGroupElement = monoProxy(
589
586
  SGroupElementType,
590
587
  descriptors.groupElement
@@ -632,12 +629,12 @@ function writeTokens(writer, tokens, tokenIds) {
632
629
  if (tokenIds) {
633
630
  writer.writeArray(
634
631
  tokens,
635
- (token, w) => w.writeUInt(tokenIds.indexOf(token.tokenId)).writeBigUInt(common.ensureBigInt(token.amount))
632
+ (w, token) => w.writeUInt(tokenIds.indexOf(token.tokenId)).writeBigUInt(common.ensureBigInt(token.amount))
636
633
  );
637
634
  } else {
638
635
  writer.writeArray(
639
636
  tokens,
640
- (token, w) => w.writeHex(token.tokenId).writeBigUInt(common.ensureBigInt(token.amount))
637
+ (w, token) => w.writeHex(token.tokenId).writeBigUInt(common.ensureBigInt(token.amount))
641
638
  );
642
639
  }
643
640
  }
@@ -649,7 +646,7 @@ function writeRegisters(writer, registers) {
649
646
  if (!value) continue;
650
647
  values.push(value);
651
648
  }
652
- writer.writeArray(values, (value, w) => w.writeHex(value));
649
+ writer.writeArray(values, (w, value) => w.writeHex(value));
653
650
  }
654
651
  function estimateBoxSize(box, withValue) {
655
652
  if (common.isUndefined(box.creationHeight)) {
@@ -818,9 +815,7 @@ var dataSerializer = {
818
815
  }
819
816
  if (type.code === descriptors.unit.code) return writer;
820
817
  if (type.code === descriptors.box.code) return serializeBox(data, writer);
821
- throw Error(
822
- `Serialization error: '0x${type.code.toString(16)}' type not implemented.`
823
- );
818
+ throw Error(`Serialization error: '0x${type.code.toString(16)}' type not implemented.`);
824
819
  },
825
820
  deserialize(type, reader) {
826
821
  if (type.embeddable) {
@@ -866,9 +861,7 @@ var dataSerializer = {
866
861
  }
867
862
  }
868
863
  case descriptors.tuple.code: {
869
- return type.elementsType.map(
870
- (t) => this.deserialize(t, reader)
871
- );
864
+ return type.elementsType.map((t) => this.deserialize(t, reader));
872
865
  }
873
866
  case descriptors.unit.code:
874
867
  return void 0;
@@ -893,9 +886,7 @@ var typeSerializer = {
893
886
  } else if (isColl(type.elementsType)) {
894
887
  const nestedColl = type.elementsType;
895
888
  if (nestedColl.elementsType.embeddable) {
896
- writer.write(
897
- descriptors.coll.nestedCollTypeCode + nestedColl.elementsType.code
898
- );
889
+ writer.write(descriptors.coll.nestedCollTypeCode + nestedColl.elementsType.code);
899
890
  } else {
900
891
  writer.write(descriptors.coll.simpleCollTypeCode);
901
892
  this.serialize(nestedColl, writer);
@@ -934,10 +925,7 @@ var typeSerializer = {
934
925
  break;
935
926
  default: {
936
927
  const len = type.elementsType.length;
937
- common.assert(
938
- len >= 2 && len <= 255,
939
- "Invalid type: tuples must have between 2 and 255 items."
940
- );
928
+ common.assert(len >= 2 && len <= 255, "Invalid type: tuples must have between 2 and 255 items.");
941
929
  writer.write(descriptors.tuple.genericTupleTypeCode);
942
930
  writer.writeUInt(len);
943
931
  }
@@ -975,12 +963,7 @@ var typeSerializer = {
975
963
  return new STupleType(internal);
976
964
  }
977
965
  case constructorCode.symmetricPair: {
978
- const internal = embdCode === 0 ? [
979
- this.deserialize(r),
980
- this.deserialize(r),
981
- this.deserialize(r),
982
- this.deserialize(r)
983
- ] : [getPrimitiveType(embdCode), getPrimitiveType(embdCode)];
966
+ const internal = embdCode === 0 ? [this.deserialize(r), this.deserialize(r), this.deserialize(r), this.deserialize(r)] : [getPrimitiveType(embdCode), getPrimitiveType(embdCode)];
984
967
  return new STupleType(internal);
985
968
  }
986
969
  }
@@ -1008,6 +991,7 @@ var MAX_CONSTANT_LENGTH = 4096;
1008
991
  var SConstant = class _SConstant {
1009
992
  #type;
1010
993
  #data;
994
+ #bytes;
1011
995
  constructor(type, data) {
1012
996
  this.#type = type;
1013
997
  this.#data = type.coerce(data);
@@ -1015,9 +999,14 @@ var SConstant = class _SConstant {
1015
999
  static from(bytes) {
1016
1000
  const reader = bytes instanceof SigmaByteReader ? bytes : new SigmaByteReader(bytes);
1017
1001
  if (reader.isEmpty) throw new Error("Empty constant bytes.");
1002
+ const start = reader.cursor;
1018
1003
  const type = typeSerializer.deserialize(reader);
1019
1004
  const data = dataSerializer.deserialize(type, reader);
1020
- return new _SConstant(type, data);
1005
+ return new _SConstant(type, data).#withBytes(reader.bytes.slice(start, reader.cursor));
1006
+ }
1007
+ #withBytes(bytes) {
1008
+ this.#bytes = bytes;
1009
+ return this;
1021
1010
  }
1022
1011
  get type() {
1023
1012
  return this.#type;
@@ -1025,16 +1014,52 @@ var SConstant = class _SConstant {
1025
1014
  get data() {
1026
1015
  return this.#data;
1027
1016
  }
1028
- toBytes() {
1029
- const writer = new SigmaByteWriter(MAX_CONSTANT_LENGTH);
1017
+ /**
1018
+ * Returns the serialized representation of the current instance as a `Uint8Array`.
1019
+ * If the bytes have already been computed and cached, returns the cached value.
1020
+ * Otherwise, serializes the instance and returns the resulting bytes.
1021
+ */
1022
+ get bytes() {
1023
+ if (this.#bytes) return this.#bytes;
1024
+ return this.serialize();
1025
+ }
1026
+ /**
1027
+ * Serializes the current object into a `Uint8Array`.
1028
+ */
1029
+ serialize() {
1030
+ const writer = new SigmaByteWriter(guessConstantBytesSize(this.type, this.data));
1030
1031
  typeSerializer.serialize(this.type, writer);
1031
1032
  dataSerializer.serialize(this.data, this.type, writer);
1032
- return writer.toBytes();
1033
+ this.#bytes = writer.toBytes();
1034
+ return this.#bytes;
1035
+ }
1036
+ /**
1037
+ * @deprecated use `serialize` instead
1038
+ */
1039
+ toBytes() {
1040
+ return this.serialize();
1033
1041
  }
1034
1042
  toHex() {
1035
- return crypto.hex.encode(this.toBytes());
1043
+ return crypto.hex.encode(this.serialize());
1036
1044
  }
1037
1045
  };
1046
+ function guessConstantBytesSize(type, data) {
1047
+ const dataSize = 1;
1048
+ if (type.code === descriptors.short.code) return dataSize + 8;
1049
+ if (type.code === descriptors.int.code) return dataSize + 16;
1050
+ if (type.code === descriptors.long.code) return dataSize + 32;
1051
+ if (type.code === descriptors.bigInt.code) return dataSize + 64;
1052
+ if (type.code === descriptors.bool.code) return dataSize + 1;
1053
+ if (type.code === descriptors.byte.code) return dataSize + 1;
1054
+ if (type.code === descriptors.unit.code) return dataSize + 0;
1055
+ if (type.code === descriptors.groupElement.code) return dataSize + 33;
1056
+ if (type.code === descriptors.sigmaProp.code) return dataSize + 35;
1057
+ if (isColl(type) && !isColl(type.elementsType) && !isTuple(type.elementsType)) {
1058
+ const len = data.length;
1059
+ return dataSize + estimateVLQSize(len) + guessConstantBytesSize(type.elementsType) * len;
1060
+ }
1061
+ return MAX_CONSTANT_LENGTH;
1062
+ }
1038
1063
  function decode(value) {
1039
1064
  if (value === void 0) return;
1040
1065
  try {
@@ -1062,7 +1087,7 @@ function parse(constant, mode = "strict") {
1062
1087
  }
1063
1088
  function serializeTransaction(transaction) {
1064
1089
  const tokenIds = getDistinctTokenIds(transaction.outputs);
1065
- return new SigmaByteWriter(1e5).writeArray(transaction.inputs, (input, w) => writeInput(w, input)).writeArray(transaction.dataInputs, (dataInput, w) => w.writeHex(dataInput.boxId)).writeArray(tokenIds, (tokenId, w) => w.writeHex(tokenId)).writeArray(transaction.outputs, (output, w) => serializeBox(output, w, tokenIds));
1090
+ 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));
1066
1091
  }
1067
1092
  function writeInput(writer, input) {
1068
1093
  if (isSignedInput(input)) {
@@ -1079,10 +1104,7 @@ function writeSignedInput(writer, input) {
1079
1104
  function writeUnsignedInput(writer, input) {
1080
1105
  writer.writeHex(input.boxId);
1081
1106
  writeProof(writer, null);
1082
- writeExtension(
1083
- writer,
1084
- isSignedInput(input) ? input.spendingProof?.extension : input.extension
1085
- );
1107
+ writeExtension(writer, isSignedInput(input) ? input.spendingProof?.extension : input.extension);
1086
1108
  }
1087
1109
  function isSignedInput(input) {
1088
1110
  return input.spendingProof !== void 0;
@@ -1107,10 +1129,7 @@ function writeExtension(writer, extension) {
1107
1129
  if (!value) continue;
1108
1130
  values.push([key, value]);
1109
1131
  }
1110
- writer.writeArray(
1111
- values,
1112
- ([key, value], w) => w.writeUInt(Number(key)).writeHex(value)
1113
- );
1132
+ writer.writeArray(values, (w, [key, value]) => w.writeUInt(Number(key)).writeHex(value));
1114
1133
  }
1115
1134
  function getDistinctTokenIds(outputs) {
1116
1135
  const tokenIds = /* @__PURE__ */ new Set();
@@ -1143,7 +1162,7 @@ function readInput(reader) {
1143
1162
  return proofBytes ? { boxId, spendingProof: { proofBytes, extension } } : { boxId, extension };
1144
1163
  }
1145
1164
  function computeId(reader, inputs) {
1146
- const bytes = new SigmaByteWriter(reader.bytes.length).writeArray(inputs, (input, writer) => writeUnsignedInput(writer, input)).writeBytes(reader.bytes.subarray(reader.cursor)).toBytes();
1165
+ const bytes = new SigmaByteWriter(reader.bytes.length).writeArray(inputs, (w, input) => writeUnsignedInput(w, input)).writeBytes(reader.bytes.subarray(reader.cursor)).toBytes();
1147
1166
  return crypto.hex.encode(crypto.blake2b256(bytes));
1148
1167
  }
1149
1168