@peerbit/rpc 2.0.0 → 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/lib/esm/controller.d.ts +4 -9
- package/lib/esm/controller.js +28 -16
- package/lib/esm/controller.js.map +1 -1
- package/lib/esm/utils.js +1 -1
- package/package.json +5 -5
- package/src/controller.ts +42 -35
- package/src/utils.ts +1 -1
package/lib/esm/controller.d.ts
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
1
|
import { AbstractType } from "@dao-xyz/borsh";
|
|
2
2
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
3
3
|
import { RPCOptions, RPCResponse, PublishOptions } from "./io.js";
|
|
4
|
-
import { Address } from "@peerbit/program";
|
|
5
4
|
import { DataEvent } from "@peerbit/pubsub-interface";
|
|
6
5
|
import { Program } from "@peerbit/program";
|
|
7
|
-
export type SearchContext = (() => Address) | Program;
|
|
8
|
-
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
9
6
|
export type RPCSetupOptions<Q, R> = {
|
|
10
7
|
topic: string;
|
|
11
8
|
queryType: AbstractType<Q>;
|
|
12
9
|
responseType: AbstractType<R>;
|
|
13
|
-
canRead?: CanRead;
|
|
14
10
|
responseHandler?: ResponseHandler<Q, R>;
|
|
15
11
|
subscriptionData?: Uint8Array;
|
|
16
12
|
};
|
|
17
|
-
export type
|
|
13
|
+
export type RequestContext = {
|
|
18
14
|
from?: PublicSignKey;
|
|
19
|
-
address: string;
|
|
20
15
|
};
|
|
21
|
-
export type ResponseHandler<Q, R> = (query: Q, context:
|
|
16
|
+
export type ResponseHandler<Q, R> = (query: Q, context: RequestContext) => Promise<R | undefined> | R | undefined;
|
|
22
17
|
export declare class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
23
|
-
canRead: CanRead;
|
|
24
18
|
private _subscribed;
|
|
25
19
|
private _responseHandler?;
|
|
26
20
|
private _responseResolver;
|
|
27
21
|
private _requestType;
|
|
22
|
+
private _requestTypeIsUint8Array;
|
|
28
23
|
private _responseType;
|
|
29
24
|
private _rpcTopic;
|
|
30
25
|
private _onMessageBinded;
|
|
@@ -37,7 +32,7 @@ export declare class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
37
32
|
close(from?: Program): Promise<boolean>;
|
|
38
33
|
drop(from?: Program): Promise<boolean>;
|
|
39
34
|
private _subscribing;
|
|
40
|
-
|
|
35
|
+
subscribe(data?: Uint8Array | undefined): Promise<void>;
|
|
41
36
|
_onMessage(evt: CustomEvent<DataEvent>): Promise<void>;
|
|
42
37
|
private seal;
|
|
43
38
|
private getPublishOptions;
|
package/lib/esm/controller.js
CHANGED
|
@@ -11,6 +11,7 @@ import { logger } from "./io.js";
|
|
|
11
11
|
import { Program } from "@peerbit/program";
|
|
12
12
|
import pDefer from "p-defer";
|
|
13
13
|
import { waitFor } from "@peerbit/time";
|
|
14
|
+
import { equals } from "uint8arrays";
|
|
14
15
|
const createValueResolver = (type) => {
|
|
15
16
|
if (type === Uint8Array) {
|
|
16
17
|
return (decrypted) => decrypted._data;
|
|
@@ -20,11 +21,11 @@ const createValueResolver = (type) => {
|
|
|
20
21
|
}
|
|
21
22
|
};
|
|
22
23
|
export let RPC = class RPC extends Program {
|
|
23
|
-
canRead;
|
|
24
24
|
_subscribed = false;
|
|
25
25
|
_responseHandler;
|
|
26
26
|
_responseResolver;
|
|
27
27
|
_requestType;
|
|
28
|
+
_requestTypeIsUint8Array;
|
|
28
29
|
_responseType;
|
|
29
30
|
_rpcTopic;
|
|
30
31
|
_onMessageBinded = undefined;
|
|
@@ -36,14 +37,13 @@ export let RPC = class RPC extends Program {
|
|
|
36
37
|
this._rpcTopic = args.topic ?? this._rpcTopic;
|
|
37
38
|
this._responseHandler = args.responseHandler;
|
|
38
39
|
this._requestType = args.queryType;
|
|
40
|
+
this._requestTypeIsUint8Array = this._requestType === Uint8Array;
|
|
39
41
|
this._responseType = args.responseType;
|
|
40
42
|
this._responseResolver = new Map();
|
|
41
|
-
this._subscriptionMetaData = args.subscriptionData;
|
|
42
|
-
this.canRead = args.canRead || (() => Promise.resolve(true));
|
|
43
43
|
this._getResponseValueFn = createValueResolver(this._responseType);
|
|
44
44
|
this._getRequestValueFn = createValueResolver(this._requestType);
|
|
45
45
|
this._keypair = await X25519Keypair.create();
|
|
46
|
-
await this.
|
|
46
|
+
await this.subscribe(args.subscriptionData);
|
|
47
47
|
}
|
|
48
48
|
async _close(from) {
|
|
49
49
|
if (this._subscribed) {
|
|
@@ -69,20 +69,36 @@ export let RPC = class RPC extends Program {
|
|
|
69
69
|
return true;
|
|
70
70
|
}
|
|
71
71
|
_subscribing;
|
|
72
|
-
async
|
|
72
|
+
async subscribe(data = this._subscriptionMetaData) {
|
|
73
73
|
await this._subscribing;
|
|
74
|
-
if (this._subscribed
|
|
74
|
+
if (this._subscribed &&
|
|
75
|
+
(this._subscriptionMetaData === data ||
|
|
76
|
+
(this._subscriptionMetaData &&
|
|
77
|
+
data &&
|
|
78
|
+
equals(this._subscriptionMetaData, data)))) {
|
|
75
79
|
return;
|
|
76
80
|
}
|
|
81
|
+
const prevSubscriptionData = this._subscriptionMetaData;
|
|
82
|
+
this._subscriptionMetaData = data;
|
|
83
|
+
const wasSubscribed = this._subscribed;
|
|
77
84
|
this._subscribed = true;
|
|
78
85
|
this._onMessageBinded = this._onMessageBinded || this._onMessage.bind(this);
|
|
86
|
+
if (wasSubscribed) {
|
|
87
|
+
await this.node.services.pubsub.unsubscribe(this.rpcTopic, {
|
|
88
|
+
data: prevSubscriptionData,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
79
91
|
this._subscribing = this.node.services.pubsub
|
|
80
|
-
.subscribe(this.rpcTopic, { data
|
|
92
|
+
.subscribe(this.rpcTopic, { data })
|
|
81
93
|
.then(() => {
|
|
82
|
-
|
|
94
|
+
if (!wasSubscribed) {
|
|
95
|
+
this.node.services.pubsub.addEventListener("data", this._onMessageBinded);
|
|
96
|
+
}
|
|
83
97
|
});
|
|
84
98
|
await this._subscribing;
|
|
85
|
-
|
|
99
|
+
if (!wasSubscribed) {
|
|
100
|
+
await this.node.services.pubsub.requestSubscribers(this.rpcTopic);
|
|
101
|
+
}
|
|
86
102
|
logger.debug("subscribing to query topic (responses): " + this.rpcTopic);
|
|
87
103
|
}
|
|
88
104
|
async _onMessage(evt) {
|
|
@@ -94,18 +110,14 @@ export let RPC = class RPC extends Program {
|
|
|
94
110
|
if (this._responseHandler) {
|
|
95
111
|
const maybeEncrypted = rpcMessage.request;
|
|
96
112
|
const decrypted = await maybeEncrypted.decrypt(this.node.keychain);
|
|
97
|
-
if (!(await this.canRead(message.sender))) {
|
|
98
|
-
throw new AccessError();
|
|
99
|
-
}
|
|
100
113
|
const response = await this._responseHandler(this._getRequestValueFn(decrypted), {
|
|
101
|
-
address: this.rpcTopic,
|
|
102
114
|
from: message.sender,
|
|
103
115
|
});
|
|
104
116
|
if (response && rpcMessage.respondTo) {
|
|
105
117
|
// send query and wait for replies in a generator like behaviour
|
|
106
118
|
const serializedResponse = serialize(response);
|
|
107
119
|
// we use the peerId/libp2p identity for signatures, since we want to be able to send a message
|
|
108
|
-
// with pubsub with a certain
|
|
120
|
+
// with pubsub with a certain receiver. If we use (this.identity) we are going to use an identity
|
|
109
121
|
// that is now known in the .pubsub network, hence the message might not be delivired if we
|
|
110
122
|
// send with { to: [RECIEVER] } param
|
|
111
123
|
const decryptedMessage = new DecryptedThing({
|
|
@@ -151,7 +163,7 @@ export let RPC = class RPC extends Program {
|
|
|
151
163
|
}
|
|
152
164
|
}
|
|
153
165
|
async seal(request, respondTo, options) {
|
|
154
|
-
const requestData = this.
|
|
166
|
+
const requestData = this._requestTypeIsUint8Array
|
|
155
167
|
? request
|
|
156
168
|
: serialize(request);
|
|
157
169
|
const decryptedMessage = new DecryptedThing({
|
|
@@ -232,7 +244,7 @@ export let RPC = class RPC extends Program {
|
|
|
232
244
|
*/
|
|
233
245
|
async request(request, options) {
|
|
234
246
|
// We are generatinga new encryption keypair for each send, so we now that when we get the responses, they are encrypted specifcally for me, and for this request
|
|
235
|
-
// this allows us to easily disregard a bunch of message just beacuse they are for a different
|
|
247
|
+
// this allows us to easily disregard a bunch of message just beacuse they are for a different receiver!
|
|
236
248
|
const keypair = await X25519Keypair.create();
|
|
237
249
|
// send query and wait for replies in a generator like behaviour
|
|
238
250
|
let timeoutFn = undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAEN,UAAU,EACV,WAAW,EACX,SAAS,EACT,OAAO,GACP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,cAAc,EAGd,QAAQ,EACR,WAAW,EAEX,aAAa,GACb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAAc,MAAM,EAA+B,MAAM,SAAS,CAAC;AAO1E,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,OAAO,MAA2B,MAAM,SAAS,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAEN,UAAU,EACV,WAAW,EACX,SAAS,EACT,OAAO,GACP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,cAAc,EAGd,QAAQ,EACR,WAAW,EAEX,aAAa,GACb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAAc,MAAM,EAA+B,MAAM,SAAS,CAAC;AAO1E,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,OAAO,MAA2B,MAAM,SAAS,CAAC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B,CAAC;IAED,SAAS;QACR,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACxB,CAAC;CACD,CAAA;AAzXY,GAAG;IADf,OAAO,CAAC,KAAK,CAAC;GACF,GAAG,CAyXf"}
|
package/lib/esm/utils.js
CHANGED
|
@@ -49,7 +49,7 @@ export const queryAll = (rpc, groups, request, responseHandler, options) => {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
if (missingReponses) {
|
|
52
|
-
throw new MissingResponsesError("Did not
|
|
52
|
+
throw new MissingResponsesError("Did not receive responses from all shards");
|
|
53
53
|
}
|
|
54
54
|
};
|
|
55
55
|
return fn();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/rpc",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "RPC calls for peers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@dao-xyz/borsh": "^5.1.5",
|
|
35
|
-
"@peerbit/crypto": "1.0.
|
|
35
|
+
"@peerbit/crypto": "1.0.5",
|
|
36
36
|
"@peerbit/logger": "1.0.1",
|
|
37
|
-
"@peerbit/program": "2.
|
|
37
|
+
"@peerbit/program": "2.2.0",
|
|
38
38
|
"@peerbit/time": "1.0.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@peerbit/test-utils": "^1.0.
|
|
41
|
+
"@peerbit/test-utils": "^1.0.16"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "464e807d679e24b897b7811ac99d6f85fbd756f9"
|
|
44
44
|
}
|
package/src/controller.ts
CHANGED
|
@@ -26,25 +26,21 @@ import { Program } from "@peerbit/program";
|
|
|
26
26
|
import { DataMessage } from "@peerbit/stream-interface";
|
|
27
27
|
import pDefer, { DeferredPromise } from "p-defer";
|
|
28
28
|
import { waitFor } from "@peerbit/time";
|
|
29
|
-
|
|
30
|
-
export type SearchContext = (() => Address) | Program;
|
|
31
|
-
export type CanRead = (key?: PublicSignKey) => Promise<boolean> | boolean;
|
|
29
|
+
import { equals } from "uint8arrays";
|
|
32
30
|
|
|
33
31
|
export type RPCSetupOptions<Q, R> = {
|
|
34
32
|
topic: string;
|
|
35
33
|
queryType: AbstractType<Q>;
|
|
36
34
|
responseType: AbstractType<R>;
|
|
37
|
-
canRead?: CanRead;
|
|
38
35
|
responseHandler?: ResponseHandler<Q, R>;
|
|
39
36
|
subscriptionData?: Uint8Array;
|
|
40
37
|
};
|
|
41
|
-
export type
|
|
38
|
+
export type RequestContext = {
|
|
42
39
|
from?: PublicSignKey;
|
|
43
|
-
address: string;
|
|
44
40
|
};
|
|
45
41
|
export type ResponseHandler<Q, R> = (
|
|
46
42
|
query: Q,
|
|
47
|
-
context:
|
|
43
|
+
context: RequestContext
|
|
48
44
|
) => Promise<R | undefined> | R | undefined;
|
|
49
45
|
|
|
50
46
|
const createValueResolver = <T>(
|
|
@@ -59,8 +55,6 @@ const createValueResolver = <T>(
|
|
|
59
55
|
|
|
60
56
|
@variant("rpc")
|
|
61
57
|
export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
62
|
-
canRead: CanRead;
|
|
63
|
-
|
|
64
58
|
private _subscribed = false;
|
|
65
59
|
private _responseHandler?: ResponseHandler<Q, (R | undefined) | R>;
|
|
66
60
|
private _responseResolver: Map<
|
|
@@ -68,6 +62,7 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
68
62
|
(properties: { response: ResponseV0; message: DataMessage }) => any
|
|
69
63
|
>;
|
|
70
64
|
private _requestType: AbstractType<Q> | Uint8ArrayConstructor;
|
|
65
|
+
private _requestTypeIsUint8Array: boolean;
|
|
71
66
|
private _responseType: AbstractType<R>;
|
|
72
67
|
private _rpcTopic: string | undefined;
|
|
73
68
|
private _onMessageBinded: ((arg: any) => any) | undefined = undefined;
|
|
@@ -77,21 +72,18 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
77
72
|
|
|
78
73
|
private _getResponseValueFn: (decrypted: DecryptedThing<R>) => R;
|
|
79
74
|
private _getRequestValueFn: (decrypted: DecryptedThing<Q>) => Q;
|
|
80
|
-
|
|
81
75
|
async open(args: RPCSetupOptions<Q, R>): Promise<void> {
|
|
82
76
|
this._rpcTopic = args.topic ?? this._rpcTopic;
|
|
83
77
|
this._responseHandler = args.responseHandler;
|
|
84
78
|
this._requestType = args.queryType;
|
|
79
|
+
this._requestTypeIsUint8Array = (this._requestType as any) === Uint8Array;
|
|
85
80
|
this._responseType = args.responseType;
|
|
86
81
|
this._responseResolver = new Map();
|
|
87
|
-
this._subscriptionMetaData = args.subscriptionData;
|
|
88
|
-
this.canRead = args.canRead || (() => Promise.resolve(true));
|
|
89
|
-
|
|
90
82
|
this._getResponseValueFn = createValueResolver(this._responseType);
|
|
91
83
|
this._getRequestValueFn = createValueResolver(this._requestType);
|
|
92
84
|
|
|
93
85
|
this._keypair = await X25519Keypair.create();
|
|
94
|
-
await this.
|
|
86
|
+
await this.subscribe(args.subscriptionData);
|
|
95
87
|
}
|
|
96
88
|
|
|
97
89
|
private async _close(from?: Program): Promise<void> {
|
|
@@ -123,25 +115,46 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
123
115
|
}
|
|
124
116
|
|
|
125
117
|
private _subscribing: Promise<void>;
|
|
126
|
-
|
|
118
|
+
async subscribe(data = this._subscriptionMetaData): Promise<void> {
|
|
127
119
|
await this._subscribing;
|
|
128
|
-
if (
|
|
120
|
+
if (
|
|
121
|
+
this._subscribed &&
|
|
122
|
+
(this._subscriptionMetaData === data ||
|
|
123
|
+
(this._subscriptionMetaData &&
|
|
124
|
+
data &&
|
|
125
|
+
equals(this._subscriptionMetaData, data)))
|
|
126
|
+
) {
|
|
129
127
|
return;
|
|
130
128
|
}
|
|
129
|
+
|
|
130
|
+
const prevSubscriptionData = this._subscriptionMetaData;
|
|
131
|
+
this._subscriptionMetaData = data;
|
|
132
|
+
const wasSubscribed = this._subscribed;
|
|
131
133
|
this._subscribed = true;
|
|
134
|
+
|
|
132
135
|
this._onMessageBinded = this._onMessageBinded || this._onMessage.bind(this);
|
|
136
|
+
|
|
137
|
+
if (wasSubscribed) {
|
|
138
|
+
await this.node.services.pubsub.unsubscribe(this.rpcTopic, {
|
|
139
|
+
data: prevSubscriptionData,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
133
143
|
this._subscribing = this.node.services.pubsub
|
|
134
|
-
.subscribe(this.rpcTopic, { data
|
|
144
|
+
.subscribe(this.rpcTopic, { data })
|
|
135
145
|
.then(() => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
146
|
+
if (!wasSubscribed) {
|
|
147
|
+
this.node.services.pubsub.addEventListener(
|
|
148
|
+
"data",
|
|
149
|
+
this._onMessageBinded!
|
|
150
|
+
);
|
|
151
|
+
}
|
|
140
152
|
});
|
|
141
153
|
|
|
142
154
|
await this._subscribing;
|
|
143
|
-
|
|
144
|
-
|
|
155
|
+
if (!wasSubscribed) {
|
|
156
|
+
await this.node.services.pubsub.requestSubscribers(this.rpcTopic);
|
|
157
|
+
}
|
|
145
158
|
logger.debug("subscribing to query topic (responses): " + this.rpcTopic);
|
|
146
159
|
}
|
|
147
160
|
|
|
@@ -155,15 +168,9 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
155
168
|
if (this._responseHandler) {
|
|
156
169
|
const maybeEncrypted = rpcMessage.request;
|
|
157
170
|
const decrypted = await maybeEncrypted.decrypt(this.node.keychain);
|
|
158
|
-
|
|
159
|
-
if (!(await this.canRead(message.sender))) {
|
|
160
|
-
throw new AccessError();
|
|
161
|
-
}
|
|
162
|
-
|
|
163
171
|
const response = await this._responseHandler(
|
|
164
172
|
this._getRequestValueFn(decrypted),
|
|
165
173
|
{
|
|
166
|
-
address: this.rpcTopic,
|
|
167
174
|
from: message.sender,
|
|
168
175
|
}
|
|
169
176
|
);
|
|
@@ -173,7 +180,7 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
173
180
|
const serializedResponse = serialize(response);
|
|
174
181
|
|
|
175
182
|
// we use the peerId/libp2p identity for signatures, since we want to be able to send a message
|
|
176
|
-
// with pubsub with a certain
|
|
183
|
+
// with pubsub with a certain receiver. If we use (this.identity) we are going to use an identity
|
|
177
184
|
// that is now known in the .pubsub network, hence the message might not be delivired if we
|
|
178
185
|
// send with { to: [RECIEVER] } param
|
|
179
186
|
|
|
@@ -237,14 +244,14 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
237
244
|
respondTo?: X25519PublicKey,
|
|
238
245
|
options?: PublishOptions
|
|
239
246
|
) {
|
|
240
|
-
const requestData =
|
|
241
|
-
(
|
|
242
|
-
|
|
243
|
-
: serialize(request);
|
|
247
|
+
const requestData = this._requestTypeIsUint8Array
|
|
248
|
+
? (request as Uint8Array)
|
|
249
|
+
: serialize(request);
|
|
244
250
|
|
|
245
251
|
const decryptedMessage = new DecryptedThing<Uint8Array>({
|
|
246
252
|
data: requestData,
|
|
247
253
|
});
|
|
254
|
+
|
|
248
255
|
let maybeEncryptedMessage: MaybeEncrypted<Uint8Array> = decryptedMessage;
|
|
249
256
|
|
|
250
257
|
if (
|
|
@@ -353,7 +360,7 @@ export class RPC<Q, R> extends Program<RPCSetupOptions<Q, R>> {
|
|
|
353
360
|
options?: RPCOptions<R>
|
|
354
361
|
): Promise<RPCResponse<R>[]> {
|
|
355
362
|
// We are generatinga new encryption keypair for each send, so we now that when we get the responses, they are encrypted specifcally for me, and for this request
|
|
356
|
-
// this allows us to easily disregard a bunch of message just beacuse they are for a different
|
|
363
|
+
// this allows us to easily disregard a bunch of message just beacuse they are for a different receiver!
|
|
357
364
|
const keypair = await X25519Keypair.create();
|
|
358
365
|
|
|
359
366
|
// send query and wait for replies in a generator like behaviour
|