@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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # @fleet-sdk/serializer
2
2
 
3
+ ## 0.8.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 9c04d3c: Add `SignedTransaction` serialization
8
+ - Updated dependencies [9c04d3c]
9
+ - @fleet-sdk/common@0.8.3
10
+ - @fleet-sdk/crypto@0.8.3
11
+
12
+ ## 0.8.2
13
+
14
+ ### Patch Changes
15
+
16
+ - 66d6fc9: Fix `ZigZag` encoding for 32-bit integers
17
+ - 1c8541a: Fix signed `SByte` parsing
18
+ - Updated dependencies [9a244b5]
19
+ - Updated dependencies [1b07c4a]
20
+ - @fleet-sdk/crypto@0.8.2
21
+
3
22
  ## 0.8.0
4
23
 
5
24
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,36 +1,37 @@
1
1
  import { ByteInput as ByteInput$1, blake2b256, Coder } from '@fleet-sdk/crypto';
2
- import { Box, Amount, BoxCandidate, UnsignedInput, DataInput } from '@fleet-sdk/common';
2
+ import { Box, Amount, BoxCandidate, UnsignedInput, DataInput, SignedTransaction } from '@fleet-sdk/common';
3
3
 
4
4
  declare class SigmaByteReader {
5
5
  #private;
6
6
  get isEmpty(): boolean;
7
7
  constructor(bytes: ByteInput$1);
8
- readBoolean(): boolean;
8
+ readBool(): boolean;
9
9
  readBits(length: number): ArrayLike<boolean>;
10
10
  readByte(): number;
11
11
  readBytes(length: number): Uint8Array;
12
12
  readVlq(): number;
13
- readShort(): number;
14
- readInt(): number;
15
- readLong(): bigint;
16
- readBigInt(): bigint;
13
+ readI8(): number;
14
+ readI16(): number;
15
+ readI32(): number;
16
+ readI64(): bigint;
17
+ readI256(): bigint;
17
18
  }
18
19
 
