@haneullabs/bcs 0.1.0 → 2.0.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.
Files changed (88) hide show
  1. package/CHANGELOG.md +43 -9
  2. package/dist/bcs-type.d.mts +138 -0
  3. package/dist/bcs-type.d.mts.map +1 -0
  4. package/dist/bcs-type.mjs +269 -0
  5. package/dist/bcs-type.mjs.map +1 -0
  6. package/dist/bcs.d.mts +184 -0
  7. package/dist/bcs.d.mts.map +1 -0
  8. package/dist/bcs.mjs +250 -0
  9. package/dist/bcs.mjs.map +1 -0
  10. package/dist/index.d.mts +8 -0
  11. package/dist/index.mjs +8 -0
  12. package/dist/reader.d.mts +96 -0
  13. package/dist/reader.d.mts.map +1 -0
  14. package/dist/reader.mjs +142 -0
  15. package/dist/reader.mjs.map +1 -0
  16. package/dist/types.d.mts +20 -0
  17. package/dist/types.d.mts.map +1 -0
  18. package/dist/types.mjs +1 -0
  19. package/dist/uleb.d.mts +9 -0
  20. package/dist/uleb.d.mts.map +1 -0
  21. package/dist/uleb.mjs +36 -0
  22. package/dist/uleb.mjs.map +1 -0
  23. package/dist/utils.d.mts +23 -0
  24. package/dist/utils.d.mts.map +1 -0
  25. package/dist/utils.mjs +56 -0
  26. package/dist/utils.mjs.map +1 -0
  27. package/dist/writer.d.mts +126 -0
  28. package/dist/writer.d.mts.map +1 -0
  29. package/dist/writer.mjs +179 -0
  30. package/dist/writer.mjs.map +1 -0
  31. package/docs/index.md +352 -0
  32. package/docs/llms-index.md +6 -0
  33. package/package.json +21 -25
  34. package/src/bcs.ts +32 -7
  35. package/src/index.ts +21 -34
  36. package/src/uleb.ts +0 -1
  37. package/dist/cjs/bcs-type.d.ts +0 -127
  38. package/dist/cjs/bcs-type.js +0 -386
  39. package/dist/cjs/bcs-type.js.map +0 -7
  40. package/dist/cjs/bcs.d.ts +0 -175
  41. package/dist/cjs/bcs.js +0 -406
  42. package/dist/cjs/bcs.js.map +0 -7
  43. package/dist/cjs/index.d.ts +0 -22
  44. package/dist/cjs/index.js +0 -59
  45. package/dist/cjs/index.js.map +0 -7
  46. package/dist/cjs/package.json +0 -5
  47. package/dist/cjs/reader.d.ts +0 -92
  48. package/dist/cjs/reader.js +0 -136
  49. package/dist/cjs/reader.js.map +0 -7
  50. package/dist/cjs/types.d.ts +0 -28
  51. package/dist/cjs/types.js +0 -17
  52. package/dist/cjs/types.js.map +0 -7
  53. package/dist/cjs/uleb.d.ts +0 -5
  54. package/dist/cjs/uleb.js +0 -66
  55. package/dist/cjs/uleb.js.map +0 -7
  56. package/dist/cjs/utils.d.ts +0 -18
  57. package/dist/cjs/utils.js +0 -74
  58. package/dist/cjs/utils.js.map +0 -7
  59. package/dist/cjs/writer.d.ts +0 -117
  60. package/dist/cjs/writer.js +0 -196
  61. package/dist/cjs/writer.js.map +0 -7
  62. package/dist/esm/bcs-type.d.ts +0 -127
  63. package/dist/esm/bcs-type.js +0 -366
  64. package/dist/esm/bcs-type.js.map +0 -7
  65. package/dist/esm/bcs.d.ts +0 -175
  66. package/dist/esm/bcs.js +0 -397
  67. package/dist/esm/bcs.js.map +0 -7
  68. package/dist/esm/index.d.ts +0 -22
  69. package/dist/esm/index.js +0 -46
  70. package/dist/esm/index.js.map +0 -7
  71. package/dist/esm/package.json +0 -5
  72. package/dist/esm/reader.d.ts +0 -92
  73. package/dist/esm/reader.js +0 -116
  74. package/dist/esm/reader.js.map +0 -7
  75. package/dist/esm/types.d.ts +0 -28
  76. package/dist/esm/types.js +0 -1
  77. package/dist/esm/types.js.map +0 -7
  78. package/dist/esm/uleb.d.ts +0 -5
  79. package/dist/esm/uleb.js +0 -46
  80. package/dist/esm/uleb.js.map +0 -7
  81. package/dist/esm/utils.d.ts +0 -18
  82. package/dist/esm/utils.js +0 -54
  83. package/dist/esm/utils.js.map +0 -7
  84. package/dist/esm/writer.d.ts +0 -117
  85. package/dist/esm/writer.js +0 -176
  86. package/dist/esm/writer.js.map +0 -7
  87. package/dist/tsconfig.esm.tsbuildinfo +0 -1
  88. package/dist/tsconfig.tsbuildinfo +0 -1
