@fleet-sdk/serializer 0.2.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.
@@ -0,0 +1,105 @@
1
+ import { SType } from "./base";
2
+ import { SCollType, STupleType } from "./generics";
3
+ import { SUnitType } from "./monomorphics";
4
+ import {
5
+ SBigIntType,
6
+ SBoolType,
7
+ SByteType,
8
+ SGroupElementType,
9
+ SIntType,
10
+ SLongType,
11
+ SShortType,
12
+ SSigmaPropType
13
+ } from "./primitives";
14
+
15
+ export const constructorCode = Object.freeze({
16
+ embeddable: 0,
17
+
18
+ simpleColl: 1,
19
+ nestedColl: 2,
20
+
21
+ option: 3,
22
+ optionCollection: 4,
23
+
24
+ pairOne: 5,
25
+ pairTwo: 6,
26
+ symmetricPair: 7,
27
+ genericTuple: 8
28
+ });
29
+
30
+ const MAX_PRIMITIVE_TYPE_CODE = 0x0b;
31
+ export const PRIMITIVE_TYPE_RANGE = MAX_PRIMITIVE_TYPE_CODE + 0x01;
32
+ const typeCodeOf = (constructor: number) => PRIMITIVE_TYPE_RANGE * constructor;
33
+
34
+ type Descriptor = { code: number; embeddable: boolean };
35
+
36
+ const collDescriptor = Object.freeze({
37
+ code: typeCodeOf(constructorCode.simpleColl),
38
+ embeddable: false,
39
+ simpleCollTypeCode: typeCodeOf(constructorCode.simpleColl),
40
+ nestedCollTypeCode: typeCodeOf(constructorCode.nestedColl)
41
+ }) satisfies Descriptor;
42
+
43
+ const tupleDescriptor = Object.freeze({
44
+ code: typeCodeOf(constructorCode.pairOne),
45
+ embeddable: false,
46
+ pairOneTypeCode: typeCodeOf(constructorCode.pairOne),
47
+ pairTwoTypeCode: typeCodeOf(constructorCode.pairTwo),
48
+ tripleTypeCode: typeCodeOf(constructorCode.pairTwo),
49
+ symmetricPairTypeCode: typeCodeOf(constructorCode.symmetricPair),
50
+ quadrupleTypeCode: typeCodeOf(constructorCode.symmetricPair),
51
+ genericTupleTypeCode: typeCodeOf(constructorCode.genericTuple)
52
+ }) satisfies Descriptor;
53
+
54
+ export const descriptors = {
55
+ bool: new SBoolType(),
56
+ byte: new SByteType(),
57
+ short: new SShortType(),
58
+ int: new SIntType(),
59
+ long: new SLongType(),
60
+ bigInt: new SBigIntType(),
61
+ groupElement: new SGroupElementType(),
62
+ sigmaProp: new SSigmaPropType(),
63
+ unit: new SUnitType(),
64
+ coll: collDescriptor,
65
+ tuple: tupleDescriptor
66
+ } satisfies { [key: string]: Descriptor };
67
+
68
+ export function isColl(type: SType): type is SCollType {
69
+ return (
70
+ type.code >= descriptors.coll.simpleCollTypeCode &&
71
+ type.code <= descriptors.coll.nestedCollTypeCode + MAX_PRIMITIVE_TYPE_CODE
72
+ );
73
+ }
74
+
75
+ export function isTuple(type: SType): type is STupleType {
76
+ return (
77
+ type.code >= descriptors.tuple.pairOneTypeCode &&
78
+ type.code <= descriptors.tuple.genericTupleTypeCode
79
+ );
80
+ }
81
+
82
+ export function getPrimitiveType(typeCode: number) {
83
+ switch (typeCode) {
84
+ case descriptors.bool.code:
85
+ return descriptors.bool;
86
+ case descriptors.byte.code:
87
+ return descriptors.byte;
88
+ case descriptors.short.code:
89
+ return descriptors.short;
90
+ case descriptors.int.code:
91
+ return descriptors.int;
92
+ case descriptors.long.code:
93
+ return descriptors.long;
94
+ case descriptors.bigInt.code:
95
+ return descriptors.bigInt;
96
+ case descriptors.groupElement.code:
97
+ return descriptors.groupElement;
98
+ case descriptors.sigmaProp.code:
99
+ return descriptors.sigmaProp;
100
+ default:
101
+ throw new Error(
102
+ `The type code '0x${typeCode.toString(16)}' is not a valid primitive type code.`
103
+ );
104
+ }
105
+ }
@@ -0,0 +1,42 @@
1
+ import { hex } from "@fleet-sdk/crypto";
2
+ import { SGenericType, SType } from "./base";
3
+ import { descriptors } from "./descriptors";
4
+
5
+ export class SCollType<T extends SType = SType> extends SGenericType<T> {
6
+ get code(): number {
7
+ return descriptors.coll.code;
8
+ }
9
+
10
+ override coerce<I, O>(elements: I[]): O[] | Uint8Array {
11
+ if (this.elementsType.code === descriptors.byte.code && !(elements instanceof Uint8Array)) {
12
+ return typeof elements === "string"
13
+ ? hex.decode(elements)
14
+ : Uint8Array.from(elements as ArrayLike<number>);
15
+ }
16
+
17
+ return elements.map((el) => this.elementsType.coerce(el)) as O[];
18
+ }
19
+
20
+ toString(): string {
21
+ return `SColl[${this.elementsType.toString()}]`;
22
+ }
23
+ }
24
+
25
+ export class STupleType<T extends SType[] = SType[]> extends SGenericType<T> {
26
+ get code(): number {
27
+ return descriptors.tuple.code;
28
+ }
29
+
30
+ override coerce<I, O>(elements: I[]): O[] {
31
+ const output = new Array(elements.length);
32
+ for (let i = 0; i < elements.length; i++) {
33
+ output[i] = this.elementsType[i].coerce(elements[i]);
34
+ }
35
+
36
+ return output;
37
+ }
38
+
39
+ toString(): string {
40
+ return `(${this.elementsType.map((el) => el.toString()).join(", ")})`;
41
+ }
42
+ }
@@ -0,0 +1,18 @@
1
+ export * from "./base";
2
+ export * from "./primitives";
3
+ export { isColl, isTuple } from "./descriptors";
4
+ export * from "./generics";
5
+ export * from "./monomorphics";
6
+ export {
7
+ SByte,
8
+ SBool,
9
+ SShort,
10
+ SInt,
11
+ SLong,
12
+ SBigInt,
13
+ SGroupElement,
14
+ SSigmaProp,
15
+ SUnit,
16
+ SColl,
17
+ SPair
18
+ } from "./constructors";
@@ -0,0 +1,11 @@
1
+ import { SMonomorphicType } from "./base";
2
+
3
+ export class SUnitType extends SMonomorphicType<undefined> {
4
+ get code(): 0x62 {
5
+ return 0x62;
6
+ }
7
+
8
+ toString(): string {
9
+ return "SUnit";
10
+ }
11
+ }
@@ -0,0 +1,97 @@
1
+ import { ensureBigInt } from "@fleet-sdk/common";
2
+ import { hex } from "@fleet-sdk/crypto";
3
+ import { SConstant } from "../sigmaConstant";
4
+ import { SPrimitiveType } from "./base";
5
+ import { BigIntInput, ByteInput } from "./constructors";
6
+
7
+ export class SBoolType extends SPrimitiveType<boolean> {
8
+ get code(): 0x01 {
9
+ return 0x01;
10
+ }
11
+
12
+ toString(): string {
13
+ return "SBool";
14
+ }
15
+ }
16
+
17
+ export class SByteType extends SPrimitiveType<number> {
18
+ get code(): 0x02 {
19
+ return 0x02;
20
+ }
21
+
22
+ toString(): string {
23
+ return "SByte";
24
+ }
25
+ }
26
+
27
+ export class SShortType extends SPrimitiveType<number> {
28
+ get code(): 0x03 {
29
+ return 0x03;
30
+ }
31
+
32
+ toString(): string {
33
+ return "SShort";
34
+ }
35
+ }
36
+
37
+ export class SIntType extends SPrimitiveType<number> {
38
+ get code(): 0x04 {
39
+ return 0x04;
40
+ }
41
+
42
+ toString(): string {
43
+ return "SInt";
44
+ }
45
+ }
46
+
47
+ export class SLongType extends SPrimitiveType<BigIntInput, bigint> {
48
+ get code(): 0x05 {
49
+ return 0x05;
50
+ }
51
+
52
+ override coerce(data: BigIntInput): bigint {
53
+ return ensureBigInt(data);
54
+ }
55
+
56
+ toString(): string {
57
+ return "SLong";
58
+ }
59
+ }
60
+
61
+ export class SBigIntType extends SPrimitiveType<string | bigint, bigint> {
62
+ get code(): number {
63
+ return 0x06;
64
+ }
65
+
66
+ override coerce(data: BigIntInput): bigint {
67
+ return ensureBigInt(data);
68
+ }
69
+
70
+ toString(): string {
71
+ return "SBigInt";
72
+ }
73
+ }
74
+
75
+ export class SGroupElementType extends SPrimitiveType<ByteInput, Uint8Array> {
76
+ get code(): 0x07 {
77
+ return 0x07;
78
+ }
79
+
80
+ override coerce(data: ByteInput): Uint8Array {
81
+ return typeof data === "string" ? hex.decode(data) : data;
82
+ }
83
+
84
+ toString(): string {
85
+ return "SGroupElement";
86
+ }
87
+ }
88
+
89
+ export class SSigmaPropType extends SPrimitiveType<SConstant<Uint8Array>> {
90
+ get code(): 0x08 {
91
+ return 0x08;
92
+ }
93
+
94
+ toString(): string {
95
+ return "SSigmaProp";
96
+ }
97
+ }