@novasamatech/statement-store 0.5.0-11 → 0.5.0-13
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CodecType } from 'scale-ts';
|
|
1
|
+
import type { Codec, CodecType } from 'scale-ts';
|
|
2
2
|
import type { StatementData } from './scale/statementData.js';
|
|
3
3
|
import type { Message } from './types.js';
|
|
4
|
-
export declare function toMessage<T>(statementData: CodecType<
|
|
4
|
+
export declare function toMessage<T>(statementData: CodecType<typeof StatementData>, codec: Codec<T>): Message<T>[];
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export function toMessage(statementData) {
|
|
1
|
+
export function toMessage(statementData, codec) {
|
|
2
2
|
switch (statementData.tag) {
|
|
3
3
|
case 'request':
|
|
4
4
|
return statementData.value.data.map((payload, index) => ({
|
|
5
5
|
type: 'request',
|
|
6
6
|
localId: `${statementData.value.requestId}-${index.toString()}`,
|
|
7
7
|
requestId: statementData.value.requestId,
|
|
8
|
-
payload,
|
|
8
|
+
payload: codec.dec(payload),
|
|
9
9
|
}));
|
|
10
10
|
case 'response':
|
|
11
11
|
return [
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import type { Codec } from 'scale-ts';
|
|
2
1
|
export type ResponseCode = 'success' | 'decryptionFailed' | 'decodingFailed' | 'unknown';
|
|
3
|
-
export declare const ResponseCodeCodec: Codec<ResponseCode>;
|
|
4
|
-
export declare const Request:
|
|
2
|
+
export declare const ResponseCodeCodec: import("scale-ts").Codec<ResponseCode>;
|
|
3
|
+
export declare const Request: import("scale-ts").Codec<{
|
|
5
4
|
requestId: string;
|
|
6
|
-
data:
|
|
5
|
+
data: Uint8Array<ArrayBufferLike>[];
|
|
7
6
|
}>;
|
|
8
|
-
export declare const Response: Codec<{
|
|
7
|
+
export declare const Response: import("scale-ts").Codec<{
|
|
9
8
|
requestId: string;
|
|
10
9
|
responseCode: ResponseCode;
|
|
11
10
|
}>;
|
|
12
|
-
export declare const StatementData:
|
|
11
|
+
export declare const StatementData: import("scale-ts").Codec<{
|
|
13
12
|
tag: "request";
|
|
14
13
|
value: {
|
|
15
14
|
requestId: string;
|
|
16
|
-
data:
|
|
15
|
+
data: Uint8Array<ArrayBufferLike>[];
|
|
17
16
|
};
|
|
18
17
|
} | {
|
|
19
18
|
tag: "response";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Enum, Struct, Vector, enhanceCodec, str, u8 } from 'scale-ts';
|
|
1
|
+
import { Bytes, Enum, Struct, Vector, enhanceCodec, str, u8 } from 'scale-ts';
|
|
2
2
|
export const ResponseCodeCodec = enhanceCodec(u8, error => {
|
|
3
3
|
switch (error) {
|
|
4
4
|
case 'success':
|
|
@@ -22,19 +22,15 @@ export const ResponseCodeCodec = enhanceCodec(u8, error => {
|
|
|
22
22
|
return 'unknown';
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
|
-
export const Request = (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
30
|
-
};
|
|
25
|
+
export const Request = Struct({
|
|
26
|
+
requestId: str,
|
|
27
|
+
data: Vector(Bytes()),
|
|
28
|
+
});
|
|
31
29
|
export const Response = Struct({
|
|
32
30
|
requestId: str,
|
|
33
31
|
responseCode: ResponseCodeCodec,
|
|
34
32
|
});
|
|
35
|
-
export const StatementData = (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
});
|
|
40
|
-
};
|
|
33
|
+
export const StatementData = Enum({
|
|
34
|
+
request: Request,
|
|
35
|
+
response: Response,
|
|
36
|
+
});
|
package/dist/session/session.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Binary } from '@polkadot-api/substrate-bindings';
|
|
2
|
+
import { toHex } from '@polkadot-api/utils';
|
|
2
3
|
import { nanoid } from 'nanoid';
|
|
3
4
|
import { ResultAsync, err, fromPromise, fromThrowable, ok, okAsync } from 'neverthrow';
|
|
4
5
|
import { Bytes } from 'scale-ts';
|
|
@@ -11,6 +12,7 @@ import { StatementData } from './scale/statementData.js';
|
|
|
11
12
|
export function createSession({ localAccount, remoteAccount, statementStore, encryption, prover, }) {
|
|
12
13
|
let subscriptions = [];
|
|
13
14
|
function submit(sessionId, channel, data) {
|
|
15
|
+
console.log('data', toHex(data));
|
|
14
16
|
return encryption
|
|
15
17
|
.encrypt(data)
|
|
16
18
|
.map(data => ({
|
|
@@ -31,11 +33,11 @@ export function createSession({ localAccount, remoteAccount, statementStore, enc
|
|
|
31
33
|
submitRequestMessage(codec, message) {
|
|
32
34
|
const requestId = nanoid();
|
|
33
35
|
const sessionId = createSessionId(remoteAccount.publicKey, localAccount, remoteAccount);
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
+
const encode = fromThrowable(StatementData.enc, toError);
|
|
37
|
+
const encoded = codec.enc(message);
|
|
36
38
|
const rawData = encode({
|
|
37
39
|
tag: 'request',
|
|
38
|
-
value: { requestId, data: [
|
|
40
|
+
value: { requestId, data: [encoded] },
|
|
39
41
|
});
|
|
40
42
|
return rawData
|
|
41
43
|
.asyncAndThen(data => submit(sessionId, createRequestChannel(sessionId), data))
|
|
@@ -43,8 +45,7 @@ export function createSession({ localAccount, remoteAccount, statementStore, enc
|
|
|
43
45
|
},
|
|
44
46
|
submitResponseMessage(requestId, responseCode) {
|
|
45
47
|
const sessionId = createSessionId(remoteAccount.publicKey, localAccount, remoteAccount);
|
|
46
|
-
const
|
|
47
|
-
const encode = fromThrowable(statementDataCodec.enc, toError);
|
|
48
|
+
const encode = fromThrowable(StatementData.enc, toError);
|
|
48
49
|
const rawData = encode({
|
|
49
50
|
tag: 'response',
|
|
50
51
|
value: { requestId, responseCode },
|
|
@@ -83,7 +84,6 @@ export function createSession({ localAccount, remoteAccount, statementStore, enc
|
|
|
83
84
|
return fromPromise(promise, toError);
|
|
84
85
|
},
|
|
85
86
|
subscribe(codec, callback) {
|
|
86
|
-
const statementDataCodec = StatementData(codec);
|
|
87
87
|
const sessionId = createSessionId(remoteAccount.publicKey, remoteAccount, localAccount);
|
|
88
88
|
function processStatement(statement) {
|
|
89
89
|
if (!statement.data)
|
|
@@ -93,12 +93,12 @@ export function createSession({ localAccount, remoteAccount, statementStore, enc
|
|
|
93
93
|
.verifyMessageProof(statement)
|
|
94
94
|
.andThen(verified => (verified ? ok() : err(new Error('Statement proof is not valid'))))
|
|
95
95
|
.andThen(() => encryption.decrypt(data))
|
|
96
|
-
.map(
|
|
96
|
+
.map(StatementData.dec)
|
|
97
97
|
.orElse(() => ok(null));
|
|
98
98
|
}
|
|
99
99
|
return statementStore.subscribeStatements([sessionId], statements => {
|
|
100
100
|
ResultAsync.combine(statements.map(processStatement))
|
|
101
|
-
.map(messages => messages.filter(nonNullable).flatMap(toMessage))
|
|
101
|
+
.map(messages => messages.filter(nonNullable).flatMap(x => toMessage(x, codec)))
|
|
102
102
|
.andTee(messages => {
|
|
103
103
|
if (messages.length > 0) {
|
|
104
104
|
callback(messages);
|