@fleet-sdk/serializer 0.8.3 → 0.9.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 CHANGED
@@ -1,5 +1,23 @@
1
1
  # @fleet-sdk/serializer
2
2
 
3
+ ## 0.9.0
4
+
5
+ ### Patch Changes
6
+
7
+ - c558227: Memoize `SConstant` bytes for efficient re-encoding
8
+ - ed7e44c: Add `SigmaByteReader#readRemainingBytes` method
9
+
10
+ ## 0.8.5
11
+
12
+ ### Patch Changes
13
+
14
+ - be6867a: Add `deserializeBox` function
15
+ - 3b2a774: Add `deserializeTransaction` function
16
+ - 2e627a5: Add `SBox` constant serialization and deserialization.
17
+ - Updated dependencies [07bafd5]
18
+ - @fleet-sdk/common@0.8.5
19
+ - @fleet-sdk/crypto@0.8.5
20
+
3
21
  ## 0.8.3
4
22
 
5
23
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,20 +1,33 @@
1
1
  import { ByteInput as ByteInput$1, blake2b256, Coder } from '@fleet-sdk/crypto';
2
- import { Box, Amount, BoxCandidate, UnsignedInput, DataInput, SignedTransaction } from '@fleet-sdk/common';
2
+ import { Box, Amount, BoxCandidate, UnsignedTransaction, SignedTransaction } from '@fleet-sdk/common';
3
3
 
4
4
  declare class SigmaByteReader {
5
5
  #private;
6
6
  get isEmpty(): boolean;
7
+ get bytes(): Uint8Array;
8
+ get cursor(): number;
7
9
  constructor(bytes: ByteInput$1);
10
+ readArray<T>(readFn: (reader: SigmaByteReader, index: number) => T): Array<T>;
8
11
  readBool(): boolean;
9
12
  readBits(length: number): ArrayLike<boolean>;
10
13
  readByte(): number;
11
14
  readBytes(length: number): Uint8Array;
12
- readVlq(): number;
15
+ readUInt(): number;
16
+ readBigUInt(): bigint;
13
17
  readI8(): number;
14
18
  readI16(): number;
15
19
  readI32(): number;
16
20
  readI64(): bigint;
17
21
  readI256(): bigint;
22
+ readRemainingBytes(): Uint8Array;
23
+ /**
24
+ * Returns bytes without advancing the cursor.
25
+ */
26
+ peek(count: number, offset?: number): Uint8Array;
27
+ /**
28
+ * Checks if the current position in the byte array starts with the given bytes.
29
+ */
30
+ match(bytes: Uint8Array, offset?: number): boolean;
18
31
  }
19
32
 
