@waku/core 0.0.10 → 0.0.11
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 +66 -1
- package/bundle/browser-2f1afe46.js +726 -0
- package/bundle/index.js +9166 -12074
- package/bundle/lib/base_protocol.js +108 -0
- package/bundle/lib/message/topic_only_message.js +3 -2
- package/bundle/lib/message/version_0.js +3 -2
- package/bundle/peer_exchange-1229c8b0.js +4302 -0
- package/bundle/{topic_only_message-b1eddea1.js → topic_only_message-e8406994.js} +12 -8
- package/bundle/{version_0-862a05e0.js → version_0-e9a6cfb0.js} +35 -35
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +21 -0
- package/dist/lib/base_protocol.js +33 -0
- package/dist/lib/base_protocol.js.map +1 -0
- package/dist/lib/connection_manager.d.ts +31 -0
- package/dist/lib/connection_manager.js +146 -0
- package/dist/lib/connection_manager.js.map +1 -0
- package/dist/lib/filter/filter_rpc.d.ts +8 -8
- package/dist/lib/filter/filter_rpc.js +6 -6
- package/dist/lib/filter/index.d.ts +5 -22
- package/dist/lib/filter/index.js +31 -86
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/group_by.d.ts +1 -1
- package/dist/lib/group_by.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +17 -0
- package/dist/lib/keep_alive_manager.js +62 -0
- package/dist/lib/keep_alive_manager.js.map +1 -0
- package/dist/lib/light_push/index.d.ts +3 -19
- package/dist/lib/light_push/index.js +13 -39
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/push_rpc.d.ts +5 -5
- package/dist/lib/light_push/push_rpc.js +6 -6
- package/dist/lib/message/topic_only_message.d.ts +5 -3
- package/dist/lib/message/topic_only_message.js +8 -5
- package/dist/lib/message/topic_only_message.js.map +1 -1
- package/dist/lib/message/version_0.d.ts +12 -12
- package/dist/lib/message/version_0.js +29 -30
- package/dist/lib/message/version_0.js.map +1 -1
- package/dist/lib/predefined_bootstrap_nodes.js +1 -1
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/relay/index.d.ts +4 -18
- package/dist/lib/relay/index.js +47 -26
- package/dist/lib/relay/index.js.map +1 -1
- package/dist/lib/relay/message_validator.d.ts +4 -0
- package/dist/lib/relay/message_validator.js +25 -0
- package/dist/lib/relay/message_validator.js.map +1 -0
- package/dist/lib/store/history_rpc.d.ts +4 -4
- package/dist/lib/store/history_rpc.js +9 -9
- package/dist/lib/store/history_rpc.js.map +1 -1
- package/dist/lib/store/index.d.ts +4 -25
- package/dist/lib/store/index.js +20 -37
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/to_proto_message.js +3 -2
- package/dist/lib/to_proto_message.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +12 -23
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/dist/lib/waku.d.ts +8 -15
- package/dist/lib/waku.js +34 -97
- package/dist/lib/waku.js.map +1 -1
- package/package.json +50 -61
- package/src/index.ts +11 -3
- package/src/lib/base_protocol.ts +47 -0
- package/src/lib/connection_manager.ts +220 -0
- package/src/lib/filter/filter_rpc.ts +10 -10
- package/src/lib/filter/index.ts +52 -147
- package/src/lib/group_by.ts +1 -1
- package/src/lib/keep_alive_manager.ts +89 -0
- package/src/lib/light_push/index.ts +17 -78
- package/src/lib/light_push/push_rpc.ts +9 -9
- package/src/lib/message/topic_only_message.ts +11 -5
- package/src/lib/message/version_0.ts +42 -37
- package/src/lib/predefined_bootstrap_nodes.ts +1 -1
- package/src/lib/relay/index.ts +77 -52
- package/src/lib/relay/message_validator.ts +35 -0
- package/src/lib/store/history_rpc.ts +12 -12
- package/src/lib/store/index.ts +30 -84
- package/src/lib/to_proto_message.ts +3 -2
- package/src/lib/wait_for_remote_peer.ts +13 -29
- package/src/lib/waku.ts +54 -136
- package/bundle/peer_exchange-df95c3a7.js +0 -11801
- package/dist/lib/random_subset.d.ts +0 -4
- package/dist/lib/random_subset.js +0 -25
- package/dist/lib/random_subset.js.map +0 -1
- package/src/lib/random_subset.ts +0 -30
@@ -1,35 +1,28 @@
|
|
1
|
-
import { getPeersForProtocol, selectConnection, selectPeerForProtocol, selectRandomPeer, } from "@waku/libp2p-utils";
|
2
1
|
import { PushResponse } from "@waku/proto";
|
3
2
|
import debug from "debug";
|
4
3
|
import all from "it-all";
|
5
4
|
import * as lp from "it-length-prefixed";
|
6
5
|
import { pipe } from "it-pipe";
|
7
6
|
import { Uint8ArrayList } from "uint8arraylist";
|
7
|
+
import { BaseProtocol } from "../base_protocol.js";
|
8
8
|
import { DefaultPubSubTopic } from "../constants.js";
|
9
|
-
import {
|
9
|
+
import { PushRpc } from "./push_rpc.js";
|
10
10
|
const log = debug("waku:light-push");
|
11
11
|
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
12
12
|
export { PushResponse };
|
13
13
|
/**
|
14
14
|
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
15
15
|
*/
|
16
|
-
class LightPush {
|
17
|
-
constructor(
|
18
|
-
|
19
|
-
this.
|
16
|
+
class LightPush extends BaseProtocol {
|
17
|
+
constructor(libp2p, options) {
|
18
|
+
super(LightPushCodec, libp2p.peerStore, libp2p.getConnections.bind(libp2p));
|
19
|
+
this.libp2p = libp2p;
|
20
|
+
this.options = options || {};
|
20
21
|
}
|
21
22
|
async push(encoder, message, opts) {
|
22
|
-
const pubSubTopic =
|
23
|
-
const
|
24
|
-
|
25
|
-
throw new Error("Failed to get a peer");
|
26
|
-
}
|
27
|
-
const { peer } = res;
|
28
|
-
const connections = this.components.connectionManager.getConnections(peer.id);
|
29
|
-
const connection = selectConnection(connections);
|
30
|
-
if (!connection)
|
31
|
-
throw "Failed to get a connection to the peer";
|
32
|
-
const stream = await connection.newStream(LightPushCodec);
|
23
|
+
const { pubSubTopic = DefaultPubSubTopic } = this.options;
|
24
|
+
const peer = await this.getPeer(opts?.peerId);
|
25
|
+
const stream = await this.newStream(peer);
|
33
26
|
const recipients = [];
|
34
27
|
try {
|
35
28
|
const protoMessage = await encoder.toProtoObj(message);
|
@@ -37,14 +30,14 @@ class LightPush {
|
|
37
30
|
log("Failed to encode to protoMessage, aborting push");
|
38
31
|
return { recipients };
|
39
32
|
}
|
40
|
-
const query =
|
33
|
+
const query = PushRpc.createRequest(protoMessage, pubSubTopic);
|
41
34
|
const res = await pipe([query.encode()], lp.encode(), stream, lp.decode(), async (source) => await all(source));
|
42
35
|
try {
|
43
36
|
const bytes = new Uint8ArrayList();
|
44
37
|
res.forEach((chunk) => {
|
45
38
|
bytes.append(chunk);
|
46
39
|
});
|
47
|
-
const response =
|
40
|
+
const response = PushRpc.decode(bytes).response;
|
48
41
|
if (!response) {
|
49
42
|
log("No response in PushRPC");
|
50
43
|
return { recipients };
|
@@ -62,27 +55,8 @@ class LightPush {
|
|
62
55
|
}
|
63
56
|
return { recipients };
|
64
57
|
}
|
65
|
-
/**
|
66
|
-
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
67
|
-
* light push protocol. Waku may or may not be currently connected to these
|
68
|
-
* peers.
|
69
|
-
*/
|
70
|
-
async peers() {
|
71
|
-
return getPeersForProtocol(this.components.peerStore, [LightPushCodec]);
|
72
|
-
}
|
73
|
-
/**
|
74
|
-
* Returns a random peer that supports light push protocol from the address
|
75
|
-
* book (`libp2p.peerStore`). Waku may or may not be currently connected to
|
76
|
-
* this peer.
|
77
|
-
*/
|
78
|
-
async randomPeer() {
|
79
|
-
return selectRandomPeer(await this.peers());
|
80
|
-
}
|
81
|
-
get peerStore() {
|
82
|
-
return this.components.peerStore;
|
83
|
-
}
|
84
58
|
}
|
85
59
|
export function wakuLightPush(init = {}) {
|
86
|
-
return (
|
60
|
+
return (libp2p) => new LightPush(libp2p, init);
|
87
61
|
}
|
88
62
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/light_push/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/light_push/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExC,MAAM,GAAG,GAAG,KAAK,CAAC,iBAAiB,CAAC,CAAC;AAErC,MAAM,CAAC,MAAM,cAAc,GAAG,iCAAiC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,CAAC;AAExB;;GAEG;AACH,MAAM,SAAU,SAAQ,YAAY;IAGlC,YAAmB,MAAc,EAAE,OAA+B;QAChE,KAAK,CAAC,cAAc,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;QAD3D,WAAM,GAAN,MAAM,CAAQ;QAE/B,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,IAAI,CACR,OAAiB,EACjB,OAAiB,EACjB,IAAsB;QAEtB,MAAM,EAAE,WAAW,GAAG,kBAAkB,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAE1D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC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AAG,CAAC,wCAAwC,EAAE,GAAG,CAAC,CAAC;SACpD;QACD,OAAO,EAAE,UAAU,EAAE,CAAC;IACxB,CAAC;CACF;AAED,MAAM,UAAU,aAAa,CAC3B,OAAuC,EAAE;IAEzC,OAAO,CAAC,MAAc,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACzD,CAAC"}
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { proto_lightpush as proto } from "@waku/proto";
|
2
2
|
import type { Uint8ArrayList } from "uint8arraylist";
|
3
|
-
export declare class
|
4
|
-
proto: proto.
|
5
|
-
constructor(proto: proto.
|
6
|
-
static createRequest(message: proto.WakuMessage, pubSubTopic: string):
|
7
|
-
static decode(bytes: Uint8ArrayList):
|
3
|
+
export declare class PushRpc {
|
4
|
+
proto: proto.PushRpc;
|
5
|
+
constructor(proto: proto.PushRpc);
|
6
|
+
static createRequest(message: proto.WakuMessage, pubSubTopic: string): PushRpc;
|
7
|
+
static decode(bytes: Uint8ArrayList): PushRpc;
|
8
8
|
encode(): Uint8Array;
|
9
9
|
get query(): proto.PushRequest | undefined;
|
10
10
|
get response(): proto.PushResponse | undefined;
|
@@ -1,25 +1,25 @@
|
|
1
1
|
import { proto_lightpush as proto } from "@waku/proto";
|
2
2
|
import { v4 as uuid } from "uuid";
|
3
|
-
export class
|
3
|
+
export class PushRpc {
|
4
4
|
constructor(proto) {
|
5
5
|
this.proto = proto;
|
6
6
|
}
|
7
7
|
static createRequest(message, pubSubTopic) {
|
8
|
-
return new
|
8
|
+
return new PushRpc({
|
9
9
|
requestId: uuid(),
|
10
10
|
request: {
|
11
11
|
message: message,
|
12
|
-
|
12
|
+
pubsubTopic: pubSubTopic,
|
13
13
|
},
|
14
14
|
response: undefined,
|
15
15
|
});
|
16
16
|
}
|
17
17
|
static decode(bytes) {
|
18
|
-
const res = proto.
|
19
|
-
return new
|
18
|
+
const res = proto.PushRpc.decode(bytes);
|
19
|
+
return new PushRpc(res);
|
20
20
|
}
|
21
21
|
encode() {
|
22
|
-
return proto.
|
22
|
+
return proto.PushRpc.encode(this.proto);
|
23
23
|
}
|
24
24
|
get query() {
|
25
25
|
return this.proto.request;
|
@@ -1,16 +1,18 @@
|
|
1
1
|
import type { IDecodedMessage, IDecoder, IProtoMessage } from "@waku/interfaces";
|
2
2
|
import { TopicOnlyMessage as ProtoTopicOnlyMessage } from "@waku/proto";
|
3
3
|
export declare class TopicOnlyMessage implements IDecodedMessage {
|
4
|
+
pubSubTopic: string;
|
4
5
|
private proto;
|
5
|
-
payload:
|
6
|
+
payload: Uint8Array;
|
6
7
|
rateLimitProof: undefined;
|
7
8
|
timestamp: undefined;
|
9
|
+
meta: undefined;
|
8
10
|
ephemeral: undefined;
|
9
|
-
constructor(proto: ProtoTopicOnlyMessage);
|
11
|
+
constructor(pubSubTopic: string, proto: ProtoTopicOnlyMessage);
|
10
12
|
get contentTopic(): string;
|
11
13
|
}
|
12
14
|
export declare class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
|
13
15
|
contentTopic: string;
|
14
16
|
fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined>;
|
15
|
-
fromProtoObj(proto: IProtoMessage): Promise<TopicOnlyMessage | undefined>;
|
17
|
+
fromProtoObj(pubSubTopic: string, proto: IProtoMessage): Promise<TopicOnlyMessage | undefined>;
|
16
18
|
}
|
@@ -2,11 +2,13 @@ import { TopicOnlyMessage as ProtoTopicOnlyMessage } from "@waku/proto";
|
|
2
2
|
import debug from "debug";
|
3
3
|
const log = debug("waku:message:topic-only");
|
4
4
|
export class TopicOnlyMessage {
|
5
|
-
constructor(proto) {
|
5
|
+
constructor(pubSubTopic, proto) {
|
6
|
+
this.pubSubTopic = pubSubTopic;
|
6
7
|
this.proto = proto;
|
8
|
+
this.payload = new Uint8Array();
|
7
9
|
}
|
8
10
|
get contentTopic() {
|
9
|
-
return this.proto.contentTopic
|
11
|
+
return this.proto.contentTopic;
|
10
12
|
}
|
11
13
|
}
|
12
14
|
export class TopicOnlyDecoder {
|
@@ -18,15 +20,16 @@ export class TopicOnlyDecoder {
|
|
18
20
|
log("Message decoded", protoMessage);
|
19
21
|
return Promise.resolve({
|
20
22
|
contentTopic: protoMessage.contentTopic,
|
21
|
-
payload:
|
23
|
+
payload: new Uint8Array(),
|
22
24
|
rateLimitProof: undefined,
|
23
25
|
timestamp: undefined,
|
26
|
+
meta: undefined,
|
24
27
|
version: undefined,
|
25
28
|
ephemeral: undefined,
|
26
29
|
});
|
27
30
|
}
|
28
|
-
async fromProtoObj(proto) {
|
29
|
-
return new TopicOnlyMessage(proto);
|
31
|
+
async fromProtoObj(pubSubTopic, proto) {
|
32
|
+
return new TopicOnlyMessage(pubSubTopic, proto);
|
30
33
|
}
|
31
34
|
}
|
32
35
|
//# sourceMappingURL=topic_only_message.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"topic_only_message.js","sourceRoot":"","sources":["../../../src/lib/message/topic_only_message.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,IAAI,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,GAAG,GAAG,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAE7C,MAAM,OAAO,gBAAgB;
|
1
|
+
{"version":3,"file":"topic_only_message.js","sourceRoot":"","sources":["../../../src/lib/message/topic_only_message.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,IAAI,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,GAAG,GAAG,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAE7C,MAAM,OAAO,gBAAgB;IAO3B,YACS,WAAmB,EAClB,KAA4B;QAD7B,gBAAW,GAAX,WAAW,CAAQ;QAClB,UAAK,GAAL,KAAK,CAAuB;QAR/B,YAAO,GAAe,IAAI,UAAU,EAAE,CAAC;IAS3C,CAAC;IAEJ,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC;IACjC,CAAC;CACF;AAED,MAAM,OAAO,gBAAgB;IAA7B;QACS,iBAAY,GAAG,EAAE,CAAC;IAsB3B,CAAC;IApBC,kBAAkB,CAAC,KAAiB;QAClC,MAAM,YAAY,GAAG,qBAAqB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzD,GAAG,CAAC,iBAAiB,EAAE,YAAY,CAAC,CAAC;QACrC,OAAO,OAAO,CAAC,OAAO,CAAC;YACrB,YAAY,EAAE,YAAY,CAAC,YAAY;YACvC,OAAO,EAAE,IAAI,UAAU,EAAE;YACzB,cAAc,EAAE,SAAS;YACzB,SAAS,EAAE,SAAS;YACpB,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,SAAS;YAClB,SAAS,EAAE,SAAS;SACrB,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,WAAmB,EACnB,KAAoB;QAEpB,OAAO,IAAI,gBAAgB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;CACF"}
|
@@ -1,23 +1,26 @@
|
|
1
|
-
import
|
1
|
+
import { IMetaSetter } from "@waku/interfaces";
|
2
|
+
import type { EncoderOptions, IDecodedMessage, IDecoder, IEncoder, IMessage, IProtoMessage, IRateLimitProof } from "@waku/interfaces";
|
2
3
|
import { proto_message as proto } from "@waku/proto";
|
3
4
|
export declare const Version = 0;
|
4
5
|
export { proto };
|
5
6
|
export declare class DecodedMessage implements IDecodedMessage {
|
7
|
+
pubSubTopic: string;
|
6
8
|
protected proto: proto.WakuMessage;
|
7
|
-
constructor(proto: proto.WakuMessage);
|
8
|
-
get _rawPayload(): Uint8Array | undefined;
|
9
|
+
constructor(pubSubTopic: string, proto: proto.WakuMessage);
|
9
10
|
get ephemeral(): boolean;
|
10
|
-
get payload(): Uint8Array
|
11
|
-
get contentTopic(): string
|
11
|
+
get payload(): Uint8Array;
|
12
|
+
get contentTopic(): string;
|
12
13
|
get _rawTimestamp(): bigint | undefined;
|
13
14
|
get timestamp(): Date | undefined;
|
15
|
+
get meta(): Uint8Array | undefined;
|
14
16
|
get version(): number;
|
15
17
|
get rateLimitProof(): IRateLimitProof | undefined;
|
16
18
|
}
|
17
19
|
export declare class Encoder implements IEncoder {
|
18
20
|
contentTopic: string;
|
19
21
|
ephemeral: boolean;
|
20
|
-
|
22
|
+
metaSetter?: IMetaSetter | undefined;
|
23
|
+
constructor(contentTopic: string, ephemeral?: boolean, metaSetter?: IMetaSetter | undefined);
|
21
24
|
toWire(message: IMessage): Promise<Uint8Array>;
|
22
25
|
toProtoObj(message: IMessage): Promise<IProtoMessage>;
|
23
26
|
}
|
@@ -29,19 +32,16 @@ export declare class Encoder implements IEncoder {
|
|
29
32
|
* pass to { @link @waku/interfaces.LightPush.push } or
|
30
33
|
* { @link @waku/interfaces.Relay.send } to automatically encode outgoing
|
31
34
|
* messages.
|
32
|
-
*
|
33
|
-
* @param contentTopic The content topic to set on outgoing messages.
|
34
|
-
* @param ephemeral An optional flag to mark message as ephemeral, ie, not to be stored by Waku Store nodes.
|
35
35
|
*/
|
36
|
-
export declare function createEncoder(contentTopic
|
36
|
+
export declare function createEncoder({ contentTopic, ephemeral, metaSetter, }: EncoderOptions): Encoder;
|
37
37
|
export declare class Decoder implements IDecoder<DecodedMessage> {
|
38
38
|
contentTopic: string;
|
39
39
|
constructor(contentTopic: string);
|
40
40
|
fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined>;
|
41
|
-
fromProtoObj(proto: IProtoMessage): Promise<DecodedMessage | undefined>;
|
41
|
+
fromProtoObj(pubSubTopic: string, proto: IProtoMessage): Promise<DecodedMessage | undefined>;
|
42
42
|
}
|
43
43
|
/**
|
44
|
-
* Creates
|
44
|
+
* Creates a decoder that decode messages without Waku level encryption.
|
45
45
|
*
|
46
46
|
* A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
47
47
|
* format when received from the Waku network. The resulting decoder can then be
|
@@ -5,20 +5,15 @@ const OneMillion = BigInt(1000000);
|
|
5
5
|
export const Version = 0;
|
6
6
|
export { proto };
|
7
7
|
export class DecodedMessage {
|
8
|
-
constructor(proto) {
|
8
|
+
constructor(pubSubTopic, proto) {
|
9
|
+
this.pubSubTopic = pubSubTopic;
|
9
10
|
this.proto = proto;
|
10
11
|
}
|
11
|
-
get _rawPayload() {
|
12
|
-
if (this.proto.payload) {
|
13
|
-
return new Uint8Array(this.proto.payload);
|
14
|
-
}
|
15
|
-
return;
|
16
|
-
}
|
17
12
|
get ephemeral() {
|
18
13
|
return Boolean(this.proto.ephemeral);
|
19
14
|
}
|
20
15
|
get payload() {
|
21
|
-
return this.
|
16
|
+
return this.proto.payload;
|
22
17
|
}
|
23
18
|
get contentTopic() {
|
24
19
|
return this.proto.contentTopic;
|
@@ -35,17 +30,18 @@ export class DecodedMessage {
|
|
35
30
|
const timestamp = this.proto.timestamp / OneMillion;
|
36
31
|
return new Date(Number(timestamp));
|
37
32
|
}
|
38
|
-
|
39
|
-
return new Date(this.proto.timestampDeprecated * 1000);
|
40
|
-
}
|
33
|
+
return;
|
41
34
|
}
|
42
35
|
catch (e) {
|
43
36
|
return;
|
44
37
|
}
|
45
|
-
|
38
|
+
}
|
39
|
+
get meta() {
|
40
|
+
return this.proto.meta;
|
46
41
|
}
|
47
42
|
get version() {
|
48
|
-
// https://
|
43
|
+
// https://rfc.vac.dev/spec/14/
|
44
|
+
// > If omitted, the value SHOULD be interpreted as version 0.
|
49
45
|
return this.proto.version ?? 0;
|
50
46
|
}
|
51
47
|
get rateLimitProof() {
|
@@ -53,23 +49,30 @@ export class DecodedMessage {
|
|
53
49
|
}
|
54
50
|
}
|
55
51
|
export class Encoder {
|
56
|
-
constructor(contentTopic, ephemeral = false) {
|
52
|
+
constructor(contentTopic, ephemeral = false, metaSetter) {
|
57
53
|
this.contentTopic = contentTopic;
|
58
54
|
this.ephemeral = ephemeral;
|
55
|
+
this.metaSetter = metaSetter;
|
59
56
|
}
|
60
57
|
async toWire(message) {
|
61
58
|
return proto.WakuMessage.encode(await this.toProtoObj(message));
|
62
59
|
}
|
63
60
|
async toProtoObj(message) {
|
64
61
|
const timestamp = message.timestamp ?? new Date();
|
65
|
-
|
62
|
+
const protoMessage = {
|
66
63
|
payload: message.payload,
|
67
64
|
version: Version,
|
68
65
|
contentTopic: this.contentTopic,
|
69
66
|
timestamp: BigInt(timestamp.valueOf()) * OneMillion,
|
67
|
+
meta: undefined,
|
70
68
|
rateLimitProof: message.rateLimitProof,
|
71
69
|
ephemeral: this.ephemeral,
|
72
70
|
};
|
71
|
+
if (this.metaSetter) {
|
72
|
+
const meta = this.metaSetter(protoMessage);
|
73
|
+
return { ...protoMessage, meta };
|
74
|
+
}
|
75
|
+
return protoMessage;
|
73
76
|
}
|
74
77
|
}
|
75
78
|
/**
|
@@ -80,12 +83,9 @@ export class Encoder {
|
|
80
83
|
* pass to { @link @waku/interfaces.LightPush.push } or
|
81
84
|
* { @link @waku/interfaces.Relay.send } to automatically encode outgoing
|
82
85
|
* messages.
|
83
|
-
*
|
84
|
-
* @param contentTopic The content topic to set on outgoing messages.
|
85
|
-
* @param ephemeral An optional flag to mark message as ephemeral, ie, not to be stored by Waku Store nodes.
|
86
86
|
*/
|
87
|
-
export function createEncoder(contentTopic, ephemeral
|
88
|
-
return new Encoder(contentTopic, ephemeral);
|
87
|
+
export function createEncoder({ contentTopic, ephemeral, metaSetter, }) {
|
88
|
+
return new Encoder(contentTopic, ephemeral, metaSetter);
|
89
89
|
}
|
90
90
|
export class Decoder {
|
91
91
|
constructor(contentTopic) {
|
@@ -95,28 +95,27 @@ export class Decoder {
|
|
95
95
|
const protoMessage = proto.WakuMessage.decode(bytes);
|
96
96
|
log("Message decoded", protoMessage);
|
97
97
|
return Promise.resolve({
|
98
|
-
payload: protoMessage.payload
|
99
|
-
contentTopic: protoMessage.contentTopic
|
98
|
+
payload: protoMessage.payload,
|
99
|
+
contentTopic: protoMessage.contentTopic,
|
100
100
|
version: protoMessage.version ?? undefined,
|
101
101
|
timestamp: protoMessage.timestamp ?? undefined,
|
102
|
+
meta: protoMessage.meta ?? undefined,
|
102
103
|
rateLimitProof: protoMessage.rateLimitProof ?? undefined,
|
103
104
|
ephemeral: protoMessage.ephemeral ?? false,
|
104
105
|
});
|
105
106
|
}
|
106
|
-
async fromProtoObj(proto) {
|
107
|
-
// https://
|
108
|
-
|
109
|
-
|
110
|
-
}
|
111
|
-
if (proto.version !== Version) {
|
107
|
+
async fromProtoObj(pubSubTopic, proto) {
|
108
|
+
// https://rfc.vac.dev/spec/14/
|
109
|
+
// > If omitted, the value SHOULD be interpreted as version 0.
|
110
|
+
if (proto.version ?? 0 !== Version) {
|
112
111
|
log("Failed to decode due to incorrect version, expected:", Version, ", actual:", proto.version);
|
113
112
|
return Promise.resolve(undefined);
|
114
113
|
}
|
115
|
-
return new DecodedMessage(proto);
|
114
|
+
return new DecodedMessage(pubSubTopic, proto);
|
116
115
|
}
|
117
116
|
}
|
118
117
|
/**
|
119
|
-
* Creates
|
118
|
+
* Creates a decoder that decode messages without Waku level encryption.
|
120
119
|
*
|
121
120
|
* A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
122
121
|
* format when received from the Waku network. The resulting decoder can then be
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"version_0.js","sourceRoot":"","sources":["../../../src/lib/message/version_0.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"version_0.js","sourceRoot":"","sources":["../../../src/lib/message/version_0.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,aAAa,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,GAAG,GAAG,KAAK,CAAC,wBAAwB,CAAC,CAAC;AAC5C,MAAM,UAAU,GAAG,MAAM,CAAC,OAAS,CAAC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AAC,KAAK,CAAC,CAAC;QACrD,GAAG,CAAC,iBAAiB,EAAE,YAAY,CAAC,CAAC;QACrC,OAAO,OAAO,CAAC,OAAO,CAAC;YACrB,OAAO,EAAE,YAAY,CAAC,OAAO;YAC7B,YAAY,EAAE,YAAY,CAAC,YAAY;YACvC,OAAO,EAAE,YAAY,CAAC,OAAO,IAAI,SAAS;YAC1C,SAAS,EAAE,YAAY,CAAC,SAAS,IAAI,SAAS;YAC9C,IAAI,EAAE,YAAY,CAAC,IAAI,IAAI,SAAS;YACpC,cAAc,EAAE,YAAY,CAAC,cAAc,IAAI,SAAS;YACxD,SAAS,EAAE,YAAY,CAAC,SAAS,IAAI,KAAK;SAC3C,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,WAAmB,EACnB,KAAoB;QAEpB,+BAA+B;QAC/B,8DAA8D;QAC9D,IAAI,KAAK,CAAC,OAAO,IAAI,CAAC,KAAK,OAAO,EAAE;YAClC,GAAG,CACD,sDAAsD,EACtD,OAAO,EACP,WAAW,EACX,KAAK,CAAC,OAAO,CACd,CAAC;YACF,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;SACnC;QAED,OAAO,IAAI,cAAc,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAAC,YAAoB;IAChD,OAAO,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC;AACnC,CAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"predefined_bootstrap_nodes.js","sourceRoot":"","sources":["../../src/lib/predefined_bootstrap_nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"predefined_bootstrap_nodes.js","sourceRoot":"","sources":["../../src/lib/predefined_bootstrap_nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,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|
1
1
|
import { GossipSubComponents, GossipsubOpts } from "@chainsafe/libp2p-gossipsub";
|
2
|
-
import type { Callback, IDecoder, IRelay } from "@waku/interfaces";
|
2
|
+
import type { Callback, IDecoder, IRelay, ProtocolCreateOptions } from "@waku/interfaces";
|
3
3
|
import { IDecodedMessage } from "@waku/interfaces";
|
4
|
-
export
|
4
|
+
export type Observer<T extends IDecodedMessage> = {
|
5
5
|
decoder: IDecoder<T>;
|
6
6
|
callback: Callback<T>;
|
7
7
|
};
|
8
|
-
export
|
9
|
-
|
10
|
-
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
11
|
-
*
|
12
|
-
* One and only one pubsub topic is used by Waku. This is used by:
|
13
|
-
* - WakuRelay to receive, route and send messages,
|
14
|
-
* - WakuLightPush to send messages,
|
15
|
-
* - WakuStore to retrieve messages.
|
16
|
-
*
|
17
|
-
* The usage of the default pubsub topic is recommended.
|
18
|
-
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
19
|
-
*
|
20
|
-
* @default {@link DefaultPubSubTopic}
|
21
|
-
*/
|
22
|
-
pubSubTopic?: string;
|
23
|
-
}
|
8
|
+
export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts;
|
9
|
+
export type ContentTopic = string;
|
24
10
|
export declare function wakuRelay(init?: Partial<RelayCreateOptions>): (components: GossipSubComponents) => IRelay;
|
package/dist/lib/relay/index.js
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
import { GossipSub, } from "@chainsafe/libp2p-gossipsub";
|
2
2
|
import { SignaturePolicy } from "@chainsafe/libp2p-gossipsub/types";
|
3
|
+
import { CustomEvent } from "@libp2p/interfaces/events";
|
3
4
|
import debug from "debug";
|
4
5
|
import { DefaultPubSubTopic } from "../constants.js";
|
5
6
|
import { TopicOnlyDecoder } from "../message/topic_only_message.js";
|
6
7
|
import { pushOrInitMapSet } from "../push_or_init_map.js";
|
7
8
|
import * as constants from "./constants.js";
|
9
|
+
import { messageValidator } from "./message_validator.js";
|
8
10
|
const log = debug("waku:relay");
|
9
11
|
/**
|
10
12
|
* Implements the [Waku v2 Relay protocol](https://rfc.vac.dev/spec/11/).
|
@@ -21,8 +23,8 @@ class Relay extends GossipSub {
|
|
21
23
|
});
|
22
24
|
super(components, options);
|
23
25
|
this.multicodecs = constants.RelayCodecs;
|
24
|
-
this.observers = new Map();
|
25
26
|
this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic;
|
27
|
+
this.observers = new Map();
|
26
28
|
// TODO: User might want to decide what decoder should be used (e.g. for RLN)
|
27
29
|
this.defaultDecoder = new TopicOnlyDecoder();
|
28
30
|
}
|
@@ -58,14 +60,50 @@ class Relay extends GossipSub {
|
|
58
60
|
decoder,
|
59
61
|
callback,
|
60
62
|
};
|
61
|
-
|
63
|
+
const contentTopic = decoder.contentTopic;
|
64
|
+
pushOrInitMapSet(this.observers, contentTopic, observer);
|
65
|
+
this.dispatchEvent(new CustomEvent("observer:added", {
|
66
|
+
detail: {
|
67
|
+
contentTopic,
|
68
|
+
},
|
69
|
+
}));
|
62
70
|
return () => {
|
63
|
-
const observers = this.observers.get(
|
71
|
+
const observers = this.observers.get(contentTopic);
|
64
72
|
if (observers) {
|
65
73
|
observers.delete(observer);
|
74
|
+
this.dispatchEvent(new CustomEvent("observer:removed", {
|
75
|
+
detail: {
|
76
|
+
contentTopic,
|
77
|
+
},
|
78
|
+
}));
|
66
79
|
}
|
67
80
|
};
|
68
81
|
}
|
82
|
+
async processIncomingMessage(pubSubTopic, bytes) {
|
83
|
+
const topicOnlyMsg = await this.defaultDecoder.fromWireToProtoObj(bytes);
|
84
|
+
if (!topicOnlyMsg || !topicOnlyMsg.contentTopic) {
|
85
|
+
log("Message does not have a content topic, skipping");
|
86
|
+
return;
|
87
|
+
}
|
88
|
+
const observers = this.observers.get(topicOnlyMsg.contentTopic);
|
89
|
+
if (!observers) {
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
await Promise.all(Array.from(observers).map(async ({ decoder, callback }) => {
|
93
|
+
const protoMsg = await decoder.fromWireToProtoObj(bytes);
|
94
|
+
if (!protoMsg) {
|
95
|
+
log("Internal error: message previously decoded failed on 2nd pass.");
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
const msg = await decoder.fromProtoObj(pubSubTopic, protoMsg);
|
99
|
+
if (msg) {
|
100
|
+
callback(msg);
|
101
|
+
}
|
102
|
+
else {
|
103
|
+
log("Failed to decode messages on", topicOnlyMsg.contentTopic);
|
104
|
+
}
|
105
|
+
}));
|
106
|
+
}
|
69
107
|
/**
|
70
108
|
* Subscribe to a pubsub topic and start emitting Waku messages to observers.
|
71
109
|
*
|
@@ -76,32 +114,15 @@ class Relay extends GossipSub {
|
|
76
114
|
if (event.detail.msg.topic !== pubSubTopic)
|
77
115
|
return;
|
78
116
|
log(`Message received on ${pubSubTopic}`);
|
79
|
-
|
80
|
-
if (!topicOnlyMsg || !topicOnlyMsg.contentTopic) {
|
81
|
-
log("Message does not have a content topic, skipping");
|
82
|
-
return;
|
83
|
-
}
|
84
|
-
const observers = this.observers.get(topicOnlyMsg.contentTopic);
|
85
|
-
if (!observers) {
|
86
|
-
return;
|
87
|
-
}
|
88
|
-
await Promise.all(Array.from(observers).map(async ({ decoder, callback }) => {
|
89
|
-
const protoMsg = await decoder.fromWireToProtoObj(event.detail.msg.data);
|
90
|
-
if (!protoMsg) {
|
91
|
-
log("Internal error: message previously decoded failed on 2nd pass.");
|
92
|
-
return;
|
93
|
-
}
|
94
|
-
const msg = await decoder.fromProtoObj(protoMsg);
|
95
|
-
if (msg) {
|
96
|
-
callback(msg);
|
97
|
-
}
|
98
|
-
else {
|
99
|
-
log("Failed to decode messages on", topicOnlyMsg.contentTopic);
|
100
|
-
}
|
101
|
-
}));
|
117
|
+
this.processIncomingMessage(event.detail.msg.topic, event.detail.msg.data).catch((e) => log("Failed to process incoming message", e));
|
102
118
|
});
|
119
|
+
this.topicValidators.set(pubSubTopic, messageValidator);
|
103
120
|
super.subscribe(pubSubTopic);
|
104
121
|
}
|
122
|
+
unsubscribe(pubSubTopic) {
|
123
|
+
super.unsubscribe(pubSubTopic);
|
124
|
+
this.topicValidators.delete(pubSubTopic);
|
125
|
+
}
|
105
126
|
getMeshPeers(topic) {
|
106
127
|
return super.getMeshPeers(topic ?? this.pubSubTopic);
|
107
128
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/relay/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,GAIV,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/relay/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,GAIV,MAAM,6BAA6B,CAAC;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAWxD,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,MAAM,GAAG,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;AAchC;;;;;GAKG;AACH,MAAM,KAAM,SAAQ,SAAS;IAW3B,YACE,UAA+B,EAC/B,OAAqC;QAErC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,EAAE;YACrC,qEAAqE;YACrE,qBAAqB,EAAE,eAAe,CAAC,YAAY;YACnD,kBAAkB,EAAE,KAAK;SAC1B,CAAC,CAAC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AAC"}
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import type { PeerId } from "@libp2p/interface-peer-id";
|
2
|
+
import type { Message } from "@libp2p/interface-pubsub";
|
3
|
+
import { TopicValidatorResult } from "@libp2p/interface-pubsub";
|
4
|
+
export declare function messageValidator(peer: PeerId, message: Message): TopicValidatorResult;
|