@haneullabs/bcs 2.1.0 → 2.1.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 +34 -0
- package/dist/bcs-type.d.mts +15 -50
- package/dist/bcs-type.d.mts.map +1 -1
- package/dist/bcs-type.mjs +48 -20
- package/dist/bcs-type.mjs.map +1 -1
- package/dist/bcs.d.mts +3 -4
- package/dist/bcs.d.mts.map +1 -1
- package/dist/bcs.mjs +46 -16
- package/dist/bcs.mjs.map +1 -1
- package/dist/reader.d.mts +1 -0
- package/dist/reader.d.mts.map +1 -1
- package/dist/reader.mjs +25 -10
- package/dist/reader.mjs.map +1 -1
- package/dist/types.d.mts +4 -5
- package/dist/types.d.mts.map +1 -1
- package/dist/uleb.d.mts.map +1 -1
- package/dist/uleb.mjs +4 -1
- package/dist/uleb.mjs.map +1 -1
- package/dist/utils.d.mts +0 -1
- package/dist/utils.d.mts.map +1 -1
- package/dist/writer.d.mts +3 -6
- package/dist/writer.d.mts.map +1 -1
- package/dist/writer.mjs +26 -19
- package/dist/writer.mjs.map +1 -1
- package/package.json +7 -7
- package/src/bcs-type.ts +68 -35
- package/src/bcs.ts +64 -25
- package/src/reader.ts +39 -16
- package/src/uleb.ts +4 -0
- package/src/writer.ts +30 -25
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 2.1.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 3aa65e2: Speed up BCS parsing and serialization: faster ULEB, integer and byte reads and writes,
|
|
8
|
+
enum and option encoding, string decoding, and a single input conversion per write for transformed
|
|
9
|
+
types. Base58, base64 and hex helpers in `@mysten/utils` are faster and no longer depend on
|
|
10
|
+
`@scure/base`. Sui address and object digest encoding is faster.
|
|
11
|
+
|
|
12
|
+
Behaviour changes:
|
|
13
|
+
|
|
14
|
+
- `BcsReader.readBytes` and `readULEB` throw instead of reading past the end of a view into the
|
|
15
|
+
rest of its buffer.
|
|
16
|
+
- `bcs.option(T)` returns `null`/`undefined` when `T` reads as `null`/`undefined` (for example a
|
|
17
|
+
nested option's inner `None`), instead of `true`.
|
|
18
|
+
- `bcs.Address.serializedSize` and `bcs.ObjectDigest.serializedSize` throw on invalid input
|
|
19
|
+
instead of returning a size.
|
|
20
|
+
|
|
21
|
+
## 2.1.2
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- 30289c1: Reject non-canonical encodings when decoding: `bool` values other than `0` or `1`,
|
|
26
|
+
non-minimal ULEB128 encodings (used for lengths and enum variant indices) and strings that are not
|
|
27
|
+
valid UTF-8. Strings now also keep a leading byte order mark instead of silently dropping it
|
|
28
|
+
|
|
29
|
+
## 2.1.1
|
|
30
|
+
|
|
31
|
+
### Patch Changes
|
|
32
|
+
|
|
33
|
+
- f2f7048: Upgrade workspace dependencies, remove the legacy dapp-kit package, and migrate the
|
|
34
|
+
remaining consumers to the current gRPC-based dapp-kit. Remove the legacy API reference while
|
|
35
|
+
retaining the migration guide and deprecation notice.
|
|
36
|
+
|
|
3
37
|
## 2.1.0
|
|
4
38
|
|
|
5
39
|
### Minor Changes
|
package/dist/bcs-type.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { BcsReader } from "./reader.mjs";
|
|
2
2
|
import { EnumInputShape, EnumOutputShape, JoinString } from "./types.mjs";
|
|
3
3
|
import { BcsWriter, BcsWriterOptions } from "./writer.mjs";
|
|
4
|
-
|
|
5
4
|
//#region src/bcs-type.d.ts
|
|
6
5
|
interface BcsTypeOptions<T, Input = T, Name extends string = string> {
|
|
7
6
|
name?: Name;
|
|
@@ -29,12 +28,7 @@ declare class BcsType<T, Input = T, const Name extends string = string> {
|
|
|
29
28
|
fromHex(hex: string): T;
|
|
30
29
|
fromBase58(b64: string): T;
|
|
31
30
|
fromBase64(b64: string): T;
|
|
32
|
-
transform<T2 = T, Input2 = Input, NewName extends string = Name>({
|
|
33
|
-
name,
|
|
34
|
-
input,
|
|
35
|
-
output,
|
|
36
|
-
validate
|
|
37
|
-
}: {
|
|
31
|
+
transform<T2 = T, Input2 = Input, NewName extends string = Name>({ name, input, output, validate }: {
|
|
38
32
|
input?: (val: Input2) => Input;
|
|
39
33
|
output?: (value: T) => T2;
|
|
40
34
|
} & BcsTypeOptions<T2, Input2, NewName>): BcsType<T2, Input2, NewName>;
|
|
@@ -51,87 +45,58 @@ declare class SerializedBcs<T, Input = T> {
|
|
|
51
45
|
toBase58(): string;
|
|
52
46
|
parse(): T;
|
|
53
47
|
}
|
|
54
|
-
declare function fixedSizeBcsType<T, Input = T, const Name extends string = string>({
|
|
55
|
-
size,
|
|
56
|
-
...options
|
|
57
|
-
}: {
|
|
48
|
+
declare function fixedSizeBcsType<T, Input = T, const Name extends string = string>({ size, ...options }: {
|
|
58
49
|
name: Name;
|
|
59
50
|
size: number;
|
|
60
51
|
read: (reader: BcsReader) => T;
|
|
61
52
|
write: (value: Input, writer: BcsWriter) => void;
|
|
62
53
|
} & BcsTypeOptions<T, Input, Name>): BcsType<T, Input, Name>;
|
|
63
|
-
declare function uIntBcsType<const Name extends string = string>({
|
|
64
|
-
readMethod,
|
|
65
|
-
writeMethod,
|
|
66
|
-
...options
|
|
67
|
-
}: {
|
|
54
|
+
declare function uIntBcsType<const Name extends string = string>({ readMethod, writeMethod, ...options }: {
|
|
68
55
|
name: Name;
|
|
69
56
|
size: number;
|
|
70
57
|
readMethod: `read${8 | 16 | 32}`;
|
|
71
58
|
writeMethod: `write${8 | 16 | 32}`;
|
|
72
59
|
maxValue: number;
|
|
73
60
|
} & BcsTypeOptions<number, number, Name>): BcsType<number, number, Name>;
|
|
74
|
-
declare function bigUIntBcsType<const Name extends string = string>({
|
|
75
|
-
readMethod,
|
|
76
|
-
writeMethod,
|
|
77
|
-
...options
|
|
78
|
-
}: {
|
|
61
|
+
declare function bigUIntBcsType<const Name extends string = string>({ readMethod, writeMethod, ...options }: {
|
|
79
62
|
name: Name;
|
|
80
63
|
size: number;
|
|
81
64
|
readMethod: `read${64 | 128 | 256}`;
|
|
82
65
|
writeMethod: `write${64 | 128 | 256}`;
|
|
83
66
|
maxValue: bigint;
|
|
84
67
|
} & BcsTypeOptions<string, string | number | bigint>): BcsType<string, string | number | bigint, Name>;
|
|
85
|
-
declare function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({
|
|
86
|
-
serialize,
|
|
87
|
-
...options
|
|
88
|
-
}: {
|
|
68
|
+
declare function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({ serialize, ...options }: {
|
|
89
69
|
name: Name;
|
|
90
70
|
read: (reader: BcsReader) => T;
|
|
91
71
|
serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
|
|
92
72
|
} & BcsTypeOptions<T, Input>): BcsType<T, Input, string>;
|
|
93
|
-
declare function stringLikeBcsType<const Name extends string = string>({
|
|
94
|
-
toBytes,
|
|
95
|
-
fromBytes,
|
|
96
|
-
...options
|
|
97
|
-
}: {
|
|
73
|
+
declare function stringLikeBcsType<const Name extends string = string>({ toBytes, fromBytes, ...options }: {
|
|
98
74
|
name: Name;
|
|
99
75
|
toBytes: (value: string) => Uint8Array;
|
|
100
76
|
fromBytes: (bytes: Uint8Array) => string;
|
|
101
77
|
serializedSize?: (value: string) => number | null;
|
|
102
78
|
} & BcsTypeOptions<string, string, Name>): BcsType<string, string, Name>;
|
|
103
79
|
declare function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>): BcsType<T, Input, string>;
|
|
104
|
-
interface BcsStructOptions<T extends Record<string, BcsType<any>>, Name extends string = string> extends Omit<BcsTypeOptions<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never }, Name>, 'name'> {
|
|
80
|
+
interface BcsStructOptions<T extends Record<string, BcsType<any>>, Name extends string = string> extends Omit<BcsTypeOptions<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never; }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never; }, Name>, 'name'> {
|
|
105
81
|
name: Name;
|
|
106
82
|
fields: T;
|
|
107
83
|
}
|
|
108
|
-
declare class BcsStruct<T extends Record<string, BcsType<any>>, const Name extends string = string> extends BcsType<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never }, Name> {
|
|
109
|
-
constructor({
|
|
110
|
-
name,
|
|
111
|
-
fields,
|
|
112
|
-
...options
|
|
113
|
-
}: BcsStructOptions<T, Name>);
|
|
84
|
+
declare class BcsStruct<T extends Record<string, BcsType<any>>, const Name extends string = string> extends BcsType<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never; }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never; }, Name> {
|
|
85
|
+
constructor({ name, fields, ...options }: BcsStructOptions<T, Name>);
|
|
114
86
|
}
|
|
115
|
-
interface BcsEnumOptions<T extends Record<string, BcsType<any> | null>, Name extends string = string> extends Omit<BcsTypeOptions<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null }>, Name>, 'name'> {
|
|
87
|
+
interface BcsEnumOptions<T extends Record<string, BcsType<any> | null>, Name extends string = string> extends Omit<BcsTypeOptions<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true; }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null; }>, Name>, 'name'> {
|
|
116
88
|
name: Name;
|
|
117
89
|
fields: T;
|
|
118
90
|
}
|
|
119
|
-
declare class BcsEnum<T extends Record<string, BcsType<any> | null>, const Name extends string = string> extends BcsType<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null }>, Name> {
|
|
120
|
-
constructor({
|
|
121
|
-
fields,
|
|
122
|
-
...options
|
|
123
|
-
}: BcsEnumOptions<T, Name>);
|
|
91
|
+
declare class BcsEnum<T extends Record<string, BcsType<any> | null>, const Name extends string = string> extends BcsType<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true; }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null; }>, Name> {
|
|
92
|
+
constructor({ fields, ...options }: BcsEnumOptions<T, Name>);
|
|
124
93
|
}
|
|
125
|
-
interface BcsTupleOptions<T extends readonly BcsType<any>[], Name extends string> extends Omit<BcsTypeOptions<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never }, { [K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never }, Name>, 'name'> {
|
|
94
|
+
interface BcsTupleOptions<T extends readonly BcsType<any>[], Name extends string> extends Omit<BcsTypeOptions<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never; }, { [K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never; }, Name>, 'name'> {
|
|
126
95
|
name?: Name;
|
|
127
96
|
fields: T;
|
|
128
97
|
}
|
|
129
|
-
declare class BcsTuple<const T extends readonly BcsType<any>[], const Name extends string = `(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`> extends BcsType<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never }, { [K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never }, Name> {
|
|
130
|
-
constructor({
|
|
131
|
-
fields,
|
|
132
|
-
name,
|
|
133
|
-
...options
|
|
134
|
-
}: BcsTupleOptions<T, Name>);
|
|
98
|
+
declare class BcsTuple<const T extends readonly BcsType<any>[], const Name extends string = `(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never; }, ', '>})`> extends BcsType<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never; }, { [K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never; }, Name> {
|
|
99
|
+
constructor({ fields, name, ...options }: BcsTupleOptions<T, Name>);
|
|
135
100
|
}
|
|
136
101
|
//#endregion
|
|
137
102
|
export { BcsEnum, BcsEnumOptions, BcsStruct, BcsStructOptions, BcsTuple, BcsTupleOptions, BcsType, BcsTypeOptions, SerializedBcs, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, isSerializedBcs, lazyBcsType, stringLikeBcsType, uIntBcsType };
|
package/dist/bcs-type.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs-type.d.mts","names":[],"sources":["../src/bcs-type.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"bcs-type.d.mts","names":[],"sources":["../src/bcs-type.ts"],"mappings":";;;;UAUiB,eAAe,GAAG,QAAQ,GAAG;EAC7C,OAAO;EACP,YAAY,OAAO;;cAGP,QAAQ,GAAG,QAAQ,SAAS;;EACxC,YAAa;EACb,aAAc;EACd,MAAM;EACN,OAAO,QAAQ,cAAc;EAC7B,iBAAiB,OAAO,OAAO,UAAU;EACzC,WAAW,OAAO;EAUlB,YACC;IACC,MAAM;IACN,OAAO,QAAQ,cAAc;IAC7B,QAAQ,OAAO,OAAO,QAAQ;IAC9B,aAAa,OAAO,OAAO,UAAU,qBAAqB,WAAW;IACrE,kBAAkB,OAAO;IACzB,YAAY,OAAO;MAChB,eAAe,GAAG,OAAO;EAoB9B,MAAM,OAAO,OAAO,QAAQ;EAS5B,UAAU,OAAO,OAAO,UAAU,mBAAgB,cAAA,GAAA;EAQlD,MAAM,OAAO,aAAa;EAK1B,QAAQ,cAAW;EAInB,WAAW,cAAW;EAItB,WAAW,cAAW;EAItB,UAAU,KAAK,GAAG,SAAS,OAAO,yBAAyB,QAC1D,MACA,OACA,QACA;IAEA,SAAS,KAAK,WAAW;IACzB,UAAU,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AK,cAEtD;EAEA,cAAc,MAAM,WAAW,WAAW,iBAAiB,GAAG;;UA4C9C,eAChB,UAAU,eAAe,sBACzB,sCACS,KACT,eACC,mBACE,WAAW,IAAI,EAAE,WAAW,cAAc,eAAe,cAE3D,kBACE,WAAW,IAAI,EAAE,WAAW,mBAAmB,UAAU,iCAE3D;EAID,MAAM;EACN,QAAQ;;cAGI,QACZ,UAAU,eAAe,4BACnB,sCACG,QACT,mBACE,WAAW,IAAI,EAAE,WAAW,cAAc,UAAU,cAEtD,kBACE,WAAW,IAAI,EAAE,WAAW,mBAAmB,UAAU,iCAE3D;EAEA,cAAc,WAAW,WAAW,eAAe,GAAG;;UA8DtC,gBAChB,mBAAmB,gBACnB,6BACS,KACT,4BAEa,WAAW,IAAI,EAAE,WAAW,cAAc,UAAU,iBAG9D,WAAW,IAAI,EAAE,WAAW,mBAAmB,KAAK,cAEtD;EAID,OAAO;EACP,QAAQ;;cAGI,eACN,mBAAmB,sBACnB,0BACD,cAAc,WAAW,IAAI,EAAE,WAAW,wBAAwB,KAAK,gCACnE,qBAEG,WAAW,IAAI,EAAE,WAAW,cAAc,UAAU,iBAG9D,WAAW,IAAI,EAAE,WAAW,mBAAmB,KAAK,cAEtD;EAEA,cAAc,QAAQ,SAAS,WAAW,gBAAgB,GAAG"}
|
package/dist/bcs-type.mjs
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { ulebEncode } from "./uleb.mjs";
|
|
2
1
|
import { BcsReader } from "./reader.mjs";
|
|
2
|
+
import { ulebEncode } from "./uleb.mjs";
|
|
3
3
|
import { BcsWriter } from "./writer.mjs";
|
|
4
4
|
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
|
|
5
5
|
//#region src/bcs-type.ts
|
|
6
6
|
var BcsType = class BcsType {
|
|
7
7
|
#write;
|
|
8
8
|
#serialize;
|
|
9
|
+
#validatedWrite = null;
|
|
10
|
+
#validatedSerialize = null;
|
|
11
|
+
#transformValidate = null;
|
|
9
12
|
constructor(options) {
|
|
10
13
|
this.name = options.name;
|
|
11
14
|
this.read = options.read;
|
|
@@ -22,10 +25,15 @@ var BcsType = class BcsType {
|
|
|
22
25
|
this.validate = options.validate ?? (() => {});
|
|
23
26
|
}
|
|
24
27
|
write(value, writer) {
|
|
28
|
+
if (this.#validatedWrite && this.validate === this.#transformValidate) {
|
|
29
|
+
this.#validatedWrite(value, writer);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
25
32
|
this.validate(value);
|
|
26
33
|
this.#write(value, writer);
|
|
27
34
|
}
|
|
28
35
|
serialize(value, options) {
|
|
36
|
+
if (this.#validatedSerialize && this.validate === this.#transformValidate) return new SerializedBcs(this, this.#validatedSerialize(value, options));
|
|
29
37
|
this.validate(value);
|
|
30
38
|
return new SerializedBcs(this, this.#serialize(value, options));
|
|
31
39
|
}
|
|
@@ -43,7 +51,7 @@ var BcsType = class BcsType {
|
|
|
43
51
|
return this.parse(fromBase64(b64));
|
|
44
52
|
}
|
|
45
53
|
transform({ name, input, output, validate }) {
|
|
46
|
-
|
|
54
|
+
const type = new BcsType({
|
|
47
55
|
name: name ?? this.name,
|
|
48
56
|
read: (reader) => output ? output(this.read(reader)) : this.read(reader),
|
|
49
57
|
write: (value, writer) => this.#write(input ? input(value) : value, writer),
|
|
@@ -54,6 +62,20 @@ var BcsType = class BcsType {
|
|
|
54
62
|
this.validate(input ? input(value) : value);
|
|
55
63
|
}
|
|
56
64
|
});
|
|
65
|
+
type.#transformValidate = type.validate;
|
|
66
|
+
type.#validatedWrite = (value, writer) => {
|
|
67
|
+
validate?.(value);
|
|
68
|
+
const converted = input ? input(value) : value;
|
|
69
|
+
this.validate(converted);
|
|
70
|
+
this.#write(converted, writer);
|
|
71
|
+
};
|
|
72
|
+
type.#validatedSerialize = (value, options) => {
|
|
73
|
+
validate?.(value);
|
|
74
|
+
const converted = input ? input(value) : value;
|
|
75
|
+
this.validate(converted);
|
|
76
|
+
return this.#serialize(converted, options);
|
|
77
|
+
};
|
|
78
|
+
return type;
|
|
57
79
|
}
|
|
58
80
|
};
|
|
59
81
|
const SERIALIZED_BCS_BRAND = Symbol.for("@haneullabs/serialized-bcs");
|
|
@@ -135,7 +157,7 @@ function stringLikeBcsType({ toBytes, fromBytes, ...options }) {
|
|
|
135
157
|
write: (hex, writer) => {
|
|
136
158
|
const bytes = toBytes(hex);
|
|
137
159
|
writer.writeULEB(bytes.length);
|
|
138
|
-
|
|
160
|
+
writer.writeBytes(bytes);
|
|
139
161
|
},
|
|
140
162
|
serialize: (value) => {
|
|
141
163
|
const bytes = toBytes(value);
|
|
@@ -167,13 +189,15 @@ function lazyBcsType(cb) {
|
|
|
167
189
|
}
|
|
168
190
|
var BcsStruct = class extends BcsType {
|
|
169
191
|
constructor({ name, fields, ...options }) {
|
|
170
|
-
const
|
|
192
|
+
const fieldNames = Object.keys(fields);
|
|
193
|
+
const fieldTypes = fieldNames.map((field) => fields[field]);
|
|
194
|
+
const fieldCount = fieldNames.length;
|
|
171
195
|
super({
|
|
172
196
|
name,
|
|
173
197
|
serializedSize: (values) => {
|
|
174
198
|
let total = 0;
|
|
175
|
-
for (
|
|
176
|
-
const size =
|
|
199
|
+
for (let i = 0; i < fieldCount; i++) {
|
|
200
|
+
const size = fieldTypes[i].serializedSize(values[fieldNames[i]]);
|
|
177
201
|
if (size == null) return null;
|
|
178
202
|
total += size;
|
|
179
203
|
}
|
|
@@ -181,11 +205,11 @@ var BcsStruct = class extends BcsType {
|
|
|
181
205
|
},
|
|
182
206
|
read: (reader) => {
|
|
183
207
|
const result = {};
|
|
184
|
-
for (
|
|
208
|
+
for (let i = 0; i < fieldCount; i++) result[fieldNames[i]] = fieldTypes[i].read(reader);
|
|
185
209
|
return result;
|
|
186
210
|
},
|
|
187
211
|
write: (value, writer) => {
|
|
188
|
-
for (
|
|
212
|
+
for (let i = 0; i < fieldCount; i++) fieldTypes[i].write(value[fieldNames[i]], writer);
|
|
189
213
|
},
|
|
190
214
|
...options,
|
|
191
215
|
validate: (value) => {
|
|
@@ -198,24 +222,29 @@ var BcsStruct = class extends BcsType {
|
|
|
198
222
|
var BcsEnum = class extends BcsType {
|
|
199
223
|
constructor({ fields, ...options }) {
|
|
200
224
|
const canonicalOrder = Object.entries(fields);
|
|
225
|
+
const variantIndex = new Map(canonicalOrder.map(([name], i) => [name, i]));
|
|
201
226
|
super({
|
|
202
227
|
read: (reader) => {
|
|
203
228
|
const index = reader.readULEB();
|
|
204
229
|
const enumEntry = canonicalOrder[index];
|
|
205
230
|
if (!enumEntry) throw new TypeError(`Unknown value ${index} for enum ${options.name}`);
|
|
206
231
|
const [kind, type] = enumEntry;
|
|
207
|
-
|
|
208
|
-
|
|
232
|
+
const value = type?.read(reader) ?? true;
|
|
233
|
+
if (kind === "__proto__") return {
|
|
234
|
+
[kind]: value,
|
|
209
235
|
$kind: kind
|
|
210
236
|
};
|
|
237
|
+
const result = {};
|
|
238
|
+
result[kind] = value;
|
|
239
|
+
result.$kind = kind;
|
|
240
|
+
return result;
|
|
211
241
|
},
|
|
212
242
|
write: (value, writer) => {
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
writer
|
|
218
|
-
optionType?.write(val, writer);
|
|
243
|
+
for (const name of Object.keys(value)) {
|
|
244
|
+
const index = variantIndex.get(name);
|
|
245
|
+
if (index !== void 0) {
|
|
246
|
+
writer.writeULEB(index);
|
|
247
|
+
canonicalOrder[index][1]?.write(value[name], writer);
|
|
219
248
|
return;
|
|
220
249
|
}
|
|
221
250
|
}
|
|
@@ -224,10 +253,9 @@ var BcsEnum = class extends BcsType {
|
|
|
224
253
|
validate: (value) => {
|
|
225
254
|
options?.validate?.(value);
|
|
226
255
|
if (typeof value !== "object" || value == null) throw new TypeError(`Expected object, found ${typeof value}`);
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (!Object.hasOwn(fields, variant)) throw new TypeError(`Invalid enum variant ${variant}`);
|
|
256
|
+
let variantCount = 0;
|
|
257
|
+
for (const key of Object.keys(value)) if (value[key] !== void 0 && variantIndex.has(key)) variantCount++;
|
|
258
|
+
if (variantCount !== 1) throw new TypeError(`Expected object with one key, but found ${variantCount} for type ${options.name}}`);
|
|
231
259
|
}
|
|
232
260
|
});
|
|
233
261
|
}
|
package/dist/bcs-type.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs-type.mjs","names":["#write","#serialize","#schema","#bytes"],"sources":["../src/bcs-type.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromBase64, toBase58, toBase64, fromHex, toHex } from '@haneullabs/utils';\nimport { BcsReader } from './reader.js';\nimport { ulebEncode } from './uleb.js';\nimport type { BcsWriterOptions } from './writer.js';\nimport { BcsWriter } from './writer.js';\nimport type { EnumInputShape, EnumOutputShape, JoinString } from './types.js';\n\nexport interface BcsTypeOptions<T, Input = T, Name extends string = string> {\n\tname?: Name;\n\tvalidate?: (value: Input) => void;\n}\n\nexport class BcsType<T, Input = T, const Name extends string = string> {\n\t$inferType!: T;\n\t$inferInput!: Input;\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserializedSize: (value: Input, options?: BcsWriterOptions) => number | null;\n\tvalidate: (value: Input) => void;\n\t#write: (value: Input, writer: BcsWriter) => void;\n\t#serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\n\tconstructor(\n\t\toptions: {\n\t\t\tname: Name;\n\t\t\tread: (reader: BcsReader) => T;\n\t\t\twrite: (value: Input, writer: BcsWriter) => void;\n\t\t\tserialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\t\t\tserializedSize?: (value: Input) => number | null;\n\t\t\tvalidate?: (value: Input) => void;\n\t\t} & BcsTypeOptions<T, Input, Name>,\n\t) {\n\t\tthis.name = options.name;\n\t\tthis.read = options.read;\n\t\tthis.serializedSize = options.serializedSize ?? (() => null);\n\t\tthis.#write = options.write;\n\t\tthis.#serialize =\n\t\t\toptions.serialize ??\n\t\t\t((value, options) => {\n\t\t\t\tconst writer = new BcsWriter({\n\t\t\t\t\tinitialSize: this.serializedSize(value) ?? undefined,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t\tthis.#write(value, writer);\n\t\t\t\treturn writer.toBytes();\n\t\t\t});\n\n\t\tthis.validate = options.validate ?? (() => {});\n\t}\n\n\twrite(value: Input, writer: BcsWriter) {\n\t\tthis.validate(value);\n\t\tthis.#write(value, writer);\n\t}\n\n\tserialize(value: Input, options?: BcsWriterOptions) {\n\t\tthis.validate(value);\n\t\treturn new SerializedBcs(this, this.#serialize(value, options));\n\t}\n\n\tparse(bytes: Uint8Array): T {\n\t\tconst reader = new BcsReader(bytes);\n\t\treturn this.read(reader);\n\t}\n\n\tfromHex(hex: string) {\n\t\treturn this.parse(fromHex(hex));\n\t}\n\n\tfromBase58(b64: string) {\n\t\treturn this.parse(fromBase58(b64));\n\t}\n\n\tfromBase64(b64: string) {\n\t\treturn this.parse(fromBase64(b64));\n\t}\n\n\ttransform<T2 = T, Input2 = Input, NewName extends string = Name>({\n\t\tname,\n\t\tinput,\n\t\toutput,\n\t\tvalidate,\n\t}: {\n\t\tinput?: (val: Input2) => Input;\n\t\toutput?: (value: T) => T2;\n\t} & BcsTypeOptions<T2, Input2, NewName>) {\n\t\treturn new BcsType<T2, Input2, NewName>({\n\t\t\tname: (name ?? this.name) as NewName,\n\t\t\tread: (reader) => (output ? output(this.read(reader)) : (this.read(reader) as never)),\n\t\t\twrite: (value, writer) => this.#write(input ? input(value) : (value as never), writer),\n\t\t\tserializedSize: (value) => this.serializedSize(input ? input(value) : (value as never)),\n\t\t\tserialize: (value, options) =>\n\t\t\t\tthis.#serialize(input ? input(value) : (value as never), options),\n\t\t\tvalidate: (value) => {\n\t\t\t\tvalidate?.(value);\n\t\t\t\tthis.validate(input ? input(value) : (value as never));\n\t\t\t},\n\t\t});\n\t}\n}\n\nconst SERIALIZED_BCS_BRAND = Symbol.for('@haneullabs/serialized-bcs') as never;\nexport function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown> {\n\treturn !!obj && typeof obj === 'object' && (obj as any)[SERIALIZED_BCS_BRAND] === true;\n}\n\nexport class SerializedBcs<T, Input = T> {\n\t#schema: BcsType<T, Input>;\n\t#bytes: Uint8Array<ArrayBuffer>;\n\n\t// Used to brand SerializedBcs so that they can be identified, even between multiple copies\n\t// of the @haneullabs/bcs package are installed\n\tget [SERIALIZED_BCS_BRAND]() {\n\t\treturn true;\n\t}\n\n\tconstructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>) {\n\t\tthis.#schema = schema;\n\t\tthis.#bytes = bytes;\n\t}\n\n\ttoBytes() {\n\t\treturn this.#bytes;\n\t}\n\n\ttoHex() {\n\t\treturn toHex(this.#bytes);\n\t}\n\n\ttoBase64() {\n\t\treturn toBase64(this.#bytes);\n\t}\n\n\ttoBase58() {\n\t\treturn toBase58(this.#bytes);\n\t}\n\n\tparse() {\n\t\treturn this.#schema.parse(this.#bytes);\n\t}\n}\n\nexport function fixedSizeBcsType<T, Input = T, const Name extends string = string>({\n\tsize,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\tread: (reader: BcsReader) => T;\n\twrite: (value: Input, writer: BcsWriter) => void;\n} & BcsTypeOptions<T, Input, Name>) {\n\treturn new BcsType<T, Input, Name>({\n\t\t...options,\n\t\tserializedSize: () => size,\n\t});\n}\n\nexport function uIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${8 | 16 | 32}`;\n\twriteMethod: `write${8 | 16 | 32}`;\n\tmaxValue: number;\n} & BcsTypeOptions<number, number, Name>) {\n\treturn fixedSizeBcsType<number, number, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](value),\n\t\tvalidate: (value) => {\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function bigUIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${64 | 128 | 256}`;\n\twriteMethod: `write${64 | 128 | 256}`;\n\tmaxValue: bigint;\n} & BcsTypeOptions<string, string | number | bigint>) {\n\treturn fixedSizeBcsType<string, string | number | bigint, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](BigInt(value)),\n\t\tvalidate: (val) => {\n\t\t\tconst value = BigInt(val);\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({\n\tserialize,\n\t...options\n}: {\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n} & BcsTypeOptions<T, Input>) {\n\tconst type = new BcsType<T, Input>({\n\t\t...options,\n\t\tserialize,\n\t\twrite: (value, writer) => {\n\t\t\tfor (const byte of type.serialize(value).toBytes()) {\n\t\t\t\twriter.write8(byte);\n\t\t\t}\n\t\t},\n\t});\n\n\treturn type;\n}\n\nexport function stringLikeBcsType<const Name extends string = string>({\n\ttoBytes,\n\tfromBytes,\n\t...options\n}: {\n\tname: Name;\n\ttoBytes: (value: string) => Uint8Array;\n\tfromBytes: (bytes: Uint8Array) => string;\n\tserializedSize?: (value: string) => number | null;\n} & BcsTypeOptions<string, string, Name>) {\n\treturn new BcsType<string, string, Name>({\n\t\t...options,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst bytes = reader.readBytes(length);\n\n\t\t\treturn fromBytes(bytes);\n\t\t},\n\t\twrite: (hex, writer) => {\n\t\t\tconst bytes = toBytes(hex);\n\t\t\twriter.writeULEB(bytes.length);\n\t\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\t\twriter.write8(bytes[i]);\n\t\t\t}\n\t\t},\n\t\tserialize: (value) => {\n\t\t\tconst bytes = toBytes(value);\n\t\t\tconst size = ulebEncode(bytes.length);\n\t\t\tconst result = new Uint8Array(size.length + bytes.length);\n\t\t\tresult.set(size, 0);\n\t\t\tresult.set(bytes, size.length);\n\n\t\t\treturn result;\n\t\t},\n\t\tvalidate: (value) => {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>) {\n\tlet lazyType: BcsType<T, Input> | null = null;\n\tfunction getType() {\n\t\tif (!lazyType) {\n\t\t\tlazyType = cb();\n\t\t}\n\t\treturn lazyType;\n\t}\n\n\treturn new BcsType<T, Input>({\n\t\tname: 'lazy' as never,\n\t\tread: (data) => getType().read(data),\n\t\tserializedSize: (value) => getType().serializedSize(value),\n\t\twrite: (value, writer) => getType().write(value, writer),\n\t\tserialize: (value, options) => getType().serialize(value, options).toBytes(),\n\t});\n}\n\nexport interface BcsStructOptions<\n\tT extends Record<string, BcsType<any>>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsStruct<\n\tT extends Record<string, BcsType<any>>,\n\tconst Name extends string = string,\n> extends BcsType<\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t},\n\tName\n> {\n\tconstructor({ name, fields, ...options }: BcsStructOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields);\n\n\t\tsuper({\n\t\t\tname,\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\tconst size = type.serializedSize(values[field]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: Record<string, unknown> = {};\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\tresult[field] = type.read(reader);\n\t\t\t\t}\n\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\ttype.write(value[field], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsEnumOptions<\n\tT extends Record<string, BcsType<any> | null>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\tEnumOutputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t}>,\n\t\tEnumInputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t}>,\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsEnum<\n\tT extends Record<string, BcsType<any> | null>,\n\tconst Name extends string = string,\n> extends BcsType<\n\tEnumOutputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true;\n\t}>,\n\tEnumInputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t}>,\n\tName\n> {\n\tconstructor({ fields, ...options }: BcsEnumOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields as object);\n\t\tsuper({\n\t\t\tread: (reader) => {\n\t\t\t\tconst index = reader.readULEB();\n\n\t\t\t\tconst enumEntry = canonicalOrder[index];\n\t\t\t\tif (!enumEntry) {\n\t\t\t\t\tthrow new TypeError(`Unknown value ${index} for enum ${options.name}`);\n\t\t\t\t}\n\n\t\t\t\tconst [kind, type] = enumEntry;\n\n\t\t\t\treturn {\n\t\t\t\t\t[kind]: type?.read(reader) ?? true,\n\t\t\t\t\t$kind: kind,\n\t\t\t\t} as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tconst [name, val] = Object.entries(value).filter(([name]) =>\n\t\t\t\t\tObject.hasOwn(fields, name),\n\t\t\t\t)[0];\n\n\t\t\t\tfor (let i = 0; i < canonicalOrder.length; i++) {\n\t\t\t\t\tconst [optionName, optionType] = canonicalOrder[i];\n\t\t\t\t\tif (optionName === name) {\n\t\t\t\t\t\twriter.writeULEB(i);\n\t\t\t\t\t\toptionType?.write(val, writer);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\n\t\t\t\tconst keys = Object.keys(value).filter(\n\t\t\t\t\t(k) => value[k] !== undefined && Object.hasOwn(fields, k),\n\t\t\t\t);\n\n\t\t\t\tif (keys.length !== 1) {\n\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t`Expected object with one key, but found ${keys.length} for type ${options.name}}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst [variant] = keys;\n\n\t\t\t\tif (!Object.hasOwn(fields, variant)) {\n\t\t\t\t\tthrow new TypeError(`Invalid enum variant ${variant}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsTupleOptions<\n\tT extends readonly BcsType<any>[],\n\tName extends string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname?: Name;\n\tfields: T;\n}\n\nexport class BcsTuple<\n\tconst T extends readonly BcsType<any>[],\n\tconst Name extends string =\n\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n> extends BcsType<\n\t{\n\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t},\n\tName\n> {\n\tconstructor({ fields, name, ...options }: BcsTupleOptions<T, Name>) {\n\t\tsuper({\n\t\t\tname: name ?? (`(${fields.map((t) => t.name).join(', ')})` as never),\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tconst size = fields[i].serializedSize(values[i]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: unknown[] = [];\n\t\t\t\tfor (const field of fields) {\n\t\t\t\t\tresult.push(field.read(reader));\n\t\t\t\t}\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tfields[i].write(value[i], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t\tif (value.length !== fields.length) {\n\t\t\t\t\tthrow new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n"],"mappings":";;;;;AAeA,IAAa,UAAb,MAAa,QAA0D;CAOtE;CACA;CAEA,YACC,SAQC;EACD,KAAK,OAAO,QAAQ;EACpB,KAAK,OAAO,QAAQ;EACpB,KAAK,iBAAiB,QAAQ,yBAAyB;EACvD,KAAKA,SAAS,QAAQ;EACtB,KAAKC,aACJ,QAAQ,eACN,OAAO,YAAY;GACpB,MAAM,SAAS,IAAI,UAAU;IAC5B,aAAa,KAAK,eAAe,KAAK,KAAK,KAAA;IAC3C,GAAG;GACJ,CAAC;GACD,KAAKD,OAAO,OAAO,MAAM;GACzB,OAAO,OAAO,QAAQ;EACvB;EAED,KAAK,WAAW,QAAQ,mBAAmB,CAAC;CAC7C;CAEA,MAAM,OAAc,QAAmB;EACtC,KAAK,SAAS,KAAK;EACnB,KAAKA,OAAO,OAAO,MAAM;CAC1B;CAEA,UAAU,OAAc,SAA4B;EACnD,KAAK,SAAS,KAAK;EACnB,OAAO,IAAI,cAAc,MAAM,KAAKC,WAAW,OAAO,OAAO,CAAC;CAC/D;CAEA,MAAM,OAAsB;EAC3B,M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IACzB,IAAI,CAAC,MAAM,QAAQ,KAAK,GACvB,MAAM,IAAI,UAAU,yBAAyB,OAAO,OAAO;IAE5D,IAAI,MAAM,WAAW,OAAO,QAC3B,MAAM,IAAI,UAAU,4BAA4B,OAAO,OAAO,UAAU,MAAM,QAAQ;GAExF;EACD,CAAC;CACF;AACD"}
|
|
1
|
+
{"version":3,"file":"bcs-type.mjs","names":["#write","#serialize","#validatedWrite","#transformValidate","#validatedSerialize","#schema","#bytes"],"sources":["../src/bcs-type.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromBase64, toBase58, toBase64, fromHex, toHex } from '@haneullabs/utils';\nimport { BcsReader } from './reader.js';\nimport { ulebEncode } from './uleb.js';\nimport type { BcsWriterOptions } from './writer.js';\nimport { BcsWriter } from './writer.js';\nimport type { EnumInputShape, EnumOutputShape, JoinString } from './types.js';\n\nexport interface BcsTypeOptions<T, Input = T, Name extends string = string> {\n\tname?: Name;\n\tvalidate?: (value: Input) => void;\n}\n\nexport class BcsType<T, Input = T, const Name extends string = string> {\n\t$inferType!: T;\n\t$inferInput!: Input;\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserializedSize: (value: Input, options?: BcsWriterOptions) => number | null;\n\tvalidate: (value: Input) => void;\n\t#write: (value: Input, writer: BcsWriter) => void;\n\t#serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\t// Set by transform: validates and writes while converting the input only once. Only used\n\t// while validate is still the one transform created, so a reassigned validate is honoured.\n\t#validatedWrite: ((value: Input, writer: BcsWriter) => void) | null = null;\n\t#validatedSerialize:\n\t\t((value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>) | null = null;\n\t#transformValidate: ((value: Input) => void) | null = null;\n\n\tconstructor(\n\t\toptions: {\n\t\t\tname: Name;\n\t\t\tread: (reader: BcsReader) => T;\n\t\t\twrite: (value: Input, writer: BcsWriter) => void;\n\t\t\tserialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\t\t\tserializedSize?: (value: Input) => number | null;\n\t\t\tvalidate?: (value: Input) => void;\n\t\t} & BcsTypeOptions<T, Input, Name>,\n\t) {\n\t\tthis.name = options.name;\n\t\tthis.read = options.read;\n\t\tthis.serializedSize = options.serializedSize ?? (() => null);\n\t\tthis.#write = options.write;\n\t\tthis.#serialize =\n\t\t\toptions.serialize ??\n\t\t\t((value, options) => {\n\t\t\t\tconst writer = new BcsWriter({\n\t\t\t\t\tinitialSize: this.serializedSize(value) ?? undefined,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t\tthis.#write(value, writer);\n\t\t\t\treturn writer.toBytes();\n\t\t\t});\n\n\t\tthis.validate = options.validate ?? (() => {});\n\t}\n\n\twrite(value: Input, writer: BcsWriter) {\n\t\tif (this.#validatedWrite && this.validate === this.#transformValidate) {\n\t\t\tthis.#validatedWrite(value, writer);\n\t\t\treturn;\n\t\t}\n\t\tthis.validate(value);\n\t\tthis.#write(value, writer);\n\t}\n\n\tserialize(value: Input, options?: BcsWriterOptions) {\n\t\tif (this.#validatedSerialize && this.validate === this.#transformValidate) {\n\t\t\treturn new SerializedBcs(this, this.#validatedSerialize(value, options));\n\t\t}\n\t\tthis.validate(value);\n\t\treturn new SerializedBcs(this, this.#serialize(value, options));\n\t}\n\n\tparse(bytes: Uint8Array): T {\n\t\tconst reader = new BcsReader(bytes);\n\t\treturn this.read(reader);\n\t}\n\n\tfromHex(hex: string) {\n\t\treturn this.parse(fromHex(hex));\n\t}\n\n\tfromBase58(b64: string) {\n\t\treturn this.parse(fromBase58(b64));\n\t}\n\n\tfromBase64(b64: string) {\n\t\treturn this.parse(fromBase64(b64));\n\t}\n\n\ttransform<T2 = T, Input2 = Input, NewName extends string = Name>({\n\t\tname,\n\t\tinput,\n\t\toutput,\n\t\tvalidate,\n\t}: {\n\t\tinput?: (val: Input2) => Input;\n\t\toutput?: (value: T) => T2;\n\t} & BcsTypeOptions<T2, Input2, NewName>) {\n\t\tconst type = new BcsType<T2, Input2, NewName>({\n\t\t\tname: (name ?? this.name) as NewName,\n\t\t\tread: (reader) => (output ? output(this.read(reader)) : (this.read(reader) as never)),\n\t\t\twrite: (value, writer) => this.#write(input ? input(value) : (value as never), writer),\n\t\t\tserializedSize: (value) => this.serializedSize(input ? input(value) : (value as never)),\n\t\t\tserialize: (value, options) =>\n\t\t\t\tthis.#serialize(input ? input(value) : (value as never), options),\n\t\t\tvalidate: (value) => {\n\t\t\t\tvalidate?.(value);\n\t\t\t\tthis.validate(input ? input(value) : (value as never));\n\t\t\t},\n\t\t});\n\n\t\t// Equivalent to validate() followed by the write above, but calls input() once instead of twice\n\t\ttype.#transformValidate = type.validate;\n\t\ttype.#validatedWrite = (value, writer) => {\n\t\t\tvalidate?.(value);\n\t\t\tconst converted = input ? input(value) : (value as never);\n\t\t\tthis.validate(converted);\n\t\t\tthis.#write(converted, writer);\n\t\t};\n\t\ttype.#validatedSerialize = (value, options) => {\n\t\t\tvalidate?.(value);\n\t\t\tconst converted = input ? input(value) : (value as never);\n\t\t\tthis.validate(converted);\n\t\t\treturn this.#serialize(converted, options);\n\t\t};\n\n\t\treturn type;\n\t}\n}\n\nconst SERIALIZED_BCS_BRAND = Symbol.for('@haneullabs/serialized-bcs') as never;\nexport function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown> {\n\treturn !!obj && typeof obj === 'object' && (obj as any)[SERIALIZED_BCS_BRAND] === true;\n}\n\nexport class SerializedBcs<T, Input = T> {\n\t#schema: BcsType<T, Input>;\n\t#bytes: Uint8Array<ArrayBuffer>;\n\n\t// Used to brand SerializedBcs so that they can be identified, even between multiple copies\n\t// of the @haneullabs/bcs package are installed\n\tget [SERIALIZED_BCS_BRAND]() {\n\t\treturn true;\n\t}\n\n\tconstructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>) {\n\t\tthis.#schema = schema;\n\t\tthis.#bytes = bytes;\n\t}\n\n\ttoBytes() {\n\t\treturn this.#bytes;\n\t}\n\n\ttoHex() {\n\t\treturn toHex(this.#bytes);\n\t}\n\n\ttoBase64() {\n\t\treturn toBase64(this.#bytes);\n\t}\n\n\ttoBase58() {\n\t\treturn toBase58(this.#bytes);\n\t}\n\n\tparse() {\n\t\treturn this.#schema.parse(this.#bytes);\n\t}\n}\n\nexport function fixedSizeBcsType<T, Input = T, const Name extends string = string>({\n\tsize,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\tread: (reader: BcsReader) => T;\n\twrite: (value: Input, writer: BcsWriter) => void;\n} & BcsTypeOptions<T, Input, Name>) {\n\treturn new BcsType<T, Input, Name>({\n\t\t...options,\n\t\tserializedSize: () => size,\n\t});\n}\n\nexport function uIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${8 | 16 | 32}`;\n\twriteMethod: `write${8 | 16 | 32}`;\n\tmaxValue: number;\n} & BcsTypeOptions<number, number, Name>) {\n\treturn fixedSizeBcsType<number, number, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](value),\n\t\tvalidate: (value) => {\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function bigUIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${64 | 128 | 256}`;\n\twriteMethod: `write${64 | 128 | 256}`;\n\tmaxValue: bigint;\n} & BcsTypeOptions<string, string | number | bigint>) {\n\treturn fixedSizeBcsType<string, string | number | bigint, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](BigInt(value)),\n\t\tvalidate: (val) => {\n\t\t\tconst value = BigInt(val);\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({\n\tserialize,\n\t...options\n}: {\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n} & BcsTypeOptions<T, Input>) {\n\tconst type = new BcsType<T, Input>({\n\t\t...options,\n\t\tserialize,\n\t\twrite: (value, writer) => {\n\t\t\tfor (const byte of type.serialize(value).toBytes()) {\n\t\t\t\twriter.write8(byte);\n\t\t\t}\n\t\t},\n\t});\n\n\treturn type;\n}\n\nexport function stringLikeBcsType<const Name extends string = string>({\n\ttoBytes,\n\tfromBytes,\n\t...options\n}: {\n\tname: Name;\n\ttoBytes: (value: string) => Uint8Array;\n\tfromBytes: (bytes: Uint8Array) => string;\n\tserializedSize?: (value: string) => number | null;\n} & BcsTypeOptions<string, string, Name>) {\n\treturn new BcsType<string, string, Name>({\n\t\t...options,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst bytes = reader.readBytes(length);\n\n\t\t\treturn fromBytes(bytes);\n\t\t},\n\t\twrite: (hex, writer) => {\n\t\t\tconst bytes = toBytes(hex);\n\t\t\twriter.writeULEB(bytes.length);\n\t\t\twriter.writeBytes(bytes);\n\t\t},\n\t\tserialize: (value) => {\n\t\t\tconst bytes = toBytes(value);\n\t\t\tconst size = ulebEncode(bytes.length);\n\t\t\tconst result = new Uint8Array(size.length + bytes.length);\n\t\t\tresult.set(size, 0);\n\t\t\tresult.set(bytes, size.length);\n\n\t\t\treturn result;\n\t\t},\n\t\tvalidate: (value) => {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>) {\n\tlet lazyType: BcsType<T, Input> | null = null;\n\tfunction getType() {\n\t\tif (!lazyType) {\n\t\t\tlazyType = cb();\n\t\t}\n\t\treturn lazyType;\n\t}\n\n\treturn new BcsType<T, Input>({\n\t\tname: 'lazy' as never,\n\t\tread: (data) => getType().read(data),\n\t\tserializedSize: (value) => getType().serializedSize(value),\n\t\twrite: (value, writer) => getType().write(value, writer),\n\t\tserialize: (value, options) => getType().serialize(value, options).toBytes(),\n\t});\n}\n\nexport interface BcsStructOptions<\n\tT extends Record<string, BcsType<any>>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsStruct<\n\tT extends Record<string, BcsType<any>>,\n\tconst Name extends string = string,\n> extends BcsType<\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t},\n\tName\n> {\n\tconstructor({ name, fields, ...options }: BcsStructOptions<T, Name>) {\n\t\tconst fieldNames = Object.keys(fields);\n\t\tconst fieldTypes = fieldNames.map((field) => fields[field]);\n\t\tconst fieldCount = fieldNames.length;\n\n\t\tsuper({\n\t\t\tname,\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (let i = 0; i < fieldCount; i++) {\n\t\t\t\t\tconst size = fieldTypes[i].serializedSize(values[fieldNames[i]]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: Record<string, unknown> = {};\n\t\t\t\tfor (let i = 0; i < fieldCount; i++) {\n\t\t\t\t\tresult[fieldNames[i]] = fieldTypes[i].read(reader);\n\t\t\t\t}\n\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (let i = 0; i < fieldCount; i++) {\n\t\t\t\t\tfieldTypes[i].write(value[fieldNames[i]], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsEnumOptions<\n\tT extends Record<string, BcsType<any> | null>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\tEnumOutputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t}>,\n\t\tEnumInputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t}>,\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsEnum<\n\tT extends Record<string, BcsType<any> | null>,\n\tconst Name extends string = string,\n> extends BcsType<\n\tEnumOutputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true;\n\t}>,\n\tEnumInputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t}>,\n\tName\n> {\n\tconstructor({ fields, ...options }: BcsEnumOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields as object);\n\t\tconst variantIndex = new Map(canonicalOrder.map(([name], i) => [name, i]));\n\t\tsuper({\n\t\t\tread: (reader) => {\n\t\t\t\tconst index = reader.readULEB();\n\n\t\t\t\tconst enumEntry = canonicalOrder[index];\n\t\t\t\tif (!enumEntry) {\n\t\t\t\t\tthrow new TypeError(`Unknown value ${index} for enum ${options.name}`);\n\t\t\t\t}\n\n\t\t\t\tconst [kind, type] = enumEntry;\n\n\t\t\t\tconst value = type?.read(reader) ?? true;\n\t\t\t\t// Assigning to __proto__ would set the prototype rather than add a property\n\t\t\t\tif (kind === '__proto__') {\n\t\t\t\t\treturn { [kind]: value, $kind: kind } as never;\n\t\t\t\t}\n\n\t\t\t\t// Avoid a computed-key object literal, which V8 builds through a slow runtime path\n\t\t\t\tconst result: Record<string, unknown> = {};\n\t\t\t\tresult[kind] = value;\n\t\t\t\tresult.$kind = kind;\n\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\t// The first own key that names a variant selects it\n\t\t\t\tfor (const name of Object.keys(value)) {\n\t\t\t\t\tconst index = variantIndex.get(name);\n\t\t\t\t\tif (index !== undefined) {\n\t\t\t\t\t\twriter.writeULEB(index);\n\t\t\t\t\t\tcanonicalOrder[index][1]?.write(value[name], writer);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\n\t\t\t\tlet variantCount = 0;\n\t\t\t\tfor (const key of Object.keys(value)) {\n\t\t\t\t\tif (value[key] !== undefined && variantIndex.has(key)) {\n\t\t\t\t\t\tvariantCount++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (variantCount !== 1) {\n\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t`Expected object with one key, but found ${variantCount} for type ${options.name}}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsTupleOptions<\n\tT extends readonly BcsType<any>[],\n\tName extends string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname?: Name;\n\tfields: T;\n}\n\nexport class BcsTuple<\n\tconst T extends readonly BcsType<any>[],\n\tconst Name extends string =\n\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n> extends BcsType<\n\t{\n\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t},\n\tName\n> {\n\tconstructor({ fields, name, ...options }: BcsTupleOptions<T, Name>) {\n\t\tsuper({\n\t\t\tname: name ?? (`(${fields.map((t) => t.name).join(', ')})` as never),\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tconst size = fields[i].serializedSize(values[i]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: unknown[] = [];\n\t\t\t\tfor (const field of fields) {\n\t\t\t\t\tresult.push(field.read(reader));\n\t\t\t\t}\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tfields[i].write(value[i], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t\tif (value.length !== fields.length) {\n\t\t\t\t\tthrow new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n"],"mappings":";;;;;AAeA,IAAa,UAAb,MAAa,QAA0D;CAOtE;CACA;CAGA,kBAAsE;CACtE,sBACkF;CAClF,qBAAsD;CAEtD,YACC,SAQC;EACD,KAAK,OAAO,QAAQ;EACpB,KAAK,OAAO,QAAQ;EACpB,KAAK,iBAAiB,QAAQ,yBAAyB;EACvD,KAAKA,SAAS,QAAQ;EACtB,KAAKC,aACJ,QAAQ,eACN,OAAO,YAAY;GACpB,MAAM,SAAS,IAAI,UAAU;IAC5B,aAAa,KAAK,eAAe,KAAK,KAAK,KAAA;IAC3C,GAAG;GACJ,CAAC;GACD,KAAKD,OAAO,OAAO,MAAM;GACzB,OAAO,OAAO,QAAQ;EACvB;EAED,KAAK,WAAW,QAAQ,mBAAmB,CAAC;CAC7C;CAEA,MAAM,OAAc,QAAmB;EACtC,IAAI,KAAKE,mBAAmB,KAAK,aAAa,KAAKC,oBAAoB;GACtE,KAAKD,gBAAgB,OAAO,MAAM;GAClC;EACD;EACA,KAAK,SAAS,KAAK;EACnB,KAAKF,OAAO,OAAO,MAAM;CAC1B;CAEA,UAAU,OAAc,SAA4B;EACnD,IAAI,KAAKI,uBAAuB,KAAK,aAAa,KAAKD,oBACtD,OAAO,IAAI,cAAc,MAAM,KAAKC,oBAAoB,OAAO,OAAO,CAAC;EAExE,KAAK,SAAS,KAAK;EACnB,OAAO,IAAI,cAAc,MAAM,KAAKH,WAAW,OAAO,OAAO,CAAC;CAC/D;CAEA,MAAM,OAAsB;EAC3B,MAAM,SAAS,IAAI,UAAU,KAAK;EAClC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OAAO,OAAO,EAAE,CAAC,eAAe,OAAO,EAAE;KAC/C,IAAI,QAAQ,MACX,OAAO;KAGR,SAAS;IACV;IAEA,OAAO;GACR;GACA,OAAO,WAAW;IACjB,MAAM,SAAoB,CAAC;IAC3B,KAAK,MAAM,SAAS,QACnB,OAAO,KAAK,MAAM,KAAK,MAAM,CAAC;IAE/B,OAAO;GACR;GACA,QAAQ,OAAO,WAAW;IACzB,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAClC,OAAO,EAAE,CAAC,MAAM,MAAM,IAAI,MAAM;GAElC;GACA,GAAG;GACH,WAAW,UAAU;IACpB,SAAS,WAAW,KAAK;IACzB,IAAI,CAAC,MAAM,QAAQ,KAAK,GACvB,MAAM,IAAI,UAAU,yBAAyB,OAAO,OAAO;IAE5D,IAAI,MAAM,WAAW,OAAO,QAC3B,MAAM,IAAI,UAAU,4BAA4B,OAAO,OAAO,UAAU,MAAM,QAAQ;GAExF;EACD,CAAC;CACF;AACD"}
|
package/dist/bcs.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { EnumInputShape, EnumOutputShape, InferBcsInput, InferBcsType, JoinString } from "./types.mjs";
|
|
2
2
|
import { BcsEnum, BcsStruct, BcsTuple, BcsType, BcsTypeOptions } from "./bcs-type.mjs";
|
|
3
|
-
|
|
4
3
|
//#region src/bcs.d.ts
|
|
5
4
|
declare function fixedArray<T extends BcsType<any>, Name extends string = string>(size: number, type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
|
|
6
5
|
length: number;
|
|
@@ -133,7 +132,7 @@ declare const bcs: {
|
|
|
133
132
|
* const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
|
|
134
133
|
* tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
|
|
135
134
|
*/
|
|
136
|
-
tuple<const T extends readonly BcsType<any, any>[], const Name extends string = `(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T_1 extends string> ? T_1 : never }, ", ">})`>(fields: T, options?: BcsTypeOptions<{ -readonly [
|
|
135
|
+
tuple<const T extends readonly BcsType<any, any>[], const Name extends string = `(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T_1 extends string> ? T_1 : never; }, ", ">})`>(fields: T, options?: BcsTypeOptions<{ -readonly [K in keyof T]: T[K] extends BcsType<infer T_1, any> ? T_1 : never; }, { [K in keyof T]: T[K] extends BcsType<any, infer T_1> ? T_1 : never; }, Name>): BcsTuple<T, Name>;
|
|
137
136
|
/**
|
|
138
137
|
* Creates a BcsType representing a struct of a given set of fields
|
|
139
138
|
* @param name The name of the struct
|
|
@@ -146,7 +145,7 @@ declare const bcs: {
|
|
|
146
145
|
* })
|
|
147
146
|
* struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
148
147
|
*/
|
|
149
|
-
struct<T extends Record<string, BcsType<any>>, const Name extends string = string>(name: Name, fields: T, options?: Omit<BcsTypeOptions<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never }>,
|
|
148
|
+
struct<T extends Record<string, BcsType<any>>, const Name extends string = string>(name: Name, fields: T, options?: Omit<BcsTypeOptions<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never; }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never; }>, 'name'>): BcsStruct<T, string>;
|
|
150
149
|
/**
|
|
151
150
|
* Creates a BcsType representing an enum of a given set of options
|
|
152
151
|
* @param name The name of the enum
|
|
@@ -163,7 +162,7 @@ declare const bcs: {
|
|
|
163
162
|
* enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
164
163
|
* enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
|
|
165
164
|
*/
|
|
166
|
-
enum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(name: Name, fields: T, options?: Omit<BcsTypeOptions<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null }>, Name>,
|
|
165
|
+
enum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(name: Name, fields: T, options?: Omit<BcsTypeOptions<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true; }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null; }>, Name>, 'name'>): BcsEnum<T, Name>;
|
|
167
166
|
/**
|
|
168
167
|
* Creates a BcsType representing a map of a given key and value type
|
|
169
168
|
* @param keyType The BcsType of the key
|
package/dist/bcs.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../src/bcs.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../src/bcs.ts"],"mappings":";;;iBAyBS,WAAW,UAAU,cAAc,8BAC3C,cACA,MAAM,GACN,UAAU,eACT,aAAa,MACb,SAAS,cAAc;EAAQ;GAC/B,QAEC,QAAQ,aAAa,MAAM,SAAS,cAAc;EAAQ;GAAkB;iBACtE,WAAW,GAAG,OAAO,8BAC7B,cACA,MAAM,QAAQ,GAAG,QACjB,UAAU,eAAe,KAAK,SAAS;EAAW;GAAkB,QAClE,QAAQ,KAAK,SAAS;EAAW;GAAkB;iBAqC7C,OAAO,UAAU,cACzB,MAAM,IACJ,QAAQ,aAAa,WAAW,cAAc,iCAAiC;iBACzE,OAAO,GAAG,OAAO,8BACzB,MAAM,QAAQ,GAAG,OAAO,QACtB,QAAQ,UAAU;iBAkCZ,OAAO,UAAU,cAAc,gCAAgC,cACvE,MAAM,GACN,UAAU,eACT,aAAa,MACb,SAAS,cAAc;EAAQ;GAC/B,QAEC,QAAQ,aAAa,MAAM,SAAS,cAAc;EA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