@waku/core 0.0.1
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 +614 -0
- package/README.md +56 -0
- package/bundle/browser-1e1a2f27.js +722 -0
- package/bundle/crypto-8551d579.js +2585 -0
- package/bundle/crypto-b00764b7.js +1772 -0
- package/bundle/enr-564d4a51.js +20785 -0
- package/bundle/enr-9fc5eed8.js +20786 -0
- package/bundle/enr-f6e82a53.js +20785 -0
- package/bundle/events-158407bb.js +1929 -0
- package/bundle/events-fcbda4dc.js +76 -0
- package/bundle/index-02d21809.js +20 -0
- package/bundle/index-0a4bdddc.js +2976 -0
- package/bundle/index-2ae915be.js +1854 -0
- package/bundle/index-64ce43f0.js +69 -0
- package/bundle/index-691c0be6.js +4059 -0
- package/bundle/index-a013a259.js +20 -0
- package/bundle/index-ba42b4fc.js +862 -0
- package/bundle/index.js +13428 -0
- package/bundle/lib/enr.js +8 -0
- package/bundle/lib/peer_discovery_dns.js +5018 -0
- package/bundle/lib/peer_discovery_static_list.js +75 -0
- package/bundle/lib/predefined_bootstrap_nodes.js +59 -0
- package/bundle/lib/utils.js +1 -0
- package/bundle/lib/wait_for_remote_peer.js +327 -0
- package/bundle/lib/waku_message/topic_only_message.js +4 -0
- package/bundle/lib/waku_message/version_0.js +4 -0
- package/bundle/lib/waku_message/version_1.js +463 -0
- package/bundle/message-e2db79d7.js +8393 -0
- package/bundle/multiaddr_to_peer_info-c406b1e1.js +19 -0
- package/bundle/multiaddr_to_peer_info-fd1de516.js +19 -0
- package/bundle/random_subset-75d1c511.js +26 -0
- package/bundle/topic_only_message-34f36fa6.js +82 -0
- package/bundle/utils-9a3221f2.js +815 -0
- package/bundle/version_0-e6fe440c.js +317 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/constants.d.ts +4 -0
- package/dist/lib/constants.js +5 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/crypto.d.ts +34 -0
- package/dist/lib/crypto.js +79 -0
- package/dist/lib/crypto.js.map +1 -0
- package/dist/lib/enr/constants.d.ts +4 -0
- package/dist/lib/enr/constants.js +8 -0
- package/dist/lib/enr/constants.js.map +1 -0
- package/dist/lib/enr/enr.d.ts +90 -0
- package/dist/lib/enr/enr.js +432 -0
- package/dist/lib/enr/enr.js.map +1 -0
- package/dist/lib/enr/index.d.ts +5 -0
- package/dist/lib/enr/index.js +6 -0
- package/dist/lib/enr/index.js.map +1 -0
- package/dist/lib/enr/keypair/index.d.ts +8 -0
- package/dist/lib/enr/keypair/index.js +53 -0
- package/dist/lib/enr/keypair/index.js.map +1 -0
- package/dist/lib/enr/keypair/secp256k1.d.ts +13 -0
- package/dist/lib/enr/keypair/secp256k1.js +57 -0
- package/dist/lib/enr/keypair/secp256k1.js.map +1 -0
- package/dist/lib/enr/keypair/types.d.ts +13 -0
- package/dist/lib/enr/keypair/types.js +7 -0
- package/dist/lib/enr/keypair/types.js.map +1 -0
- package/dist/lib/enr/multiaddr_from_fields.d.ts +2 -0
- package/dist/lib/enr/multiaddr_from_fields.js +8 -0
- package/dist/lib/enr/multiaddr_from_fields.js.map +1 -0
- package/dist/lib/enr/multiaddrs_codec.d.ts +3 -0
- package/dist/lib/enr/multiaddrs_codec.js +32 -0
- package/dist/lib/enr/multiaddrs_codec.js.map +1 -0
- package/dist/lib/enr/types.d.ts +8 -0
- package/dist/lib/enr/types.js +3 -0
- package/dist/lib/enr/types.js.map +1 -0
- package/dist/lib/enr/v4.d.ts +3 -0
- package/dist/lib/enr/v4.js +14 -0
- package/dist/lib/enr/v4.js.map +1 -0
- package/dist/lib/enr/waku2_codec.d.ts +8 -0
- package/dist/lib/enr/waku2_codec.js +36 -0
- package/dist/lib/enr/waku2_codec.js.map +1 -0
- package/dist/lib/group_by.d.ts +3 -0
- package/dist/lib/group_by.js +13 -0
- package/dist/lib/group_by.js.map +1 -0
- package/dist/lib/multiaddr_to_peer_info.d.ts +3 -0
- package/dist/lib/multiaddr_to_peer_info.js +15 -0
- package/dist/lib/multiaddr_to_peer_info.js.map +1 -0
- package/dist/lib/peer_discovery_dns/dns.d.ts +48 -0
- package/dist/lib/peer_discovery_dns/dns.js +158 -0
- package/dist/lib/peer_discovery_dns/dns.js.map +1 -0
- package/dist/lib/peer_discovery_dns/dns_over_https.d.ts +32 -0
- package/dist/lib/peer_discovery_dns/dns_over_https.js +87 -0
- package/dist/lib/peer_discovery_dns/dns_over_https.js.map +1 -0
- package/dist/lib/peer_discovery_dns/enrtree.d.ts +33 -0
- package/dist/lib/peer_discovery_dns/enrtree.js +76 -0
- package/dist/lib/peer_discovery_dns/enrtree.js.map +1 -0
- package/dist/lib/peer_discovery_dns/fetch_nodes.d.ts +14 -0
- package/dist/lib/peer_discovery_dns/fetch_nodes.js +133 -0
- package/dist/lib/peer_discovery_dns/fetch_nodes.js.map +1 -0
- package/dist/lib/peer_discovery_dns/index.d.ts +30 -0
- package/dist/lib/peer_discovery_dns/index.js +54 -0
- package/dist/lib/peer_discovery_dns/index.js.map +1 -0
- package/dist/lib/peer_discovery_static_list.d.ts +44 -0
- package/dist/lib/peer_discovery_static_list.js +72 -0
- package/dist/lib/peer_discovery_static_list.js.map +1 -0
- package/dist/lib/predefined_bootstrap_nodes.d.ts +35 -0
- package/dist/lib/predefined_bootstrap_nodes.js +56 -0
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -0
- package/dist/lib/push_or_init_map.d.ts +1 -0
- package/dist/lib/push_or_init_map.js +9 -0
- package/dist/lib/push_or_init_map.js.map +1 -0
- package/dist/lib/random_subset.d.ts +4 -0
- package/dist/lib/random_subset.js +25 -0
- package/dist/lib/random_subset.js.map +1 -0
- package/dist/lib/select_connection.d.ts +2 -0
- package/dist/lib/select_connection.js +19 -0
- package/dist/lib/select_connection.js.map +1 -0
- package/dist/lib/select_peer.d.ts +15 -0
- package/dist/lib/select_peer.js +59 -0
- package/dist/lib/select_peer.js.map +1 -0
- package/dist/lib/to_proto_message.d.ts +3 -0
- package/dist/lib/to_proto_message.js +11 -0
- package/dist/lib/to_proto_message.js.map +1 -0
- package/dist/lib/utils.d.ts +22 -0
- package/dist/lib/utils.js +40 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/lib/wait_for_remote_peer.d.ts +22 -0
- package/dist/lib/wait_for_remote_peer.js +113 -0
- package/dist/lib/wait_for_remote_peer.js.map +1 -0
- package/dist/lib/waku.d.ts +61 -0
- package/dist/lib/waku.js +174 -0
- package/dist/lib/waku.js.map +1 -0
- package/dist/lib/waku_filter/filter_rpc.d.ts +25 -0
- package/dist/lib/waku_filter/filter_rpc.js +44 -0
- package/dist/lib/waku_filter/filter_rpc.js.map +1 -0
- package/dist/lib/waku_filter/index.d.ts +50 -0
- package/dist/lib/waku_filter/index.js +181 -0
- package/dist/lib/waku_filter/index.js.map +1 -0
- package/dist/lib/waku_light_push/index.d.ts +38 -0
- package/dist/lib/waku_light_push/index.js +83 -0
- package/dist/lib/waku_light_push/index.js.map +1 -0
- package/dist/lib/waku_light_push/push_rpc.d.ts +11 -0
- package/dist/lib/waku_light_push/push_rpc.js +31 -0
- package/dist/lib/waku_light_push/push_rpc.js.map +1 -0
- package/dist/lib/waku_message/constants.d.ts +12 -0
- package/dist/lib/waku_message/constants.js +10 -0
- package/dist/lib/waku_message/constants.js.map +1 -0
- package/dist/lib/waku_message/ecies.d.ts +17 -0
- package/dist/lib/waku_message/ecies.js +126 -0
- package/dist/lib/waku_message/ecies.js.map +1 -0
- package/dist/lib/waku_message/symmetric.d.ts +3 -0
- package/dist/lib/waku_message/symmetric.js +18 -0
- package/dist/lib/waku_message/symmetric.js.map +1 -0
- package/dist/lib/waku_message/topic_only_message.d.ts +15 -0
- package/dist/lib/waku_message/topic_only_message.js +31 -0
- package/dist/lib/waku_message/topic_only_message.js.map +1 -0
- package/dist/lib/waku_message/version_0.d.ts +26 -0
- package/dist/lib/waku_message/version_0.js +96 -0
- package/dist/lib/waku_message/version_0.js.map +1 -0
- package/dist/lib/waku_message/version_1.d.ts +93 -0
- package/dist/lib/waku_message/version_1.js +325 -0
- package/dist/lib/waku_message/version_1.js.map +1 -0
- package/dist/lib/waku_relay/constants.d.ts +63 -0
- package/dist/lib/waku_relay/constants.js +67 -0
- package/dist/lib/waku_relay/constants.js.map +1 -0
- package/dist/lib/waku_relay/index.d.ts +65 -0
- package/dist/lib/waku_relay/index.js +111 -0
- package/dist/lib/waku_relay/index.js.map +1 -0
- package/dist/lib/waku_store/history_rpc.d.ts +27 -0
- package/dist/lib/waku_store/history_rpc.js +71 -0
- package/dist/lib/waku_store/history_rpc.js.map +1 -0
- package/dist/lib/waku_store/index.d.ts +126 -0
- package/dist/lib/waku_store/index.js +218 -0
- package/dist/lib/waku_store/index.js.map +1 -0
- package/dist/proto/filter.d.ts +65 -0
- package/dist/proto/filter.js +425 -0
- package/dist/proto/filter.js.map +1 -0
- package/dist/proto/light_push.d.ts +57 -0
- package/dist/proto/light_push.js +369 -0
- package/dist/proto/light_push.js.map +1 -0
- package/dist/proto/message.d.ts +29 -0
- package/dist/proto/message.js +215 -0
- package/dist/proto/message.js.map +1 -0
- package/dist/proto/store.d.ts +104 -0
- package/dist/proto/store.js +602 -0
- package/dist/proto/store.js.map +1 -0
- package/dist/proto/topic_only_message.d.ts +10 -0
- package/dist/proto/topic_only_message.js +46 -0
- package/dist/proto/topic_only_message.js.map +1 -0
- package/package.json +292 -0
- package/src/index.ts +33 -0
- package/src/lib/constants.ts +4 -0
- package/src/lib/crypto.ts +100 -0
- package/src/lib/enr/constants.ts +10 -0
- package/src/lib/enr/enr.ts +516 -0
- package/src/lib/enr/index.ts +5 -0
- package/src/lib/enr/keypair/index.ts +76 -0
- package/src/lib/enr/keypair/secp256k1.ts +69 -0
- package/src/lib/enr/keypair/types.ts +14 -0
- package/src/lib/enr/multiaddr_from_fields.ts +18 -0
- package/src/lib/enr/multiaddrs_codec.ts +50 -0
- package/src/lib/enr/types.ts +11 -0
- package/src/lib/enr/v4.ts +22 -0
- package/src/lib/enr/waku2_codec.ts +39 -0
- package/src/lib/group_by.ts +14 -0
- package/src/lib/multiaddr_to_peer_info.ts +17 -0
- package/src/lib/peer_discovery_dns/dns.ts +223 -0
- package/src/lib/peer_discovery_dns/dns_over_https.ts +98 -0
- package/src/lib/peer_discovery_dns/enrtree.ts +123 -0
- package/src/lib/peer_discovery_dns/fetch_nodes.ts +180 -0
- package/src/lib/peer_discovery_dns/index.ts +84 -0
- package/src/lib/peer_discovery_static_list.ts +118 -0
- package/src/lib/predefined_bootstrap_nodes.ts +72 -0
- package/src/lib/push_or_init_map.ts +13 -0
- package/src/lib/random_subset.ts +30 -0
- package/src/lib/select_connection.ts +24 -0
- package/src/lib/select_peer.ts +77 -0
- package/src/lib/to_proto_message.ts +15 -0
- package/src/lib/utils.ts +50 -0
- package/src/lib/wait_for_remote_peer.ts +151 -0
- package/src/lib/waku.ts +258 -0
- package/src/lib/waku_filter/filter_rpc.ts +57 -0
- package/src/lib/waku_filter/index.ts +291 -0
- package/src/lib/waku_light_push/index.ts +137 -0
- package/src/lib/waku_light_push/push_rpc.ts +39 -0
- package/src/lib/waku_message/constants.ts +10 -0
- package/src/lib/waku_message/ecies.ts +194 -0
- package/src/lib/waku_message/symmetric.ts +33 -0
- package/src/lib/waku_message/topic_only_message.ts +40 -0
- package/src/lib/waku_message/version_0.ts +121 -0
- package/src/lib/waku_message/version_1.ts +457 -0
- package/src/lib/waku_relay/constants.ts +77 -0
- package/src/lib/waku_relay/index.ts +189 -0
- package/src/lib/waku_store/history_rpc.ts +94 -0
- package/src/lib/waku_store/index.ts +372 -0
- package/src/proto/filter.ts +602 -0
- package/src/proto/light_push.ts +526 -0
- package/src/proto/message.ts +304 -0
- package/src/proto/store.ts +844 -0
- package/src/proto/topic_only_message.ts +67 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
/**
|
2
|
+
* Convert input to a byte array.
|
3
|
+
*
|
4
|
+
* Handles both `0x` prefixed and non-prefixed strings.
|
5
|
+
*/
|
6
|
+
export declare function hexToBytes(hex: string | Uint8Array): Uint8Array;
|
7
|
+
/**
|
8
|
+
* Convert byte array to hex string (no `0x` prefix).
|
9
|
+
*/
|
10
|
+
export declare const bytesToHex: (bytes: Uint8Array) => string;
|
11
|
+
/**
|
12
|
+
* Decode byte array to utf-8 string.
|
13
|
+
*/
|
14
|
+
export declare const bytesToUtf8: (b: Uint8Array) => string;
|
15
|
+
/**
|
16
|
+
* Encode utf-8 string to byte array.
|
17
|
+
*/
|
18
|
+
export declare const utf8ToBytes: (s: string) => Uint8Array;
|
19
|
+
/**
|
20
|
+
* Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
|
21
|
+
*/
|
22
|
+
export declare function concat(byteArrays: Uint8Array[], totalLength?: number): Uint8Array;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { fromString } from "uint8arrays/from-string";
|
2
|
+
import { toString } from "uint8arrays/to-string";
|
3
|
+
/**
|
4
|
+
* Convert input to a byte array.
|
5
|
+
*
|
6
|
+
* Handles both `0x` prefixed and non-prefixed strings.
|
7
|
+
*/
|
8
|
+
export function hexToBytes(hex) {
|
9
|
+
if (typeof hex === "string") {
|
10
|
+
const _hex = hex.replace(/^0x/i, "");
|
11
|
+
return fromString(_hex.toLowerCase(), "base16");
|
12
|
+
}
|
13
|
+
return hex;
|
14
|
+
}
|
15
|
+
/**
|
16
|
+
* Convert byte array to hex string (no `0x` prefix).
|
17
|
+
*/
|
18
|
+
export const bytesToHex = (bytes) => toString(bytes, "base16");
|
19
|
+
/**
|
20
|
+
* Decode byte array to utf-8 string.
|
21
|
+
*/
|
22
|
+
export const bytesToUtf8 = (b) => toString(b, "utf8");
|
23
|
+
/**
|
24
|
+
* Encode utf-8 string to byte array.
|
25
|
+
*/
|
26
|
+
export const utf8ToBytes = (s) => fromString(s, "utf8");
|
27
|
+
/**
|
28
|
+
* Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
|
29
|
+
*/
|
30
|
+
export function concat(byteArrays, totalLength) {
|
31
|
+
const len = totalLength ?? byteArrays.reduce((acc, curr) => acc + curr.length, 0);
|
32
|
+
const res = new Uint8Array(len);
|
33
|
+
let offset = 0;
|
34
|
+
for (const bytes of byteArrays) {
|
35
|
+
res.set(bytes, offset);
|
36
|
+
offset += bytes.length;
|
37
|
+
}
|
38
|
+
return res;
|
39
|
+
}
|
40
|
+
//# sourceMappingURL=utils.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/lib/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAwB;IACjD,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;QAC3B,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QACrC,OAAO,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,QAAQ,CAAC,CAAC;KACjD;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAiB,EAAU,EAAE,CACtD,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAE5B;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAa,EAAU,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AAE1E;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAS,EAAc,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;AAE5E;;GAEG;AACH,MAAM,UAAU,MAAM,CACpB,UAAwB,EACxB,WAAoB;IAEpB,MAAM,GAAG,GACP,WAAW,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IACxE,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;IAEhC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE;QAC9B,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;KACxB;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import type { Waku } from "@waku/interfaces";
|
2
|
+
import { Protocols } from "@waku/interfaces";
|
3
|
+
/**
|
4
|
+
* Wait for a remote peer to be ready given the passed protocols.
|
5
|
+
* Must be used after attempting to connect to nodes, using
|
6
|
+
* {@link index.waku.WakuNode.dial} or a bootstrap method with
|
7
|
+
* {@link lib/create_waku.createLightNode}.
|
8
|
+
*
|
9
|
+
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
10
|
+
* a peer is in a mesh, to help ensure that other peers will send and receive
|
11
|
+
* message to us.
|
12
|
+
*
|
13
|
+
* @param waku The Waku Node
|
14
|
+
* @param protocols The protocols that need to be enabled by remote peers.
|
15
|
+
* @param timeoutMs A timeout value in milliseconds..
|
16
|
+
*
|
17
|
+
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
18
|
+
* remote nodes, **rejects** if the timeoutMs is reached.
|
19
|
+
* @throws If passing a protocol that is not mounted
|
20
|
+
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
21
|
+
*/
|
22
|
+
export declare function waitForRemotePeer(waku: Waku, protocols?: Protocols[], timeoutMs?: number): Promise<void>;
|
@@ -0,0 +1,113 @@
|
|
1
|
+
import { Protocols } from "@waku/interfaces";
|
2
|
+
import debug from "debug";
|
3
|
+
import { pEvent } from "p-event";
|
4
|
+
import { FilterCodec } from "./waku_filter/index.js";
|
5
|
+
import { LightPushCodec } from "./waku_light_push/index.js";
|
6
|
+
import { StoreCodec } from "./waku_store/index.js";
|
7
|
+
const log = debug("waku:wait-for-remote-peer");
|
8
|
+
/**
|
9
|
+
* Wait for a remote peer to be ready given the passed protocols.
|
10
|
+
* Must be used after attempting to connect to nodes, using
|
11
|
+
* {@link index.waku.WakuNode.dial} or a bootstrap method with
|
12
|
+
* {@link lib/create_waku.createLightNode}.
|
13
|
+
*
|
14
|
+
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
15
|
+
* a peer is in a mesh, to help ensure that other peers will send and receive
|
16
|
+
* message to us.
|
17
|
+
*
|
18
|
+
* @param waku The Waku Node
|
19
|
+
* @param protocols The protocols that need to be enabled by remote peers.
|
20
|
+
* @param timeoutMs A timeout value in milliseconds..
|
21
|
+
*
|
22
|
+
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
23
|
+
* remote nodes, **rejects** if the timeoutMs is reached.
|
24
|
+
* @throws If passing a protocol that is not mounted
|
25
|
+
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
26
|
+
*/
|
27
|
+
export async function waitForRemotePeer(waku, protocols, timeoutMs) {
|
28
|
+
protocols = protocols ?? getEnabledProtocols(waku);
|
29
|
+
if (!waku.isStarted())
|
30
|
+
return Promise.reject("Waku node is not started");
|
31
|
+
const promises = [];
|
32
|
+
if (protocols.includes(Protocols.Relay)) {
|
33
|
+
if (!waku.relay)
|
34
|
+
throw new Error("Cannot wait for Relay peer: protocol not mounted");
|
35
|
+
promises.push(waitForGossipSubPeerInMesh(waku.relay));
|
36
|
+
}
|
37
|
+
if (protocols.includes(Protocols.Store)) {
|
38
|
+
if (!waku.store)
|
39
|
+
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
40
|
+
promises.push(waitForConnectedPeer(waku.store, [StoreCodec]));
|
41
|
+
}
|
42
|
+
if (protocols.includes(Protocols.LightPush)) {
|
43
|
+
if (!waku.lightPush)
|
44
|
+
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
45
|
+
promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec]));
|
46
|
+
}
|
47
|
+
if (protocols.includes(Protocols.Filter)) {
|
48
|
+
if (!waku.filter)
|
49
|
+
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
50
|
+
promises.push(waitForConnectedPeer(waku.filter, [FilterCodec]));
|
51
|
+
}
|
52
|
+
if (timeoutMs) {
|
53
|
+
await rejectOnTimeout(Promise.all(promises), timeoutMs, "Timed out waiting for a remote peer.");
|
54
|
+
}
|
55
|
+
else {
|
56
|
+
await Promise.all(promises);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Wait for a peer with the given protocol to be connected.
|
61
|
+
*/
|
62
|
+
async function waitForConnectedPeer(waku, codecs) {
|
63
|
+
const peers = await waku.peers();
|
64
|
+
if (peers.length) {
|
65
|
+
log(`${codecs} peer found: `, peers[0].id.toString());
|
66
|
+
return;
|
67
|
+
}
|
68
|
+
await new Promise((resolve) => {
|
69
|
+
const cb = (evt) => {
|
70
|
+
for (const codec of codecs) {
|
71
|
+
if (evt.detail.protocols.includes(codec)) {
|
72
|
+
log("Resolving for", codec, evt.detail.protocols);
|
73
|
+
waku.libp2p.peerStore.removeEventListener("change:protocols", cb);
|
74
|
+
resolve();
|
75
|
+
break;
|
76
|
+
}
|
77
|
+
}
|
78
|
+
};
|
79
|
+
waku.libp2p.peerStore.addEventListener("change:protocols", cb);
|
80
|
+
});
|
81
|
+
}
|
82
|
+
/**
|
83
|
+
* Wait for a peer with the given protocol to be connected and in the gossipsub
|
84
|
+
* mesh.
|
85
|
+
*/
|
86
|
+
async function waitForGossipSubPeerInMesh(waku) {
|
87
|
+
let peers = waku.getMeshPeers();
|
88
|
+
while (peers.length == 0) {
|
89
|
+
await pEvent(waku, "gossipsub:heartbeat");
|
90
|
+
peers = waku.getMeshPeers();
|
91
|
+
}
|
92
|
+
}
|
93
|
+
const awaitTimeout = (ms, rejectReason) => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
94
|
+
async function rejectOnTimeout(promise, timeoutMs, rejectReason) {
|
95
|
+
await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
|
96
|
+
}
|
97
|
+
function getEnabledProtocols(waku) {
|
98
|
+
const protocols = [];
|
99
|
+
if (waku.relay) {
|
100
|
+
protocols.push(Protocols.Relay);
|
101
|
+
}
|
102
|
+
if (waku.filter) {
|
103
|
+
protocols.push(Protocols.Filter);
|
104
|
+
}
|
105
|
+
if (waku.store) {
|
106
|
+
protocols.push(Protocols.Store);
|
107
|
+
}
|
108
|
+
if (waku.lightPush) {
|
109
|
+
protocols.push(Protocols.LightPush);
|
110
|
+
}
|
111
|
+
return protocols;
|
112
|
+
}
|
113
|
+
//# sourceMappingURL=wait_for_remote_peer.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"wait_for_remote_peer.js","sourceRoot":"","sources":["../../src/lib/wait_for_remote_peer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,MAAM,GAAG,GAAG,KAAK,CAAC,2BAA2B,CAAC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,IAAU,EACV,SAAuB,EACvB,SAAkB;IAElB,SAAS,GAAG,SAAS,IAAI,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAEnD,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;QAAE,OAAO,OAAO,CAAC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H,CAAC;AAED,MAAM,YAAY,GAAG,CAAC,EAAU,EAAE,YAAoB,EAAiB,EAAE,CACvE,IAAI,OAAO,CAAC,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAEhF,KAAK,UAAU,eAAe,CAC5B,OAAmB,EACnB,SAAiB,EACjB,YAAoB;IAEpB,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,YAAY,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,mBAAmB,CAAC,IAAU;IACrC,MAAM,SAAS,GAAG,EAAE,CAAC;IAErB,IAAI,IAAI,CAAC,KAAK,EAAE;QACd,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;KACjC;IAED,IAAI,IAAI,CAAC,MAAM,EAAE;QACf,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;KAClC;IAED,IAAI,IAAI,CAAC,KAAK,EAAE;QACd,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;KACjC;IAED,IAAI,IAAI,CAAC,SAAS,EAAE;QAClB,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;KACrC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import type { Stream } from "@libp2p/interface-connection";
|
2
|
+
import type { PeerId } from "@libp2p/interface-peer-id";
|
3
|
+
import type { Multiaddr } from "@multiformats/multiaddr";
|
4
|
+
import type { Waku } from "@waku/interfaces";
|
5
|
+
import { Protocols } from "@waku/interfaces";
|
6
|
+
import type { Libp2p } from "libp2p";
|
7
|
+
import { WakuFilter } from "./waku_filter";
|
8
|
+
import { WakuLightPush } from "./waku_light_push";
|
9
|
+
import { WakuRelay } from "./waku_relay";
|
10
|
+
import { WakuStore } from "./waku_store";
|
11
|
+
export declare const DefaultPingKeepAliveValueSecs = 0;
|
12
|
+
export declare const DefaultRelayKeepAliveValueSecs: number;
|
13
|
+
export interface WakuOptions {
|
14
|
+
/**
|
15
|
+
* Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
|
16
|
+
* request to each peer after the set number of seconds. Set to 0 to disable.
|
17
|
+
*
|
18
|
+
* @default {@link DefaultPingKeepAliveValueSecs}
|
19
|
+
*/
|
20
|
+
pingKeepAlive?: number;
|
21
|
+
/**
|
22
|
+
* Set keep alive frequency in seconds: Waku will send a ping message over
|
23
|
+
* relay to each peer after the set number of seconds. Set to 0 to disable.
|
24
|
+
*
|
25
|
+
* @default {@link DefaultRelayKeepAliveValueSecs}
|
26
|
+
*/
|
27
|
+
relayKeepAlive?: number;
|
28
|
+
}
|
29
|
+
export declare class WakuNode implements Waku {
|
30
|
+
libp2p: Libp2p;
|
31
|
+
relay?: WakuRelay;
|
32
|
+
store?: WakuStore;
|
33
|
+
filter?: WakuFilter;
|
34
|
+
lightPush?: WakuLightPush;
|
35
|
+
private pingKeepAliveTimers;
|
36
|
+
private relayKeepAliveTimers;
|
37
|
+
constructor(options: WakuOptions, libp2p: Libp2p, store?: WakuStore, lightPush?: WakuLightPush, filter?: WakuFilter);
|
38
|
+
/**
|
39
|
+
* Dials to the provided peer.
|
40
|
+
*
|
41
|
+
* @param peer The peer to dial
|
42
|
+
* @param protocols Waku protocols we expect from the peer; Default to Relay
|
43
|
+
*/
|
44
|
+
dial(peer: PeerId | Multiaddr, protocols?: Protocols[]): Promise<Stream>;
|
45
|
+
/**
|
46
|
+
* Add peer to address book, it will be auto-dialed in the background.
|
47
|
+
*/
|
48
|
+
addPeerToAddressBook(peerId: PeerId | string, multiaddrs: Multiaddr[] | string[]): Promise<void>;
|
49
|
+
start(): Promise<void>;
|
50
|
+
stop(): Promise<void>;
|
51
|
+
isStarted(): boolean;
|
52
|
+
/**
|
53
|
+
* Return the local multiaddr with peer id on which libp2p is listening.
|
54
|
+
*
|
55
|
+
* @throws if libp2p is not listening on localhost.
|
56
|
+
*/
|
57
|
+
getLocalMultiaddrWithID(): string;
|
58
|
+
private startKeepAlive;
|
59
|
+
private stopKeepAlive;
|
60
|
+
private stopAllKeepAlives;
|
61
|
+
}
|
package/dist/lib/waku.js
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
import { peerIdFromString } from "@libp2p/peer-id";
|
2
|
+
import { multiaddr } from "@multiformats/multiaddr";
|
3
|
+
import { Protocols } from "@waku/interfaces";
|
4
|
+
import debug from "debug";
|
5
|
+
import { FilterCodec } from "./waku_filter/index.js";
|
6
|
+
import { LightPushCodec } from "./waku_light_push/index.js";
|
7
|
+
import { EncoderV0 } from "./waku_message/version_0.js";
|
8
|
+
import { RelayCodecs, RelayPingContentTopic } from "./waku_relay/constants.js";
|
9
|
+
import * as relayConstants from "./waku_relay/constants.js";
|
10
|
+
import { StoreCodec } from "./waku_store/index.js";
|
11
|
+
export const DefaultPingKeepAliveValueSecs = 0;
|
12
|
+
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
13
|
+
const log = debug("waku:waku");
|
14
|
+
export class WakuNode {
|
15
|
+
constructor(options, libp2p, store, lightPush, filter) {
|
16
|
+
this.libp2p = libp2p;
|
17
|
+
this.store = store;
|
18
|
+
this.filter = filter;
|
19
|
+
this.lightPush = lightPush;
|
20
|
+
if (isWakuRelay(libp2p.pubsub)) {
|
21
|
+
this.relay = libp2p.pubsub;
|
22
|
+
}
|
23
|
+
log("Waku node created", this.libp2p.peerId.toString(), `relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
|
24
|
+
.lightPush}, filter: ${!!this.filter}`);
|
25
|
+
this.pingKeepAliveTimers = {};
|
26
|
+
this.relayKeepAliveTimers = {};
|
27
|
+
const pingKeepAlive = options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
28
|
+
const relayKeepAlive = this.relay
|
29
|
+
? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
|
30
|
+
: 0;
|
31
|
+
libp2p.connectionManager.addEventListener("peer:connect", (evt) => {
|
32
|
+
this.startKeepAlive(evt.detail.remotePeer, pingKeepAlive, relayKeepAlive);
|
33
|
+
});
|
34
|
+
/**
|
35
|
+
* NOTE: Event is not being emitted on closing nor losing a connection.
|
36
|
+
* @see https://github.com/libp2p/js-libp2p/issues/939
|
37
|
+
* @see https://github.com/status-im/js-waku/issues/252
|
38
|
+
*
|
39
|
+
* >This event will be triggered anytime we are disconnected from another peer,
|
40
|
+
* >regardless of the circumstances of that disconnection.
|
41
|
+
* >If we happen to have multiple connections to a peer,
|
42
|
+
* >this event will **only** be triggered when the last connection is closed.
|
43
|
+
* @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100
|
44
|
+
*/
|
45
|
+
libp2p.connectionManager.addEventListener("peer:disconnect", (evt) => {
|
46
|
+
this.stopKeepAlive(evt.detail.remotePeer);
|
47
|
+
});
|
48
|
+
}
|
49
|
+
/**
|
50
|
+
* Dials to the provided peer.
|
51
|
+
*
|
52
|
+
* @param peer The peer to dial
|
53
|
+
* @param protocols Waku protocols we expect from the peer; Default to Relay
|
54
|
+
*/
|
55
|
+
async dial(peer, protocols) {
|
56
|
+
const _protocols = protocols ?? [Protocols.Relay];
|
57
|
+
const codecs = [];
|
58
|
+
if (_protocols.includes(Protocols.Relay)) {
|
59
|
+
RelayCodecs.forEach((codec) => codecs.push(codec));
|
60
|
+
}
|
61
|
+
if (_protocols.includes(Protocols.Store)) {
|
62
|
+
codecs.push(StoreCodec);
|
63
|
+
}
|
64
|
+
if (_protocols.includes(Protocols.LightPush)) {
|
65
|
+
codecs.push(LightPushCodec);
|
66
|
+
}
|
67
|
+
if (_protocols.includes(Protocols.Filter)) {
|
68
|
+
codecs.push(FilterCodec);
|
69
|
+
}
|
70
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
71
|
+
// @ts-ignore: new Multiaddr is not backward compatible
|
72
|
+
return this.libp2p.dialProtocol(peer, codecs);
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* Add peer to address book, it will be auto-dialed in the background.
|
76
|
+
*/
|
77
|
+
async addPeerToAddressBook(peerId, multiaddrs) {
|
78
|
+
let peer;
|
79
|
+
if (typeof peerId === "string") {
|
80
|
+
peer = peerIdFromString(peerId);
|
81
|
+
}
|
82
|
+
else {
|
83
|
+
peer = peerId;
|
84
|
+
}
|
85
|
+
const addresses = multiaddrs.map((addr) => {
|
86
|
+
if (typeof addr === "string") {
|
87
|
+
return multiaddr(addr);
|
88
|
+
}
|
89
|
+
else {
|
90
|
+
return addr;
|
91
|
+
}
|
92
|
+
});
|
93
|
+
await this.libp2p.peerStore.addressBook.set(peer, addresses);
|
94
|
+
}
|
95
|
+
async start() {
|
96
|
+
await this.libp2p.start();
|
97
|
+
}
|
98
|
+
async stop() {
|
99
|
+
this.stopAllKeepAlives();
|
100
|
+
await this.libp2p.stop();
|
101
|
+
}
|
102
|
+
isStarted() {
|
103
|
+
return this.libp2p.isStarted();
|
104
|
+
}
|
105
|
+
/**
|
106
|
+
* Return the local multiaddr with peer id on which libp2p is listening.
|
107
|
+
*
|
108
|
+
* @throws if libp2p is not listening on localhost.
|
109
|
+
*/
|
110
|
+
getLocalMultiaddrWithID() {
|
111
|
+
const localMultiaddr = this.libp2p
|
112
|
+
.getMultiaddrs()
|
113
|
+
.find((addr) => addr.toString().match(/127\.0\.0\.1/));
|
114
|
+
if (!localMultiaddr || localMultiaddr.toString() === "") {
|
115
|
+
throw "Not listening on localhost";
|
116
|
+
}
|
117
|
+
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
|
118
|
+
}
|
119
|
+
startKeepAlive(peerId, pingPeriodSecs, relayPeriodSecs) {
|
120
|
+
// Just in case a timer already exist for this peer
|
121
|
+
this.stopKeepAlive(peerId);
|
122
|
+
const peerIdStr = peerId.toString();
|
123
|
+
if (pingPeriodSecs !== 0) {
|
124
|
+
this.pingKeepAliveTimers[peerIdStr] = setInterval(() => {
|
125
|
+
this.libp2p.ping(peerId).catch((e) => {
|
126
|
+
log(`Ping failed (${peerIdStr})`, e);
|
127
|
+
});
|
128
|
+
}, pingPeriodSecs * 1000);
|
129
|
+
}
|
130
|
+
const relay = this.relay;
|
131
|
+
if (relay && relayPeriodSecs !== 0) {
|
132
|
+
const encoder = new EncoderV0(RelayPingContentTopic);
|
133
|
+
this.relayKeepAliveTimers[peerIdStr] = setInterval(() => {
|
134
|
+
log("Sending Waku Relay ping message");
|
135
|
+
relay
|
136
|
+
.send(encoder, { payload: new Uint8Array() })
|
137
|
+
.catch((e) => log("Failed to send relay ping", e));
|
138
|
+
}, relayPeriodSecs * 1000);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
stopKeepAlive(peerId) {
|
142
|
+
const peerIdStr = peerId.toString();
|
143
|
+
if (this.pingKeepAliveTimers[peerIdStr]) {
|
144
|
+
clearInterval(this.pingKeepAliveTimers[peerIdStr]);
|
145
|
+
delete this.pingKeepAliveTimers[peerIdStr];
|
146
|
+
}
|
147
|
+
if (this.relayKeepAliveTimers[peerIdStr]) {
|
148
|
+
clearInterval(this.relayKeepAliveTimers[peerIdStr]);
|
149
|
+
delete this.relayKeepAliveTimers[peerIdStr];
|
150
|
+
}
|
151
|
+
}
|
152
|
+
stopAllKeepAlives() {
|
153
|
+
for (const timer of [
|
154
|
+
...Object.values(this.pingKeepAliveTimers),
|
155
|
+
...Object.values(this.relayKeepAliveTimers),
|
156
|
+
]) {
|
157
|
+
clearInterval(timer);
|
158
|
+
}
|
159
|
+
this.pingKeepAliveTimers = {};
|
160
|
+
this.relayKeepAliveTimers = {};
|
161
|
+
}
|
162
|
+
}
|
163
|
+
function isWakuRelay(pubsub) {
|
164
|
+
if (pubsub) {
|
165
|
+
try {
|
166
|
+
return pubsub.multicodecs.includes(relayConstants.RelayCodecs[relayConstants.RelayCodecs.length - 1]);
|
167
|
+
// Exception is expected if `libp2p` was not instantiated with pubsub
|
168
|
+
// eslint-disable-next-line no-empty
|
169
|
+
}
|
170
|
+
catch (e) { }
|
171
|
+
}
|
172
|
+
return false;
|
173
|
+
}
|
174
|
+
//# sourceMappingURL=waku.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/waku.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,EAAE,WAAW,EAAc,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,cAAc,EAAiB,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAErD,OAAO,EAAE,WAAW,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,KAAK,cAAc,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,UAAU,EAAa,MAAM,cAAc,CAAC;AAErD,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAC;AAC/C,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,GAAG,EAAE,CAAC;AAErD,MAAM,GAAG,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC;AAmB/B,MAAM,OAAO,QAAQ;IAcnB,YACE,OAAoB,EACpB,MAAc,EACd,KAAiB,EACjB,SAAyB,EACzB,MAAmB;QAEnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAE3B,IAAI,WAAW,CAAC,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AG,EAAE,CAAC;QAC9B,IAAI,CAAC,oBAAoB,GAAG,EAAE,CAAC;IACjC,CAAC;CACF;AAED,SAAS,WAAW,CAAC,MAAc;IACjC,IAAI,MAAM,EAAE;QACV,IAAI;YACF,OAAO,MAAM,CAAC,WAAW,CAAC,QAAQ,CAChC,cAAc,CAAC,WAAW,CAAC,cAAc,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAClE,CAAC;YACF,qEAAqE;YACrE,oCAAoC;SACrC;QAAC,OAAO,CAAC,EAAE,GAAE;KACf;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import * as proto from "../../proto/filter";
|
2
|
+
export declare type ContentFilter = {
|
3
|
+
contentTopic: string;
|
4
|
+
};
|
5
|
+
/**
|
6
|
+
* FilterRPC represents a message conforming to the Waku Filter protocol
|
7
|
+
*/
|
8
|
+
export declare class FilterRPC {
|
9
|
+
proto: proto.FilterRPC;
|
10
|
+
constructor(proto: proto.FilterRPC);
|
11
|
+
static createRequest(topic: string, contentFilters: ContentFilter[], requestId?: string, subscribe?: boolean): FilterRPC;
|
12
|
+
/**
|
13
|
+
*
|
14
|
+
* @param bytes Uint8Array of bytes from a FilterRPC message
|
15
|
+
* @returns FilterRPC
|
16
|
+
*/
|
17
|
+
static decode(bytes: Uint8Array): FilterRPC;
|
18
|
+
/**
|
19
|
+
* Encode the current FilterRPC request to bytes
|
20
|
+
* @returns Uint8Array
|
21
|
+
*/
|
22
|
+
encode(): Uint8Array;
|
23
|
+
get push(): proto.MessagePush | undefined;
|
24
|
+
get requestId(): string | undefined;
|
25
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { v4 as uuid } from "uuid";
|
2
|
+
import * as proto from "../../proto/filter.js";
|
3
|
+
/**
|
4
|
+
* FilterRPC represents a message conforming to the Waku Filter protocol
|
5
|
+
*/
|
6
|
+
export class FilterRPC {
|
7
|
+
constructor(proto) {
|
8
|
+
this.proto = proto;
|
9
|
+
}
|
10
|
+
static createRequest(topic, contentFilters, requestId, subscribe = true) {
|
11
|
+
return new FilterRPC({
|
12
|
+
requestId: requestId || uuid(),
|
13
|
+
request: {
|
14
|
+
subscribe,
|
15
|
+
topic,
|
16
|
+
contentFilters,
|
17
|
+
},
|
18
|
+
push: undefined,
|
19
|
+
});
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
*
|
23
|
+
* @param bytes Uint8Array of bytes from a FilterRPC message
|
24
|
+
* @returns FilterRPC
|
25
|
+
*/
|
26
|
+
static decode(bytes) {
|
27
|
+
const res = proto.FilterRPC.decode(bytes);
|
28
|
+
return new FilterRPC(res);
|
29
|
+
}
|
30
|
+
/**
|
31
|
+
* Encode the current FilterRPC request to bytes
|
32
|
+
* @returns Uint8Array
|
33
|
+
*/
|
34
|
+
encode() {
|
35
|
+
return proto.FilterRPC.encode(this.proto);
|
36
|
+
}
|
37
|
+
get push() {
|
38
|
+
return this.proto.push;
|
39
|
+
}
|
40
|
+
get requestId() {
|
41
|
+
return this.proto.requestId;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
//# sourceMappingURL=filter_rpc.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"filter_rpc.js","sourceRoot":"","sources":["../../../src/lib/waku_filter/filter_rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,KAAK,KAAK,MAAM,oBAAoB,CAAC;AAM5C;;GAEG;AACH,MAAM,OAAO,SAAS;IACpB,YAA0B,KAAsB;QAAtB,UAAK,GAAL,KAAK,CAAiB;IAAG,CAAC;IAEpD,MAAM,CAAC,aAAa,CAClB,KAAa,EACb,cAA+B,EAC/B,SAAkB,EAClB,SAAS,GAAG,IAAI;QAEhB,OAAO,IAAI,SAAS,CAAC;YACnB,SAAS,EAAE,SAAS,IAAI,IAAI,EAAE;YAC9B,OAAO,EAAE;gBACP,SAAS;gBACT,KAAK;gBACL,cAAc;aACf;YACD,IAAI,EAAE,SAAS;SAChB,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,KAAiB;QAC7B,MAAM,GAAG,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC1C,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IAED;;;OAGG;IACH,MAAM;QACJ,OAAO,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;IACzB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;IAC9B,CAAC;CACF"}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import type { Peer } from "@libp2p/interface-peer-store";
|
2
|
+
import type { Callback, Decoder, Filter, Message, ProtocolOptions } from "@waku/interfaces";
|
3
|
+
import type { Libp2p } from "libp2p";
|
4
|
+
import { ContentFilter } from "./filter_rpc";
|
5
|
+
export { ContentFilter };
|
6
|
+
export declare const FilterCodec = "/vac/waku/filter/2.0.0-beta1";
|
7
|
+
export interface CreateOptions {
|
8
|
+
/**
|
9
|
+
* The PubSub Topic to use. Defaults to {@link DefaultPubSubTopic}.
|
10
|
+
*
|
11
|
+
* The usage of the default pubsub topic is recommended.
|
12
|
+
* See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details.
|
13
|
+
*
|
14
|
+
* @default {@link DefaultPubSubTopic}
|
15
|
+
*/
|
16
|
+
pubSubTopic?: string;
|
17
|
+
}
|
18
|
+
export declare type UnsubscribeFunction = () => Promise<void>;
|
19
|
+
/**
|
20
|
+
* Implements client side of the [Waku v2 Filter protocol](https://rfc.vac.dev/spec/12/).
|
21
|
+
*
|
22
|
+
* Note this currently only works in NodeJS when the Waku node is listening on a port, see:
|
23
|
+
* - https://github.com/status-im/go-waku/issues/245
|
24
|
+
* - https://github.com/status-im/nwaku/issues/948
|
25
|
+
*/
|
26
|
+
export declare class WakuFilter implements Filter {
|
27
|
+
libp2p: Libp2p;
|
28
|
+
pubSubTopic: string;
|
29
|
+
private subscriptions;
|
30
|
+
private decoders;
|
31
|
+
constructor(libp2p: Libp2p, options?: CreateOptions);
|
32
|
+
/**
|
33
|
+
* @param decoders Array of Decoders to use to decode messages, it also specifies the content topics.
|
34
|
+
* @param callback A function that will be called on each message returned by the filter.
|
35
|
+
* @param opts The FilterSubscriptionOpts used to narrow which messages are returned, and which peer to connect to.
|
36
|
+
* @returns Unsubscribe function that can be used to end the subscription.
|
37
|
+
*/
|
38
|
+
subscribe<T extends Message>(decoders: Decoder<T>[], callback: Callback<T>, opts?: ProtocolOptions): Promise<UnsubscribeFunction>;
|
39
|
+
private onRequest;
|
40
|
+
private pushMessages;
|
41
|
+
private addCallback;
|
42
|
+
private deleteCallback;
|
43
|
+
private addDecoders;
|
44
|
+
private deleteDecoders;
|
45
|
+
private unsubscribe;
|
46
|
+
private newStream;
|
47
|
+
private getPeer;
|
48
|
+
peers(): Promise<Peer[]>;
|
49
|
+
randomPeer(): Promise<Peer | undefined>;
|
50
|
+
}
|