@novasamatech/scale 0.5.3-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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @novasamatech/host-papp
2
+
3
+ Polkadot app integration layer for host applications.
4
+
5
+ ## Overview
6
+
7
+ ## Installation
8
+
9
+ ```shell
10
+ npm install @novasamatech/host-papp --save -E
11
+ ```
package/dist/enum.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { Codec, StringRecord } from 'scale-ts';
2
+ type FilterStringRecord<T extends Record<string, Codec<any>>> = T extends StringRecord<Codec<any>> ? T : never;
3
+ export type EnumCodec<T extends Record<string, Codec<any>>> = ReturnType<typeof Enum<T>>;
4
+ export declare const Enum: <T extends Record<string, Codec<any>>>(inner: T) => Codec<(FilterStringRecord<T> extends infer T_1 extends StringRecord<Codec<any>> ? { [K in keyof T_1]: {
5
+ tag: K;
6
+ value: import("scale-ts").CodecType<T_1[K]>;
7
+ }; } : never)[keyof FilterStringRecord<T>]>;
8
+ export {};
package/dist/enum.js ADDED
@@ -0,0 +1,2 @@
1
+ import { Enum as ScaleEnum } from 'scale-ts';
2
+ export const Enum = (inner) => ScaleEnum(inner);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { bool, u8 } from 'scale-ts';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { Enum } from './enum.js';
4
+ describe('Enum', () => {
5
+ it('should correctly encode/decode Enum', () => {
6
+ const codec = Enum({
7
+ a: u8,
8
+ b: bool,
9
+ });
10
+ expect(codec.enc({ tag: 'a', value: 1 })).toEqual(new Uint8Array([0, 1]));
11
+ expect(codec.enc({ tag: 'b', value: true })).toEqual(new Uint8Array([1, 1]));
12
+ expect(codec.dec(new Uint8Array([0, 1]))).toEqual({ tag: 'a', value: 1 });
13
+ expect(codec.dec(new Uint8Array([1, 1]))).toEqual({ tag: 'b', value: true });
14
+ });
15
+ });
package/dist/err.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { Codec } from 'scale-ts';
2
+ export type CodecError<T, Name extends string> = Error & {
3
+ name: Name;
4
+ instance: string;
5
+ payload: T;
6
+ };
7
+ export type ErrCodec<T, Name extends string> = Codec<CodecError<T, Name>> & CodecErrorConstructor<T, Name>;
8
+ type Constructor<A extends Array<any>, T> = new (...args: A) => T;
9
+ type CodecErrorConstructor<T, Name extends string> = Constructor<T extends undefined ? [void] : [T], CodecError<T, Name>>;
10
+ export declare function Err<const T, const Name extends string>(name: Name, value: Codec<T>, message: string | ((value: NoInfer<T>) => string), className?: string): ErrCodec<T, Name>;
11
+ export {};
package/dist/err.js ADDED
@@ -0,0 +1,31 @@
1
+ import { enhanceCodec } from 'scale-ts';
2
+ export function Err(name, value, message, className = name) {
3
+ // Defining class with dynamic name
4
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
5
+ const C = {
6
+ [className]: class extends Error {
7
+ instance = className;
8
+ name = name;
9
+ payload;
10
+ constructor(data) {
11
+ super(typeof message === 'function' ? message(data) : message);
12
+ this.payload = data;
13
+ }
14
+ // codec array destructuring workaround
15
+ static [Symbol.iterator]() {
16
+ return errorCodec[Symbol.iterator]();
17
+ }
18
+ // codec fields access workaround
19
+ get enc() {
20
+ return errorCodec.enc;
21
+ }
22
+ get dec() {
23
+ return errorCodec.dec;
24
+ }
25
+ },
26
+ }[className];
27
+ const errorCodec = enhanceCodec(value, v => v.payload,
28
+ // @ts-expect-error don't want to fix it really
29
+ v => new C(v));
30
+ return Object.assign(C, errorCodec);
31
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import { Struct, _void, bool } from 'scale-ts';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { Err } from './err.js';
4
+ describe('Err', () => {
5
+ it('should correctly construct Error', () => {
6
+ const ErrorCodec = Err('TestError', _void, 'Test message');
7
+ const error = new ErrorCodec(undefined);
8
+ expect(error).toBeInstanceOf(ErrorCodec);
9
+ expect(error.name).toBe('TestError');
10
+ expect(error.message).toBe('Test message');
11
+ });
12
+ it('should correctly encode/decode Err', () => {
13
+ const payload = Struct({ enable: bool });
14
+ const ErrorCodec = Err('TestError', payload, 'Test message');
15
+ const error = new ErrorCodec({ enable: true });
16
+ expect(ErrorCodec.enc(error)).toEqual(new Uint8Array([1]));
17
+ expect(ErrorCodec.dec(ErrorCodec.enc(error))).toEqual(new ErrorCodec({ enable: true }));
18
+ });
19
+ });
@@ -0,0 +1,11 @@
1
+ import type { Codec, CodecType } from 'scale-ts';
2
+ import type { CodecError, ErrCodec } from './err.js';
3
+ type MapErrEnum<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = {
4
+ [K in keyof T]: ErrCodec<CodecType<T[K][0]>, K extends string ? `${Name}::${K}` : Name>;
5
+ };
6
+ type ErrEnumInput<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = {
7
+ [K in keyof T]: CodecError<CodecType<T[K][0]>, K extends string ? `${Name}::${K}` : Name>;
8
+ }[keyof T];
9
+ type ErrEnumArguments<T> = [value: Codec<T>, message: string | ((value: T) => string)];
10
+ export declare function ErrEnum<const Name extends string, const T extends Record<string, ErrEnumArguments<any>>>(name: Name, inner: T): Codec<ErrEnumInput<Name, T>> & MapErrEnum<Name, T>;
11
+ export {};
@@ -0,0 +1,10 @@
1
+ import { enhanceCodec } from 'scale-ts';
2
+ import { Enum } from './enum.js';
3
+ import { Err } from './err.js';
4
+ export function ErrEnum(name, inner) {
5
+ const values = Object.fromEntries(Object.entries(inner).map(([k, [value, message]]) => {
6
+ return [k, Err(`${name}::${k}`, value, message, k)];
7
+ }));
8
+ const codec = enhanceCodec(Enum(values), v => ({ tag: v.instance, value: v }), v => v.value);
9
+ return Object.assign(codec, values);
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ import { _void } from 'scale-ts';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { ErrEnum } from './errEnum.js';
4
+ describe('ErrEnum', () => {
5
+ it('should correctly construct ErrorEnum field', () => {
6
+ const ErrorCodec = ErrEnum('ErrorCodec', {
7
+ TestError: [_void, 'Test message'],
8
+ });
9
+ const error = new ErrorCodec.TestError(undefined);
10
+ expect(error).toBeInstanceOf(ErrorCodec.TestError);
11
+ expect(error.name).toBe('ErrorCodec::TestError');
12
+ expect(error.message).toBe('Test message');
13
+ });
14
+ it('should correctly serialize/deserialize', () => {
15
+ const ErrorCodec = ErrEnum('ErrorCodec', {
16
+ First: [_void, 'First'],
17
+ Second: [_void, 'Second'],
18
+ });
19
+ const first = new ErrorCodec.First(undefined);
20
+ const second = new ErrorCodec.Second(undefined);
21
+ expect(ErrorCodec.enc(first)).toEqual(new Uint8Array([0]));
22
+ expect(ErrorCodec.enc(second)).toEqual(new Uint8Array([1]));
23
+ expect(ErrorCodec.dec(ErrorCodec.enc(first))).toEqual(first);
24
+ expect(ErrorCodec.dec(ErrorCodec.enc(second))).toEqual(second);
25
+ expect(ErrorCodec.dec(ErrorCodec.enc(first))).not.toEqual(second);
26
+ expect(ErrorCodec.dec(ErrorCodec.enc(second))).not.toEqual(first);
27
+ });
28
+ });
@@ -0,0 +1,29 @@
1
+ import type { ResultPayload } from 'scale-ts';
2
+ import type { HexString } from './hex.js';
3
+ export declare function unwrapResultOrThrow<Ok, Err>(response: ResultPayload<Ok, Err>, toError: (e: Err) => Error): Ok;
4
+ export declare function resultOk<const T>(value: T): {
5
+ success: true;
6
+ value: T;
7
+ };
8
+ export declare function resultErr<const T>(e: T): {
9
+ success: false;
10
+ value: T;
11
+ };
12
+ export declare function enumValue<const Tag extends string, const Value>(tag: Tag, value: Value): {
13
+ tag: Tag;
14
+ value: Value;
15
+ };
16
+ export declare function isEnumVariant<const Enum extends {
17
+ tag: string;
18
+ value: unknown;
19
+ }, const Tag extends Enum['tag']>(v: Enum, tag: Tag): v is Extract<Enum, {
20
+ tag: Tag;
21
+ }>;
22
+ export declare function assertEnumVariant<const Enum extends {
23
+ tag: string;
24
+ value: unknown;
25
+ }, const Tag extends Enum['tag']>(v: Enum, tag: Tag, message: string): asserts v is Extract<Enum, {
26
+ tag: Tag;
27
+ }>;
28
+ export declare function toHex(data: Uint8Array): HexString;
29
+ export declare function fromHex(hex: string): Uint8Array<ArrayBufferLike>;
@@ -0,0 +1,30 @@
1
+ import { fromHex as papiFromHex, toHex as papiToHex } from '@polkadot-api/utils';
2
+ export function unwrapResultOrThrow(response, toError) {
3
+ if (response.success) {
4
+ return response.value;
5
+ }
6
+ throw toError(response.value);
7
+ }
8
+ export function resultOk(value) {
9
+ return { success: true, value };
10
+ }
11
+ export function resultErr(e) {
12
+ return { success: false, value: e };
13
+ }
14
+ export function enumValue(tag, value) {
15
+ return { tag, value };
16
+ }
17
+ export function isEnumVariant(v, tag) {
18
+ return v.tag === tag;
19
+ }
20
+ export function assertEnumVariant(v, tag, message) {
21
+ if (!isEnumVariant(v, tag)) {
22
+ throw new Error(message);
23
+ }
24
+ }
25
+ export function toHex(data) {
26
+ return papiToHex(data);
27
+ }
28
+ export function fromHex(hex) {
29
+ return papiFromHex(hex);
30
+ }
package/dist/hex.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export type HexString = `0x${string}`;
2
+ /**
3
+ * Wrapper around Bytes codec. Every usage of Hex codec should be threaded as raw Bytes with mapping to hex string.
4
+ * @param [length] Optional, corresponds to byte array size, not the length of hex string.
5
+ */
6
+ export declare const Hex: (length?: number) => import("scale-ts").Codec<`0x${string}`>;
package/dist/hex.js ADDED
@@ -0,0 +1,7 @@
1
+ import { fromHex, toHex } from '@polkadot-api/utils';
2
+ import { Bytes, enhanceCodec } from 'scale-ts';
3
+ /**
4
+ * Wrapper around Bytes codec. Every usage of Hex codec should be threaded as raw Bytes with mapping to hex string.
5
+ * @param [length] Optional, corresponds to byte array size, not the length of hex string.
6
+ */
7
+ export const Hex = (length) => enhanceCodec(Bytes(length), fromHex, v => toHex(v));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { Hex } from './hex.js';
3
+ describe('Hex', () => {
4
+ it('should correctly encode/decode Hex with arbitrary length', () => {
5
+ const hex = '0xffffff';
6
+ const codec = Hex();
7
+ expect(codec.enc(hex)).toEqual(new Uint8Array([12, 255, 255, 255]));
8
+ expect(codec.dec(codec.enc(hex))).toEqual(hex);
9
+ });
10
+ it('should correctly encode/decode Hex with fixed length', () => {
11
+ const hex = '0xffffff';
12
+ const codec = Hex(3);
13
+ expect(codec.enc(hex)).toEqual(new Uint8Array([255, 255, 255]));
14
+ expect(codec.dec(codec.enc(hex))).toEqual(hex);
15
+ });
16
+ });
@@ -0,0 +1,10 @@
1
+ export type { HexString } from './hex.js';
2
+ export { Hex } from './hex.js';
3
+ export { Nullable } from './nullable.js';
4
+ export { Status } from './status.js';
5
+ export type { EnumCodec } from './enum.js';
6
+ export { Enum } from './enum.js';
7
+ export type { CodecError, ErrCodec } from './err.js';
8
+ export { Err } from './err.js';
9
+ export { ErrEnum } from './errEnum.js';
10
+ export { assertEnumVariant, enumValue, fromHex, isEnumVariant, resultErr, resultOk, toHex, unwrapResultOrThrow, } from './helpers.js';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { Hex } from './hex.js';
2
+ export { Nullable } from './nullable.js';
3
+ export { Status } from './status.js';
4
+ export { Enum } from './enum.js';
5
+ export { Err } from './err.js';
6
+ export { ErrEnum } from './errEnum.js';
7
+ export { assertEnumVariant, enumValue, fromHex, isEnumVariant, resultErr, resultOk, toHex, unwrapResultOrThrow, } from './helpers.js';
@@ -0,0 +1,2 @@
1
+ import type { Codec } from 'scale-ts';
2
+ export declare function Nullable<T>(inner: Codec<T>): Codec<T | null>;
@@ -0,0 +1,4 @@
1
+ import { Option, enhanceCodec } from 'scale-ts';
2
+ export function Nullable(inner) {
3
+ return enhanceCodec(Option(inner), v => (v === null ? undefined : v), v => (v === undefined ? null : v));
4
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Enum without values
3
+ */
4
+ export declare function Status<const T>(...list: T[]): import("scale-ts").Codec<T>;
package/dist/status.js ADDED
@@ -0,0 +1,19 @@
1
+ import { enhanceCodec, u8 } from 'scale-ts';
2
+ /**
3
+ * Enum without values
4
+ */
5
+ export function Status(...list) {
6
+ return enhanceCodec(u8, v => {
7
+ const i = list.indexOf(v);
8
+ if (i === -1) {
9
+ throw new Error(`Unknown status value: ${v}`);
10
+ }
11
+ return i;
12
+ }, i => {
13
+ const v = list.at(i);
14
+ if (v === undefined) {
15
+ throw new Error(`Unknown status index: ${i}`);
16
+ }
17
+ return v;
18
+ });
19
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { Status } from './status.js';
3
+ describe('Status', () => {
4
+ it('should correctly encode/decode Status', () => {
5
+ const codec = Status('New', 'Used');
6
+ expect(codec.enc('New')).toEqual(new Uint8Array([0]));
7
+ expect(codec.enc('Used')).toEqual(new Uint8Array([1]));
8
+ expect(codec.dec('0x00')).toEqual('New');
9
+ expect(codec.dec('0x01')).toEqual('Used');
10
+ // @ts-expect-error for test
11
+ expect(() => codec.enc('Unknown')).toThrowErrorMatchingInlineSnapshot(`[Error: Unknown status value: Unknown]`);
12
+ expect(() => codec.dec('0x03')).toThrowErrorMatchingInlineSnapshot(`[Error: Unknown status index: 3]`);
13
+ });
14
+ });
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@novasamatech/scale",
3
+ "type": "module",
4
+ "version": "0.5.3-0",
5
+ "description": "additional scale-ts bindings",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/novasamatech/spektr-sdk.git"
10
+ },
11
+ "keywords": [
12
+ "polkadot"
13
+ ],
14
+ "main": "dist/index.js",
15
+ "exports": {
16
+ "./package.json": "./package.json",
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "dependencies": {
27
+ "@polkadot-api/utils": "^0.2.0",
28
+ "scale-ts": "1.6.1"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ }
33
+ }