19
20
  declare class SigmaByteWriter {
20
21
  #private;
21
22
  get length(): number;
22
23
  constructor(length: number);
23
- writeBoolean(value: boolean): SigmaByteWriter;
24
+ writeBool(value: boolean): SigmaByteWriter;
24
25
  writeVLQ(value: number): SigmaByteWriter;
25
26
  writeBigVLQ(value: bigint): SigmaByteWriter;
26
- writeShort(value: number): SigmaByteWriter;
27
- writeInt(value: number): SigmaByteWriter;
28
- writeLong(value: bigint): SigmaByteWriter;
27
+ writeI16(value: number): SigmaByteWriter;
28
+ writeI32(value: number): SigmaByteWriter;
29
+ writeI64(value: bigint): SigmaByteWriter;
30
+ writeI256(value: bigint): SigmaByteWriter;
29
31
  write(byte: number): SigmaByteWriter;
30
32
  writeBytes(bytes: ArrayLike<number>): SigmaByteWriter;
31
33
  writeHex(bytesHex: string): SigmaByteWriter;
32
34
  writeBits(bits: ArrayLike<boolean>): SigmaByteWriter;
33
- writeBigInt(value: bigint): SigmaByteWriter;
34
35
  writeChecksum(length?: number, hashFn?: typeof blake2b256): SigmaByteWriter;
35
36
  encode<T>(coder: Coder<Uint8Array, T>): T;
36
37
  toBytes(): Uint8Array;
@@ -94,8 +95,8 @@ declare const SShort: SProxy<SShortType, number, number>;
94
95
  declare const SInt: SProxy<SIntType, number, number>;
95
96
  declare const SLong: SProxy<SLongType, BigIntInput, bigint>;
96
97
  declare const SBigInt: SProxy<SBigIntType, BigIntInput, bigint>;
97
- declare const SGroupElement: SProxy<SGroupElementType, ByteInput, Uint8Array>;
98
- declare const SSigmaProp: SProxy<SSigmaPropType, SConstant<Uint8Array, SType<unknown, unknown>>, SConstant<Uint8Array, SType<unknown, unknown>>>;
98
+ declare const SGroupElement: SProxy<SGroupElementType, ByteInput, Uint8Array<ArrayBufferLike>>;
99
+ declare const SSigmaProp: SProxy<SSigmaPropType, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>>;
99
100
  type SUnit = (value?: undefined) => SConstant<undefined, SUnitType>;
100
101
  declare const SUnit: SUnit;
101
102
  type SColl = {
@@ -194,11 +195,11 @@ declare function serializeBox(box: BoxCandidate<Amount>, writer: SigmaByteWriter
194
195
  declare function estimateBoxSize(box: Box<Amount> | BoxCandidate<Amount>, withValue?: Amount): number;
195
196
 
196
197
  type MinimalUnsignedTransaction = {
197
- inputs: readonly UnsignedInput[];
198
- dataInputs: readonly DataInput[];
199
- outputs: readonly BoxCandidate<Amount>[];
198
+ inputs: UnsignedInput[];
199
+ dataInputs: DataInput[];
200
+ outputs: BoxCandidate<Amount>[];
200
201
  };
201
- declare function serializeTransaction(transaction: MinimalUnsignedTransaction): SigmaByteWriter;
202
+ declare function serializeTransaction(transaction: MinimalUnsignedTransaction | SignedTransaction): SigmaByteWriter;
202
203
 
203
204
  declare const dataSerializer: {
204
205
  serialize(data: unknown, type: SType, writer: SigmaByteWriter): SigmaByteWriter;
package/dist/index.d.ts CHANGED
@@ -1,36 +1,37 @@
1
1
  import { ByteInput as ByteInput$1, blake2b256, Coder } from '@fleet-sdk/crypto';
2
- import { Box, Amount, BoxCandidate, UnsignedInput, DataInput } from '@fleet-sdk/common';
2
+ import { Box, Amount, BoxCandidate, UnsignedInput, DataInput, SignedTransaction } from '@fleet-sdk/common';
3
3
 
4
4
  declare class SigmaByteReader {
5
5
  #private;
6
6
  get isEmpty(): boolean;
7
7
  constructor(bytes: ByteInput$1);
8
- readBoolean(): boolean;
8
+ readBool(): boolean;
9
9
  readBits(length: number): ArrayLike<boolean>;
10
10
  readByte(): number;
11
11
  readBytes(length: number): Uint8Array;
12
12
  readVlq(): number;
13
- readShort(): number;
14
- readInt(): number;
15
- readLong(): bigint;
16
- readBigInt(): bigint;
13
+ readI8(): number;
14
+ readI16(): number;
15
+ readI32(): number;
16
+ readI64(): bigint;
17
+ readI256(): bigint;
17
18
  }
18
19
 
19
20
  declare class SigmaByteWriter {
20
21
  #private;
21
22
  get length(): number;
22
23
  constructor(length: number);
23
- writeBoolean(value: boolean): SigmaByteWriter;
24
+ writeBool(value: boolean): SigmaByteWriter;
24
25
  writeVLQ(value: number): SigmaByteWriter;
25
26
  writeBigVLQ(value: bigint): SigmaByteWriter;
26
- writeShort(value: number): SigmaByteWriter;
27
- writeInt(value: number): SigmaByteWriter;
28
- writeLong(value: bigint): SigmaByteWriter;
27
+ writeI16(value: number): SigmaByteWriter;
28
+ writeI32(value: number): SigmaByteWriter;
29
+ writeI64(value: bigint): SigmaByteWriter;
30
+ writeI256(value: bigint): SigmaByteWriter;
29
31
  write(byte: number): SigmaByteWriter;
30
32
  writeBytes(bytes: ArrayLike<number>): SigmaByteWriter;
31
33
  writeHex(bytesHex: string): SigmaByteWriter;
32
34
  writeBits(bits: ArrayLike<boolean>): SigmaByteWriter;
33
- writeBigInt(value: bigint): SigmaByteWriter;
34
35
  writeChecksum(length?: number, hashFn?: typeof blake2b256): SigmaByteWriter;
35
36
  encode<T>(coder: Coder<Uint8Array, T>): T;
36
37
  toBytes(): Uint8Array;
@@ -94,8 +95,8 @@ declare const SShort: SProxy<SShortType, number, number>;
94
95
  declare const SInt: SProxy<SIntType, number, number>;
95
96
  declare const SLong: SProxy<SLongType, BigIntInput, bigint>;
96
97
  declare const SBigInt: SProxy<SBigIntType, BigIntInput, bigint>;
97
- declare const SGroupElement: SProxy<SGroupElementType, ByteInput, Uint8Array>;
98
- declare const SSigmaProp: SProxy<SSigmaPropType, SConstant<Uint8Array, SType<unknown, unknown>>, SConstant<Uint8Array, SType<unknown, unknown>>>;
98
+ declare const SGroupElement: SProxy<SGroupElementType, ByteInput, Uint8Array<ArrayBufferLike>>;
99
+ declare const SSigmaProp: SProxy<SSigmaPropType, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>>;
99
100
  type SUnit = (value?: undefined) => SConstant<undefined, SUnitType>;
100
101
  declare const SUnit: SUnit;
101
102
  type SColl = {
@@ -194,11 +195,11 @@ declare function serializeBox(box: BoxCandidate<Amount>, writer: SigmaByteWriter
194
195
  declare function estimateBoxSize(box: Box<Amount> | BoxCandidate<Amount>, withValue?: Amount): number;
195
196
 
196
197
  type MinimalUnsignedTransaction = {
197
- inputs: readonly UnsignedInput[];
198
- dataInputs: readonly DataInput[];
199
- outputs: readonly BoxCandidate<Amount>[];
198
+ inputs: UnsignedInput[];
199
+ dataInputs: DataInput[];
200
+ outputs: BoxCandidate<Amount>[];
200
201
  };
201
- declare function serializeTransaction(transaction: MinimalUnsignedTransaction): SigmaByteWriter;
202
+ declare function serializeTransaction(transaction: MinimalUnsignedTransaction | SignedTransaction): SigmaByteWriter;
202
203
 
203
204
  declare const dataSerializer: {
204
205
  serialize(data: unknown, type: SType, writer: SigmaByteWriter): SigmaByteWriter;
package/dist/index.js CHANGED
@@ -4,20 +4,20 @@ var common = require('@fleet-sdk/common');
4
4
  var crypto = require('@fleet-sdk/crypto');
5
5
 
6
6
  // src/coders/sigmaByteReader.ts
7
- function hexToBigInt(hex6) {
8
- const value = BigInt(hex6.length % 2 ? `0x0${hex6}` : `0x${hex6}`);
9
- const highByte = Number.parseInt(hex6.slice(0, 2), 16);
7
+ function hexToBigInt(hex7) {
8
+ const value = BigInt(hex7.length % 2 ? `0x0${hex7}` : `0x${hex7}`);
9
+ const highByte = Number.parseInt(hex7.slice(0, 2), 16);
10
10
  if (128 & highByte) return -negateAndMask(value);
11
11
  return value;
12
12
  }
13
13
  function bigIntToHex(value) {
14
14
  const positive = value >= common._0n;
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}`;
15
+ let hex7 = (positive ? value : negateAndMask(value)).toString(16);
16
+ if (hex7.length % 2) hex7 = `0${hex7}`;
17
+ if (positive && 128 & Number.parseInt(hex7.slice(0, 2), 16)) {
18
+ return `00${hex7}`;
19
19
  }
20
- return hex6;
20
+ return hex7;
21
21
  }
22
22
  function negateAndMask(value) {
23
23
  let val = value;
@@ -105,18 +105,45 @@ function estimateVLQSize(value) {
105
105
  } while (val > common._0n);
106
106
  return size;
107
107
  }
108
- function zigZagEncode(input) {
109
- return input << 1 ^ input >> 63;
110
- }
111
- function zigZagDecode(input) {
112
- return input >> 1 ^ -(input & 1);
113
- }
114
- function zigZagEncodeBigInt(input) {
115
- return input << common._1n ^ input >> common._63n;
116
- }
117
- function zigZagDecodeBigInt(input) {
118
- return input >> common._1n ^ -(input & common._1n);
119
- }
108
+ var _31n = BigInt(31);
109
+ var u64 = (v) => BigInt.asUintN(64, v);
110
+ var i64 = (v) => BigInt.asIntN(64, v);
111
+ var i32 = (v) => BigInt.asIntN(32, v);
112
+ var u32 = (v) => BigInt.asUintN(32, v);
113
+ var zigZag32 = {
114
+ encode: (input) => {
115
+ const v = i32(BigInt(input));
116
+ return u64(i32(v << common._1n) ^ i32(v >> _31n));
117
+ },
118
+ decode: (input) => {
119
+ const v = u32(input);
120
+ return Number(v >> common._1n ^ -(v & common._1n));
121
+ }
122
+ };
123
+ var zigZag64 = {
124
+ encode: (input) => {
125
+ return u64(input << common._1n ^ input >> common._63n);
126
+ },
127
+ decode: (input) => {
128
+ return i64(input >> common._1n ^ -(input & common._1n));
129
+ }
130
+ };
131
+
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
+ );
120
147
 
121
148
  // src/coders/sigmaByteReader.ts
122
149
  var SigmaByteReader = class {
@@ -129,7 +156,7 @@ var SigmaByteReader = class {
129
156
  this.#bytes = crypto.ensureBytes(bytes);
130
157
  this.#cursor = 0;
131
158
  }
132
- readBoolean() {
159
+ readBool() {
133
160
  return this.readByte() === 1;
134
161
  }
135
162
  readBits(length) {
@@ -155,16 +182,20 @@ var SigmaByteReader = class {
155
182
  readVlq() {
156
183
  return readVLQ(this);
157
184
  }
158
- readShort() {
159
- return Number(zigZagDecode(readVLQ(this)));
185
+ readI8() {
186
+ const byte = this.readByte();
187
+ return byte > MAX_I8 ? byte - (MAX_U8 + 1) : byte;
160
188
  }
161
- readInt() {
162
- return Number(this.readLong());
189
+ readI16() {
190
+ return zigZag32.decode(readBigVLQ(this));
163
191
  }
164
- readLong() {
165
- return zigZagDecodeBigInt(readBigVLQ(this));
192
+ readI32() {
193
+ return zigZag32.decode(readBigVLQ(this));
166
194
  }
167
- readBigInt() {
195
+ readI64() {
196
+ return zigZag64.decode(readBigVLQ(this));
197
+ }
198
+ readI256() {
168
199
  const len = readVLQ(this);
169
200
  return hexToBigInt(crypto.hex.encode(this.readBytes(len)));
170
201
  }
@@ -179,7 +210,7 @@ var SigmaByteWriter = class {
179
210
  this.#bytes = new Uint8Array(length);
180
211
  this.#cursor = 0;
181
212
  }
182
- writeBoolean(value) {
213
+ writeBool(value) {
183
214
  this.write(value === true ? 1 : 0);
184
215
  return this;
185
216
  }
@@ -189,17 +220,31 @@ var SigmaByteWriter = class {
189
220
  writeBigVLQ(value) {
190
221
  return writeBigVLQ(this, value);
191
222
  }
192
- writeShort(value) {
193
- this.writeVLQ(zigZagEncode(value));
223
+ writeI16(value) {
224
+ if (value < MIN_I16 || value > MAX_I16) {
225
+ throw new RangeError(`Value ${value} is out of range for a 16-bit integer`);
226
+ }
227
+ this.writeBigVLQ(zigZag32.encode(value));
194
228
  return this;
195
229
  }
196
- writeInt(value) {
197
- this.writeLong(BigInt(value));
198
- return this;
230
+ writeI32(value) {
231
+ if (value < MIN_I32 || value > MAX_I32) {
232
+ throw new RangeError(`Value ${value} is out of range for a 32-bit integer`);
233
+ }
234
+ return this.writeBigVLQ(zigZag32.encode(value));
199
235
  }
200
- writeLong(value) {
201
- this.writeBigVLQ(zigZagEncodeBigInt(value));
202
- return this;
236
+ writeI64(value) {
237
+ if (value < MIN_I64 || value > MAX_I64) {
238
+ throw new RangeError(`Value ${value} is out of range for a 64-bit integer`);
239
+ }
240
+ return this.writeBigVLQ(zigZag64.encode(value));
241
+ }
242
+ writeI256(value) {
243
+ if (value < MIN_I256 || value > MAX_I256) {
244
+ throw new RangeError(`Value ${value} is out of range for a 256-bit integer`);
245
+ }
246
+ const hex7 = bigIntToHex(value);
247
+ return this.writeVLQ(hex7.length / 2).writeHex(hex7);
203
248
  }
204
249
  write(byte) {
205
250
  this.#bytes[this.#cursor++] = byte;
@@ -229,10 +274,6 @@ var SigmaByteWriter = class {
229
274
  if (bitOffset > 0) this.#cursor++;
230
275
  return this;
231
276
  }
232
- writeBigInt(value) {
233
- const hex6 = bigIntToHex(value);
234
- return this.writeVLQ(hex6.length / 2).writeHex(hex6);
235
- }
236
277
  writeChecksum(length = 4, hashFn = crypto.blake2b256) {
237
278
  const hash = hashFn(this.toBytes());
238
279
  return this.writeBytes(length ? hash.subarray(0, length) : hash);
@@ -522,17 +563,17 @@ var dataSerializer = {
522
563
  if (type.embeddable) {
523
564
  switch (type.code) {
524
565
  case descriptors.bool.code:
525
- return writer.writeBoolean(data);
566
+ return writer.writeBool(data);
526
567
  case descriptors.byte.code:
527
568
  return writer.write(data);
528
569
  case descriptors.short.code:
529
- return writer.writeShort(data);
570
+ return writer.writeI16(data);
530
571
  case descriptors.int.code:
531
- return writer.writeInt(data);
572
+ return writer.writeI32(data);
532
573
  case descriptors.long.code:
533
- return writer.writeLong(data);
574
+ return writer.writeI64(data);
534
575
  case descriptors.bigInt.code:
535
- return writer.writeBigInt(data);
576
+ return writer.writeI256(data);
536
577
  case descriptors.groupElement.code:
537
578
  return writer.writeBytes(data);
538
579
  case descriptors.sigmaProp.code: {
@@ -590,17 +631,17 @@ var dataSerializer = {
590
631
  if (type.embeddable) {
591
632
  switch (type.code) {
592
633
  case descriptors.bool.code:
593
- return reader.readBoolean();
634
+ return reader.readBool();
594
635
  case descriptors.byte.code:
595
- return reader.readByte();
636
+ return reader.readI8();
596
637
  case descriptors.short.code:
597
- return reader.readShort();
638
+ return reader.readI16();
598
639
  case descriptors.int.code:
599
- return reader.readInt();
640
+ return reader.readI32();
600
641
  case descriptors.long.code:
601
- return reader.readLong();
642
+ return reader.readI64();
602
643
  case descriptors.bigInt.code:
603
- return reader.readBigInt();
644
+ return reader.readI256();
604
645
  case descriptors.groupElement.code:
605
646
  return reader.readBytes(GROUP_ELEMENT_LENGTH);
606
647
  case descriptors.sigmaProp.code: {
@@ -903,24 +944,42 @@ function serializeTransaction(transaction) {
903
944
  }
904
945
  function writeInput(writer, input) {
905
946
  writer.writeHex(input.boxId);
906
- writer.write(0);
947
+ if (isSignedInput(input)) {
948
+ writeProof(writer, input.spendingProof?.proofBytes);
949
+ writeExtension(writer, input.spendingProof?.extension);
950
+ return;
951
+ }
952
+ writeProof(writer, null);
907
953
  writeExtension(writer, input.extension);
908
954
  }
955
+ function isSignedInput(input) {
956
+ return input.spendingProof !== void 0;
957
+ }
958
+ function writeProof(writer, proof) {
959
+ if (!proof) {
960
+ writer.write(0);
961
+ return;
962
+ }
963
+ const bytes = crypto.hex.decode(proof);
964
+ writer.writeVLQ(bytes.length);
965
+ writer.writeBytes(bytes);
966
+ }
909
967
  function writeExtension(writer, extension) {
968
+ if (!extension) {
969
+ writer.write(0);
970
+ return;
971
+ }
910
972
  const keys = Object.keys(extension);
911
973
  let length = 0;
912
974
  for (const key of keys) {
913
- const ext = extension[key];
914
- if (common.isDefined(ext)) {
915
- length++;
916
- }
975
+ if (common.isDefined(extension[key])) length++;
917
976
  }
918
977
  writer.writeVLQ(length);
919
978
  if (length === 0) return;
920
979
  for (const key of keys) {
921
- const ext = extension[key];
922
- if (common.isDefined(ext)) {
923
- writer.writeVLQ(Number(key)).writeHex(ext);
980
+ const val = extension[key];
981
+ if (common.isDefined(val)) {
982
+ writer.writeVLQ(Number(key)).writeHex(val);
924
983
  }
925
984
  }
926
985
  }