20
33
  declare class SigmaByteWriter {
@@ -22,8 +35,8 @@ declare class SigmaByteWriter {
22
35
  get length(): number;
23
36
  constructor(length: number);
24
37
  writeBool(value: boolean): SigmaByteWriter;
25
- writeVLQ(value: number): SigmaByteWriter;
26
- writeBigVLQ(value: bigint): SigmaByteWriter;
38
+ writeUInt(value: number): SigmaByteWriter;
39
+ writeBigUInt(value: bigint): SigmaByteWriter;
27
40
  writeI16(value: number): SigmaByteWriter;
28
41
  writeI32(value: number): SigmaByteWriter;
29
42
  writeI64(value: bigint): SigmaByteWriter;
@@ -33,6 +46,16 @@ declare class SigmaByteWriter {
33
46
  writeHex(bytesHex: string): SigmaByteWriter;
34
47
  writeBits(bits: ArrayLike<boolean>): SigmaByteWriter;
35
48
  writeChecksum(length?: number, hashFn?: typeof blake2b256): SigmaByteWriter;
49
+ /**
50
+ * Writes a length-delimited array of items to the byte stream using a provided
51
+ * serializer function.
52
+ *
53
+ * @typeParam T - The type of items in the array.
54
+ * @param items - The array of items to serialize and write.
55
+ * @param serializer - A function that serializes each item and writes it using the provided SigmaByteWriter.
56
+ * @returns The current instance of SigmaByteWriter for method chaining.
57
+ */
58
+ writeArray<T>(items: T[], serializer: (writer: SigmaByteWriter, item: T) => void): SigmaByteWriter;
36
59
  encode<T>(coder: Coder<Uint8Array, T>): T;
37
60
  toBytes(): Uint8Array;
38
61
  }
@@ -81,6 +104,10 @@ declare class SUnitType extends SMonomorphicType<undefined> {
81
104
  get code(): 0x62;
82
105
  toString(): string;
83
106
  }
107
+ declare class SBoxType extends SMonomorphicType<SConstant<Box<bigint>>> {
108
+ get code(): 0x63;
109
+ toString(): string;
110
+ }
84
111
 
85
112
  type BigIntInput = string | bigint;
86
113
  type ByteInput = Uint8Array | string;
@@ -97,8 +124,10 @@ declare const SLong: SProxy<SLongType, BigIntInput, bigint>;
97
124
  declare const SBigInt: SProxy<SBigIntType, BigIntInput, bigint>;
98
125
  declare const SGroupElement: SProxy<SGroupElementType, ByteInput, Uint8Array<ArrayBufferLike>>;
99
126
  declare const SSigmaProp: SProxy<SSigmaPropType, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>>;
100
- type SUnit = (value?: undefined) => SConstant<undefined, SUnitType>;
127
+ type SUnit = () => SConstant<undefined, SUnitType>;
101
128
  declare const SUnit: SUnit;
129
+ type SBox = (value?: Box) => SConstant<Box<bigint>, SBoxType>;
130
+ declare const SBox: SBox;
102
131
  type SColl = {
103
132
  <D, T extends SByteType>(type: SConstructor<D, T>, elements: ByteInput | D[]): SConstant<Uint8Array, T>;
104
133
  <D, T extends SByteType>(type: SConstructor<D, T>, elements: ByteInput[]): SConstant<Uint8Array[], T>;
@@ -155,9 +184,22 @@ declare function isTuple(type: SType): type is STupleType;
155
184
  declare class SConstant<D = unknown, T extends SType = SType> {
156
185
  #private;
157
186
  constructor(type: T, data: D);
158
- static from<D, T extends SType = SType>(bytes: ByteInput$1): SConstant<D, T>;
187
+ static from<D, T extends SType = SType>(bytes: ByteInput$1 | SigmaByteReader): SConstant<D, T>;
159
188
  get type(): T;
160
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
+ */
161
203
  toBytes(): Uint8Array;
162
204
  toHex(): string;
163
205
  }
@@ -185,21 +227,31 @@ declare function parse<T>(constant: ByteInput$1, mode: "strict"): T;
185
227
  /** @deprecated use `decode` instead */
186
228
  declare function parse<T>(constant: ByteInput$1 | undefined, mode: "safe"): T | undefined;
187
229
 
188
- declare function serializeBox(box: Box<Amount>): SigmaByteWriter;
189
- declare function serializeBox(box: Box<Amount>, writer: SigmaByteWriter): SigmaByteWriter;
190
- declare function serializeBox(box: BoxCandidate<Amount>, writer: SigmaByteWriter, distinctTokenIds: string[]): SigmaByteWriter;
230
+ declare function serializeBox(box: Box<Amount> | BoxCandidate<Amount>, writer?: SigmaByteWriter, distinctTokenIds?: string[]): SigmaByteWriter;
191
231
  /**
192
232
  * Estimates the byte size a box.
193
233
  * @returns byte size of the box.
194
234
  */
195
235
  declare function estimateBoxSize(box: Box<Amount> | BoxCandidate<Amount>, withValue?: Amount): number;
236
+ /**
237
+ * Deserializes a box embedded in a transaction.
238
+ *
239
+ * It efficiently calculates the box ID by accumulating the serialized data during
240
+ * deserialization and applying blake2b256 hashing, avoiding redundant serialization
241
+ * operations.
242
+ *
243
+ * @param reader - SigmaByteReader containing the serialized box data
244
+ * @param distinctTokenIds - Array of TokenIDs referenced in the parent transaction
245
+ * @param transactionId - ID of the transaction containing this box
246
+ * @param index - Index position of the box in the transaction outputs
247
+ * @returns A fully deserialized Box with all properties including boxId
248
+ */
249
+ declare function deserializeEmbeddedBox(reader: SigmaByteReader, distinctTokenIds: string[], transactionId: string, index: number): Box<bigint>;
250
+ declare function deserializeBox(input: ByteInput | SigmaByteReader): BoxCandidate<bigint> | Box<bigint>;
196
251
 
197
- type MinimalUnsignedTransaction = {
198
- inputs: UnsignedInput[];
199
- dataInputs: DataInput[];
200
- outputs: BoxCandidate<Amount>[];
201
- };
202
- declare function serializeTransaction(transaction: MinimalUnsignedTransaction | SignedTransaction): SigmaByteWriter;
252
+ type Transaction = UnsignedTransaction | SignedTransaction;
253
+ declare function serializeTransaction(transaction: Transaction): SigmaByteWriter;
254
+ declare function deserializeTransaction<T extends Transaction>(input: ByteInput): T;
203
255
 
204
256
  declare const dataSerializer: {
205
257
  serialize(data: unknown, type: SType, writer: SigmaByteWriter): SigmaByteWriter;
@@ -211,4 +263,4 @@ declare const typeSerializer: {
211
263
  deserialize(r: SigmaByteReader): SType;
212
264
  };
213
265
 
214
- export { type MinimalUnsignedTransaction, SBigInt, SBigIntType, SBool, SBoolType, SByte, SByteType, SColl, SCollType, SConstant, SGenericType, SGroupElement, SGroupElementType, SInt, SIntType, SLong, SLongType, SMonomorphicType, SPair, SPrimitiveType, SShort, SShortType, SSigmaProp, SSigmaPropType, STupleType, SType, SUnit, SUnitType, SigmaByteReader, SigmaByteWriter, dataSerializer, decode, estimateBoxSize, estimateVLQSize, isColl, isTuple, parse, serializeBox, serializeTransaction, stypeof, typeSerializer };
266
+ export { SBigInt, SBigIntType, SBool, SBoolType, SBox, SBoxType, SByte, SByteType, SColl, SCollType, SConstant, SGenericType, SGroupElement, SGroupElementType, SInt, SIntType, SLong, SLongType, SMonomorphicType, SPair, SPrimitiveType, SShort, SShortType, SSigmaProp, SSigmaPropType, STupleType, SType, SUnit, SUnitType, SigmaByteReader, SigmaByteWriter, dataSerializer, decode, deserializeBox, deserializeEmbeddedBox, deserializeTransaction, estimateBoxSize, estimateVLQSize, isColl, isTuple, parse, serializeBox, serializeTransaction, stypeof, typeSerializer };
package/dist/index.d.ts CHANGED
@@ -1,20 +1,33 @@
1
1
  import { ByteInput as ByteInput$1, blake2b256, Coder } from '@fleet-sdk/crypto';
2
- import { Box, Amount, BoxCandidate, UnsignedInput, DataInput, SignedTransaction } from '@fleet-sdk/common';
2
+ import { Box, Amount, BoxCandidate, UnsignedTransaction, SignedTransaction } from '@fleet-sdk/common';
3
3
 
4
4
  declare class SigmaByteReader {
5
5
  #private;
6
6
  get isEmpty(): boolean;
7
+ get bytes(): Uint8Array;
8
+ get cursor(): number;
7
9
  constructor(bytes: ByteInput$1);
10
+ readArray<T>(readFn: (reader: SigmaByteReader, index: number) => T): Array<T>;
8
11
  readBool(): boolean;
9
12
  readBits(length: number): ArrayLike<boolean>;
10
13
  readByte(): number;
11
14
  readBytes(length: number): Uint8Array;
12
- readVlq(): number;
15
+ readUInt(): number;
16
+ readBigUInt(): bigint;
13
17
  readI8(): number;
14
18
  readI16(): number;
15
19
  readI32(): number;
16
20
  readI64(): bigint;
17
21
  readI256(): bigint;
22
+ readRemainingBytes(): Uint8Array;
23
+ /**
24
+ * Returns bytes without advancing the cursor.
25
+ */
26
+ peek(count: number, offset?: number): Uint8Array;
27
+ /**
28
+ * Checks if the current position in the byte array starts with the given bytes.
29
+ */
30
+ match(bytes: Uint8Array, offset?: number): boolean;
18
31
  }
19
32
 
20
33
  declare class SigmaByteWriter {
@@ -22,8 +35,8 @@ declare class SigmaByteWriter {
22
35
  get length(): number;
23
36
  constructor(length: number);
24
37
  writeBool(value: boolean): SigmaByteWriter;
25
- writeVLQ(value: number): SigmaByteWriter;
26
- writeBigVLQ(value: bigint): SigmaByteWriter;
38
+ writeUInt(value: number): SigmaByteWriter;
39
+ writeBigUInt(value: bigint): SigmaByteWriter;
27
40
  writeI16(value: number): SigmaByteWriter;
28
41
  writeI32(value: number): SigmaByteWriter;
29
42
  writeI64(value: bigint): SigmaByteWriter;
@@ -33,6 +46,16 @@ declare class SigmaByteWriter {
33
46
  writeHex(bytesHex: string): SigmaByteWriter;
34
47
  writeBits(bits: ArrayLike<boolean>): SigmaByteWriter;
35
48
  writeChecksum(length?: number, hashFn?: typeof blake2b256): SigmaByteWriter;
49
+ /**
50
+ * Writes a length-delimited array of items to the byte stream using a provided
51
+ * serializer function.
52
+ *
53
+ * @typeParam T - The type of items in the array.
54
+ * @param items - The array of items to serialize and write.
55
+ * @param serializer - A function that serializes each item and writes it using the provided SigmaByteWriter.
56
+ * @returns The current instance of SigmaByteWriter for method chaining.
57
+ */
58
+ writeArray<T>(items: T[], serializer: (writer: SigmaByteWriter, item: T) => void): SigmaByteWriter;
36
59
  encode<T>(coder: Coder<Uint8Array, T>): T;
37
60
  toBytes(): Uint8Array;
38
61
  }
@@ -81,6 +104,10 @@ declare class SUnitType extends SMonomorphicType<undefined> {
81
104
  get code(): 0x62;
82
105
  toString(): string;
83
106
  }
107
+ declare class SBoxType extends SMonomorphicType<SConstant<Box<bigint>>> {
108
+ get code(): 0x63;
109
+ toString(): string;
110
+ }
84
111
 
85
112
  type BigIntInput = string | bigint;
86
113
  type ByteInput = Uint8Array | string;
@@ -97,8 +124,10 @@ declare const SLong: SProxy<SLongType, BigIntInput, bigint>;
97
124
  declare const SBigInt: SProxy<SBigIntType, BigIntInput, bigint>;
98
125
  declare const SGroupElement: SProxy<SGroupElementType, ByteInput, Uint8Array<ArrayBufferLike>>;
99
126
  declare const SSigmaProp: SProxy<SSigmaPropType, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>, SConstant<Uint8Array<ArrayBufferLike>, SType<unknown, unknown>>>;
100
- type SUnit = (value?: undefined) => SConstant<undefined, SUnitType>;
127
+ type SUnit = () => SConstant<undefined, SUnitType>;
101
128
  declare const SUnit: SUnit;
129
+ type SBox = (value?: Box) => SConstant<Box<bigint>, SBoxType>;
130
+ declare const SBox: SBox;
102
131
  type SColl = {
103
132
  <D, T extends SByteType>(type: SConstructor<D, T>, elements: ByteInput | D[]): SConstant<Uint8Array, T>;
104
133
  <D, T extends SByteType>(type: SConstructor<D, T>, elements: ByteInput[]): SConstant<Uint8Array[], T>;
@@ -155,9 +184,22 @@ declare function isTuple(type: SType): type is STupleType;
155
184
  declare class SConstant<D = unknown, T extends SType = SType> {
156
185
  #private;
157
186
  constructor(type: T, data: D);
158
- static from<D, T extends SType = SType>(bytes: ByteInput$1): SConstant<D, T>;
187
+ static from<D, T extends SType = SType>(bytes: ByteInput$1 | SigmaByteReader): SConstant<D, T>;
159
188
  get type(): T;
160
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
+ */
161
203
  toBytes(): Uint8Array;
162
204
  toHex(): string;
163
205
  }
@@ -185,21 +227,31 @@ declare function parse<T>(constant: ByteInput$1, mode: "strict"): T;
185
227
  /** @deprecated use `decode` instead */
186
228
  declare function parse<T>(constant: ByteInput$1 | undefined, mode: "safe"): T | undefined;
187
229
 
188
- declare function serializeBox(box: Box<Amount>): SigmaByteWriter;
189
- declare function serializeBox(box: Box<Amount>, writer: SigmaByteWriter): SigmaByteWriter;
190
- declare function serializeBox(box: BoxCandidate<Amount>, writer: SigmaByteWriter, distinctTokenIds: string[]): SigmaByteWriter;
230
+ declare function serializeBox(box: Box<Amount> | BoxCandidate<Amount>, writer?: SigmaByteWriter, distinctTokenIds?: string[]): SigmaByteWriter;
191
231
  /**
192
232
  * Estimates the byte size a box.
193
233
  * @returns byte size of the box.
194
234
  */
195
235
  declare function estimateBoxSize(box: Box<Amount> | BoxCandidate<Amount>, withValue?: Amount): number;
236
+ /**
237
+ * Deserializes a box embedded in a transaction.
238
+ *
239
+ * It efficiently calculates the box ID by accumulating the serialized data during
240
+ * deserialization and applying blake2b256 hashing, avoiding redundant serialization
241
+ * operations.
242
+ *
243
+ * @param reader - SigmaByteReader containing the serialized box data
244
+ * @param distinctTokenIds - Array of TokenIDs referenced in the parent transaction
245
+ * @param transactionId - ID of the transaction containing this box
246
+ * @param index - Index position of the box in the transaction outputs
247
+ * @returns A fully deserialized Box with all properties including boxId
248
+ */
249
+ declare function deserializeEmbeddedBox(reader: SigmaByteReader, distinctTokenIds: string[], transactionId: string, index: number): Box<bigint>;
250
+ declare function deserializeBox(input: ByteInput | SigmaByteReader): BoxCandidate<bigint> | Box<bigint>;
196
251
 
197
- type MinimalUnsignedTransaction = {
198
- inputs: UnsignedInput[];
199
- dataInputs: DataInput[];
200
- outputs: BoxCandidate<Amount>[];
201
- };
202
- declare function serializeTransaction(transaction: MinimalUnsignedTransaction | SignedTransaction): SigmaByteWriter;
252
+ type Transaction = UnsignedTransaction | SignedTransaction;
253
+ declare function serializeTransaction(transaction: Transaction): SigmaByteWriter;
254
+ declare function deserializeTransaction<T extends Transaction>(input: ByteInput): T;
203
255
 
204
256
  declare const dataSerializer: {
205
257
  serialize(data: unknown, type: SType, writer: SigmaByteWriter): SigmaByteWriter;
@@ -211,4 +263,4 @@ declare const typeSerializer: {
211
263
  deserialize(r: SigmaByteReader): SType;
212
264
  };
213
265
 
214
- export { type MinimalUnsignedTransaction, SBigInt, SBigIntType, SBool, SBoolType, SByte, SByteType, SColl, SCollType, SConstant, SGenericType, SGroupElement, SGroupElementType, SInt, SIntType, SLong, SLongType, SMonomorphicType, SPair, SPrimitiveType, SShort, SShortType, SSigmaProp, SSigmaPropType, STupleType, SType, SUnit, SUnitType, SigmaByteReader, SigmaByteWriter, dataSerializer, decode, estimateBoxSize, estimateVLQSize, isColl, isTuple, parse, serializeBox, serializeTransaction, stypeof, typeSerializer };
266
+ export { SBigInt, SBigIntType, SBool, SBoolType, SBox, SBoxType, SByte, SByteType, SColl, SCollType, SConstant, SGenericType, SGroupElement, SGroupElementType, SInt, SIntType, SLong, SLongType, SMonomorphicType, SPair, SPrimitiveType, SShort, SShortType, SSigmaProp, SSigmaPropType, STupleType, SType, SUnit, SUnitType, SigmaByteReader, SigmaByteWriter, dataSerializer, decode, deserializeBox, deserializeEmbeddedBox, deserializeTransaction, estimateBoxSize, estimateVLQSize, isColl, isTuple, parse, serializeBox, serializeTransaction, stypeof, typeSerializer };