package/dist/cjs/bcs.d.ts DELETED
@@ -1,175 +0,0 @@
1
- import type { BcsTypeOptions } from './bcs-type.js';
2
- import { BcsEnum, BcsStruct, BcsTuple, BcsType } from './bcs-type.js';
3
- import type { EnumInputShape, EnumOutputShape, InferBcsInput, InferBcsType, JoinString } from './types.js';
4
- declare function fixedArray<T extends BcsType<any>, Name extends string = string>(size: number, type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
5
- length: number;
6
- }, Name>): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
7
- length: number;
8
- }, Name>;
9
- declare function fixedArray<T, Input, Name extends string = string>(size: number, type: BcsType<T, Input>, options?: BcsTypeOptions<T[], Iterable<Input> & {
10
- length: number;
11
- }, Name>): BcsType<T[], Iterable<Input> & {
12
- length: number;
13
- }, Name>;
14
- declare function option<T extends BcsType<any>>(type: T): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;
15
- declare function option<T, Input, Name extends string = string>(type: BcsType<T, Input, Name>): BcsType<T | null, Input | null | undefined>;
16
- declare function vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
17
- length: number;
18
- }, Name>): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
19
- length: number;
20
- }, Name>;
21
- declare function vector<T, Input, Name extends string = string>(type: BcsType<T, Input, Name>, options?: BcsTypeOptions<T[], Iterable<Input> & {
22
- length: number;
23
- }, `vector<${Name}>`>): BcsType<T[], Iterable<Input> & {
24
- length: number;
25
- }, `vector<${Name}>`>;
26
- declare function map<K extends BcsType<any>, V extends BcsType<any>>(keyType: K, valueType: V): BcsType<Map<InferBcsType<K>, InferBcsType<V>>, Map<InferBcsInput<K>, InferBcsInput<V>>, `Map<${K['name']}, ${V['name']}>`>;
27
- declare function map<K, V, InputK = K, InputV = V>(keyType: BcsType<K, InputK>, valueType: BcsType<V, InputV>): BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;
28
- export declare const bcs: {
29
- /**
30
- * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
31
- * @example
32
- * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
33
- */
34
- u8(options?: BcsTypeOptions<number>): BcsType<number, number, "u8">;
35
- /**
36
- * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
37
- * @example
38
- * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
39
- */
40
- u16(options?: BcsTypeOptions<number>): BcsType<number, number, "u16">;
41
- /**
42
- * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
43
- * @example
44
- * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
45
- */
46
- u32(options?: BcsTypeOptions<number>): BcsType<number, number, "u32">;
47
- /**
48
- * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
49
- * @example
50
- * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
51
- */
52
- u64(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u64">;
53
- /**
54
- * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
55
- * @example
56
- * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
57
- */
58
- u128(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u128">;
59
- /**
60
- * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
61
- * @example
62
- * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
63
- */
64
- u256(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u256">;
65
- /**
66
- * Creates a BcsType that can be used to read and write boolean values.
67
- * @example
68
- * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
69
- */
70
- bool(options?: BcsTypeOptions<boolean>): BcsType<boolean, boolean, "bool">;
71
- /**
72
- * Creates a BcsType that can be used to read and write unsigned LEB encoded integers
73
- * @example
74
- *
75
- */
76
- uleb128(options?: BcsTypeOptions<number>): BcsType<number, number, string>;
77
- /**
78
- * Creates a BcsType representing a fixed length byte array
79
- * @param size The number of bytes this types represents
80
- * @example
81
- * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
82
- */
83
- bytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, `bytes[${T}]`>;
84
- /**
85
- * Creates a BcsType representing a variable length byte array
86
- *
87
- * @example
88
- * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
89
- */
90
- byteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
91
- /**
92
- * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
93
- * @example
94
- * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
95
- */
96
- string(options?: BcsTypeOptions<string>): BcsType<string, string, "string">;
97
- /**
98
- * Creates a BcsType that represents a fixed length array of a given type
99
- * @param size The number of elements in the array
100
- * @param type The BcsType of each element in the array
101
- * @example
102
- * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
103
- */
104
- fixedArray: typeof fixedArray;
105
- /**
106
- * Creates a BcsType representing an optional value
107
- * @param type The BcsType of the optional value
108
- * @example
109
- * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
110
- * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
111
- */
112
- option: typeof option;
113
- /**
114
- * Creates a BcsType representing a variable length vector of a given type
115
- * @param type The BcsType of each element in the vector
116
- *
117
- * @example
118
- * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
119
- */
120
- vector: typeof vector;
121
- /**
122
- * Creates a BcsType representing a tuple of a given set of types
123
- * @param types The BcsTypes for each element in the tuple
124
- *
125
- * @example
126
- * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
127
- * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
128
- */
129
- 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_1 in keyof T]: T[K_1] extends BcsType<infer T_1, any> ? T_1 : never; }, { [K_1 in keyof T]: T[K_1] extends BcsType<any, infer T_1> ? T_1 : never; }, Name>): BcsTuple<T, Name>;
130
- /**
131
- * Creates a BcsType representing a struct of a given set of fields
132
- * @param name The name of the struct
133
- * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
134
- *
135
- * @example
136
- * const struct = bcs.struct('MyStruct', {
137
- * a: bcs.u8(),
138
- * b: bcs.string(),
139
- * })
140
- * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
141
- */
142
- 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>;
143
- /**
144
- * Creates a BcsType representing an enum of a given set of options
145
- * @param name The name of the enum
146
- * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
147
- * null can be used to represent a variant with no data.
148
- *
149
- * @example
150
- * const enum = bcs.enum('MyEnum', {
151
- * A: bcs.u8(),
152
- * B: bcs.string(),
153
- * C: null,
154
- * })
155
- * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
156
- * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
157
- * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
158
- */
159
- 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>;
160
- /**
161
- * Creates a BcsType representing a map of a given key and value type
162
- * @param keyType The BcsType of the key
163
- * @param valueType The BcsType of the value
164
- * @example
165
- * const map = bcs.map(bcs.u8(), bcs.string())
166
- * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
167
- */
168
- map: typeof map;
169
- /**
170
- * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
171
- * @param cb A callback that returns the BcsType
172
- */
173
- lazy<T extends BcsType<any>>(cb: () => T): T;
174
- };
175
- export {};
package/dist/cjs/bcs.js DELETED
@@ -1,406 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var bcs_exports = {};
20
- __export(bcs_exports, {
21
- bcs: () => bcs
22
- });
23
- module.exports = __toCommonJS(bcs_exports);
24
- var import_bcs_type = require("./bcs-type.js");
25
- var import_uleb = require("./uleb.js");
26
- function fixedArray(size, type, options) {
27
- return new import_bcs_type.BcsType({
28
- read: (reader) => {
29
- const result = new Array(size);
30
- for (let i = 0; i < size; i++) {
31
- result[i] = type.read(reader);
32
- }
33
- return result;
34
- },
35
- write: (value, writer) => {
36
- for (const item of value) {
37
- type.write(item, writer);
38
- }
39
- },
40
- ...options,
41
- name: options?.name ?? `${type.name}[${size}]`,
42
- validate: (value) => {
43
- options?.validate?.(value);
44
- if (!value || typeof value !== "object" || !("length" in value)) {
45
- throw new TypeError(`Expected array, found ${typeof value}`);
46
- }
47
- if (value.length !== size) {
48
- throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
49
- }
50
- }
51
- });
52
- }
53
- function option(type) {
54
- return bcs.enum(`Option<${type.name}>`, {
55
- None: null,
56
- Some: type
57
- }).transform({
58
- input: (value) => {
59
- if (value == null) {
60
- return { None: true };
61
- }
62
- return { Some: value };
63
- },
64
- output: (value) => {
65
- if (value.$kind === "Some") {
66
- return value.Some;
67
- }
68
- return null;
69
- }
70
- });
71
- }
72
- function vector(type, options) {
73
- return new import_bcs_type.BcsType({
74
- read: (reader) => {
75
- const length = reader.readULEB();
76
- const result = new Array(length);
77
- for (let i = 0; i < length; i++) {
78
- result[i] = type.read(reader);
79
- }
80
- return result;
81
- },
82
- write: (value, writer) => {
83
- writer.writeULEB(value.length);
84
- for (const item of value) {
85
- type.write(item, writer);
86
- }
87
- },
88
- ...options,
89
- name: options?.name ?? `vector<${type.name}>`,
90
- validate: (value) => {
91
- options?.validate?.(value);
92
- if (!value || typeof value !== "object" || !("length" in value)) {
93
- throw new TypeError(`Expected array, found ${typeof value}`);
94
- }
95
- }
96
- });
97
- }
98
- function map(keyType, valueType) {
99
- return bcs.vector(bcs.tuple([keyType, valueType])).transform({
100
- name: `Map<${keyType.name}, ${valueType.name}>`,
101
- input: (value) => {
102
- return [...value.entries()];
103
- },
104
- output: (value) => {
105
- const result = /* @__PURE__ */ new Map();
106
- for (const [key, val] of value) {
107
- result.set(key, val);
108
- }
109
- return result;
110
- }
111
- });
112
- }
113
- const bcs = {
114
- /**
115
- * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
116
- * @example
117
- * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
118
- */
119
- u8(options) {
120
- return (0, import_bcs_type.uIntBcsType)({
121
- readMethod: "read8",
122
- writeMethod: "write8",
123
- size: 1,
124
- maxValue: 2 ** 8 - 1,
125
- ...options,
126
- name: options?.name ?? "u8"
127
- });
128
- },
129
- /**
130
- * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
131
- * @example
132
- * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
133
- */
134
- u16(options) {
135
- return (0, import_bcs_type.uIntBcsType)({
136
- readMethod: "read16",
137
- writeMethod: "write16",
138
- size: 2,
139
- maxValue: 2 ** 16 - 1,
140
- ...options,
141
- name: options?.name ?? "u16"
142
- });
143
- },
144
- /**
145
- * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
146
- * @example
147
- * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
148
- */
149
- u32(options) {
150
- return (0, import_bcs_type.uIntBcsType)({
151
- readMethod: "read32",
152
- writeMethod: "write32",
153
- size: 4,
154
- maxValue: 2 ** 32 - 1,
155
- ...options,
156
- name: options?.name ?? "u32"
157
- });
158
- },
159
- /**
160
- * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
161
- * @example
162
- * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
163
- */
164
- u64(options) {
165
- return (0, import_bcs_type.bigUIntBcsType)({
166
- readMethod: "read64",
167
- writeMethod: "write64",
168
- size: 8,
169
- maxValue: 2n ** 64n - 1n,
170
- ...options,
171
- name: options?.name ?? "u64"
172
- });
173
- },
174
- /**
175
- * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
176
- * @example
177
- * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
178
- */
179
- u128(options) {
180
- return (0, import_bcs_type.bigUIntBcsType)({
181
- readMethod: "read128",
182
- writeMethod: "write128",
183
- size: 16,
184
- maxValue: 2n ** 128n - 1n,
185
- ...options,
186
- name: options?.name ?? "u128"
187
- });
188
- },
189
- /**
190
- * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
191
- * @example
192
- * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
193
- */
194
- u256(options) {
195
- return (0, import_bcs_type.bigUIntBcsType)({
196
- readMethod: "read256",
197
- writeMethod: "write256",
198
- size: 32,
199
- maxValue: 2n ** 256n - 1n,
200
- ...options,
201
- name: options?.name ?? "u256"
202
- });
203
- },
204
- /**
205
- * Creates a BcsType that can be used to read and write boolean values.
206
- * @example
207
- * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
208
- */
209
- bool(options) {
210
- return (0, import_bcs_type.fixedSizeBcsType)({
211
- size: 1,
212
- read: (reader) => reader.read8() === 1,
213
- write: (value, writer) => writer.write8(value ? 1 : 0),
214
- ...options,
215
- name: options?.name ?? "bool",
216
- validate: (value) => {
217
- options?.validate?.(value);
218
- if (typeof value !== "boolean") {
219
- throw new TypeError(`Expected boolean, found ${typeof value}`);
220
- }
221
- }
222
- });
223
- },
224
- /**
225
- * Creates a BcsType that can be used to read and write unsigned LEB encoded integers
226
- * @example
227
- *
228
- */
229
- uleb128(options) {
230
- return (0, import_bcs_type.dynamicSizeBcsType)({
231
- read: (reader) => reader.readULEB(),
232
- serialize: (value) => {
233
- return Uint8Array.from((0, import_uleb.ulebEncode)(value));
234
- },
235
- ...options,
236
- name: options?.name ?? "uleb128"
237
- });
238
- },
239
- /**
240
- * Creates a BcsType representing a fixed length byte array
241
- * @param size The number of bytes this types represents
242
- * @example
243
- * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
244
- */
245
- bytes(size, options) {
246
- return (0, import_bcs_type.fixedSizeBcsType)({
247
- size,
248
- read: (reader) => reader.readBytes(size),
249
- write: (value, writer) => {
250
- writer.writeBytes(new Uint8Array(value));
251
- },
252
- ...options,
253
- name: options?.name ?? `bytes[${size}]`,
254
- validate: (value) => {
255
- options?.validate?.(value);
256
- if (!value || typeof value !== "object" || !("length" in value)) {
257
- throw new TypeError(`Expected array, found ${typeof value}`);
258
- }
259
- if (value.length !== size) {
260
- throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
261
- }
262
- }
263
- });
264
- },
265
- /**
266
- * Creates a BcsType representing a variable length byte array
267
- *
268
- * @example
269
- * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
270
- */
271
- byteVector(options) {
272
- return new import_bcs_type.BcsType({
273
- read: (reader) => {
274
- const length = reader.readULEB();
275
- return reader.readBytes(length);
276
- },
277
- write: (value, writer) => {
278
- const array = new Uint8Array(value);
279
- writer.writeULEB(array.length);
280
- writer.writeBytes(array);
281
- },
282
- ...options,
283
- name: options?.name ?? "vector<u8>",
284
- serializedSize: (value) => {
285
- const length = "length" in value ? value.length : null;
286
- return length == null ? null : (0, import_uleb.ulebEncode)(length).length + length;
287
- },
288
- validate: (value) => {
289
- options?.validate?.(value);
290
- if (!value || typeof value !== "object" || !("length" in value)) {
291
- throw new TypeError(`Expected array, found ${typeof value}`);
292
- }
293
- }
294
- });
295
- },
296
- /**
297
- * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
298
- * @example
299
- * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
300
- */
301
- string(options) {
302
- return (0, import_bcs_type.stringLikeBcsType)({
303
- toBytes: (value) => new TextEncoder().encode(value),
304
- fromBytes: (bytes) => new TextDecoder().decode(bytes),
305
- ...options,
306
- name: options?.name ?? "string"
307
- });
308
- },
309
- /**
310
- * Creates a BcsType that represents a fixed length array of a given type
311
- * @param size The number of elements in the array
312
- * @param type The BcsType of each element in the array
313
- * @example
314
- * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
315
- */
316
- fixedArray,
317
- /**
318
- * Creates a BcsType representing an optional value
319
- * @param type The BcsType of the optional value
320
- * @example
321
- * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
322
- * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
323
- */
324
- option,
325
- /**
326
- * Creates a BcsType representing a variable length vector of a given type
327
- * @param type The BcsType of each element in the vector
328
- *
329
- * @example
330
- * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
331
- */
332
- vector,
333
- /**
334
- * Creates a BcsType representing a tuple of a given set of types
335
- * @param types The BcsTypes for each element in the tuple
336
- *
337
- * @example
338
- * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
339
- * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
340
- */
341
- tuple(fields, options) {
342
- return new import_bcs_type.BcsTuple({
343
- fields,
344
- ...options
345
- });
346
- },
347
- /**
348
- * Creates a BcsType representing a struct of a given set of fields
349
- * @param name The name of the struct
350
- * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
351
- *
352
- * @example
353
- * const struct = bcs.struct('MyStruct', {
354
- * a: bcs.u8(),
355
- * b: bcs.string(),
356
- * })
357
- * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
358
- */
359
- struct(name, fields, options) {
360
- return new import_bcs_type.BcsStruct({
361
- name,
362
- fields,
363
- ...options
364
- });
365
- },
366
- /**
367
- * Creates a BcsType representing an enum of a given set of options
368
- * @param name The name of the enum
369
- * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
370
- * null can be used to represent a variant with no data.
371
- *
372
- * @example
373
- * const enum = bcs.enum('MyEnum', {
374
- * A: bcs.u8(),
375
- * B: bcs.string(),
376
- * C: null,
377
- * })
378
- * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
379
- * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
380
- * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
381
- */
382
- enum(name, fields, options) {
383
- return new import_bcs_type.BcsEnum({
384
- name,
385
- fields,
386
- ...options
387
- });
388
- },
389
- /**
390
- * Creates a BcsType representing a map of a given key and value type
391
- * @param keyType The BcsType of the key
392
- * @param valueType The BcsType of the value
393
- * @example
394
- * const map = bcs.map(bcs.u8(), bcs.string())
395
- * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
396
- */
397
- map,
398
- /**
399
- * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
400
- * @param cb A callback that returns the BcsType
401
- */
402
- lazy(cb) {
403
- return (0, import_bcs_type.lazyBcsType)(cb);
404
- }
405
- };
406
- //# sourceMappingURL=bcs.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/bcs.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsTypeOptions } from './bcs-type.js';\nimport {\n\tBcsEnum,\n\tBcsStruct,\n\tBcsTuple,\n\tBcsType,\n\tbigUIntBcsType,\n\tdynamicSizeBcsType,\n\tfixedSizeBcsType,\n\tlazyBcsType,\n\tstringLikeBcsType,\n\tuIntBcsType,\n} from './bcs-type.js';\nimport type {\n\tEnumInputShape,\n\tEnumOutputShape,\n\tInferBcsInput,\n\tInferBcsType,\n\tJoinString,\n} from './types.js';\nimport { ulebEncode } from './uleb.js';\n\nfunction fixedArray<T extends BcsType<any>, Name extends string = string>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction fixedArray<T, Input, Name extends string = string>(\n\tsize: number,\n\ttype: BcsType<T, Input>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, Name>,\n): BcsType<T[], Iterable<Input> & { length: number }, Name>;\nfunction fixedArray<T extends BcsType<any>, Name extends string = `${T['name']}[${number}]`>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst result: InferBcsType<T>[] = new Array(size);\n\t\t\tfor (let i = 0; i < size; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `${type.name}[${size}]`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t\tif (value.length !== size) {\n\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;\nfunction option<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n): BcsType<T | null, Input | null | undefined>;\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`> {\n\treturn bcs\n\t\t.enum(`Option<${type.name}>`, {\n\t\t\tNone: null,\n\t\t\tSome: type,\n\t\t})\n\t\t.transform({\n\t\t\tinput: (value: InferBcsInput<T> | null | undefined) => {\n\t\t\t\tif (value == null) {\n\t\t\t\t\treturn { None: true };\n\t\t\t\t}\n\n\t\t\t\treturn { Some: value };\n\t\t\t},\n\t\t\toutput: (value) => {\n\t\t\t\tif (value.$kind === 'Some') {\n\t\t\t\t\treturn value.Some as InferBcsType<T>;\n\t\t\t\t}\n\n\t\t\t\treturn null;\n\t\t\t},\n\t\t});\n}\n\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction vector<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>,\n): BcsType<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>;\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result: InferBcsType<T>[] = new Array(length);\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\twriter.writeULEB(value.length);\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `vector<${type.name}>`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n>;\nfunction map<K, V, InputK = K, InputV = V>(\n\tkeyType: BcsType<K, InputK>,\n\tvalueType: BcsType<V, InputV>,\n): BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n> {\n\treturn bcs.vector(bcs.tuple([keyType, valueType])).transform({\n\t\tname: `Map<${keyType.name}, ${valueType.name}>`,\n\t\tinput: (value: Map<InferBcsInput<K>, InferBcsInput<V>>) => {\n\t\t\treturn [...value.entries()];\n\t\t},\n\t\toutput: (value) => {\n\t\t\tconst result = new Map<InferBcsType<K>, InferBcsType<V>>();\n\t\t\tfor (const [key, val] of value) {\n\t\t\t\tresult.set(key, val);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t});\n}\n\nexport const bcs = {\n\t/**\n\t * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.\n\t * @example\n\t * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]\n\t */\n\tu8(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read8',\n\t\t\twriteMethod: 'write8',\n\t\t\tsize: 1,\n\t\t\tmaxValue: 2 ** 8 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u8') as 'u8',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.\n\t * @example\n\t * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]\n\t */\n\tu16(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read16',\n\t\t\twriteMethod: 'write16',\n\t\t\tsize: 2,\n\t\t\tmaxValue: 2 ** 16 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u16') as 'u16',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.\n\t * @example\n\t * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]\n\t */\n\tu32(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read32',\n\t\t\twriteMethod: 'write32',\n\t\t\tsize: 4,\n\t\t\tmaxValue: 2 ** 32 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u32') as 'u32',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.\n\t * @example\n\t * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]\n\t */\n\tu64(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read64',\n\t\t\twriteMethod: 'write64',\n\t\t\tsize: 8,\n\t\t\tmaxValue: 2n ** 64n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u64') as 'u64',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.\n\t * @example\n\t * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu128(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read128',\n\t\t\twriteMethod: 'write128',\n\t\t\tsize: 16,\n\t\t\tmaxValue: 2n ** 128n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u128') as 'u128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.\n\t * @example\n\t * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu256(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read256',\n\t\t\twriteMethod: 'write256',\n\t\t\tsize: 32,\n\t\t\tmaxValue: 2n ** 256n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u256') as 'u256',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write boolean values.\n\t * @example\n\t * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]\n\t */\n\tbool(options?: BcsTypeOptions<boolean>) {\n\t\treturn fixedSizeBcsType({\n\t\t\tsize: 1,\n\t\t\tread: (reader) => reader.read8() === 1,\n\t\t\twrite: (value, writer) => writer.write8(value ? 1 : 0),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'bool') as 'bool',\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'boolean') {\n\t\t\t\t\tthrow new TypeError(`Expected boolean, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write unsigned LEB encoded integers\n\t * @example\n\t *\n\t */\n\tuleb128(options?: BcsTypeOptions<number>) {\n\t\treturn dynamicSizeBcsType({\n\t\t\tread: (reader) => reader.readULEB(),\n\t\t\tserialize: (value) => {\n\t\t\t\treturn Uint8Array.from(ulebEncode(value));\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'uleb128') as 'uleb128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a fixed length byte array\n\t * @param size The number of bytes this types represents\n\t * @example\n\t * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]\n\t */\n\tbytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {\n\t\treturn fixedSizeBcsType<Uint8Array, Iterable<number>, `bytes[${T}]`>({\n\t\t\tsize,\n\t\t\tread: (reader) => reader.readBytes(size),\n\t\t\twrite: (value, writer) => {\n\t\t\t\twriter.writeBytes(new Uint8Array(value));\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? `bytes[${size}]`) as `bytes[${T}]`,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!value || typeof value !== 'object' || !('length' in 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 !== size) {\n\t\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a variable length byte array\n\t *\n\t * @example\n\t * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]\n\t */\n\tbyteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {\n\t\treturn new BcsType<Uint8Array, Iterable<number>, 'vector<u8>'>({\n\t\t\tread: (reader) => {\n\t\t\t\tconst length = reader.readULEB();\n\n\t\t\t\treturn reader.readBytes(length);\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tconst array = new Uint8Array(value);\n\t\t\t\twriter.writeULEB(array.length);\n\t\t\t\twriter.writeBytes(array);\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'vector<u8>') as 'vector<u8>',\n\t\t\tserializedSize: (value) => {\n\t\t\t\tconst length = 'length' in value ? (value.length as number) : null;\n\t\t\t\treturn length == null ? null : ulebEncode(length).length + length;\n\t\t\t},\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded\n\t * @example\n\t * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]\n\t */\n\tstring(options?: BcsTypeOptions<string>) {\n\t\treturn stringLikeBcsType({\n\t\t\ttoBytes: (value) => new TextEncoder().encode(value),\n\t\t\tfromBytes: (bytes) => new TextDecoder().decode(bytes),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'string') as 'string',\n\t\t});\n\t},\n\t/**\n\t * Creates a BcsType that represents a fixed length array of a given type\n\t * @param size The number of elements in the array\n\t * @param type The BcsType of each element in the array\n\t * @example\n\t * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]\n\t */\n\tfixedArray,\n\n\t/**\n\t * Creates a BcsType representing an optional value\n\t * @param type The BcsType of the optional value\n\t * @example\n\t * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]\n\t * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]\n\t */\n\toption,\n\n\t/**\n\t * Creates a BcsType representing a variable length vector of a given type\n\t * @param type The BcsType of each element in the vector\n\t *\n\t * @example\n\t * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]\n\t */\n\tvector,\n\n\t/**\n\t * Creates a BcsType representing a tuple of a given set of types\n\t * @param types The BcsTypes for each element in the tuple\n\t *\n\t * @example\n\t * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])\n\t * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]\n\t */\n\ttuple<\n\t\tconst T extends readonly BcsType<any, any>[],\n\t\tconst Name extends string =\n\t\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n\t>(\n\t\tfields: T,\n\t\toptions?: BcsTypeOptions<\n\t\t\t{\n\t\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t\t},\n\t\t\t{\n\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t\t},\n\t\t\tName\n\t\t>,\n\t) {\n\t\treturn new BcsTuple<T, Name>({\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a struct of a given set of fields\n\t * @param name The name of the struct\n\t * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized\n\t *\n\t * @example\n\t * const struct = bcs.struct('MyStruct', {\n\t * a: bcs.u8(),\n\t * b: bcs.string(),\n\t * })\n\t * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t */\n\tstruct<T extends Record<string, BcsType<any>>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t\t\t}\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsStruct<T>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing an enum of a given set of options\n\t * @param name The name of the enum\n\t * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.\n\t * null can be used to represent a variant with no data.\n\t *\n\t * @example\n\t * const enum = bcs.enum('MyEnum', {\n\t * A: bcs.u8(),\n\t * B: bcs.string(),\n\t * C: null,\n\t * })\n\t * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]\n\t * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]\n\t */\n\tenum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\tEnumOutputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t\t\t}>,\n\t\t\t\tEnumInputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t\t\t}>,\n\t\t\t\tName\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsEnum<T, Name>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a map of a given key and value type\n\t * @param keyType The BcsType of the key\n\t * @param valueType The BcsType of the value\n\t * @example\n\t * const map = bcs.map(bcs.u8(), bcs.string())\n\t * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]\n\t */\n\tmap,\n\n\t/**\n\t * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.\n\t * @param cb A callback that returns the BcsType\n\t */\n\tlazy<T extends BcsType<any>>(cb: () => T): T {\n\t\treturn lazyBcsType(cb) as T;\n\t},\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;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AA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAKC,QACA,SASC;AACD,WAAO,IAAI,yBAAkB;AAAA,MAC5B;AAAA,MACA,GAAG;AAAA,IACJ,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,OACC,MACA,QACA,SAWC;AACD,WAAO,IAAI,0BAAa;AAAA,MACvB;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACJ,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBA,KACC,MACA,QACA,SAYC;AACD,WAAO,IAAI,wBAAiB;AAAA,MAC3B;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACJ,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAA6B,IAAgB;AAC5C,eAAO,6BAAY,EAAE;AAAA,EACtB;AACD;",
6
- "names": []
7
- }
@@ -1,22 +0,0 @@
1
- import { toBase58, fromBase58, toBase64, fromBase64, toHex, fromHex } from '@haneullabs/utils';
2
- import type { BcsTypeOptions } from './bcs-type.js';
3
- import { BcsType, BcsStruct, BcsEnum, BcsTuple, isSerializedBcs, SerializedBcs } from './bcs-type.js';
4
- import { bcs } from './bcs.js';
5
- import { BcsReader } from './reader.js';
6
- import type { EnumInputShape, EnumOutputShape, EnumOutputShapeWithKeys, InferBcsInput, InferBcsType } from './types.js';
7
- import { decodeStr, encodeStr, splitGenericParameters } from './utils.js';
8
- import type { BcsWriterOptions } from './writer.js';
9
- import { BcsWriter } from './writer.js';
10
- export { bcs, BcsType, BcsStruct, BcsEnum, BcsTuple, type BcsTypeOptions, SerializedBcs, isSerializedBcs, toBase58, fromBase58, toBase64, fromBase64, toHex, fromHex, encodeStr, decodeStr, splitGenericParameters, BcsReader, BcsWriter, type BcsWriterOptions, type InferBcsInput, type InferBcsType, type EnumOutputShape, type EnumInputShape, type EnumOutputShapeWithKeys, };
11
- /** @deprecated use toBase58 instead */
12
- export declare const toB58: (buffer: Uint8Array) => string;
13
- /** @deprecated use fromBase58 instead */
14
- export declare const fromB58: (str: string) => Uint8Array<ArrayBuffer>;
15
- /** @deprecated use toBase64 instead */
16
- export declare const toB64: typeof toBase64;
17
- /** @deprecated use fromBase64 instead */
18
- export declare const fromB64: typeof fromBase64;
19
- /** @deprecated use toHex instead */
20
- export declare const toHEX: typeof toHex;
21
- /** @deprecated use fromHex instead */
22
- export declare const fromHEX: typeof fromHex;