@haneullabs/bcs 2.0.2 → 2.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 +36 -0
- package/README.md +1 -1
- package/dist/bcs-type.d.mts.map +1 -1
- package/dist/bcs-type.mjs +4 -5
- package/dist/bcs-type.mjs.map +1 -1
- package/dist/bcs.d.mts +2 -2
- package/dist/bcs.d.mts.map +1 -1
- package/dist/bcs.mjs +127 -2
- package/dist/bcs.mjs.map +1 -1
- package/dist/index.mjs +1 -2
- package/dist/reader.d.mts.map +1 -1
- package/dist/reader.mjs +1 -2
- package/dist/reader.mjs.map +1 -1
- package/dist/types.d.mts +0 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +1 -1
- package/dist/uleb.d.mts.map +1 -1
- package/dist/uleb.mjs +1 -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/utils.mjs +1 -2
- package/dist/utils.mjs.map +1 -1
- package/dist/writer.d.mts.map +1 -1
- package/dist/writer.mjs +1 -2
- package/dist/writer.mjs.map +1 -1
- package/docs/index.md +358 -0
- package/docs/llms-index.md +4 -0
- package/package.json +11 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 2.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- bbf63cb: Updated dependencies
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [bbf63cb]
|
|
12
|
+
- @mysten/utils@0.4.0
|
|
13
|
+
|
|
14
|
+
## 2.0.5
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- f7de3e5: Restore docs in published tarballs.
|
|
19
|
+
- Updated dependencies [f7de3e5]
|
|
20
|
+
- @mysten/utils@0.3.3
|
|
21
|
+
|
|
22
|
+
## 2.0.4
|
|
23
|
+
|
|
24
|
+
### Patch Changes
|
|
25
|
+
|
|
26
|
+
- 9e067cf: Validate the new per-package release flow end-to-end across every public @mysten package.
|
|
27
|
+
No functional changes — empty patch bump to force the orchestrator to dispatch every
|
|
28
|
+
release-<pkg>.yml workflow with `dry_run=false` so each package publishes via OIDC trusted
|
|
29
|
+
publishing.
|
|
30
|
+
- Updated dependencies [9e067cf]
|
|
31
|
+
- @mysten/utils@0.3.2
|
|
32
|
+
|
|
33
|
+
## 2.0.3
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- 43e69f8: Add embedded LLM-friendly docs to published packages
|
|
38
|
+
|
|
3
39
|
## 2.0.2
|
|
4
40
|
|
|
5
41
|
### Patch Changes
|
package/README.md
CHANGED
package/dist/bcs-type.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs-type.d.mts","names":[],"sources":["../src/bcs-type.ts"],"
|
|
1
|
+
{"version":3,"file":"bcs-type.d.mts","names":[],"sources":["../src/bcs-type.ts"],"mappings":";;;;;UAUiB,cAAA,YAA0B,CAAA;EAC1C,IAAA,GAAO,IAAA;EACP,QAAA,IAAY,KAAA,EAAO,KAAA;AAAA;AAAA,cAGP,OAAA,YAAmB,CAAA;EAAA;EAC/B,UAAA,EAAa,CAAA;EACb,WAAA,EAAc,KAAA;EACd,IAAA,EAAM,IAAA;EACN,IAAA,GAAO,MAAA,EAAQ,SAAA,KAAc,CAAA;EAC7B,cAAA,GAAiB,KAAA,EAAO,KAAA,EAAO,OAAA,GAAU,gBAAA;EACzC,QAAA,GAAW,KAAA,EAAO,KAAA;cAKjB,OAAA;IACC,IAAA,EAAM,IAAA;IACN,IAAA,GAAO,MAAA,EAAQ,SAAA,KAAc,CAAA;IAC7B,KAAA,GAAQ,KAAA,EAAO,KAAA,EAAO,MAAA,EAAQ,SAAA;IAC9B,SAAA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A,iBAAwB,CAAA,0BAGnD,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,iBAAwB,CAAA,YAEtD,IAAA;EAID,IAAA,GAAO,IAAA;EACP,MAAA,EAAQ,CAAA;AAAA;AAAA,cAGI,QAAA,0BACa,OAAA,yCAEpB,UAAA,eAAyB,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,sBAA6B,CAAA,8BACnE,OAAA,yBAEc,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,iBAAwB,CAAA,0BAGnD,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,iBAAwB,CAAA,YAEtD,IAAA;;IAEc,MAAA;IAAQ,IAAA;IAAA,GAAS;EAAA,GAAW,eAAA,CAAgB,CAAA,EAAG,IAAA;AAAA"}
|
package/dist/bcs-type.mjs
CHANGED
|
@@ -2,7 +2,6 @@ import { ulebEncode } from "./uleb.mjs";
|
|
|
2
2
|
import { BcsReader } from "./reader.mjs";
|
|
3
3
|
import { BcsWriter } from "./writer.mjs";
|
|
4
4
|
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
|
|
5
|
-
|
|
6
5
|
//#region src/bcs-type.ts
|
|
7
6
|
var BcsType = class BcsType {
|
|
8
7
|
#write;
|
|
@@ -12,10 +11,10 @@ var BcsType = class BcsType {
|
|
|
12
11
|
this.read = options.read;
|
|
13
12
|
this.serializedSize = options.serializedSize ?? (() => null);
|
|
14
13
|
this.#write = options.write;
|
|
15
|
-
this.#serialize = options.serialize ?? ((value, options
|
|
14
|
+
this.#serialize = options.serialize ?? ((value, options) => {
|
|
16
15
|
const writer = new BcsWriter({
|
|
17
16
|
initialSize: this.serializedSize(value) ?? void 0,
|
|
18
|
-
...options
|
|
17
|
+
...options
|
|
19
18
|
});
|
|
20
19
|
this.#write(value, writer);
|
|
21
20
|
return writer.toBytes();
|
|
@@ -211,7 +210,7 @@ var BcsEnum = class extends BcsType {
|
|
|
211
210
|
};
|
|
212
211
|
},
|
|
213
212
|
write: (value, writer) => {
|
|
214
|
-
const [name, val] = Object.entries(value).filter(([name
|
|
213
|
+
const [name, val] = Object.entries(value).filter(([name]) => Object.hasOwn(fields, name))[0];
|
|
215
214
|
for (let i = 0; i < canonicalOrder.length; i++) {
|
|
216
215
|
const [optionName, optionType] = canonicalOrder[i];
|
|
217
216
|
if (optionName === name) {
|
|
@@ -263,7 +262,7 @@ var BcsTuple = class extends BcsType {
|
|
|
263
262
|
});
|
|
264
263
|
}
|
|
265
264
|
};
|
|
266
|
-
|
|
267
265
|
//#endregion
|
|
268
266
|
export { BcsEnum, BcsStruct, BcsTuple, BcsType, SerializedBcs, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, isSerializedBcs, lazyBcsType, stringLikeBcsType, uIntBcsType };
|
|
267
|
+
|
|
269
268
|
//# sourceMappingURL=bcs-type.mjs.map
|
package/dist/bcs-type.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs-type.mjs","names":["#write","#serialize","options","#schema","#bytes","name"],"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;AACD,OAAK,OAAO,QAAQ;AACpB,OAAK,OAAO,QAAQ;AACpB,OAAK,iBAAiB,QAAQ,yBAAyB;AACvD,QAAKA,QAAS,QAAQ;AACtB,QAAKC,YACJ,QAAQ,eACN,OAAO,cAAY;GACpB,MAAM,SAAS,IAAI,UAAU;IAC5B,aAAa,KAAK,eAAe,MAAM,IAAI;I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,GAAG,MAAM,MAAM,IAAI,OAAO;;GAGnC,GAAG;GACH,WAAW,UAAU;AACpB,aAAS,WAAW,MAAM;AAC1B,QAAI,CAAC,MAAM,QAAQ,MAAM,CACxB,OAAM,IAAI,UAAU,yBAAyB,OAAO,QAAQ;AAE7D,QAAI,MAAM,WAAW,OAAO,OAC3B,OAAM,IAAI,UAAU,4BAA4B,OAAO,OAAO,UAAU,MAAM,SAAS;;GAGzF,CAAC"}
|
|
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,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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
|
@@ -30,8 +30,8 @@ declare function vector<T, Input, Name extends string = string>(type: BcsType<T,
|
|
|
30
30
|
* Comparison is done byte-by-byte first, then by length if all compared bytes are equal.
|
|
31
31
|
*/
|
|
32
32
|
declare function compareBcsBytes(a: Uint8Array, b: Uint8Array): number;
|
|
33
|
-
declare function map<K
|
|
34
|
-
declare function map<K
|
|
33
|
+
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']}>`>;
|
|
34
|
+
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}>`>;
|
|
35
35
|
declare const bcs: {
|
|
36
36
|
/**
|
|
37
37
|
* Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
|
package/dist/bcs.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../src/bcs.ts"],"
|
|
1
|
+
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../src/bcs.ts"],"mappings":";;;;iBAyBS,UAAA,WAAqB,OAAA,qCAC7B,IAAA,UACA,IAAA,EAAM,CAAA,EACN,OAAA,GAAU,cAAA,CACT,YAAA,CAAa,CAAA,KACb,QAAA,CAAS,aAAA,CAAc,CAAA;EAAQ,MAAA;AAAA,GAC/B,IAAA,IAEC,OAAA,C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ACL,CAAA,GAAA,CAAA,CAAA,CAAA,UAAA,OAAA,uCAAA,GAAA,sBAAA,MAAA,EAEZ,CAAA,EAAC,OAAA,GACC,cAAA,2BAEc,CAAA,GAAI,CAAA,CAAE,GAAA,UAAW,OAAA,mBAAwB,GAAA,4BAGnD,CAAA,GAAI,CAAA,CAAE,GAAA,UAAW,OAAA,mBAAwB,GAAA,YAEtD,IAAA,IACA,QAAA,CAAA,CAAA,EAAA,IAAA;EAtYF;;;;;AACyB;AAAA;;;;;;mBAyZR,MAAA,SAAe,OAAA,4CAAwC,IAAA,EACjE,IAAA,EAAI,MAAA,EACF,CAAA,EAAC,OAAA,GACC,IAAA,CACT,cAAA,eAEc,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,iBAAwB,CAAA,0BAGzC,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,iBAAwB,CAAA,uBAIvD,SAAA,CAAA,CAAA;EAxYD;;;;;;;;;;;;;;;;iBAiac,MAAA,SAAe,OAAA,mDAA+C,IAAA,EACtE,IAAA,EAAI,MAAA,EACF,CAAA,EAAC,OAAA,GACC,IAAA,CACT,cAAA,CACC,eAAA,eACa,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,sBAA6B,CAAA,YAE3D,cAAA,eACa,CAAA,GAAI,CAAA,CAAE,CAAA,UAAW,OAAA,sBAA6B,CAAA,+BAE3D,IAAA,aAGD,OAAA,CAAA,CAAA,EAAA,IAAA;EAlbqC;;;;;;;;;EAIf;;;;iBAqcT,OAAA,OAAY,EAAA,QAAY,CAAA,GAAI,CAAA;AAAA"}
|
package/dist/bcs.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ulebEncode } from "./uleb.mjs";
|
|
2
2
|
import { BcsEnum, BcsStruct, BcsTuple, BcsType, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, lazyBcsType, stringLikeBcsType, uIntBcsType } from "./bcs-type.mjs";
|
|
3
|
-
|
|
4
3
|
//#region src/bcs.ts
|
|
5
4
|
function fixedArray(size, type, options) {
|
|
6
5
|
return new BcsType({
|
|
@@ -86,6 +85,11 @@ function map(keyType, valueType) {
|
|
|
86
85
|
});
|
|
87
86
|
}
|
|
88
87
|
const bcs = {
|
|
88
|
+
/**
|
|
89
|
+
* Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
|
|
90
|
+
* @example
|
|
91
|
+
* bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
|
|
92
|
+
*/
|
|
89
93
|
u8(options) {
|
|
90
94
|
return uIntBcsType({
|
|
91
95
|
readMethod: "read8",
|
|
@@ -96,6 +100,11 @@ const bcs = {
|
|
|
96
100
|
name: options?.name ?? "u8"
|
|
97
101
|
});
|
|
98
102
|
},
|
|
103
|
+
/**
|
|
104
|
+
* Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
|
|
105
|
+
* @example
|
|
106
|
+
* bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
|
|
107
|
+
*/
|
|
99
108
|
u16(options) {
|
|
100
109
|
return uIntBcsType({
|
|
101
110
|
readMethod: "read16",
|
|
@@ -106,6 +115,11 @@ const bcs = {
|
|
|
106
115
|
name: options?.name ?? "u16"
|
|
107
116
|
});
|
|
108
117
|
},
|
|
118
|
+
/**
|
|
119
|
+
* Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
|
|
120
|
+
* @example
|
|
121
|
+
* bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
|
|
122
|
+
*/
|
|
109
123
|
u32(options) {
|
|
110
124
|
return uIntBcsType({
|
|
111
125
|
readMethod: "read32",
|
|
@@ -116,6 +130,11 @@ const bcs = {
|
|
|
116
130
|
name: options?.name ?? "u32"
|
|
117
131
|
});
|
|
118
132
|
},
|
|
133
|
+
/**
|
|
134
|
+
* Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
|
|
135
|
+
* @example
|
|
136
|
+
* bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
|
|
137
|
+
*/
|
|
119
138
|
u64(options) {
|
|
120
139
|
return bigUIntBcsType({
|
|
121
140
|
readMethod: "read64",
|
|
@@ -126,6 +145,11 @@ const bcs = {
|
|
|
126
145
|
name: options?.name ?? "u64"
|
|
127
146
|
});
|
|
128
147
|
},
|
|
148
|
+
/**
|
|
149
|
+
* Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
|
|
150
|
+
* @example
|
|
151
|
+
* bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
|
|
152
|
+
*/
|
|
129
153
|
u128(options) {
|
|
130
154
|
return bigUIntBcsType({
|
|
131
155
|
readMethod: "read128",
|
|
@@ -136,6 +160,11 @@ const bcs = {
|
|
|
136
160
|
name: options?.name ?? "u128"
|
|
137
161
|
});
|
|
138
162
|
},
|
|
163
|
+
/**
|
|
164
|
+
* Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
|
|
165
|
+
* @example
|
|
166
|
+
* bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
|
|
167
|
+
*/
|
|
139
168
|
u256(options) {
|
|
140
169
|
return bigUIntBcsType({
|
|
141
170
|
readMethod: "read256",
|
|
@@ -146,6 +175,11 @@ const bcs = {
|
|
|
146
175
|
name: options?.name ?? "u256"
|
|
147
176
|
});
|
|
148
177
|
},
|
|
178
|
+
/**
|
|
179
|
+
* Creates a BcsType that can be used to read and write boolean values.
|
|
180
|
+
* @example
|
|
181
|
+
* bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
|
|
182
|
+
*/
|
|
149
183
|
bool(options) {
|
|
150
184
|
return fixedSizeBcsType({
|
|
151
185
|
size: 1,
|
|
@@ -159,6 +193,11 @@ const bcs = {
|
|
|
159
193
|
}
|
|
160
194
|
});
|
|
161
195
|
},
|
|
196
|
+
/**
|
|
197
|
+
* Creates a BcsType that can be used to read and write unsigned LEB encoded integers
|
|
198
|
+
* @example
|
|
199
|
+
*
|
|
200
|
+
*/
|
|
162
201
|
uleb128(options) {
|
|
163
202
|
return dynamicSizeBcsType({
|
|
164
203
|
read: (reader) => reader.readULEB(),
|
|
@@ -169,6 +208,12 @@ const bcs = {
|
|
|
169
208
|
name: options?.name ?? "uleb128"
|
|
170
209
|
});
|
|
171
210
|
},
|
|
211
|
+
/**
|
|
212
|
+
* Creates a BcsType representing a fixed length byte array
|
|
213
|
+
* @param size The number of bytes this types represents
|
|
214
|
+
* @example
|
|
215
|
+
* bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
|
|
216
|
+
*/
|
|
172
217
|
bytes(size, options) {
|
|
173
218
|
return fixedSizeBcsType({
|
|
174
219
|
size,
|
|
@@ -185,6 +230,12 @@ const bcs = {
|
|
|
185
230
|
}
|
|
186
231
|
});
|
|
187
232
|
},
|
|
233
|
+
/**
|
|
234
|
+
* Creates a BcsType representing a variable length byte array
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
|
|
238
|
+
*/
|
|
188
239
|
byteVector(options) {
|
|
189
240
|
return new BcsType({
|
|
190
241
|
read: (reader) => {
|
|
@@ -208,6 +259,11 @@ const bcs = {
|
|
|
208
259
|
}
|
|
209
260
|
});
|
|
210
261
|
},
|
|
262
|
+
/**
|
|
263
|
+
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
|
|
264
|
+
* @example
|
|
265
|
+
* bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
|
|
266
|
+
*/
|
|
211
267
|
string(options) {
|
|
212
268
|
return stringLikeBcsType({
|
|
213
269
|
toBytes: (value) => new TextEncoder().encode(value),
|
|
@@ -216,15 +272,56 @@ const bcs = {
|
|
|
216
272
|
name: options?.name ?? "string"
|
|
217
273
|
});
|
|
218
274
|
},
|
|
275
|
+
/**
|
|
276
|
+
* Creates a BcsType that represents a fixed length array of a given type
|
|
277
|
+
* @param size The number of elements in the array
|
|
278
|
+
* @param type The BcsType of each element in the array
|
|
279
|
+
* @example
|
|
280
|
+
* bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
|
|
281
|
+
*/
|
|
219
282
|
fixedArray,
|
|
283
|
+
/**
|
|
284
|
+
* Creates a BcsType representing an optional value
|
|
285
|
+
* @param type The BcsType of the optional value
|
|
286
|
+
* @example
|
|
287
|
+
* bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
|
|
288
|
+
* bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
|
|
289
|
+
*/
|
|
220
290
|
option,
|
|
291
|
+
/**
|
|
292
|
+
* Creates a BcsType representing a variable length vector of a given type
|
|
293
|
+
* @param type The BcsType of each element in the vector
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
|
|
297
|
+
*/
|
|
221
298
|
vector,
|
|
299
|
+
/**
|
|
300
|
+
* Creates a BcsType representing a tuple of a given set of types
|
|
301
|
+
* @param types The BcsTypes for each element in the tuple
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
|
|
305
|
+
* tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
|
|
306
|
+
*/
|
|
222
307
|
tuple(fields, options) {
|
|
223
308
|
return new BcsTuple({
|
|
224
309
|
fields,
|
|
225
310
|
...options
|
|
226
311
|
});
|
|
227
312
|
},
|
|
313
|
+
/**
|
|
314
|
+
* Creates a BcsType representing a struct of a given set of fields
|
|
315
|
+
* @param name The name of the struct
|
|
316
|
+
* @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* const struct = bcs.struct('MyStruct', {
|
|
320
|
+
* a: bcs.u8(),
|
|
321
|
+
* b: bcs.string(),
|
|
322
|
+
* })
|
|
323
|
+
* struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
324
|
+
*/
|
|
228
325
|
struct(name, fields, options) {
|
|
229
326
|
return new BcsStruct({
|
|
230
327
|
name,
|
|
@@ -232,6 +329,22 @@ const bcs = {
|
|
|
232
329
|
...options
|
|
233
330
|
});
|
|
234
331
|
},
|
|
332
|
+
/**
|
|
333
|
+
* Creates a BcsType representing an enum of a given set of options
|
|
334
|
+
* @param name The name of the enum
|
|
335
|
+
* @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
|
|
336
|
+
* null can be used to represent a variant with no data.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* const enum = bcs.enum('MyEnum', {
|
|
340
|
+
* A: bcs.u8(),
|
|
341
|
+
* B: bcs.string(),
|
|
342
|
+
* C: null,
|
|
343
|
+
* })
|
|
344
|
+
* enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
|
|
345
|
+
* enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
346
|
+
* enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
|
|
347
|
+
*/
|
|
235
348
|
enum(name, fields, options) {
|
|
236
349
|
return new BcsEnum({
|
|
237
350
|
name,
|
|
@@ -239,12 +352,24 @@ const bcs = {
|
|
|
239
352
|
...options
|
|
240
353
|
});
|
|
241
354
|
},
|
|
355
|
+
/**
|
|
356
|
+
* Creates a BcsType representing a map of a given key and value type
|
|
357
|
+
* @param keyType The BcsType of the key
|
|
358
|
+
* @param valueType The BcsType of the value
|
|
359
|
+
* @example
|
|
360
|
+
* const map = bcs.map(bcs.u8(), bcs.string())
|
|
361
|
+
* map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
|
|
362
|
+
*/
|
|
242
363
|
map,
|
|
364
|
+
/**
|
|
365
|
+
* Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
|
|
366
|
+
* @param cb A callback that returns the BcsType
|
|
367
|
+
*/
|
|
243
368
|
lazy(cb) {
|
|
244
369
|
return lazyBcsType(cb);
|
|
245
370
|
}
|
|
246
371
|
};
|
|
247
|
-
|
|
248
372
|
//#endregion
|
|
249
373
|
export { bcs, compareBcsBytes };
|
|
374
|
+
|
|
250
375
|
//# sourceMappingURL=bcs.mjs.map
|