@waku/core 0.0.31-ce62600.0 → 0.0.31
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/CHANGELOG.md +29 -0
- package/bundle/{base_protocol-C6HnrRx8.js → base_protocol-C47QkJ2o.js} +22 -25
- package/bundle/{index-DnW8ifxc.js → index-tdQNdKHx.js} +62 -57
- package/bundle/index.js +381 -315
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-DQ9xsSLk.js → version_0-BrbNEwD-.js} +154 -308
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +3 -4
- package/dist/lib/base_protocol.js +4 -8
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +9 -6
- package/dist/lib/connection_manager.js +70 -45
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/filter_rpc.js.map +1 -1
- package/dist/lib/filter/index.d.ts +4 -3
- package/dist/lib/filter/index.js +37 -35
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/health_manager.d.ts +14 -0
- package/dist/lib/health_manager.js +70 -0
- package/dist/lib/health_manager.js.map +1 -0
- package/dist/lib/keep_alive_manager.d.ts +13 -7
- package/dist/lib/keep_alive_manager.js +9 -9
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +3 -2
- package/dist/lib/light_push/index.js +17 -3
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/push_rpc.js.map +1 -1
- package/dist/lib/light_push/utils.d.ts +3 -0
- package/dist/lib/light_push/utils.js +26 -0
- package/dist/lib/light_push/utils.js.map +1 -0
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +2 -2
- package/dist/lib/metadata/index.js +26 -26
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.d.ts +5 -44
- package/dist/lib/store/index.js +39 -45
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/store/rpc.d.ts +22 -0
- package/dist/lib/store/rpc.js +74 -0
- package/dist/lib/store/rpc.js.map +1 -0
- package/dist/lib/stream_manager/index.d.ts +1 -0
- package/dist/lib/stream_manager/index.js +2 -0
- package/dist/lib/stream_manager/index.js.map +1 -0
- package/dist/lib/{stream_manager.js → stream_manager/stream_manager.js} +1 -1
- package/dist/lib/stream_manager/stream_manager.js.map +1 -0
- package/dist/lib/stream_manager/utils.d.ts +2 -0
- package/dist/lib/stream_manager/utils.js +19 -0
- package/dist/lib/stream_manager/utils.js.map +1 -0
- package/package.json +130 -1
- package/src/index.ts +3 -3
- package/src/lib/base_protocol.ts +7 -10
- package/src/lib/connection_manager.ts +100 -57
- package/src/lib/filter/filter_rpc.ts +21 -19
- package/src/lib/filter/index.ts +54 -56
- package/src/lib/health_manager.ts +90 -0
- package/src/lib/keep_alive_manager.ts +27 -18
- package/src/lib/light_push/index.ts +21 -11
- package/src/lib/light_push/push_rpc.ts +5 -5
- package/src/lib/light_push/utils.ts +31 -0
- package/src/lib/message/version_0.ts +17 -15
- package/src/lib/metadata/index.ts +43 -44
- package/src/lib/store/index.ts +54 -94
- package/src/lib/store/rpc.ts +92 -0
- package/src/lib/stream_manager/index.ts +1 -0
- package/src/lib/{stream_manager.ts → stream_manager/stream_manager.ts} +3 -2
- package/src/lib/stream_manager/utils.ts +22 -0
- package/dist/lib/store/history_rpc.d.ts +0 -27
- package/dist/lib/store/history_rpc.js +0 -72
- package/dist/lib/store/history_rpc.js.map +0 -1
- package/dist/lib/stream_manager.js.map +0 -1
- package/src/lib/store/history_rpc.ts +0 -93
- /package/dist/lib/{stream_manager.d.ts → stream_manager/stream_manager.d.ts} +0 -0
package/src/lib/filter/index.ts
CHANGED
@@ -5,7 +5,6 @@ import {
|
|
5
5
|
type CoreProtocolResult,
|
6
6
|
type IBaseProtocolCore,
|
7
7
|
type Libp2p,
|
8
|
-
type ProtocolCreateOptions,
|
9
8
|
ProtocolError,
|
10
9
|
type PubsubTopic
|
11
10
|
} from "@waku/interfaces";
|
@@ -32,21 +31,16 @@ export const FilterCodecs = {
|
|
32
31
|
};
|
33
32
|
|
34
33
|
export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
35
|
-
constructor(
|
34
|
+
public constructor(
|
36
35
|
private handleIncomingMessage: (
|
37
36
|
pubsubTopic: PubsubTopic,
|
38
|
-
wakuMessage: WakuMessage
|
37
|
+
wakuMessage: WakuMessage,
|
38
|
+
peerIdStr: string
|
39
39
|
) => Promise<void>,
|
40
|
-
|
41
|
-
|
40
|
+
public readonly pubsubTopics: PubsubTopic[],
|
41
|
+
libp2p: Libp2p
|
42
42
|
) {
|
43
|
-
super(
|
44
|
-
FilterCodecs.SUBSCRIBE,
|
45
|
-
libp2p.components,
|
46
|
-
log,
|
47
|
-
options!.pubsubTopics!,
|
48
|
-
options
|
49
|
-
);
|
43
|
+
super(FilterCodecs.SUBSCRIBE, libp2p.components, log, pubsubTopics);
|
50
44
|
|
51
45
|
libp2p
|
52
46
|
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
@@ -57,43 +51,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
57
51
|
});
|
58
52
|
}
|
59
53
|
|
60
|
-
|
61
|
-
const { connection, stream } = streamData;
|
62
|
-
const { remotePeer } = connection;
|
63
|
-
log.info(`Received message from ${remotePeer.toString()}`);
|
64
|
-
try {
|
65
|
-
pipe(stream, lp.decode, async (source) => {
|
66
|
-
for await (const bytes of source) {
|
67
|
-
const response = FilterPushRpc.decode(bytes.slice());
|
68
|
-
|
69
|
-
const { pubsubTopic, wakuMessage } = response;
|
70
|
-
|
71
|
-
if (!wakuMessage) {
|
72
|
-
log.error("Received empty message");
|
73
|
-
return;
|
74
|
-
}
|
75
|
-
|
76
|
-
if (!pubsubTopic) {
|
77
|
-
log.error("Pubsub topic missing from push message");
|
78
|
-
return;
|
79
|
-
}
|
80
|
-
|
81
|
-
await this.handleIncomingMessage(pubsubTopic, wakuMessage);
|
82
|
-
}
|
83
|
-
}).then(
|
84
|
-
() => {
|
85
|
-
log.info("Receiving pipe closed.");
|
86
|
-
},
|
87
|
-
(e) => {
|
88
|
-
log.error("Error with receiving pipe", e);
|
89
|
-
}
|
90
|
-
);
|
91
|
-
} catch (e) {
|
92
|
-
log.error("Error decoding message", e);
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
async subscribe(
|
54
|
+
public async subscribe(
|
97
55
|
pubsubTopic: PubsubTopic,
|
98
56
|
peer: Peer,
|
99
57
|
contentTopics: ContentTopic[]
|
@@ -147,7 +105,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
147
105
|
};
|
148
106
|
}
|
149
107
|
|
150
|
-
async unsubscribe(
|
108
|
+
public async unsubscribe(
|
151
109
|
pubsubTopic: PubsubTopic,
|
152
110
|
peer: Peer,
|
153
111
|
contentTopics: ContentTopic[]
|
@@ -163,7 +121,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
163
121
|
return {
|
164
122
|
success: null,
|
165
123
|
failure: {
|
166
|
-
error: ProtocolError.
|
124
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
167
125
|
peerId: peer.id
|
168
126
|
}
|
169
127
|
};
|
@@ -193,7 +151,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
193
151
|
};
|
194
152
|
}
|
195
153
|
|
196
|
-
async unsubscribeAll(
|
154
|
+
public async unsubscribeAll(
|
197
155
|
pubsubTopic: PubsubTopic,
|
198
156
|
peer: Peer
|
199
157
|
): Promise<CoreProtocolResult> {
|
@@ -212,7 +170,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
212
170
|
if (!res || !res.length) {
|
213
171
|
return {
|
214
172
|
failure: {
|
215
|
-
error: ProtocolError.
|
173
|
+
error: ProtocolError.NO_RESPONSE,
|
216
174
|
peerId: peer.id
|
217
175
|
},
|
218
176
|
success: null
|
@@ -241,7 +199,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
241
199
|
};
|
242
200
|
}
|
243
201
|
|
244
|
-
async ping(peer: Peer): Promise<CoreProtocolResult> {
|
202
|
+
public async ping(peer: Peer): Promise<CoreProtocolResult> {
|
245
203
|
let stream: Stream | undefined;
|
246
204
|
try {
|
247
205
|
stream = await this.getStream(peer);
|
@@ -253,7 +211,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
253
211
|
return {
|
254
212
|
success: null,
|
255
213
|
failure: {
|
256
|
-
error: ProtocolError.
|
214
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
257
215
|
peerId: peer.id
|
258
216
|
}
|
259
217
|
};
|
@@ -285,7 +243,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
285
243
|
return {
|
286
244
|
success: null,
|
287
245
|
failure: {
|
288
|
-
error: ProtocolError.
|
246
|
+
error: ProtocolError.NO_RESPONSE,
|
289
247
|
peerId: peer.id
|
290
248
|
}
|
291
249
|
};
|
@@ -311,4 +269,44 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
|
|
311
269
|
failure: null
|
312
270
|
};
|
313
271
|
}
|
272
|
+
|
273
|
+
private onRequest(streamData: IncomingStreamData): void {
|
274
|
+
const { connection, stream } = streamData;
|
275
|
+
const { remotePeer } = connection;
|
276
|
+
log.info(`Received message from ${remotePeer.toString()}`);
|
277
|
+
try {
|
278
|
+
pipe(stream, lp.decode, async (source) => {
|
279
|
+
for await (const bytes of source) {
|
280
|
+
const response = FilterPushRpc.decode(bytes.slice());
|
281
|
+
|
282
|
+
const { pubsubTopic, wakuMessage } = response;
|
283
|
+
|
284
|
+
if (!wakuMessage) {
|
285
|
+
log.error("Received empty message");
|
286
|
+
return;
|
287
|
+
}
|
288
|
+
|
289
|
+
if (!pubsubTopic) {
|
290
|
+
log.error("Pubsub topic missing from push message");
|
291
|
+
return;
|
292
|
+
}
|
293
|
+
|
294
|
+
await this.handleIncomingMessage(
|
295
|
+
pubsubTopic,
|
296
|
+
wakuMessage,
|
297
|
+
connection.remotePeer.toString()
|
298
|
+
);
|
299
|
+
}
|
300
|
+
}).then(
|
301
|
+
() => {
|
302
|
+
log.info("Receiving pipe closed.");
|
303
|
+
},
|
304
|
+
(e) => {
|
305
|
+
log.error("Error with receiving pipe", e);
|
306
|
+
}
|
307
|
+
);
|
308
|
+
} catch (e) {
|
309
|
+
log.error("Error decoding message", e);
|
310
|
+
}
|
311
|
+
}
|
314
312
|
}
|
@@ -0,0 +1,90 @@
|
|
1
|
+
import {
|
2
|
+
HealthStatus,
|
3
|
+
type IHealthManager,
|
4
|
+
NodeHealth,
|
5
|
+
type ProtocolHealth,
|
6
|
+
Protocols
|
7
|
+
} from "@waku/interfaces";
|
8
|
+
|
9
|
+
class HealthManager implements IHealthManager {
|
10
|
+
public static instance: HealthManager;
|
11
|
+
private readonly health: NodeHealth;
|
12
|
+
|
13
|
+
private constructor() {
|
14
|
+
this.health = {
|
15
|
+
overallStatus: HealthStatus.Unhealthy,
|
16
|
+
protocolStatuses: new Map()
|
17
|
+
};
|
18
|
+
}
|
19
|
+
|
20
|
+
public static getInstance(): HealthManager {
|
21
|
+
if (!HealthManager.instance) {
|
22
|
+
HealthManager.instance = new HealthManager();
|
23
|
+
}
|
24
|
+
return HealthManager.instance;
|
25
|
+
}
|
26
|
+
|
27
|
+
public getHealthStatus(): HealthStatus {
|
28
|
+
return this.health.overallStatus;
|
29
|
+
}
|
30
|
+
|
31
|
+
public getProtocolStatus(protocol: Protocols): ProtocolHealth | undefined {
|
32
|
+
return this.health.protocolStatuses.get(protocol);
|
33
|
+
}
|
34
|
+
|
35
|
+
public updateProtocolHealth(
|
36
|
+
multicodec: string,
|
37
|
+
connectedPeers: number
|
38
|
+
): void {
|
39
|
+
const protocol = this.getNameFromMulticodec(multicodec);
|
40
|
+
|
41
|
+
let status: HealthStatus = HealthStatus.Unhealthy;
|
42
|
+
if (connectedPeers == 1) {
|
43
|
+
status = HealthStatus.MinimallyHealthy;
|
44
|
+
} else if (connectedPeers >= 2) {
|
45
|
+
status = HealthStatus.SufficientlyHealthy;
|
46
|
+
}
|
47
|
+
|
48
|
+
this.health.protocolStatuses.set(protocol, {
|
49
|
+
name: protocol,
|
50
|
+
status: status,
|
51
|
+
lastUpdate: new Date()
|
52
|
+
});
|
53
|
+
|
54
|
+
this.updateOverallHealth();
|
55
|
+
}
|
56
|
+
|
57
|
+
private getNameFromMulticodec(multicodec: string): Protocols {
|
58
|
+
let name: Protocols;
|
59
|
+
if (multicodec.includes("filter")) {
|
60
|
+
name = Protocols.Filter;
|
61
|
+
} else if (multicodec.includes("lightpush")) {
|
62
|
+
name = Protocols.LightPush;
|
63
|
+
} else if (multicodec.includes("store")) {
|
64
|
+
name = Protocols.Store;
|
65
|
+
} else {
|
66
|
+
throw new Error(`Unknown protocol: ${multicodec}`);
|
67
|
+
}
|
68
|
+
return name;
|
69
|
+
}
|
70
|
+
|
71
|
+
private updateOverallHealth(): void {
|
72
|
+
const relevantProtocols = [Protocols.LightPush, Protocols.Filter];
|
73
|
+
const statuses = relevantProtocols.map(
|
74
|
+
(p) => this.getProtocolStatus(p)?.status
|
75
|
+
);
|
76
|
+
|
77
|
+
if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
|
78
|
+
this.health.overallStatus = HealthStatus.Unhealthy;
|
79
|
+
} else if (
|
80
|
+
statuses.some((status) => status === HealthStatus.MinimallyHealthy)
|
81
|
+
) {
|
82
|
+
this.health.overallStatus = HealthStatus.MinimallyHealthy;
|
83
|
+
} else {
|
84
|
+
this.health.overallStatus = HealthStatus.SufficientlyHealthy;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
export const getHealthManager = (): HealthManager =>
|
90
|
+
HealthManager.getInstance();
|
@@ -1,6 +1,5 @@
|
|
1
|
-
import type { PeerId
|
2
|
-
import type {
|
3
|
-
import type { IRelay, PeerIdStr } from "@waku/interfaces";
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import type { IRelay, Libp2p, PeerIdStr } from "@waku/interfaces";
|
4
3
|
import type { KeepAliveOptions } from "@waku/interfaces";
|
5
4
|
import { Logger, pubsubTopicToSingleShardInfo } from "@waku/utils";
|
6
5
|
import { utf8ToBytes } from "@waku/utils/bytes";
|
@@ -10,24 +9,34 @@ import { createEncoder } from "./message/version_0.js";
|
|
10
9
|
export const RelayPingContentTopic = "/relay-ping/1/ping/null";
|
11
10
|
const log = new Logger("keep-alive");
|
12
11
|
|
12
|
+
type CreateKeepAliveManagerOptions = {
|
13
|
+
options: KeepAliveOptions;
|
14
|
+
libp2p: Libp2p;
|
15
|
+
relay?: IRelay;
|
16
|
+
};
|
17
|
+
|
13
18
|
export class KeepAliveManager {
|
14
|
-
private
|
15
|
-
private
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
private readonly relay?: IRelay;
|
20
|
+
private readonly libp2p: Libp2p;
|
21
|
+
|
22
|
+
private readonly options: KeepAliveOptions;
|
23
|
+
|
24
|
+
private pingKeepAliveTimers: Map<string, ReturnType<typeof setInterval>> =
|
25
|
+
new Map();
|
26
|
+
private relayKeepAliveTimers: Map<PeerId, ReturnType<typeof setInterval>[]> =
|
27
|
+
new Map();
|
28
|
+
|
29
|
+
public constructor({
|
30
|
+
options,
|
31
|
+
relay,
|
32
|
+
libp2p
|
33
|
+
}: CreateKeepAliveManagerOptions) {
|
22
34
|
this.options = options;
|
23
35
|
this.relay = relay;
|
36
|
+
this.libp2p = libp2p;
|
24
37
|
}
|
25
38
|
|
26
|
-
public start(
|
27
|
-
peerId: PeerId,
|
28
|
-
libp2pPing: PingService,
|
29
|
-
peerStore: PeerStore
|
30
|
-
): void {
|
39
|
+
public start(peerId: PeerId): void {
|
31
40
|
// Just in case a timer already exists for this peer
|
32
41
|
this.stop(peerId);
|
33
42
|
|
@@ -46,7 +55,7 @@ export class KeepAliveManager {
|
|
46
55
|
// ping the peer for keep alive
|
47
56
|
// also update the peer store with the latency
|
48
57
|
try {
|
49
|
-
ping = await
|
58
|
+
ping = await this.libp2p.services.ping.ping(peerId);
|
50
59
|
log.info(`Ping succeeded (${peerIdStr})`, ping);
|
51
60
|
} catch (error) {
|
52
61
|
log.error(`Ping failed for peer (${peerIdStr}).
|
@@ -56,7 +65,7 @@ export class KeepAliveManager {
|
|
56
65
|
}
|
57
66
|
|
58
67
|
try {
|
59
|
-
await peerStore.merge(peerId, {
|
68
|
+
await this.libp2p.peerStore.merge(peerId, {
|
60
69
|
metadata: {
|
61
70
|
ping: utf8ToBytes(ping.toString())
|
62
71
|
}
|
@@ -5,8 +5,8 @@ import {
|
|
5
5
|
type IEncoder,
|
6
6
|
type IMessage,
|
7
7
|
type Libp2p,
|
8
|
-
type ProtocolCreateOptions,
|
9
8
|
ProtocolError,
|
9
|
+
PubsubTopic,
|
10
10
|
type ThisOrThat
|
11
11
|
} from "@waku/interfaces";
|
12
12
|
import { PushResponse } from "@waku/proto";
|
@@ -20,6 +20,7 @@ import { Uint8ArrayList } from "uint8arraylist";
|
|
20
20
|
import { BaseProtocol } from "../base_protocol.js";
|
21
21
|
|
22
22
|
import { PushRpc } from "./push_rpc.js";
|
23
|
+
import { isRLNResponseError, matchRLNErrorMessage } from "./utils.js";
|
23
24
|
|
24
25
|
const log = new Logger("light-push");
|
25
26
|
|
@@ -32,14 +33,11 @@ type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
|
32
33
|
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
33
34
|
*/
|
34
35
|
export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
35
|
-
constructor(
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
options!.pubsubTopics!,
|
41
|
-
options
|
42
|
-
);
|
36
|
+
public constructor(
|
37
|
+
public readonly pubsubTopics: PubsubTopic[],
|
38
|
+
libp2p: Libp2p
|
39
|
+
) {
|
40
|
+
super(LightPushCodec, libp2p.components, log, pubsubTopics);
|
43
41
|
}
|
44
42
|
|
45
43
|
private async preparePushMessage(
|
@@ -78,7 +76,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
78
76
|
}
|
79
77
|
}
|
80
78
|
|
81
|
-
async send(
|
79
|
+
public async send(
|
82
80
|
encoder: IEncoder,
|
83
81
|
message: IMessage,
|
84
82
|
peer: Peer
|
@@ -156,7 +154,19 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
156
154
|
return {
|
157
155
|
success: null,
|
158
156
|
failure: {
|
159
|
-
error: ProtocolError.
|
157
|
+
error: ProtocolError.NO_RESPONSE,
|
158
|
+
peerId: peer.id
|
159
|
+
}
|
160
|
+
};
|
161
|
+
}
|
162
|
+
|
163
|
+
if (isRLNResponseError(response.info)) {
|
164
|
+
const rlnErrorCase = matchRLNErrorMessage(response.info!);
|
165
|
+
log.error("Remote peer rejected the message: ", rlnErrorCase);
|
166
|
+
return {
|
167
|
+
success: null,
|
168
|
+
failure: {
|
169
|
+
error: rlnErrorCase,
|
160
170
|
peerId: peer.id
|
161
171
|
}
|
162
172
|
};
|
@@ -5,7 +5,7 @@ import { v4 as uuid } from "uuid";
|
|
5
5
|
export class PushRpc {
|
6
6
|
public constructor(public proto: proto.PushRpc) {}
|
7
7
|
|
8
|
-
static createRequest(
|
8
|
+
public static createRequest(
|
9
9
|
message: proto.WakuMessage,
|
10
10
|
pubsubTopic: string
|
11
11
|
): PushRpc {
|
@@ -19,20 +19,20 @@ export class PushRpc {
|
|
19
19
|
});
|
20
20
|
}
|
21
21
|
|
22
|
-
static decode(bytes: Uint8ArrayList): PushRpc {
|
22
|
+
public static decode(bytes: Uint8ArrayList): PushRpc {
|
23
23
|
const res = proto.PushRpc.decode(bytes);
|
24
24
|
return new PushRpc(res);
|
25
25
|
}
|
26
26
|
|
27
|
-
encode(): Uint8Array {
|
27
|
+
public encode(): Uint8Array {
|
28
28
|
return proto.PushRpc.encode(this.proto);
|
29
29
|
}
|
30
30
|
|
31
|
-
get query(): proto.PushRequest | undefined {
|
31
|
+
public get query(): proto.PushRequest | undefined {
|
32
32
|
return this.proto.request;
|
33
33
|
}
|
34
34
|
|
35
|
-
get response(): proto.PushResponse | undefined {
|
35
|
+
public get response(): proto.PushResponse | undefined {
|
36
36
|
return this.proto.response;
|
37
37
|
}
|
38
38
|
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { ProtocolError } from "@waku/interfaces";
|
2
|
+
|
3
|
+
// should match nwaku
|
4
|
+
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/rln_relay.nim#L309
|
5
|
+
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim#L20
|
6
|
+
const RLN_GENERATION_PREFIX_ERROR = "could not generate rln-v2 proof";
|
7
|
+
|
8
|
+
export const isRLNResponseError = (info?: string): boolean => {
|
9
|
+
if (!info) {
|
10
|
+
return false;
|
11
|
+
}
|
12
|
+
|
13
|
+
return info.includes(RLN_GENERATION_PREFIX_ERROR);
|
14
|
+
};
|
15
|
+
|
16
|
+
export const matchRLNErrorMessage = (info: string): ProtocolError => {
|
17
|
+
const rlnErrorMap: { [key: string]: ProtocolError } = {
|
18
|
+
[ProtocolError.RLN_IDENTITY_MISSING]: ProtocolError.RLN_IDENTITY_MISSING,
|
19
|
+
[ProtocolError.RLN_MEMBERSHIP_INDEX]: ProtocolError.RLN_MEMBERSHIP_INDEX,
|
20
|
+
[ProtocolError.RLN_LIMIT_MISSING]: ProtocolError.RLN_LIMIT_MISSING
|
21
|
+
};
|
22
|
+
|
23
|
+
const infoLowerCase = info.toLowerCase();
|
24
|
+
for (const errorKey in rlnErrorMap) {
|
25
|
+
if (infoLowerCase.includes(errorKey.toLowerCase())) {
|
26
|
+
return rlnErrorMap[errorKey];
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
return ProtocolError.RLN_PROOF_GENERATION;
|
31
|
+
};
|
@@ -20,28 +20,28 @@ export const Version = 0;
|
|
20
20
|
export { proto };
|
21
21
|
|
22
22
|
export class DecodedMessage implements IDecodedMessage {
|
23
|
-
constructor(
|
23
|
+
public constructor(
|
24
24
|
public pubsubTopic: string,
|
25
25
|
protected proto: proto.WakuMessage
|
26
26
|
) {}
|
27
27
|
|
28
|
-
get ephemeral(): boolean {
|
28
|
+
public get ephemeral(): boolean {
|
29
29
|
return Boolean(this.proto.ephemeral);
|
30
30
|
}
|
31
31
|
|
32
|
-
get payload(): Uint8Array {
|
32
|
+
public get payload(): Uint8Array {
|
33
33
|
return this.proto.payload;
|
34
34
|
}
|
35
35
|
|
36
|
-
get contentTopic(): string {
|
36
|
+
public get contentTopic(): string {
|
37
37
|
return this.proto.contentTopic;
|
38
38
|
}
|
39
39
|
|
40
|
-
get _rawTimestamp(): bigint | undefined {
|
40
|
+
public get _rawTimestamp(): bigint | undefined {
|
41
41
|
return this.proto.timestamp;
|
42
42
|
}
|
43
43
|
|
44
|
-
get timestamp(): Date | undefined {
|
44
|
+
public get timestamp(): Date | undefined {
|
45
45
|
// In the case we receive a value that is bigger than JS's max number,
|
46
46
|
// we catch the error and return undefined.
|
47
47
|
try {
|
@@ -56,23 +56,23 @@ export class DecodedMessage implements IDecodedMessage {
|
|
56
56
|
}
|
57
57
|
}
|
58
58
|
|
59
|
-
get meta(): Uint8Array | undefined {
|
59
|
+
public get meta(): Uint8Array | undefined {
|
60
60
|
return this.proto.meta;
|
61
61
|
}
|
62
62
|
|
63
|
-
get version(): number {
|
63
|
+
public get version(): number {
|
64
64
|
// https://rfc.vac.dev/spec/14/
|
65
65
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
66
66
|
return this.proto.version ?? 0;
|
67
67
|
}
|
68
68
|
|
69
|
-
get rateLimitProof(): IRateLimitProof | undefined {
|
69
|
+
public get rateLimitProof(): IRateLimitProof | undefined {
|
70
70
|
return this.proto.rateLimitProof;
|
71
71
|
}
|
72
72
|
}
|
73
73
|
|
74
74
|
export class Encoder implements IEncoder {
|
75
|
-
constructor(
|
75
|
+
public constructor(
|
76
76
|
public contentTopic: string,
|
77
77
|
public ephemeral: boolean = false,
|
78
78
|
public pubsubTopic: PubsubTopic,
|
@@ -83,11 +83,11 @@ export class Encoder implements IEncoder {
|
|
83
83
|
}
|
84
84
|
}
|
85
85
|
|
86
|
-
async toWire(message: IMessage): Promise<Uint8Array> {
|
86
|
+
public async toWire(message: IMessage): Promise<Uint8Array> {
|
87
87
|
return proto.WakuMessage.encode(await this.toProtoObj(message));
|
88
88
|
}
|
89
89
|
|
90
|
-
async toProtoObj(message: IMessage): Promise<IProtoMessage> {
|
90
|
+
public async toProtoObj(message: IMessage): Promise<IProtoMessage> {
|
91
91
|
const timestamp = message.timestamp ?? new Date();
|
92
92
|
|
93
93
|
const protoMessage = {
|
@@ -133,7 +133,7 @@ export function createEncoder({
|
|
133
133
|
}
|
134
134
|
|
135
135
|
export class Decoder implements IDecoder<DecodedMessage> {
|
136
|
-
constructor(
|
136
|
+
public constructor(
|
137
137
|
public pubsubTopic: PubsubTopic,
|
138
138
|
public contentTopic: string
|
139
139
|
) {
|
@@ -142,7 +142,9 @@ export class Decoder implements IDecoder<DecodedMessage> {
|
|
142
142
|
}
|
143
143
|
}
|
144
144
|
|
145
|
-
fromWireToProtoObj(
|
145
|
+
public fromWireToProtoObj(
|
146
|
+
bytes: Uint8Array
|
147
|
+
): Promise<IProtoMessage | undefined> {
|
146
148
|
const protoMessage = proto.WakuMessage.decode(bytes);
|
147
149
|
return Promise.resolve({
|
148
150
|
payload: protoMessage.payload,
|
@@ -155,7 +157,7 @@ export class Decoder implements IDecoder<DecodedMessage> {
|
|
155
157
|
});
|
156
158
|
}
|
157
159
|
|
158
|
-
async fromProtoObj(
|
160
|
+
public async fromProtoObj(
|
159
161
|
pubsubTopic: string,
|
160
162
|
proto: IProtoMessage
|
161
163
|
): Promise<DecodedMessage | undefined> {
|