@haneullabs/bcs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +388 -0
- package/README.md +358 -0
- package/dist/cjs/bcs-type.d.ts +127 -0
- package/dist/cjs/bcs-type.js +386 -0
- package/dist/cjs/bcs-type.js.map +7 -0
- package/dist/cjs/bcs.d.ts +175 -0
- package/dist/cjs/bcs.js +406 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/reader.d.ts +92 -0
- package/dist/cjs/reader.js +136 -0
- package/dist/cjs/reader.js.map +7 -0
- package/dist/cjs/types.d.ts +28 -0
- package/dist/cjs/types.js +17 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/uleb.d.ts +5 -0
- package/dist/cjs/uleb.js +66 -0
- package/dist/cjs/uleb.js.map +7 -0
- package/dist/cjs/utils.d.ts +18 -0
- package/dist/cjs/utils.js +74 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/cjs/writer.d.ts +117 -0
- package/dist/cjs/writer.js +196 -0
- package/dist/cjs/writer.js.map +7 -0
- package/dist/esm/bcs-type.d.ts +127 -0
- package/dist/esm/bcs-type.js +366 -0
- package/dist/esm/bcs-type.js.map +7 -0
- package/dist/esm/bcs.d.ts +175 -0
- package/dist/esm/bcs.js +397 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.js +46 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/reader.d.ts +92 -0
- package/dist/esm/reader.js +116 -0
- package/dist/esm/reader.js.map +7 -0
- package/dist/esm/types.d.ts +28 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/uleb.d.ts +5 -0
- package/dist/esm/uleb.js +46 -0
- package/dist/esm/uleb.js.map +7 -0
- package/dist/esm/utils.d.ts +18 -0
- package/dist/esm/utils.js +54 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/esm/writer.d.ts +117 -0
- package/dist/esm/writer.js +176 -0
- package/dist/esm/writer.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +73 -0
- package/src/bcs-type.ts +531 -0
- package/src/bcs.ts +543 -0
- package/src/index.ts +82 -0
- package/src/reader.ts +156 -0
- package/src/types.ts +52 -0
- package/src/uleb.ts +61 -0
- package/src/utils.ts +75 -0
- package/src/writer.ts +222 -0
package/README.md
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
# BCS - Binary Canonical Serialization
|
|
2
|
+
|
|
3
|
+
This small and lightweight library implements
|
|
4
|
+
[Binary Canonical Serialization (BCS)](https://github.com/zefchain/bcs) in TypeScript, making BCS
|
|
5
|
+
available in both Browser and NodeJS environments in a type-safe way.`
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
To install, add the [`@haneullabs/bcs`](https://www.npmjs.com/package/@haneullabs/bcs) package to your
|
|
10
|
+
project:
|
|
11
|
+
|
|
12
|
+
```sh npm2yarn
|
|
13
|
+
npm i @haneullabs/bcs
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quickstart
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { bcs, fromHex, toHex } from '@haneullabs/bcs';
|
|
20
|
+
|
|
21
|
+
// define UID as a 32-byte array, then add a transform to/from hex strings
|
|
22
|
+
const UID = bcs.fixedArray(32, bcs.u8()).transform({
|
|
23
|
+
input: (id: string) => fromHex(id),
|
|
24
|
+
output: (id) => toHex(Uint8Array.from(id)),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const Coin = bcs.struct('Coin', {
|
|
28
|
+
id: UID,
|
|
29
|
+
value: bcs.u64(),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// deserialization: BCS bytes into Coin
|
|
33
|
+
const bcsBytes = Coin.serialize({
|
|
34
|
+
id: '0000000000000000000000000000000000000000000000000000000000000001',
|
|
35
|
+
value: 1000000n,
|
|
36
|
+
}).toBytes();
|
|
37
|
+
|
|
38
|
+
const coin = Coin.parse(bcsBytes);
|
|
39
|
+
|
|
40
|
+
// serialization: Object into bytes - an Option with <T = Coin>
|
|
41
|
+
const hex = bcs.option(Coin).serialize(coin).toHex();
|
|
42
|
+
|
|
43
|
+
console.log(hex);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Description
|
|
47
|
+
|
|
48
|
+
BCS defines the way the data is serialized, and the serialized results contains no type information.
|
|
49
|
+
To be able to serialize the data and later deserialize it, a schema has to be created (based on the
|
|
50
|
+
built-in primitives, such as `string` or `u64`). There are no type hints in the serialized bytes on
|
|
51
|
+
what they mean, so the schema used for decoding must match the schema used to encode the data.
|
|
52
|
+
|
|
53
|
+
The `@haneullabs/bcs` library can be used to define schemas that can serialize and deserialize BCS
|
|
54
|
+
encoded data, and can infer the correct TypeScript for the schema from the definitions themselves
|
|
55
|
+
rather than having to define them manually.
|
|
56
|
+
|
|
57
|
+
## Basic types
|
|
58
|
+
|
|
59
|
+
bcs supports a number of built in base types that can be combined to create more complex types. The
|
|
60
|
+
following table lists the primitive types available:
|
|
61
|
+
|
|
62
|
+
| Method | TS Type | TS Input Type | Description |
|
|
63
|
+
| --------------------- | ------------ | ---------------------------- | --------------------------------------------------------------------------- |
|
|
64
|
+
| `bool` | `boolean` | `boolean` | Boolean type (converts to `true` / `false`) |
|
|
65
|
+
| `u8`, `u16`, `u32` | `number` | `number` | Unsigned Integer types |
|
|
66
|
+
| `u64`, `u128`, `u256` | `string` | `number \| string \| bigint` | Unsigned Integer types, decoded as `string` to allow for JSON serialization |
|
|
67
|
+
| `uleb128` | `number` | `number` | Unsigned LEB128 integer type |
|
|
68
|
+
| `string` | `string` | `string` | UTF-8 encoded string |
|
|
69
|
+
| `bytes(size)` | `Uint8Array` | `Iterable<number>` | Fixed length bytes |
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { bcs } from '@haneullabs/bcs';
|
|
73
|
+
|
|
74
|
+
// Integers
|
|
75
|
+
const u8 = bcs.u8().serialize(100).toBytes();
|
|
76
|
+
const u64 = bcs.u64().serialize(1000000n).toBytes();
|
|
77
|
+
const u128 = bcs.u128().serialize('100000010000001000000').toBytes();
|
|
78
|
+
|
|
79
|
+
// Other types
|
|
80
|
+
const str = bcs.string().serialize('this is an ascii string').toBytes();
|
|
81
|
+
const hex = bcs.hex().serialize('C0FFEE').toBytes();
|
|
82
|
+
const bytes = bcs.bytes(4).serialize([1, 2, 3, 4]).toBytes();
|
|
83
|
+
|
|
84
|
+
// Parsing data back into original types
|
|
85
|
+
const parsedU8 = bcs.u8().parse(u8);
|
|
86
|
+
// u64-u256 will be represented as bigints regardless of how they were provided when serializing them
|
|
87
|
+
const parsedU64 = bcs.u64().parse(u64);
|
|
88
|
+
const parsedU128 = bcs.u128().parse(u128);
|
|
89
|
+
|
|
90
|
+
const parsedStr = bcs.string().parse(str);
|
|
91
|
+
const parsedHex = bcs.hex().parse(hex);
|
|
92
|
+
const parsedBytes = bcs.bytes(4).parse(bytes);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Compound types
|
|
96
|
+
|
|
97
|
+
For most use-cases you'll want to combine primitive types into more complex types like `vectors`,
|
|
98
|
+
`structs` and `enums`. The following table lists methods available for creating compound types:
|
|
99
|
+
|
|
100
|
+
| Method | Description |
|
|
101
|
+
| ---------------------- | ----------------------------------------------------- |
|
|
102
|
+
| `vector(type: T)` | A variable length list of values of type `T` |
|
|
103
|
+
| `fixedArray(size, T)` | A fixed length array of values of type `T` |
|
|
104
|
+
| `option(type: T)` | A value of type `T` or `null` |
|
|
105
|
+
| `enum(name, values)` | An enum value representing one of the provided values |
|
|
106
|
+
| `struct(name, fields)` | A struct with named fields of the provided types |
|
|
107
|
+
| `tuple(types)` | A tuple of the provided types |
|
|
108
|
+
| `map(K, V)` | A map of keys of type `K` to values of type `V` |
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { bcs } from '@haneullabs/bcs';
|
|
112
|
+
|
|
113
|
+
// Vectors
|
|
114
|
+
const intList = bcs.vector(bcs.u8()).serialize([1, 2, 3, 4, 5]).toBytes();
|
|
115
|
+
const stringList = bcs.vector(bcs.string()).serialize(['a', 'b', 'c']).toBytes();
|
|
116
|
+
|
|
117
|
+
// Fixed length Arrays
|
|
118
|
+
const intArray = bcs.fixedArray(4, bcs.u8()).serialize([1, 2, 3, 4]).toBytes();
|
|
119
|
+
const stringArray = bcs.fixedArray(3, bcs.string()).serialize(['a', 'b', 'c']).toBytes();
|
|
120
|
+
|
|
121
|
+
// Option
|
|
122
|
+
const option = bcs.option(bcs.string()).serialize('some value').toBytes();
|
|
123
|
+
const nullOption = bcs.option(bcs.string()).serialize(null).toBytes();
|
|
124
|
+
|
|
125
|
+
// Enum
|
|
126
|
+
const MyEnum = bcs.enum('MyEnum', {
|
|
127
|
+
NoType: null,
|
|
128
|
+
Int: bcs.u8(),
|
|
129
|
+
String: bcs.string(),
|
|
130
|
+
Array: bcs.fixedArray(3, bcs.u8()),
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const noTypeEnum = MyEnum.serialize({ NoType: null }).toBytes();
|
|
134
|
+
const intEnum = MyEnum.serialize({ Int: 100 }).toBytes();
|
|
135
|
+
const stringEnum = MyEnum.serialize({ String: 'string' }).toBytes();
|
|
136
|
+
const arrayEnum = MyEnum.serialize({ Array: [1, 2, 3] }).toBytes();
|
|
137
|
+
|
|
138
|
+
// Struct
|
|
139
|
+
const MyStruct = bcs.struct('MyStruct', {
|
|
140
|
+
id: bcs.u8(),
|
|
141
|
+
name: bcs.string(),
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const struct = MyStruct.serialize({ id: 1, name: 'name' }).toBytes();
|
|
145
|
+
|
|
146
|
+
// Tuple
|
|
147
|
+
const tuple = bcs.tuple([bcs.u8(), bcs.string()]).serialize([1, 'name']).toBytes();
|
|
148
|
+
|
|
149
|
+
// Map
|
|
150
|
+
const map = bcs
|
|
151
|
+
.map(bcs.u8(), bcs.string())
|
|
152
|
+
.serialize(
|
|
153
|
+
new Map([
|
|
154
|
+
[1, 'one'],
|
|
155
|
+
[2, 'two'],
|
|
156
|
+
]),
|
|
157
|
+
)
|
|
158
|
+
.toBytes();
|
|
159
|
+
|
|
160
|
+
// Parsing data back into original types
|
|
161
|
+
|
|
162
|
+
// Vectors
|
|
163
|
+
const parsedIntList = bcs.vector(bcs.u8()).parse(intList);
|
|
164
|
+
const parsedStringList = bcs.vector(bcs.string()).parse(stringList);
|
|
165
|
+
|
|
166
|
+
// Fixed length Arrays
|
|
167
|
+
const parsedIntArray = bcs.fixedArray(4, bcs.u8()).parse(intArray);
|
|
168
|
+
|
|
169
|
+
// Option
|
|
170
|
+
const parsedOption = bcs.option(bcs.string()).parse(option);
|
|
171
|
+
const parsedNullOption = bcs.option(bcs.string()).parse(nullOption);
|
|
172
|
+
|
|
173
|
+
// Enum
|
|
174
|
+
const parsedNoTypeEnum = MyEnum.parse(noTypeEnum);
|
|
175
|
+
const parsedIntEnum = MyEnum.parse(intEnum);
|
|
176
|
+
const parsedStringEnum = MyEnum.parse(stringEnum);
|
|
177
|
+
const parsedArrayEnum = MyEnum.parse(arrayEnum);
|
|
178
|
+
|
|
179
|
+
// Struct
|
|
180
|
+
const parsedStruct = MyStruct.parse(struct);
|
|
181
|
+
|
|
182
|
+
// Tuple
|
|
183
|
+
const parsedTuple = bcs.tuple([bcs.u8(), bcs.string()]).parse(tuple);
|
|
184
|
+
|
|
185
|
+
// Map
|
|
186
|
+
const parsedMap = bcs.map(bcs.u8(), bcs.string()).parse(map);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Generics
|
|
190
|
+
|
|
191
|
+
To define a generic struct or an enum, you can define a generic typescript function helper
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
// Example: Generics
|
|
195
|
+
import { bcs, BcsType } from '@haneullabs/bcs';
|
|
196
|
+
|
|
197
|
+
// The T typescript generic is a placeholder for the typescript type of the generic value
|
|
198
|
+
// The T argument will be the bcs type passed in when creating a concrete instance of the Container type
|
|
199
|
+
function Container<T>(T: BcsType<T>) {
|
|
200
|
+
return bcs.struct('Container<T>', {
|
|
201
|
+
contents: T,
|
|
202
|
+
}),
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// When serializing, we have to pass the type to use for `T`
|
|
206
|
+
const bytes = Container(bcs.u8()).serialize({ contents: 100 }).toBytes();
|
|
207
|
+
|
|
208
|
+
// Alternatively we can save the concrete type as a variable
|
|
209
|
+
const U8Container = Container(bcs.u8());
|
|
210
|
+
const bytes = U8Container.serialize({ contents: 100 }).toBytes();
|
|
211
|
+
|
|
212
|
+
// Using multiple generics
|
|
213
|
+
function VecMap<K, V>, (K: BcsType<K>, V: BcsType<V>) {
|
|
214
|
+
// You can use the names of the generic params in the type name to
|
|
215
|
+
return bcs.struct(
|
|
216
|
+
// You can use the names of the generic params to give your type a more useful name
|
|
217
|
+
`VecMap<${K.name}, ${V.name}>`,
|
|
218
|
+
{
|
|
219
|
+
keys: bcs.vector(K),
|
|
220
|
+
values: bcs.vector(V),
|
|
221
|
+
}
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// To serialize VecMap, we can use:
|
|
226
|
+
VecMap(bcs.string(), bcs.string())
|
|
227
|
+
.serialize({
|
|
228
|
+
keys: ['key1', 'key2', 'key3'],
|
|
229
|
+
values: ['value1', 'value2', 'value3'],
|
|
230
|
+
})
|
|
231
|
+
.toBytes();
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Transforms
|
|
235
|
+
|
|
236
|
+
If the format you use in your code is different from the format expected for BCS serialization, you
|
|
237
|
+
can use the `transform` API to map between the types you use in your application, and the types
|
|
238
|
+
needed for serialization.
|
|
239
|
+
|
|
240
|
+
The `address` type used by Move code is a good example of this. In many cases, you'll want to
|
|
241
|
+
represent an address as a hex string, but the BCS serialization format for addresses is a 32 byte
|
|
242
|
+
array. To handle this, you can use the `transform` API to map between the two formats:
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
import { bcs, toHex } from '@haneullabs/bcs';
|
|
246
|
+
|
|
247
|
+
const Address = bcs.bytes(32).transform({
|
|
248
|
+
// To change the input type, you need to provide a type definition for the input
|
|
249
|
+
input: (val: string) => fromHex(val),
|
|
250
|
+
output: (val) => toHex(val),
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const serialized = Address.serialize('0x000000...').toBytes();
|
|
254
|
+
const parsed = Address.parse(serialized); // will return a hex string
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
When using a transform, a new type is created that uses the inferred return value of `output` as the
|
|
258
|
+
return type of the `parse` method, and the type of the `input` argument as the allowed input type
|
|
259
|
+
when calling `serialize`. The `output` type can generally be inferred from the definition, but the
|
|
260
|
+
input type will need to be provided explicitly. In some cases, for complex transforms, you'll need
|
|
261
|
+
to manually type the return
|
|
262
|
+
|
|
263
|
+
transforms can also handle more complex types. For instance, `@haneullabs/haneul` uses the following
|
|
264
|
+
definition to transform enums into a more TypeScript friends format:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
type Merge<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
|
|
268
|
+
type EnumKindTransform<T> = T extends infer U
|
|
269
|
+
? Merge<(U[keyof U] extends null | boolean ? object : U[keyof U]) & { kind: keyof U }>
|
|
270
|
+
: never;
|
|
271
|
+
|
|
272
|
+
function enumKind<T extends object, Input extends object>(type: BcsType<T, Input>) {
|
|
273
|
+
return type.transform({
|
|
274
|
+
input: ({ kind, ...val }: EnumKindTransform<Input>) =>
|
|
275
|
+
({
|
|
276
|
+
[kind]: val,
|
|
277
|
+
}) as Input,
|
|
278
|
+
output: (val) => {
|
|
279
|
+
const key = Object.keys(val)[0] as keyof T;
|
|
280
|
+
|
|
281
|
+
return { kind: key, ...val[key] } as EnumKindTransform<T>;
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const MyEnum = enumKind(
|
|
287
|
+
bcs.enum('MyEnum', {
|
|
288
|
+
A: bcs.struct('A', {
|
|
289
|
+
id: bcs.u8(),
|
|
290
|
+
}),
|
|
291
|
+
B: bcs.struct('B', {
|
|
292
|
+
val: bcs.string(),
|
|
293
|
+
}),
|
|
294
|
+
}),
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
// Enums wrapped with enumKind flatten the enum variants and add a `kind` field to differentiate them
|
|
298
|
+
const A = MyEnum.serialize({ kind: 'A', id: 1 }).toBytes();
|
|
299
|
+
const B = MyEnum.serialize({ kind: 'B', val: 'xyz' }).toBytes();
|
|
300
|
+
|
|
301
|
+
const parsedA = MyEnum.parse(A); // returns { kind: 'A', id: 1 }
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Formats for serialized bytes
|
|
305
|
+
|
|
306
|
+
When you call `serialize` on a `BcsType`, you will receive a `SerializedBcs` instance. This wrapper
|
|
307
|
+
preserves type information for the serialized bytes, and can be used to get raw data in various
|
|
308
|
+
formats.
|
|
309
|
+
|
|
310
|
+
```ts
|
|
311
|
+
import { bcs, fromBase58, fromBase64, fromHex } from '@haneullabs/bcs';
|
|
312
|
+
|
|
313
|
+
const serializedString = bcs.string().serialize('this is a string');
|
|
314
|
+
|
|
315
|
+
// SerializedBcs.toBytes() returns a Uint8Array
|
|
316
|
+
const bytes: Uint8Array = serializedString.toBytes();
|
|
317
|
+
|
|
318
|
+
// You can get the serialized bytes encoded as hex, base64 or base58
|
|
319
|
+
const hex: string = serializedString.toHex();
|
|
320
|
+
const base64: string = bcsWriter.toBase64();
|
|
321
|
+
const base58: string = bcsWriter.toBase58();
|
|
322
|
+
|
|
323
|
+
// To parse a BCS value from bytes, the bytes need to be a Uint8Array
|
|
324
|
+
const str1 = bcs.string().parse(bytes);
|
|
325
|
+
|
|
326
|
+
// If your data is encoded as string, you need to convert it to Uint8Array first
|
|
327
|
+
const str2 = bcs.string().parse(fromHex(hex));
|
|
328
|
+
const str3 = bcs.string().parse(fromBase64(base64));
|
|
329
|
+
const str4 = bcs.string().parse(fromBase58(base58));
|
|
330
|
+
|
|
331
|
+
console.assert((str1 == str2) == (str3 == str4), 'Result is the same');
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Inferring types
|
|
335
|
+
|
|
336
|
+
BCS types have both a `type` and an `inputType`. For some types these are the same, but for others
|
|
337
|
+
(like `u64`) the types diverge slightly to make inputs more flexible. For instance, `u64` will
|
|
338
|
+
always be `string` for it's type, but can be a `number`, `string` or `bigint` for it's input type.
|
|
339
|
+
|
|
340
|
+
You can infer these types in one of 2 ways, either using the `$inferType` and `$inferInput`
|
|
341
|
+
properties on a `BcsType`, or using the `InferBcsType` and `InferBcsInput` type helpers.
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
import { bcs, type InferBcsType, type InferBcsInput } from '@haneullabs/bcs';
|
|
345
|
+
|
|
346
|
+
const MyStruct = bcs.struct('MyStruct', {
|
|
347
|
+
id: bcs.u64(),
|
|
348
|
+
name: bcs.string(),
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// using the $inferType and $inferInput properties
|
|
352
|
+
type MyStructType = typeof MyStruct.$inferType; // { id: string; name: string; }
|
|
353
|
+
type MyStructInput = typeof MyStruct.$inferInput; // { id: number | string | bigint; name: string; }
|
|
354
|
+
|
|
355
|
+
// using the InferBcsType and InferBcsInput type helpers
|
|
356
|
+
type MyStructType = InferBcsType<typeof MyStruct>; // { id: string; name: string; }
|
|
357
|
+
type MyStructInput = InferBcsInput<typeof MyStruct>; // { id: number | string | bigint; name: string; }
|
|
358
|
+
```
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { BcsReader } from './reader.js';
|
|
2
|
+
import type { BcsWriterOptions } from './writer.js';
|
|
3
|
+
import { BcsWriter } from './writer.js';
|
|
4
|
+
import type { EnumInputShape, EnumOutputShape, JoinString } from './types.js';
|
|
5
|
+
export interface BcsTypeOptions<T, Input = T, Name extends string = string> {
|
|
6
|
+
name?: Name;
|
|
7
|
+
validate?: (value: Input) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare class BcsType<T, Input = T, const Name extends string = string> {
|
|
10
|
+
#private;
|
|
11
|
+
$inferType: T;
|
|
12
|
+
$inferInput: Input;
|
|
13
|
+
name: Name;
|
|
14
|
+
read: (reader: BcsReader) => T;
|
|
15
|
+
serializedSize: (value: Input, options?: BcsWriterOptions) => number | null;
|
|
16
|
+
validate: (value: Input) => void;
|
|
17
|
+
constructor(options: {
|
|
18
|
+
name: Name;
|
|
19
|
+
read: (reader: BcsReader) => T;
|
|
20
|
+
write: (value: Input, writer: BcsWriter) => void;
|
|
21
|
+
serialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
|
|
22
|
+
serializedSize?: (value: Input) => number | null;
|
|
23
|
+
validate?: (value: Input) => void;
|
|
24
|
+
} & BcsTypeOptions<T, Input, Name>);
|
|
25
|
+
write(value: Input, writer: BcsWriter): void;
|
|
26
|
+
serialize(value: Input, options?: BcsWriterOptions): SerializedBcs<T, Input>;
|
|
27
|
+
parse(bytes: Uint8Array): T;
|
|
28
|
+
fromHex(hex: string): T;
|
|
29
|
+
fromBase58(b64: string): T;
|
|
30
|
+
fromBase64(b64: string): T;
|
|
31
|
+
transform<T2 = T, Input2 = Input, NewName extends string = Name>({ name, input, output, validate, }: {
|
|
32
|
+
input?: (val: Input2) => Input;
|
|
33
|
+
output?: (value: T) => T2;
|
|
34
|
+
} & BcsTypeOptions<T2, Input2, NewName>): BcsType<T2, Input2, NewName>;
|
|
35
|
+
}
|
|
36
|
+
declare const SERIALIZED_BCS_BRAND: never;
|
|
37
|
+
export declare function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown>;
|
|
38
|
+
export declare class SerializedBcs<T, Input = T> {
|
|
39
|
+
#private;
|
|
40
|
+
[SERIALIZED_BCS_BRAND]: boolean;
|
|
41
|
+
constructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>);
|
|
42
|
+
toBytes(): Uint8Array<ArrayBuffer>;
|
|
43
|
+
toHex(): string;
|
|
44
|
+
toBase64(): string;
|
|
45
|
+
toBase58(): string;
|
|
46
|
+
parse(): T;
|
|
47
|
+
}
|
|
48
|
+
export declare function fixedSizeBcsType<T, Input = T, const Name extends string = string>({ size, ...options }: {
|
|
49
|
+
name: Name;
|
|
50
|
+
size: number;
|
|
51
|
+
read: (reader: BcsReader) => T;
|
|
52
|
+
write: (value: Input, writer: BcsWriter) => void;
|
|
53
|
+
} & BcsTypeOptions<T, Input, Name>): BcsType<T, Input, Name>;
|
|
54
|
+
export declare function uIntBcsType<const Name extends string = string>({ readMethod, writeMethod, ...options }: {
|
|
55
|
+
name: Name;
|
|
56
|
+
size: number;
|
|
57
|
+
readMethod: `read${8 | 16 | 32}`;
|
|
58
|
+
writeMethod: `write${8 | 16 | 32}`;
|
|
59
|
+
maxValue: number;
|
|
60
|
+
} & BcsTypeOptions<number, number, Name>): BcsType<number, number, Name>;
|
|
61
|
+
export declare function bigUIntBcsType<const Name extends string = string>({ readMethod, writeMethod, ...options }: {
|
|
62
|
+
name: Name;
|
|
63
|
+
size: number;
|
|
64
|
+
readMethod: `read${64 | 128 | 256}`;
|
|
65
|
+
writeMethod: `write${64 | 128 | 256}`;
|
|
66
|
+
maxValue: bigint;
|
|
67
|
+
} & BcsTypeOptions<string, string | number | bigint>): BcsType<string, string | number | bigint, Name>;
|
|
68
|
+
export declare function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({ serialize, ...options }: {
|
|
69
|
+
name: Name;
|
|
70
|
+
read: (reader: BcsReader) => T;
|
|
71
|
+
serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
|
|
72
|
+
} & BcsTypeOptions<T, Input>): BcsType<T, Input, string>;
|
|
73
|
+
export declare function stringLikeBcsType<const Name extends string = string>({ toBytes, fromBytes, ...options }: {
|
|
74
|
+
name: Name;
|
|
75
|
+
toBytes: (value: string) => Uint8Array;
|
|
76
|
+
fromBytes: (bytes: Uint8Array) => string;
|
|
77
|
+
serializedSize?: (value: string) => number | null;
|
|
78
|
+
} & BcsTypeOptions<string, string, Name>): BcsType<string, string, Name>;
|
|
79
|
+
export declare function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>): BcsType<T, Input, string>;
|
|
80
|
+
export interface BcsStructOptions<T extends Record<string, BcsType<any>>, Name extends string = string> extends Omit<BcsTypeOptions<{
|
|
81
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;
|
|
82
|
+
}, {
|
|
83
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;
|
|
84
|
+
}, Name>, 'name'> {
|
|
85
|
+
name: Name;
|
|
86
|
+
fields: T;
|
|
87
|
+
}
|
|
88
|
+
export declare class BcsStruct<T extends Record<string, BcsType<any>>, const Name extends string = string> extends BcsType<{
|
|
89
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;
|
|
90
|
+
}, {
|
|
91
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;
|
|
92
|
+
}, Name> {
|
|
93
|
+
constructor({ name, fields, ...options }: BcsStructOptions<T, Name>);
|
|
94
|
+
}
|
|
95
|
+
export interface BcsEnumOptions<T extends Record<string, BcsType<any> | null>, Name extends string = string> extends Omit<BcsTypeOptions<EnumOutputShape<{
|
|
96
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;
|
|
97
|
+
}>, EnumInputShape<{
|
|
98
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;
|
|
99
|
+
}>, Name>, 'name'> {
|
|
100
|
+
name: Name;
|
|
101
|
+
fields: T;
|
|
102
|
+
}
|
|
103
|
+
export declare class BcsEnum<T extends Record<string, BcsType<any> | null>, const Name extends string = string> extends BcsType<EnumOutputShape<{
|
|
104
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true;
|
|
105
|
+
}>, EnumInputShape<{
|
|
106
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;
|
|
107
|
+
}>, Name> {
|
|
108
|
+
constructor({ fields, ...options }: BcsEnumOptions<T, Name>);
|
|
109
|
+
}
|
|
110
|
+
export interface BcsTupleOptions<T extends readonly BcsType<any>[], Name extends string> extends Omit<BcsTypeOptions<{
|
|
111
|
+
-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;
|
|
112
|
+
}, {
|
|
113
|
+
[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;
|
|
114
|
+
}, Name>, 'name'> {
|
|
115
|
+
name?: Name;
|
|
116
|
+
fields: T;
|
|
117
|
+
}
|
|
118
|
+
export declare class BcsTuple<const T extends readonly BcsType<any>[], const Name extends string = `(${JoinString<{
|
|
119
|
+
[K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never;
|
|
120
|
+
}, ', '>})`> extends BcsType<{
|
|
121
|
+
-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;
|
|
122
|
+
}, {
|
|
123
|
+
[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;
|
|
124
|
+
}, Name> {
|
|
125
|
+
constructor({ fields, name, ...options }: BcsTupleOptions<T, Name>);
|
|
126
|
+
}
|
|
127
|
+
export {};
|