@novasamatech/host-api 0.5.1 → 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 +3 -3
- package/dist/helpers.d.ts +0 -29
- package/dist/helpers.js +0 -30
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/protocol/commonCodecs.d.ts +3 -38
- package/dist/protocol/commonCodecs.js +2 -59
- package/dist/protocol/impl.d.ts +3 -3
- package/dist/protocol/impl.js +3 -3
- package/dist/protocol/messageCodec.d.ts +109 -493
- package/dist/protocol/messageCodec.js +3 -3
- package/dist/protocol/v1/accounts.d.ts +37 -209
- package/dist/protocol/v1/accounts.js +2 -1
- package/dist/protocol/v1/chat.d.ts +27 -87
- package/dist/protocol/v1/chat.js +2 -1
- package/dist/protocol/v1/createTransaction.d.ts +20 -116
- package/dist/protocol/v1/createTransaction.js +2 -1
- package/dist/protocol/v1/feature.d.ts +3 -7
- package/dist/protocol/v1/feature.js +2 -1
- package/dist/protocol/v1/handshake.d.ts +15 -75
- package/dist/protocol/v1/handshake.js +2 -1
- package/dist/protocol/v1/jsonRpc.d.ts +3 -7
- package/dist/protocol/v1/permission.d.ts +14 -54
- package/dist/protocol/v1/permission.js +2 -1
- package/dist/protocol/v1/sign.d.ts +18 -90
- package/dist/protocol/v1/sign.js +2 -1
- package/dist/protocol/v1/statementStore.d.ts +15 -75
- package/dist/protocol/v1/statementStore.js +2 -1
- package/dist/protocol/v1/storage.d.ts +20 -76
- package/dist/protocol/v1/storage.js +3 -2
- package/dist/transport.js +5 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ Transport is a low-level wrapper around protocol and provider.
|
|
|
29
29
|
It encapsulates serialization/deserialization and request/subscription logic.
|
|
30
30
|
|
|
31
31
|
```typescript
|
|
32
|
-
import { createTransport,
|
|
32
|
+
import { createTransport, resultOk } from '@novasamatech/host-api';
|
|
33
33
|
import { provider } from './custom-provider.js';
|
|
34
34
|
|
|
35
35
|
const transport = createTransport(provider);
|
|
@@ -43,9 +43,9 @@ const response = await transport.request('storage_read', payload);
|
|
|
43
43
|
const stop = transport.handleRequest('storage_read', async (payload) => {
|
|
44
44
|
try {
|
|
45
45
|
const result = await readFromStorage(payload);
|
|
46
|
-
return
|
|
46
|
+
return resultOk(result);
|
|
47
47
|
} catch (e) {
|
|
48
|
-
return
|
|
48
|
+
return resultErr(e);
|
|
49
49
|
}
|
|
50
50
|
});
|
|
51
51
|
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { ResultPayload } from 'scale-ts';
|
|
2
1
|
import type { ComposeMessageAction } from './protocol/messageCodec.js';
|
|
3
|
-
import type { HexString } from './protocol/types.js';
|
|
4
2
|
export declare function delay(ttl: number): Promise<void>;
|
|
5
3
|
type PromiseWithResolvers<T> = {
|
|
6
4
|
promise: Promise<T>;
|
|
@@ -8,34 +6,7 @@ type PromiseWithResolvers<T> = {
|
|
|
8
6
|
reject: (reason: unknown) => void;
|
|
9
7
|
};
|
|
10
8
|
export declare const promiseWithResolvers: <const T>() => PromiseWithResolvers<T>;
|
|
11
|
-
export declare function unwrapResultOrThrow<Ok, Err>(response: ResultPayload<Ok, Err>, toError: (e: Err) => Error): Ok;
|
|
12
|
-
export declare function okResult<const T>(value: T): {
|
|
13
|
-
success: true;
|
|
14
|
-
value: T;
|
|
15
|
-
};
|
|
16
|
-
export declare function errResult<const T>(e: T): {
|
|
17
|
-
success: false;
|
|
18
|
-
value: T;
|
|
19
|
-
};
|
|
20
|
-
export declare function enumValue<const Tag extends string, const Value>(tag: Tag, value: Value): {
|
|
21
|
-
tag: Tag;
|
|
22
|
-
value: Value;
|
|
23
|
-
};
|
|
24
|
-
export declare function isEnumVariant<const Enum extends {
|
|
25
|
-
tag: string;
|
|
26
|
-
value: unknown;
|
|
27
|
-
}, const Tag extends Enum['tag']>(v: Enum, tag: Tag): v is Extract<Enum, {
|
|
28
|
-
tag: Tag;
|
|
29
|
-
}>;
|
|
30
|
-
export declare function assertEnumVariant<const Enum extends {
|
|
31
|
-
tag: string;
|
|
32
|
-
value: unknown;
|
|
33
|
-
}, const Tag extends Enum['tag']>(v: Enum, tag: Tag, message: string): asserts v is Extract<Enum, {
|
|
34
|
-
tag: Tag;
|
|
35
|
-
}>;
|
|
36
9
|
export declare function composeAction<const Method extends string, const Suffix extends string>(method: Method, suffix: Suffix): ComposeMessageAction<Method, Suffix>;
|
|
37
10
|
export declare function createRequestId(): string;
|
|
38
11
|
export declare function extractErrorMessage(err: unknown): string;
|
|
39
|
-
export declare function toHex(value: Uint8Array): HexString;
|
|
40
|
-
export declare function fromHex(value: string): Uint8Array;
|
|
41
12
|
export {};
|
package/dist/helpers.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { fromHex as papiFromHex, toHex as papiToHex } from '@polkadot-api/utils';
|
|
2
1
|
import { nanoid } from 'nanoid';
|
|
3
2
|
export function delay(ttl) {
|
|
4
3
|
return new Promise(resolve => setTimeout(resolve, ttl));
|
|
@@ -13,29 +12,6 @@ export const promiseWithResolvers = () => {
|
|
|
13
12
|
// @ts-expect-error before assign
|
|
14
13
|
return { promise, resolve, reject };
|
|
15
14
|
};
|
|
16
|
-
export function unwrapResultOrThrow(response, toError) {
|
|
17
|
-
if (response.success) {
|
|
18
|
-
return response.value;
|
|
19
|
-
}
|
|
20
|
-
throw toError(response.value);
|
|
21
|
-
}
|
|
22
|
-
export function okResult(value) {
|
|
23
|
-
return { success: true, value };
|
|
24
|
-
}
|
|
25
|
-
export function errResult(e) {
|
|
26
|
-
return { success: false, value: e };
|
|
27
|
-
}
|
|
28
|
-
export function enumValue(tag, value) {
|
|
29
|
-
return { tag, value };
|
|
30
|
-
}
|
|
31
|
-
export function isEnumVariant(v, tag) {
|
|
32
|
-
return v.tag === tag;
|
|
33
|
-
}
|
|
34
|
-
export function assertEnumVariant(v, tag, message) {
|
|
35
|
-
if (!isEnumVariant(v, tag)) {
|
|
36
|
-
throw new Error(message);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
15
|
export function composeAction(method, suffix) {
|
|
40
16
|
return `${method}_${suffix}`;
|
|
41
17
|
}
|
|
@@ -51,9 +27,3 @@ export function extractErrorMessage(err) {
|
|
|
51
27
|
}
|
|
52
28
|
return 'Unknown error occurred.';
|
|
53
29
|
}
|
|
54
|
-
export function toHex(value) {
|
|
55
|
-
return papiToHex(value);
|
|
56
|
-
}
|
|
57
|
-
export function fromHex(value) {
|
|
58
|
-
return papiFromHex(value);
|
|
59
|
-
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
export type { ConnectionStatus, Logger, Transport } from './types.js';
|
|
2
2
|
export type { Provider } from './provider.js';
|
|
3
|
-
export {
|
|
4
|
-
export type { HexString } from './protocol/types.js';
|
|
3
|
+
export { createRequestId } from './helpers.js';
|
|
5
4
|
export { createHostApi } from './hostApi.js';
|
|
6
5
|
export { createTransport } from './transport.js';
|
|
7
6
|
export { createDefaultLogger } from './logger.js';
|
|
8
7
|
export type { HostApiProtocol, VersionedProtocolRequest, VersionedProtocolSubscription } from './protocol/impl.js';
|
|
9
8
|
export { hostApiProtocol } from './protocol/impl.js';
|
|
10
9
|
export type { Codec, CodecType } from 'scale-ts';
|
|
10
|
+
export type { HexString } from '@novasamatech/scale';
|
|
11
|
+
export { assertEnumVariant, enumValue, fromHex, isEnumVariant, resultErr, resultOk, toHex, unwrapResultOrThrow, } from '@novasamatech/scale';
|
|
11
12
|
export { GenericError } from './protocol/commonCodecs.js';
|
|
12
13
|
export { CreateTransactionErr, VersionedPublicTxPayload } from './protocol/v1/createTransaction.js';
|
|
13
14
|
export { Account, AccountId, CreateProofErr, ProductAccountId, RequestCredentialsErr } from './protocol/v1/accounts.js';
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createRequestId } from './helpers.js';
|
|
2
2
|
export { createHostApi } from './hostApi.js';
|
|
3
3
|
export { createTransport } from './transport.js';
|
|
4
4
|
export { createDefaultLogger } from './logger.js';
|
|
5
5
|
export { hostApiProtocol } from './protocol/impl.js';
|
|
6
|
+
export { assertEnumVariant, enumValue, fromHex, isEnumVariant, resultErr, resultOk, toHex, unwrapResultOrThrow, } from '@novasamatech/scale';
|
|
6
7
|
// Codecs
|
|
7
8
|
export { GenericError } from './protocol/commonCodecs.js';
|
|
8
9
|
export { CreateTransactionErr, VersionedPublicTxPayload } from './protocol/v1/createTransaction.js';
|
|
@@ -1,42 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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: CodecType<T_1[K]>;
|
|
7
|
-
}; } : never)[keyof FilterStringRecord<T>]>;
|
|
8
|
-
/**
|
|
9
|
-
* Enum without values
|
|
10
|
-
*/
|
|
11
|
-
export declare function Status<T extends string>(...list: T[]): Codec<T>;
|
|
12
|
-
export declare function Nullable<T>(inner: Codec<T>): Codec<T | null>;
|
|
13
|
-
type Constructor<A extends Array<any>, T> = new (...args: A) => T;
|
|
14
|
-
type CodecError<T, Name extends string> = Error & {
|
|
15
|
-
name: Name;
|
|
16
|
-
className: string;
|
|
17
|
-
payload: T;
|
|
18
|
-
};
|
|
19
|
-
type CodecErrorConstructor<T, Name extends string> = Constructor<T extends undefined ? [void] : [T], CodecError<T, Name>>;
|
|
20
|
-
export type ErrCodec<T, Name extends string> = Codec<CodecError<T, Name>> & CodecErrorConstructor<T, Name>;
|
|
21
|
-
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>;
|
|
22
|
-
type MapErrEnum<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = {
|
|
23
|
-
[K in keyof T]: ErrCodec<CodecType<T[K][0]>, K extends string ? `${Name}::${K}` : Name>;
|
|
24
|
-
};
|
|
25
|
-
type ErrEnumInput<Name extends string, T extends Record<string, ErrEnumArguments<any>>> = {
|
|
26
|
-
[K in keyof T]: CodecError<CodecType<T[K][0]>, K extends string ? `${Name}::${K}` : Name>;
|
|
27
|
-
}[keyof T];
|
|
28
|
-
type ErrEnumArguments<T> = [value: Codec<T>, message: string | ((value: T) => string)];
|
|
29
|
-
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>;
|
|
30
|
-
/**
|
|
31
|
-
* Wrapper around Bytes codec. Every usage of Hex codec should be threaded as raw Bytes with mapping to hex string.
|
|
32
|
-
* @param [length] Optional, corresponds to byte array size, not the length of hex string.
|
|
33
|
-
*/
|
|
34
|
-
export declare const Hex: (length?: number) => Codec<`0x${string}`>;
|
|
35
|
-
export declare const GenesisHash: Codec<`0x${string}`>;
|
|
36
|
-
export declare const GenericErr: Codec<{
|
|
1
|
+
export declare const GenesisHash: import("scale-ts").Codec<`0x${string}`>;
|
|
2
|
+
export declare const GenericErr: import("scale-ts").Codec<{
|
|
37
3
|
reason: string;
|
|
38
4
|
}>;
|
|
39
|
-
export declare const GenericError: ErrCodec<{
|
|
5
|
+
export declare const GenericError: import("@novasamatech/scale").ErrCodec<{
|
|
40
6
|
reason: string;
|
|
41
7
|
}, "GenericError">;
|
|
42
|
-
export {};
|
|
@@ -1,62 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export const Enum = (inner) => ScaleEnum(inner);
|
|
4
|
-
/**
|
|
5
|
-
* Enum without values
|
|
6
|
-
*/
|
|
7
|
-
export function Status(...list) {
|
|
8
|
-
return enhanceCodec(u8, v => {
|
|
9
|
-
const i = list.indexOf(v);
|
|
10
|
-
if (i === -1) {
|
|
11
|
-
throw new Error(`Unknown status value: ${v}`);
|
|
12
|
-
}
|
|
13
|
-
return i;
|
|
14
|
-
}, i => {
|
|
15
|
-
const v = list.at(i);
|
|
16
|
-
if (v === undefined) {
|
|
17
|
-
throw new Error(`Unknown status index: ${i}`);
|
|
18
|
-
}
|
|
19
|
-
return v;
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
export function Nullable(inner) {
|
|
23
|
-
return enhanceCodec(Option(inner), v => (v === null ? undefined : v), v => (v === undefined ? null : v));
|
|
24
|
-
}
|
|
25
|
-
export function Err(name, value, message, className = name) {
|
|
26
|
-
// Defining class with dynamic name
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
28
|
-
const C = {
|
|
29
|
-
[className]: class extends Error {
|
|
30
|
-
className = className;
|
|
31
|
-
name = name;
|
|
32
|
-
payload;
|
|
33
|
-
constructor(data) {
|
|
34
|
-
super(typeof message === 'function' ? message(data) : message);
|
|
35
|
-
this.payload = data;
|
|
36
|
-
}
|
|
37
|
-
// workaround for codec array destructuring
|
|
38
|
-
static [Symbol.iterator]() {
|
|
39
|
-
return errorCodec[Symbol.iterator]();
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
}[className];
|
|
43
|
-
const errorCodec = enhanceCodec(value, v => v.payload,
|
|
44
|
-
// @ts-expect-error don't want to fix it really
|
|
45
|
-
v => new C(v));
|
|
46
|
-
return Object.assign(C, errorCodec);
|
|
47
|
-
}
|
|
48
|
-
export function ErrEnum(name, inner) {
|
|
49
|
-
const values = Object.fromEntries(Object.entries(inner).map(([k, [value, message]]) => {
|
|
50
|
-
return [k, Err(`${name}::${k}`, value, message, k)];
|
|
51
|
-
}));
|
|
52
|
-
const codec = enhanceCodec(Enum(values), v => ({ tag: v.className, value: v }), v => v.value);
|
|
53
|
-
return Object.assign(codec, values);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Wrapper around Bytes codec. Every usage of Hex codec should be threaded as raw Bytes with mapping to hex string.
|
|
57
|
-
* @param [length] Optional, corresponds to byte array size, not the length of hex string.
|
|
58
|
-
*/
|
|
59
|
-
export const Hex = (length) => enhanceCodec(Bytes(length), fromHex, toHex);
|
|
1
|
+
import { Err, Hex } from '@novasamatech/scale';
|
|
2
|
+
import { Struct, str } from 'scale-ts';
|
|
60
3
|
export const GenesisHash = Hex();
|
|
61
4
|
export const GenericErr = Struct({
|
|
62
5
|
reason: str,
|
package/dist/protocol/impl.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { EnumCodec } from '@novasamatech/scale';
|
|
1
2
|
import type { Codec } from 'scale-ts';
|
|
2
|
-
import type { EnumCodec } from './commonCodecs.js';
|
|
3
3
|
import { AccountCreateProofV1_request, AccountCreateProofV1_response, AccountGetAliasV1_request, AccountGetAliasV1_response, AccountGetV1_request, AccountGetV1_response, GetNonProductAccountsV1_request, GetNonProductAccountsV1_response } from './v1/accounts.js';
|
|
4
4
|
import { ChatActionSubscribeV1_receive, ChatActionSubscribeV1_start, ChatCreateContactV1_request, ChatCreateContactV1_response, ChatPostMessageV1_request, ChatPostMessageV1_response } from './v1/chat.js';
|
|
5
5
|
import { CreateTransactionV1_request, CreateTransactionV1_response, CreateTransactionWithNonProductAccountV1_request, CreateTransactionWithNonProductAccountV1_response } from './v1/createTransaction.js';
|
|
@@ -15,12 +15,12 @@ type InferVersionedArgument<EnumValues extends VersionedArguments, N extends num
|
|
|
15
15
|
[V in keyof EnumValues]: EnumValues[V][N];
|
|
16
16
|
};
|
|
17
17
|
export type VersionedProtocolRequest<T extends VersionedArguments = VersionedArguments> = {
|
|
18
|
-
|
|
18
|
+
method: 'request';
|
|
19
19
|
request: EnumCodec<InferVersionedArgument<T, 0>>;
|
|
20
20
|
response: EnumCodec<InferVersionedArgument<T, 1>>;
|
|
21
21
|
};
|
|
22
22
|
export type VersionedProtocolSubscription<T extends VersionedArguments = VersionedArguments> = {
|
|
23
|
-
|
|
23
|
+
method: 'subscribe';
|
|
24
24
|
start: EnumCodec<InferVersionedArgument<T, 0>>;
|
|
25
25
|
receive: EnumCodec<InferVersionedArgument<T, 1>>;
|
|
26
26
|
};
|
package/dist/protocol/impl.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Enum } from '
|
|
1
|
+
import { Enum } from '@novasamatech/scale';
|
|
2
2
|
import { AccountCreateProofV1_request, AccountCreateProofV1_response, AccountGetAliasV1_request, AccountGetAliasV1_response, AccountGetV1_request, AccountGetV1_response, GetNonProductAccountsV1_request, GetNonProductAccountsV1_response, } from './v1/accounts.js';
|
|
3
3
|
import { ChatActionSubscribeV1_receive, ChatActionSubscribeV1_start, ChatCreateContactV1_request, ChatCreateContactV1_response, ChatPostMessageV1_request, ChatPostMessageV1_response, } from './v1/chat.js';
|
|
4
4
|
import { CreateTransactionV1_request, CreateTransactionV1_response, CreateTransactionWithNonProductAccountV1_request, CreateTransactionWithNonProductAccountV1_response, } from './v1/createTransaction.js';
|
|
@@ -14,14 +14,14 @@ const enumFromArg = (enumValues, n) => {
|
|
|
14
14
|
};
|
|
15
15
|
const versionedRequest = (values) => {
|
|
16
16
|
return {
|
|
17
|
-
|
|
17
|
+
method: 'request',
|
|
18
18
|
request: enumFromArg(values, 0),
|
|
19
19
|
response: enumFromArg(values, 1),
|
|
20
20
|
};
|
|
21
21
|
};
|
|
22
22
|
const versionedSubscription = (values) => {
|
|
23
23
|
return {
|
|
24
|
-
|
|
24
|
+
method: 'subscribe',
|
|
25
25
|
start: enumFromArg(values, 0),
|
|
26
26
|
receive: enumFromArg(values, 1),
|
|
27
27
|
};
|