@novasamatech/statement-store 0.5.0-17 → 0.5.0-19
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/dist/adapter/rpc.js +35 -2
- package/dist/adapter/types.d.ts +28 -1
- package/dist/adapter/types.js +47 -1
- package/dist/session/session.js +0 -2
- package/package.json +4 -4
- package/dist/transport/impl.d.ts +0 -32
- package/dist/transport/impl.js +0 -59
- package/dist/transport/scale/opaque.d.ts +0 -1
- package/dist/transport/scale/opaque.js +0 -1
- package/dist/transport/scale/statementData.d.ts +0 -24
- package/dist/transport/scale/statementData.js +0 -36
- package/dist/transport/transport.d.ts +0 -31
- package/dist/transport/transport.js +0 -56
package/dist/adapter/rpc.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createStatementSdk } from '@polkadot-api/sdk-statement';
|
|
2
2
|
import { Binary } from '@polkadot-api/substrate-bindings';
|
|
3
3
|
import { toHex } from '@polkadot-api/utils';
|
|
4
|
-
import { fromPromise } from 'neverthrow';
|
|
4
|
+
import { errAsync, fromPromise, okAsync } from 'neverthrow';
|
|
5
5
|
import { toError } from '../helpers.js';
|
|
6
|
+
import { AccountFullError, BadProofError, DataTooLargeError, EncodingTooLargeError, NoProofError, PriorityTooLowError, StorageFullError, } from './types.js';
|
|
6
7
|
const POLLING_INTERVAL = 1500;
|
|
7
8
|
function createKey(topics) {
|
|
8
9
|
return topics.map(toHex).sort().join('');
|
|
@@ -62,7 +63,39 @@ export function createPapiStatementStoreAdapter(lazyClient) {
|
|
|
62
63
|
};
|
|
63
64
|
},
|
|
64
65
|
submitStatement(statement) {
|
|
65
|
-
return fromPromise(sdk.submit(statement), toError)
|
|
66
|
+
return fromPromise(sdk.submit(statement), toError).andThen(result => {
|
|
67
|
+
switch (result.status) {
|
|
68
|
+
case 'new':
|
|
69
|
+
case 'known':
|
|
70
|
+
return okAsync(undefined);
|
|
71
|
+
case 'rejected':
|
|
72
|
+
switch (result.reason) {
|
|
73
|
+
case 'dataTooLarge':
|
|
74
|
+
return errAsync(new DataTooLargeError(result.submitted_size, result.available_size));
|
|
75
|
+
case 'channelPriorityTooLow':
|
|
76
|
+
return errAsync(new PriorityTooLowError(result.submitted_priority, result.min_priority));
|
|
77
|
+
case 'accountFull':
|
|
78
|
+
return errAsync(new AccountFullError());
|
|
79
|
+
case 'storeFull':
|
|
80
|
+
return errAsync(new StorageFullError());
|
|
81
|
+
default:
|
|
82
|
+
return errAsync(new Error('Unknown rejection reason'));
|
|
83
|
+
}
|
|
84
|
+
case 'invalid':
|
|
85
|
+
switch (result.reason) {
|
|
86
|
+
case 'noProof':
|
|
87
|
+
return errAsync(new NoProofError());
|
|
88
|
+
case 'badProof':
|
|
89
|
+
return errAsync(new BadProofError());
|
|
90
|
+
case 'encodingTooLarge':
|
|
91
|
+
return errAsync(new EncodingTooLargeError(result.submitted_size, result.max_size));
|
|
92
|
+
default:
|
|
93
|
+
return errAsync(new Error('Unknown rejection reason: invalid'));
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return okAsync(undefined);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
66
99
|
},
|
|
67
100
|
};
|
|
68
101
|
return transportProvider;
|
package/dist/adapter/types.d.ts
CHANGED
|
@@ -3,5 +3,32 @@ import type { ResultAsync } from 'neverthrow';
|
|
|
3
3
|
export type StatementStoreAdapter = {
|
|
4
4
|
queryStatements(topics: Uint8Array[], destination?: Uint8Array): ResultAsync<Statement[], Error>;
|
|
5
5
|
subscribeStatements(topics: Uint8Array[], callback: (statements: Statement[]) => unknown): VoidFunction;
|
|
6
|
-
submitStatement(statement: SignedStatement): ResultAsync<void, Error>;
|
|
6
|
+
submitStatement(statement: SignedStatement): ResultAsync<void, DataTooLargeError | PriorityTooLowError | AccountFullError | StorageFullError | NoProofError | BadProofError | EncodingTooLargeError | Error>;
|
|
7
7
|
};
|
|
8
|
+
export declare class DataTooLargeError extends Error {
|
|
9
|
+
readonly submitted: number;
|
|
10
|
+
readonly available: number;
|
|
11
|
+
constructor(submitted: number, available: number);
|
|
12
|
+
}
|
|
13
|
+
export declare class PriorityTooLowError extends Error {
|
|
14
|
+
readonly submitted: number;
|
|
15
|
+
readonly min: number;
|
|
16
|
+
constructor(submitted: number, min: number);
|
|
17
|
+
}
|
|
18
|
+
export declare class AccountFullError extends Error {
|
|
19
|
+
constructor();
|
|
20
|
+
}
|
|
21
|
+
export declare class StorageFullError extends Error {
|
|
22
|
+
constructor();
|
|
23
|
+
}
|
|
24
|
+
export declare class NoProofError extends Error {
|
|
25
|
+
constructor();
|
|
26
|
+
}
|
|
27
|
+
export declare class BadProofError extends Error {
|
|
28
|
+
constructor();
|
|
29
|
+
}
|
|
30
|
+
export declare class EncodingTooLargeError extends Error {
|
|
31
|
+
readonly submitted: number;
|
|
32
|
+
readonly max: number;
|
|
33
|
+
constructor(submitted: number, max: number);
|
|
34
|
+
}
|
package/dist/adapter/types.js
CHANGED
|
@@ -1 +1,47 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export class DataTooLargeError extends Error {
|
|
2
|
+
submitted;
|
|
3
|
+
available;
|
|
4
|
+
constructor(submitted, available) {
|
|
5
|
+
super(`Submit failed, data too large: ${submitted} > ${available}`);
|
|
6
|
+
this.submitted = submitted;
|
|
7
|
+
this.available = available;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export class PriorityTooLowError extends Error {
|
|
11
|
+
submitted;
|
|
12
|
+
min;
|
|
13
|
+
constructor(submitted, min) {
|
|
14
|
+
super(`Submit failed, priority too low: ${submitted} > ${min}`);
|
|
15
|
+
this.submitted = submitted;
|
|
16
|
+
this.min = min;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export class AccountFullError extends Error {
|
|
20
|
+
constructor() {
|
|
21
|
+
super(`Submit failed, account full`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class StorageFullError extends Error {
|
|
25
|
+
constructor() {
|
|
26
|
+
super(`Submit failed, storage is full`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class NoProofError extends Error {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(`Submit failed, no proof provided`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class BadProofError extends Error {
|
|
35
|
+
constructor() {
|
|
36
|
+
super(`Submit failed, bad proof provided`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class EncodingTooLargeError extends Error {
|
|
40
|
+
submitted;
|
|
41
|
+
max;
|
|
42
|
+
constructor(submitted, max) {
|
|
43
|
+
super(`Submit failed, encoding too large`);
|
|
44
|
+
this.submitted = submitted;
|
|
45
|
+
this.max = max;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/session/session.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Binary } from '@polkadot-api/substrate-bindings';
|
|
2
|
-
import { toHex } from '@polkadot-api/utils';
|
|
3
2
|
import { nanoid } from 'nanoid';
|
|
4
3
|
import { ResultAsync, err, fromPromise, fromThrowable, ok, okAsync } from 'neverthrow';
|
|
5
4
|
import { Bytes } from 'scale-ts';
|
|
@@ -12,7 +11,6 @@ import { StatementData } from './scale/statementData.js';
|
|
|
12
11
|
export function createSession({ localAccount, remoteAccount, statementStore, encryption, prover, }) {
|
|
13
12
|
let subscriptions = [];
|
|
14
13
|
function submit(sessionId, channel, data) {
|
|
15
|
-
console.log('data', toHex(data));
|
|
16
14
|
return encryption
|
|
17
15
|
.encrypt(data)
|
|
18
16
|
.map(data => ({
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@novasamatech/statement-store",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.0-
|
|
4
|
+
"version": "0.5.0-19",
|
|
5
5
|
"description": "Statement store integration",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@polkadot-api/sdk-statement": "^0.
|
|
27
|
+
"@polkadot-api/sdk-statement": "^0.3.0",
|
|
28
28
|
"@polkadot-api/substrate-bindings": "^0.16.5",
|
|
29
29
|
"@noble/hashes": "2.0.1",
|
|
30
30
|
"@noble/ciphers": "2.1.1",
|
|
31
|
-
"polkadot-api": "^1.23.
|
|
31
|
+
"polkadot-api": "^1.23.2",
|
|
32
32
|
"neverthrow": "^8.2.0",
|
|
33
|
-
"scale-ts": "
|
|
33
|
+
"scale-ts": "1.6.1"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
package/dist/transport/impl.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { SignedStatement, Statement } from '@polkadot-api/sdk-statement';
|
|
2
|
-
import type { ResultAsync } from 'neverthrow';
|
|
3
|
-
import type { Codec } from 'scale-ts';
|
|
4
|
-
import type { StatementAdapter } from '../adapter/types.js';
|
|
5
|
-
import type { SessionId } from '../model/session.js';
|
|
6
|
-
import type { Callback } from '../types.js';
|
|
7
|
-
import type { TransportError } from './scale/statementData.js';
|
|
8
|
-
export type Transport = ReturnType<typeof createTransport>;
|
|
9
|
-
type RequestMessage<T> = {
|
|
10
|
-
type: 'request';
|
|
11
|
-
requestId: string;
|
|
12
|
-
data: T;
|
|
13
|
-
};
|
|
14
|
-
type ResponseMessage = {
|
|
15
|
-
type: 'response';
|
|
16
|
-
requestId: string;
|
|
17
|
-
code: TransportError;
|
|
18
|
-
};
|
|
19
|
-
type Message<T> = RequestMessage<T> | ResponseMessage;
|
|
20
|
-
type Params = {
|
|
21
|
-
adapter: StatementAdapter;
|
|
22
|
-
};
|
|
23
|
-
export declare function createTransport({ adapter }: Params): {
|
|
24
|
-
subscribeStatements(topic: Uint8Array, callback: Callback<Statement[]>): VoidFunction;
|
|
25
|
-
subscribe<T>({ sessionId, sharedSecret, codec, }: {
|
|
26
|
-
sessionId: SessionId;
|
|
27
|
-
sharedSecret: Uint8Array;
|
|
28
|
-
codec: Codec<T>;
|
|
29
|
-
}, callback: Callback<Message<T>[]>): VoidFunction;
|
|
30
|
-
submitRequest(statement: SignedStatement): ResultAsync<void, Error>;
|
|
31
|
-
};
|
|
32
|
-
export {};
|
package/dist/transport/impl.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { Result, fromThrowable, ok } from 'neverthrow';
|
|
2
|
-
import { decrypt } from '../crypto.js';
|
|
3
|
-
import { nonNullable, toError } from '../helpers.js';
|
|
4
|
-
import { StatementData } from './scale/statementData.js';
|
|
5
|
-
const decryptResults = fromThrowable(decrypt, toError);
|
|
6
|
-
function mapMessage(statementData) {
|
|
7
|
-
switch (statementData.tag) {
|
|
8
|
-
case 'request':
|
|
9
|
-
return statementData.value.data.map((data, index) => ({
|
|
10
|
-
type: 'request',
|
|
11
|
-
requestId: `${statementData.value.requestId}-${index.toString()}`,
|
|
12
|
-
data,
|
|
13
|
-
}));
|
|
14
|
-
case 'response':
|
|
15
|
-
return [
|
|
16
|
-
{
|
|
17
|
-
type: 'response',
|
|
18
|
-
requestId: statementData.value.requestId,
|
|
19
|
-
code: statementData.value.responseCode,
|
|
20
|
-
},
|
|
21
|
-
];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
// function createRequestChannel(session: Uint8Array) {
|
|
25
|
-
// return khash(session, stringToBytes('request'));
|
|
26
|
-
// }
|
|
27
|
-
//
|
|
28
|
-
// export function createResponseChannel(session: Uint8Array) {
|
|
29
|
-
// return khash(session, stringToBytes('response'));
|
|
30
|
-
// }
|
|
31
|
-
export function createTransport({ adapter }) {
|
|
32
|
-
const transport = {
|
|
33
|
-
subscribeStatements(topic, callback) {
|
|
34
|
-
return adapter.subscribeStatements([topic], callback);
|
|
35
|
-
},
|
|
36
|
-
subscribe({ sessionId, sharedSecret, codec, }, callback) {
|
|
37
|
-
const statementDataCodec = StatementData(codec);
|
|
38
|
-
return adapter.subscribeStatements([sessionId], statements => {
|
|
39
|
-
Result.combine(statements.map(statement => {
|
|
40
|
-
if (!statement.data)
|
|
41
|
-
return ok(null);
|
|
42
|
-
return decryptResults(sharedSecret, statement.data.asBytes())
|
|
43
|
-
.map(statementDataCodec.dec)
|
|
44
|
-
.orElse(() => ok(null));
|
|
45
|
-
}))
|
|
46
|
-
.map(messages => messages.filter(nonNullable).flatMap(mapMessage))
|
|
47
|
-
.andTee(messages => {
|
|
48
|
-
if (messages.length > 0) {
|
|
49
|
-
callback(messages);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
submitRequest(statement) {
|
|
55
|
-
return adapter.submitStatement(statement);
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
return transport;
|
|
59
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { Codec } from 'scale-ts';
|
|
2
|
-
export type TransportError = 'decryptionFailed' | 'decodingFailed' | 'unknown';
|
|
3
|
-
export declare const TransportErrorCodec: Codec<TransportError>;
|
|
4
|
-
export declare const Request: <T>(data: Codec<T>) => Codec<{
|
|
5
|
-
requestId: string;
|
|
6
|
-
data: T[];
|
|
7
|
-
}>;
|
|
8
|
-
export declare const Response: Codec<{
|
|
9
|
-
requestId: string;
|
|
10
|
-
responseCode: TransportError;
|
|
11
|
-
}>;
|
|
12
|
-
export declare const StatementData: <T>(data: Codec<T>) => Codec<{
|
|
13
|
-
tag: "request";
|
|
14
|
-
value: {
|
|
15
|
-
requestId: string;
|
|
16
|
-
data: T[];
|
|
17
|
-
};
|
|
18
|
-
} | {
|
|
19
|
-
tag: "response";
|
|
20
|
-
value: {
|
|
21
|
-
requestId: string;
|
|
22
|
-
responseCode: TransportError;
|
|
23
|
-
};
|
|
24
|
-
}>;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Enum, Struct, Vector, enhanceCodec, str, u8 } from 'scale-ts';
|
|
2
|
-
export const TransportErrorCodec = enhanceCodec(u8, error => {
|
|
3
|
-
switch (error) {
|
|
4
|
-
case 'decryptionFailed':
|
|
5
|
-
return 1;
|
|
6
|
-
case 'decodingFailed':
|
|
7
|
-
return 2;
|
|
8
|
-
case 'unknown':
|
|
9
|
-
return 255;
|
|
10
|
-
}
|
|
11
|
-
}, code => {
|
|
12
|
-
switch (code) {
|
|
13
|
-
case 1:
|
|
14
|
-
return 'decryptionFailed';
|
|
15
|
-
case 2:
|
|
16
|
-
return 'decodingFailed';
|
|
17
|
-
default:
|
|
18
|
-
return 'unknown';
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
export const Request = (data) => {
|
|
22
|
-
return Struct({
|
|
23
|
-
requestId: str,
|
|
24
|
-
data: Vector(data),
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
export const Response = Struct({
|
|
28
|
-
requestId: str,
|
|
29
|
-
responseCode: TransportErrorCodec,
|
|
30
|
-
});
|
|
31
|
-
export const StatementData = (data) => {
|
|
32
|
-
return Enum({
|
|
33
|
-
request: Request(data),
|
|
34
|
-
response: Response,
|
|
35
|
-
});
|
|
36
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { SignedStatement } from '@polkadot-api/sdk-statement';
|
|
2
|
-
import type { ResultAsync } from 'neverthrow';
|
|
3
|
-
import type { Codec } from 'scale-ts';
|
|
4
|
-
import type { StatementAdapter } from '../adapter/types.js';
|
|
5
|
-
import type { SessionId } from '../model/session.js';
|
|
6
|
-
import type { Callback } from '../types.js';
|
|
7
|
-
import type { TransportError } from './scale/statementData.js';
|
|
8
|
-
export type Transport = ReturnType<typeof createTransport>;
|
|
9
|
-
type RequestMessage<T> = {
|
|
10
|
-
type: 'request';
|
|
11
|
-
requestId: string;
|
|
12
|
-
data: T;
|
|
13
|
-
};
|
|
14
|
-
type ResponseMessage = {
|
|
15
|
-
type: 'response';
|
|
16
|
-
requestId: string;
|
|
17
|
-
code: TransportError;
|
|
18
|
-
};
|
|
19
|
-
type Message<T> = RequestMessage<T> | ResponseMessage;
|
|
20
|
-
type Params = {
|
|
21
|
-
adapter: StatementAdapter;
|
|
22
|
-
};
|
|
23
|
-
export declare function createTransport({ adapter }: Params): {
|
|
24
|
-
subscribe<T>({ sessionId, sharedSecret, codec, }: {
|
|
25
|
-
sessionId: SessionId;
|
|
26
|
-
sharedSecret: Uint8Array;
|
|
27
|
-
codec: Codec<T>;
|
|
28
|
-
}, callback: Callback<Message<T>[]>): VoidFunction;
|
|
29
|
-
submitRequest(statement: SignedStatement): ResultAsync<void, Error>;
|
|
30
|
-
};
|
|
31
|
-
export {};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { Result, fromThrowable, ok } from 'neverthrow';
|
|
2
|
-
import { decrypt } from '../crypto.js';
|
|
3
|
-
import { nonNullable, toError } from '../helpers.js';
|
|
4
|
-
import { StatementData } from './scale/statementData.js';
|
|
5
|
-
const decryptResults = fromThrowable(decrypt, toError);
|
|
6
|
-
function mapMessage(statementData) {
|
|
7
|
-
switch (statementData.tag) {
|
|
8
|
-
case 'request':
|
|
9
|
-
return statementData.value.data.map((data, index) => ({
|
|
10
|
-
type: 'request',
|
|
11
|
-
requestId: `${statementData.value.requestId}-${index.toString()}`,
|
|
12
|
-
data,
|
|
13
|
-
}));
|
|
14
|
-
case 'response':
|
|
15
|
-
return [
|
|
16
|
-
{
|
|
17
|
-
type: 'response',
|
|
18
|
-
requestId: statementData.value.requestId,
|
|
19
|
-
code: statementData.value.responseCode,
|
|
20
|
-
},
|
|
21
|
-
];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
// function createRequestChannel(session: Uint8Array) {
|
|
25
|
-
// return khash(session, stringToBytes('request'));
|
|
26
|
-
// }
|
|
27
|
-
//
|
|
28
|
-
// export function createResponseChannel(session: Uint8Array) {
|
|
29
|
-
// return khash(session, stringToBytes('response'));
|
|
30
|
-
// }
|
|
31
|
-
export function createTransport({ adapter }) {
|
|
32
|
-
const transport = {
|
|
33
|
-
subscribe({ sessionId, sharedSecret, codec, }, callback) {
|
|
34
|
-
const statementDataCodec = StatementData(codec);
|
|
35
|
-
return adapter.subscribeStatements([sessionId], statements => {
|
|
36
|
-
Result.combine(statements.map(statement => {
|
|
37
|
-
if (!statement.data)
|
|
38
|
-
return ok(null);
|
|
39
|
-
return decryptResults(sharedSecret, statement.data.asBytes())
|
|
40
|
-
.map(statementDataCodec.dec)
|
|
41
|
-
.orElse(() => ok(null));
|
|
42
|
-
}))
|
|
43
|
-
.map(messages => messages.filter(nonNullable).flatMap(mapMessage))
|
|
44
|
-
.andTee(messages => {
|
|
45
|
-
if (messages.length > 0) {
|
|
46
|
-
callback(messages);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
submitRequest(statement) {
|
|
52
|
-
return adapter.submitStatement(statement);
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
return transport;
|
|
56
|
-
}
|