@waku/core 0.0.26 → 0.0.27
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 +26 -0
- package/bundle/{base_protocol-pDODy0G6.js → base_protocol-LhsIWF3-.js} +137 -89
- package/bundle/{browser-mTOOnVZp.js → browser-BQyFvtq6.js} +501 -700
- package/bundle/{index-cmONXM-V.js → index-8YyfzF9R.js} +96 -41
- package/bundle/index.js +3033 -21649
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/{version_0-LQTFNC7k.js → version_0-FXfzO8Km.js} +1246 -2444
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -5
- package/dist/index.js +1 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +13 -10
- package/dist/lib/base_protocol.js +38 -22
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager.d.ts +2 -2
- package/dist/lib/connection_manager.js +16 -6
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -1
- package/dist/lib/filter/index.js +140 -82
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/filterPeers.d.ts +8 -5
- package/dist/lib/filterPeers.js +12 -5
- package/dist/lib/filterPeers.js.map +1 -1
- package/dist/lib/keep_alive_manager.d.ts +2 -3
- package/dist/lib/keep_alive_manager.js.map +1 -1
- package/dist/lib/light_push/index.js +3 -6
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +2 -2
- package/dist/lib/metadata/index.js +25 -11
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.js +1 -3
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +2 -2
- package/dist/lib/stream_manager.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.d.ts +1 -1
- package/dist/lib/wait_for_remote_peer.js +40 -10
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/package.json +14 -12
- package/src/index.ts +1 -6
- package/src/lib/base_protocol.ts +59 -34
- package/src/lib/connection_manager.ts +17 -10
- package/src/lib/filter/index.ts +228 -137
- package/src/lib/filterPeers.ts +15 -7
- package/src/lib/keep_alive_manager.ts +2 -3
- package/src/lib/light_push/index.ts +11 -10
- package/src/lib/metadata/index.ts +51 -19
- package/src/lib/store/index.ts +3 -6
- package/src/lib/stream_manager.ts +2 -3
- package/src/lib/wait_for_remote_peer.ts +58 -12
- package/dist/lib/waku.d.ts +0 -57
- package/dist/lib/waku.js +0 -130
- package/dist/lib/waku.js.map +0 -1
- package/src/lib/waku.ts +0 -214
@@ -1,14 +1,13 @@
|
|
1
|
-
import type { PeerId } from "@libp2p/interface
|
2
|
-
import { IncomingStreamData } from "@libp2p/interface
|
3
|
-
import { encodeRelayShard } from "@waku/enr";
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import { IncomingStreamData } from "@libp2p/interface";
|
4
3
|
import type {
|
5
4
|
IMetadata,
|
6
5
|
Libp2pComponents,
|
7
|
-
|
8
|
-
|
6
|
+
PeerIdStr,
|
7
|
+
ShardInfo
|
9
8
|
} from "@waku/interfaces";
|
10
9
|
import { proto_metadata } from "@waku/proto";
|
11
|
-
import { Logger } from "@waku/utils";
|
10
|
+
import { encodeRelayShard, Logger, shardInfoToPubsubTopics } from "@waku/utils";
|
12
11
|
import all from "it-all";
|
13
12
|
import * as lp from "it-length-prefixed";
|
14
13
|
import { pipe } from "it-pipe";
|
@@ -20,13 +19,21 @@ const log = new Logger("metadata");
|
|
20
19
|
|
21
20
|
export const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
22
21
|
|
23
|
-
class Metadata extends BaseProtocol {
|
24
|
-
private readonly shardInfo: ShardingParams;
|
22
|
+
class Metadata extends BaseProtocol implements IMetadata {
|
25
23
|
private libp2pComponents: Libp2pComponents;
|
26
|
-
|
27
|
-
|
24
|
+
handshakesConfirmed: Set<PeerIdStr> = new Set();
|
25
|
+
|
26
|
+
constructor(
|
27
|
+
public shardInfo: ShardInfo,
|
28
|
+
libp2p: Libp2pComponents
|
29
|
+
) {
|
30
|
+
super(
|
31
|
+
MetadataCodec,
|
32
|
+
libp2p.components,
|
33
|
+
log,
|
34
|
+
shardInfoToPubsubTopics(shardInfo)
|
35
|
+
);
|
28
36
|
this.libp2pComponents = libp2p;
|
29
|
-
this.shardInfo = shardInfo;
|
30
37
|
void libp2p.registrar.handle(MetadataCodec, (streamData) => {
|
31
38
|
void this.onRequest(streamData);
|
32
39
|
});
|
@@ -53,12 +60,10 @@ class Metadata extends BaseProtocol {
|
|
53
60
|
const remoteShardInfoResponse =
|
54
61
|
this.decodeMetadataResponse(encodedResponse);
|
55
62
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
}
|
61
|
-
});
|
63
|
+
await this.savePeerShardInfo(
|
64
|
+
connection.remotePeer,
|
65
|
+
remoteShardInfoResponse
|
66
|
+
);
|
62
67
|
} catch (error) {
|
63
68
|
log.error("Error handling metadata request", error);
|
64
69
|
}
|
@@ -70,7 +75,10 @@ class Metadata extends BaseProtocol {
|
|
70
75
|
async query(peerId: PeerId): Promise<ShardInfo> {
|
71
76
|
const request = proto_metadata.WakuMetadataRequest.encode(this.shardInfo);
|
72
77
|
|
73
|
-
const peer = await this.
|
78
|
+
const peer = await this.peerStore.get(peerId);
|
79
|
+
if (!peer) {
|
80
|
+
throw new Error(`Peer ${peerId.toString()} not found`);
|
81
|
+
}
|
74
82
|
|
75
83
|
const stream = await this.getStream(peer);
|
76
84
|
|
@@ -84,9 +92,19 @@ class Metadata extends BaseProtocol {
|
|
84
92
|
|
85
93
|
const decodedResponse = this.decodeMetadataResponse(encodedResponse);
|
86
94
|
|
95
|
+
await this.savePeerShardInfo(peerId, decodedResponse);
|
96
|
+
|
87
97
|
return decodedResponse;
|
88
98
|
}
|
89
99
|
|
100
|
+
public async confirmOrAttemptHandshake(peerId: PeerId): Promise<void> {
|
101
|
+
if (this.handshakesConfirmed.has(peerId.toString())) return;
|
102
|
+
|
103
|
+
await this.query(peerId);
|
104
|
+
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
|
90
108
|
private decodeMetadataResponse(encodedResponse: Uint8ArrayList[]): ShardInfo {
|
91
109
|
const bytes = new Uint8ArrayList();
|
92
110
|
|
@@ -101,10 +119,24 @@ class Metadata extends BaseProtocol {
|
|
101
119
|
|
102
120
|
return response;
|
103
121
|
}
|
122
|
+
|
123
|
+
private async savePeerShardInfo(
|
124
|
+
peerId: PeerId,
|
125
|
+
shardInfo: ShardInfo
|
126
|
+
): Promise<void> {
|
127
|
+
// add or update the shardInfo to peer store
|
128
|
+
await this.libp2pComponents.peerStore.merge(peerId, {
|
129
|
+
metadata: {
|
130
|
+
shardInfo: encodeRelayShard(shardInfo)
|
131
|
+
}
|
132
|
+
});
|
133
|
+
|
134
|
+
this.handshakesConfirmed.add(peerId.toString());
|
135
|
+
}
|
104
136
|
}
|
105
137
|
|
106
138
|
export function wakuMetadata(
|
107
|
-
shardInfo:
|
139
|
+
shardInfo: ShardInfo
|
108
140
|
): (components: Libp2pComponents) => IMetadata {
|
109
141
|
return (components: Libp2pComponents) => new Metadata(shardInfo, components);
|
110
142
|
}
|
package/src/lib/store/index.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { Stream } from "@libp2p/interface
|
1
|
+
import type { Stream } from "@libp2p/interface";
|
2
2
|
import { sha256 } from "@noble/hashes/sha256";
|
3
3
|
import {
|
4
4
|
Cursor,
|
@@ -6,8 +6,7 @@ import {
|
|
6
6
|
IDecoder,
|
7
7
|
IStore,
|
8
8
|
Libp2p,
|
9
|
-
ProtocolCreateOptions
|
10
|
-
PubsubTopic
|
9
|
+
ProtocolCreateOptions
|
11
10
|
} from "@waku/interfaces";
|
12
11
|
import { proto_store as proto } from "@waku/proto";
|
13
12
|
import { ensurePubsubTopicIsConfigured, isDefined } from "@waku/utils";
|
@@ -74,12 +73,10 @@ export interface QueryOptions {
|
|
74
73
|
* The Waku Store protocol can be used to retrieved historical messages.
|
75
74
|
*/
|
76
75
|
class Store extends BaseProtocol implements IStore {
|
77
|
-
private readonly pubsubTopics: PubsubTopic[];
|
78
76
|
private readonly NUM_PEERS_PROTOCOL = 1;
|
79
77
|
|
80
78
|
constructor(libp2p: Libp2p, options?: ProtocolCreateOptions) {
|
81
|
-
super(StoreCodec, libp2p.components);
|
82
|
-
this.pubsubTopics = this.initializePubsubTopic(options);
|
79
|
+
super(StoreCodec, libp2p.components, log, options!.pubsubTopics!, options);
|
83
80
|
}
|
84
81
|
|
85
82
|
/**
|
@@ -1,6 +1,5 @@
|
|
1
|
-
import type { PeerUpdate } from "@libp2p/interface";
|
2
|
-
import
|
3
|
-
import { Peer } from "@libp2p/interface/peer-store";
|
1
|
+
import type { PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
+
import { Peer } from "@libp2p/interface";
|
4
3
|
import { Libp2p } from "@waku/interfaces";
|
5
4
|
import { Logger } from "@waku/utils";
|
6
5
|
import { selectConnection } from "@waku/utils/libp2p";
|
@@ -1,15 +1,14 @@
|
|
1
1
|
import type { IdentifyResult } from "@libp2p/interface";
|
2
|
-
import type { IBaseProtocol, IRelay, Waku } from "@waku/interfaces";
|
2
|
+
import type { IBaseProtocol, IMetadata, IRelay, Waku } from "@waku/interfaces";
|
3
3
|
import { Protocols } from "@waku/interfaces";
|
4
4
|
import { Logger } from "@waku/utils";
|
5
5
|
import { pEvent } from "p-event";
|
6
|
-
|
7
6
|
const log = new Logger("wait-for-remote-peer");
|
8
7
|
|
9
8
|
/**
|
10
9
|
* Wait for a remote peer to be ready given the passed protocols.
|
11
10
|
* Must be used after attempting to connect to nodes, using
|
12
|
-
* {@link @waku/
|
11
|
+
* {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
|
13
12
|
* {@link @waku/sdk!createLightNode}.
|
14
13
|
*
|
15
14
|
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
@@ -45,19 +44,25 @@ export async function waitForRemotePeer(
|
|
45
44
|
if (protocols.includes(Protocols.Store)) {
|
46
45
|
if (!waku.store)
|
47
46
|
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
48
|
-
promises.push(
|
47
|
+
promises.push(
|
48
|
+
waitForConnectedPeer(waku.store, waku.libp2p.services.metadata)
|
49
|
+
);
|
49
50
|
}
|
50
51
|
|
51
52
|
if (protocols.includes(Protocols.LightPush)) {
|
52
53
|
if (!waku.lightPush)
|
53
54
|
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
54
|
-
promises.push(
|
55
|
+
promises.push(
|
56
|
+
waitForConnectedPeer(waku.lightPush, waku.libp2p.services.metadata)
|
57
|
+
);
|
55
58
|
}
|
56
59
|
|
57
60
|
if (protocols.includes(Protocols.Filter)) {
|
58
61
|
if (!waku.filter)
|
59
62
|
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
60
|
-
promises.push(
|
63
|
+
promises.push(
|
64
|
+
waitForConnectedPeer(waku.filter, waku.libp2p.services.metadata)
|
65
|
+
);
|
61
66
|
}
|
62
67
|
|
63
68
|
if (timeoutMs) {
|
@@ -73,21 +78,62 @@ export async function waitForRemotePeer(
|
|
73
78
|
|
74
79
|
/**
|
75
80
|
* Wait for a peer with the given protocol to be connected.
|
81
|
+
* If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
|
76
82
|
*/
|
77
|
-
async function waitForConnectedPeer(
|
83
|
+
async function waitForConnectedPeer(
|
84
|
+
protocol: IBaseProtocol,
|
85
|
+
metadataService?: IMetadata
|
86
|
+
): Promise<void> {
|
78
87
|
const codec = protocol.multicodec;
|
79
|
-
const peers = await protocol.
|
88
|
+
const peers = await protocol.connectedPeers();
|
80
89
|
|
81
90
|
if (peers.length) {
|
82
|
-
|
83
|
-
|
91
|
+
if (!metadataService) {
|
92
|
+
log.info(`${codec} peer found: `, peers[0].id.toString());
|
93
|
+
return;
|
94
|
+
}
|
95
|
+
|
96
|
+
// once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled
|
97
|
+
try {
|
98
|
+
await Promise.any(
|
99
|
+
peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id))
|
100
|
+
);
|
101
|
+
return;
|
102
|
+
} catch (e) {
|
103
|
+
if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED")
|
104
|
+
log.error(
|
105
|
+
`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
|
106
|
+
);
|
107
|
+
|
108
|
+
log.error(`Error waiting for handshake confirmation: ${e}`);
|
109
|
+
}
|
84
110
|
}
|
85
111
|
|
112
|
+
log.info(`Waiting for ${codec} peer`);
|
113
|
+
|
114
|
+
// else we'll just wait for the next peer to connect
|
86
115
|
await new Promise<void>((resolve) => {
|
87
116
|
const cb = (evt: CustomEvent<IdentifyResult>): void => {
|
88
117
|
if (evt.detail?.protocols?.includes(codec)) {
|
89
|
-
|
90
|
-
|
118
|
+
if (metadataService) {
|
119
|
+
metadataService
|
120
|
+
.confirmOrAttemptHandshake(evt.detail.peerId)
|
121
|
+
.then(() => {
|
122
|
+
protocol.removeLibp2pEventListener("peer:identify", cb);
|
123
|
+
resolve();
|
124
|
+
})
|
125
|
+
.catch((e) => {
|
126
|
+
if (e.code === "ERR_CONNECTION_BEING_CLOSED")
|
127
|
+
log.error(
|
128
|
+
`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
|
129
|
+
);
|
130
|
+
|
131
|
+
log.error(`Error waiting for handshake confirmation: ${e}`);
|
132
|
+
});
|
133
|
+
} else {
|
134
|
+
protocol.removeLibp2pEventListener("peer:identify", cb);
|
135
|
+
resolve();
|
136
|
+
}
|
91
137
|
}
|
92
138
|
};
|
93
139
|
protocol.addLibp2pEventListener("peer:identify", cb);
|
package/dist/lib/waku.d.ts
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
import type { Stream } from "@libp2p/interface/connection";
|
2
|
-
import { PeerId } from "@libp2p/interface/peer-id";
|
3
|
-
import { MultiaddrInput } from "@multiformats/multiaddr";
|
4
|
-
import type { IFilter, ILightPush, IRelay, IStore, Libp2p, PubsubTopic, ShardingParams, Waku } from "@waku/interfaces";
|
5
|
-
import { Protocols } from "@waku/interfaces";
|
6
|
-
import { ConnectionManager } from "./connection_manager.js";
|
7
|
-
export declare const DefaultPingKeepAliveValueSecs: number;
|
8
|
-
export declare const DefaultRelayKeepAliveValueSecs: number;
|
9
|
-
export declare const DefaultUserAgent = "js-waku";
|
10
|
-
export interface WakuOptions {
|
11
|
-
/**
|
12
|
-
* Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
|
13
|
-
* request to each peer after the set number of seconds. Set to 0 to disable.
|
14
|
-
*
|
15
|
-
* @default {@link @waku/core.DefaultPingKeepAliveValueSecs}
|
16
|
-
*/
|
17
|
-
pingKeepAlive?: number;
|
18
|
-
/**
|
19
|
-
* Set keep alive frequency in seconds: Waku will send a ping message over
|
20
|
-
* relay to each peer after the set number of seconds. Set to 0 to disable.
|
21
|
-
*
|
22
|
-
* @default {@link @waku/core.DefaultRelayKeepAliveValueSecs}
|
23
|
-
*/
|
24
|
-
relayKeepAlive?: number;
|
25
|
-
/**
|
26
|
-
* Set the user agent string to be used in identification of the node.
|
27
|
-
* @default {@link @waku/core.DefaultUserAgent}
|
28
|
-
*/
|
29
|
-
userAgent?: string;
|
30
|
-
}
|
31
|
-
export declare class WakuNode implements Waku {
|
32
|
-
libp2p: Libp2p;
|
33
|
-
relay?: IRelay;
|
34
|
-
store?: IStore;
|
35
|
-
filter?: IFilter;
|
36
|
-
lightPush?: ILightPush;
|
37
|
-
connectionManager: ConnectionManager;
|
38
|
-
readonly pubsubTopics: PubsubTopic[];
|
39
|
-
constructor(options: WakuOptions, pubsubTopics: string[] | undefined, libp2p: Libp2p, pubsubShardInfo?: ShardingParams, store?: (libp2p: Libp2p) => IStore, lightPush?: (libp2p: Libp2p) => ILightPush, filter?: (libp2p: Libp2p) => IFilter, relay?: (libp2p: Libp2p) => IRelay);
|
40
|
-
/**
|
41
|
-
* Dials to the provided peer.
|
42
|
-
*
|
43
|
-
* @param peer The peer to dial
|
44
|
-
* @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
|
45
|
-
*/
|
46
|
-
dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise<Stream>;
|
47
|
-
start(): Promise<void>;
|
48
|
-
stop(): Promise<void>;
|
49
|
-
isStarted(): boolean;
|
50
|
-
isConnected(): boolean;
|
51
|
-
/**
|
52
|
-
* Return the local multiaddr with peer id on which libp2p is listening.
|
53
|
-
*
|
54
|
-
* @throws if libp2p is not listening on localhost.
|
55
|
-
*/
|
56
|
-
getLocalMultiaddrWithID(): string;
|
57
|
-
}
|
package/dist/lib/waku.js
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
import { isPeerId } from "@libp2p/interface/peer-id";
|
2
|
-
import { multiaddr } from "@multiformats/multiaddr";
|
3
|
-
import { DefaultPubsubTopic, Protocols } from "@waku/interfaces";
|
4
|
-
import { Logger, shardInfoToPubsubTopics } from "@waku/utils";
|
5
|
-
import { ConnectionManager } from "./connection_manager.js";
|
6
|
-
export const DefaultPingKeepAliveValueSecs = 5 * 60;
|
7
|
-
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
8
|
-
export const DefaultUserAgent = "js-waku";
|
9
|
-
const log = new Logger("waku");
|
10
|
-
export class WakuNode {
|
11
|
-
libp2p;
|
12
|
-
relay;
|
13
|
-
store;
|
14
|
-
filter;
|
15
|
-
lightPush;
|
16
|
-
connectionManager;
|
17
|
-
pubsubTopics;
|
18
|
-
constructor(options, pubsubTopics = [], libp2p, pubsubShardInfo, store, lightPush, filter, relay) {
|
19
|
-
if (!pubsubShardInfo) {
|
20
|
-
this.pubsubTopics =
|
21
|
-
pubsubTopics.length > 0 ? pubsubTopics : [DefaultPubsubTopic];
|
22
|
-
}
|
23
|
-
else {
|
24
|
-
this.pubsubTopics = shardInfoToPubsubTopics(pubsubShardInfo);
|
25
|
-
}
|
26
|
-
this.libp2p = libp2p;
|
27
|
-
if (store) {
|
28
|
-
this.store = store(libp2p);
|
29
|
-
}
|
30
|
-
if (filter) {
|
31
|
-
this.filter = filter(libp2p);
|
32
|
-
}
|
33
|
-
if (lightPush) {
|
34
|
-
this.lightPush = lightPush(libp2p);
|
35
|
-
}
|
36
|
-
if (relay) {
|
37
|
-
this.relay = relay(libp2p);
|
38
|
-
}
|
39
|
-
const pingKeepAlive = options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
40
|
-
const relayKeepAlive = this.relay
|
41
|
-
? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
|
42
|
-
: 0;
|
43
|
-
const peerId = this.libp2p.peerId.toString();
|
44
|
-
this.connectionManager = ConnectionManager.create(peerId, libp2p, { pingKeepAlive, relayKeepAlive }, this.pubsubTopics, this.relay);
|
45
|
-
log.info("Waku node created", peerId, `relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
|
46
|
-
.lightPush}, filter: ${!!this.filter}`);
|
47
|
-
}
|
48
|
-
/**
|
49
|
-
* Dials to the provided peer.
|
50
|
-
*
|
51
|
-
* @param peer The peer to dial
|
52
|
-
* @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
|
53
|
-
*/
|
54
|
-
async dial(peer, protocols) {
|
55
|
-
const _protocols = protocols ?? [];
|
56
|
-
const peerId = mapToPeerIdOrMultiaddr(peer);
|
57
|
-
if (typeof protocols === "undefined") {
|
58
|
-
this.relay && _protocols.push(Protocols.Relay);
|
59
|
-
this.store && _protocols.push(Protocols.Store);
|
60
|
-
this.filter && _protocols.push(Protocols.Filter);
|
61
|
-
this.lightPush && _protocols.push(Protocols.LightPush);
|
62
|
-
}
|
63
|
-
const codecs = [];
|
64
|
-
if (_protocols.includes(Protocols.Relay)) {
|
65
|
-
if (this.relay) {
|
66
|
-
this.relay.gossipSub.multicodecs.forEach((codec) => codecs.push(codec));
|
67
|
-
}
|
68
|
-
else {
|
69
|
-
log.error("Relay codec not included in dial codec: protocol not mounted locally");
|
70
|
-
}
|
71
|
-
}
|
72
|
-
if (_protocols.includes(Protocols.Store)) {
|
73
|
-
if (this.store) {
|
74
|
-
codecs.push(this.store.multicodec);
|
75
|
-
}
|
76
|
-
else {
|
77
|
-
log.error("Store codec not included in dial codec: protocol not mounted locally");
|
78
|
-
}
|
79
|
-
}
|
80
|
-
if (_protocols.includes(Protocols.LightPush)) {
|
81
|
-
if (this.lightPush) {
|
82
|
-
codecs.push(this.lightPush.multicodec);
|
83
|
-
}
|
84
|
-
else {
|
85
|
-
log.error("Light Push codec not included in dial codec: protocol not mounted locally");
|
86
|
-
}
|
87
|
-
}
|
88
|
-
if (_protocols.includes(Protocols.Filter)) {
|
89
|
-
if (this.filter) {
|
90
|
-
codecs.push(this.filter.multicodec);
|
91
|
-
}
|
92
|
-
else {
|
93
|
-
log.error("Filter codec not included in dial codec: protocol not mounted locally");
|
94
|
-
}
|
95
|
-
}
|
96
|
-
log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
|
97
|
-
return this.libp2p.dialProtocol(peerId, codecs);
|
98
|
-
}
|
99
|
-
async start() {
|
100
|
-
await this.libp2p.start();
|
101
|
-
}
|
102
|
-
async stop() {
|
103
|
-
this.connectionManager.stop();
|
104
|
-
await this.libp2p.stop();
|
105
|
-
}
|
106
|
-
isStarted() {
|
107
|
-
return this.libp2p.isStarted();
|
108
|
-
}
|
109
|
-
isConnected() {
|
110
|
-
return this.connectionManager.isConnected();
|
111
|
-
}
|
112
|
-
/**
|
113
|
-
* Return the local multiaddr with peer id on which libp2p is listening.
|
114
|
-
*
|
115
|
-
* @throws if libp2p is not listening on localhost.
|
116
|
-
*/
|
117
|
-
getLocalMultiaddrWithID() {
|
118
|
-
const localMultiaddr = this.libp2p
|
119
|
-
.getMultiaddrs()
|
120
|
-
.find((addr) => addr.toString().match(/127\.0\.0\.1/));
|
121
|
-
if (!localMultiaddr || localMultiaddr.toString() === "") {
|
122
|
-
throw "Not listening on localhost";
|
123
|
-
}
|
124
|
-
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
|
125
|
-
}
|
126
|
-
}
|
127
|
-
function mapToPeerIdOrMultiaddr(peerId) {
|
128
|
-
return isPeerId(peerId) ? peerId : multiaddr(peerId);
|
129
|
-
}
|
130
|
-
//# sourceMappingURL=waku.js.map
|
package/dist/lib/waku.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"waku.js","sourceRoot":"","sources":["../../src/lib/waku.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAU,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,SAAS,EAA6B,MAAM,yBAAyB,CAAC;AAW/E,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,GAAG,EAAE,CAAC;AACpD,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,GAAG,EAAE,CAAC;AACrD,MAAM,CAAC,MAAM,gBAAgB,GAAG,SAAS,CAAC;AAE1C,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;AAwB/B,MAAM,OAAO,QAAQ;IACZ,MAAM,CAAS;IACf,KAAK,CAAU;IACf,KAAK,CAAU;IACf,MAAM,CAAW;IACjB,SAAS,CAAc;IACvB,iBAAiB,CAAoB;IAC5B,YAAY,CAAgB;IAE5C,YACE,OAAoB,EACpB,eAA8B,EAAE,EAChC,MAAc,EACd,eAAgC,EAChC,KAAkC,EAClC,SAA0C,EAC1C,MAAoC,EACpC,KAAkC;QAElC,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,IAAI,CAAC,YAAY;g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AC9C,CAAC;IAED;;;;OAIG;IACH,uBAAuB;QACrB,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM;aAC/B,aAAa,EAAE;aACf,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;QACzD,IAAI,CAAC,cAAc,IAAI,cAAc,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC;YACxD,MAAM,4BAA4B,CAAC;QACrC,CAAC;QACD,OAAO,cAAc,GAAG,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAClE,CAAC;CACF;AACD,SAAS,sBAAsB,CAC7B,MAA+B;IAE/B,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;AACvD,CAAC"}
|
package/src/lib/waku.ts
DELETED
@@ -1,214 +0,0 @@
|
|
1
|
-
import type { Stream } from "@libp2p/interface/connection";
|
2
|
-
import { isPeerId, PeerId } from "@libp2p/interface/peer-id";
|
3
|
-
import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
|
4
|
-
import type {
|
5
|
-
IFilter,
|
6
|
-
ILightPush,
|
7
|
-
IRelay,
|
8
|
-
IStore,
|
9
|
-
Libp2p,
|
10
|
-
PubsubTopic,
|
11
|
-
ShardingParams,
|
12
|
-
Waku
|
13
|
-
} from "@waku/interfaces";
|
14
|
-
import { DefaultPubsubTopic, Protocols } from "@waku/interfaces";
|
15
|
-
import { Logger, shardInfoToPubsubTopics } from "@waku/utils";
|
16
|
-
|
17
|
-
import { ConnectionManager } from "./connection_manager.js";
|
18
|
-
|
19
|
-
export const DefaultPingKeepAliveValueSecs = 5 * 60;
|
20
|
-
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
|
21
|
-
export const DefaultUserAgent = "js-waku";
|
22
|
-
|
23
|
-
const log = new Logger("waku");
|
24
|
-
|
25
|
-
export interface WakuOptions {
|
26
|
-
/**
|
27
|
-
* Set keep alive frequency in seconds: Waku will send a `/ipfs/ping/1.0.0`
|
28
|
-
* request to each peer after the set number of seconds. Set to 0 to disable.
|
29
|
-
*
|
30
|
-
* @default {@link @waku/core.DefaultPingKeepAliveValueSecs}
|
31
|
-
*/
|
32
|
-
pingKeepAlive?: number;
|
33
|
-
/**
|
34
|
-
* Set keep alive frequency in seconds: Waku will send a ping message over
|
35
|
-
* relay to each peer after the set number of seconds. Set to 0 to disable.
|
36
|
-
*
|
37
|
-
* @default {@link @waku/core.DefaultRelayKeepAliveValueSecs}
|
38
|
-
*/
|
39
|
-
relayKeepAlive?: number;
|
40
|
-
/**
|
41
|
-
* Set the user agent string to be used in identification of the node.
|
42
|
-
* @default {@link @waku/core.DefaultUserAgent}
|
43
|
-
*/
|
44
|
-
userAgent?: string;
|
45
|
-
}
|
46
|
-
|
47
|
-
export class WakuNode implements Waku {
|
48
|
-
public libp2p: Libp2p;
|
49
|
-
public relay?: IRelay;
|
50
|
-
public store?: IStore;
|
51
|
-
public filter?: IFilter;
|
52
|
-
public lightPush?: ILightPush;
|
53
|
-
public connectionManager: ConnectionManager;
|
54
|
-
public readonly pubsubTopics: PubsubTopic[];
|
55
|
-
|
56
|
-
constructor(
|
57
|
-
options: WakuOptions,
|
58
|
-
pubsubTopics: PubsubTopic[] = [],
|
59
|
-
libp2p: Libp2p,
|
60
|
-
pubsubShardInfo?: ShardingParams,
|
61
|
-
store?: (libp2p: Libp2p) => IStore,
|
62
|
-
lightPush?: (libp2p: Libp2p) => ILightPush,
|
63
|
-
filter?: (libp2p: Libp2p) => IFilter,
|
64
|
-
relay?: (libp2p: Libp2p) => IRelay
|
65
|
-
) {
|
66
|
-
if (!pubsubShardInfo) {
|
67
|
-
this.pubsubTopics =
|
68
|
-
pubsubTopics.length > 0 ? pubsubTopics : [DefaultPubsubTopic];
|
69
|
-
} else {
|
70
|
-
this.pubsubTopics = shardInfoToPubsubTopics(pubsubShardInfo);
|
71
|
-
}
|
72
|
-
|
73
|
-
this.libp2p = libp2p;
|
74
|
-
|
75
|
-
if (store) {
|
76
|
-
this.store = store(libp2p);
|
77
|
-
}
|
78
|
-
if (filter) {
|
79
|
-
this.filter = filter(libp2p);
|
80
|
-
}
|
81
|
-
if (lightPush) {
|
82
|
-
this.lightPush = lightPush(libp2p);
|
83
|
-
}
|
84
|
-
|
85
|
-
if (relay) {
|
86
|
-
this.relay = relay(libp2p);
|
87
|
-
}
|
88
|
-
|
89
|
-
const pingKeepAlive =
|
90
|
-
options.pingKeepAlive || DefaultPingKeepAliveValueSecs;
|
91
|
-
const relayKeepAlive = this.relay
|
92
|
-
? options.relayKeepAlive || DefaultRelayKeepAliveValueSecs
|
93
|
-
: 0;
|
94
|
-
|
95
|
-
const peerId = this.libp2p.peerId.toString();
|
96
|
-
|
97
|
-
this.connectionManager = ConnectionManager.create(
|
98
|
-
peerId,
|
99
|
-
libp2p,
|
100
|
-
{ pingKeepAlive, relayKeepAlive },
|
101
|
-
this.pubsubTopics,
|
102
|
-
this.relay
|
103
|
-
);
|
104
|
-
|
105
|
-
log.info(
|
106
|
-
"Waku node created",
|
107
|
-
peerId,
|
108
|
-
`relay: ${!!this.relay}, store: ${!!this.store}, light push: ${!!this
|
109
|
-
.lightPush}, filter: ${!!this.filter}`
|
110
|
-
);
|
111
|
-
}
|
112
|
-
|
113
|
-
/**
|
114
|
-
* Dials to the provided peer.
|
115
|
-
*
|
116
|
-
* @param peer The peer to dial
|
117
|
-
* @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
|
118
|
-
*/
|
119
|
-
async dial(
|
120
|
-
peer: PeerId | MultiaddrInput,
|
121
|
-
protocols?: Protocols[]
|
122
|
-
): Promise<Stream> {
|
123
|
-
const _protocols = protocols ?? [];
|
124
|
-
const peerId = mapToPeerIdOrMultiaddr(peer);
|
125
|
-
|
126
|
-
if (typeof protocols === "undefined") {
|
127
|
-
this.relay && _protocols.push(Protocols.Relay);
|
128
|
-
this.store && _protocols.push(Protocols.Store);
|
129
|
-
this.filter && _protocols.push(Protocols.Filter);
|
130
|
-
this.lightPush && _protocols.push(Protocols.LightPush);
|
131
|
-
}
|
132
|
-
|
133
|
-
const codecs: string[] = [];
|
134
|
-
if (_protocols.includes(Protocols.Relay)) {
|
135
|
-
if (this.relay) {
|
136
|
-
this.relay.gossipSub.multicodecs.forEach((codec: string) =>
|
137
|
-
codecs.push(codec)
|
138
|
-
);
|
139
|
-
} else {
|
140
|
-
log.error(
|
141
|
-
"Relay codec not included in dial codec: protocol not mounted locally"
|
142
|
-
);
|
143
|
-
}
|
144
|
-
}
|
145
|
-
if (_protocols.includes(Protocols.Store)) {
|
146
|
-
if (this.store) {
|
147
|
-
codecs.push(this.store.multicodec);
|
148
|
-
} else {
|
149
|
-
log.error(
|
150
|
-
"Store codec not included in dial codec: protocol not mounted locally"
|
151
|
-
);
|
152
|
-
}
|
153
|
-
}
|
154
|
-
if (_protocols.includes(Protocols.LightPush)) {
|
155
|
-
if (this.lightPush) {
|
156
|
-
codecs.push(this.lightPush.multicodec);
|
157
|
-
} else {
|
158
|
-
log.error(
|
159
|
-
"Light Push codec not included in dial codec: protocol not mounted locally"
|
160
|
-
);
|
161
|
-
}
|
162
|
-
}
|
163
|
-
if (_protocols.includes(Protocols.Filter)) {
|
164
|
-
if (this.filter) {
|
165
|
-
codecs.push(this.filter.multicodec);
|
166
|
-
} else {
|
167
|
-
log.error(
|
168
|
-
"Filter codec not included in dial codec: protocol not mounted locally"
|
169
|
-
);
|
170
|
-
}
|
171
|
-
}
|
172
|
-
|
173
|
-
log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
|
174
|
-
|
175
|
-
return this.libp2p.dialProtocol(peerId, codecs);
|
176
|
-
}
|
177
|
-
|
178
|
-
async start(): Promise<void> {
|
179
|
-
await this.libp2p.start();
|
180
|
-
}
|
181
|
-
|
182
|
-
async stop(): Promise<void> {
|
183
|
-
this.connectionManager.stop();
|
184
|
-
await this.libp2p.stop();
|
185
|
-
}
|
186
|
-
|
187
|
-
isStarted(): boolean {
|
188
|
-
return this.libp2p.isStarted();
|
189
|
-
}
|
190
|
-
|
191
|
-
isConnected(): boolean {
|
192
|
-
return this.connectionManager.isConnected();
|
193
|
-
}
|
194
|
-
|
195
|
-
/**
|
196
|
-
* Return the local multiaddr with peer id on which libp2p is listening.
|
197
|
-
*
|
198
|
-
* @throws if libp2p is not listening on localhost.
|
199
|
-
*/
|
200
|
-
getLocalMultiaddrWithID(): string {
|
201
|
-
const localMultiaddr = this.libp2p
|
202
|
-
.getMultiaddrs()
|
203
|
-
.find((addr) => addr.toString().match(/127\.0\.0\.1/));
|
204
|
-
if (!localMultiaddr || localMultiaddr.toString() === "") {
|
205
|
-
throw "Not listening on localhost";
|
206
|
-
}
|
207
|
-
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
|
208
|
-
}
|
209
|
-
}
|
210
|
-
function mapToPeerIdOrMultiaddr(
|
211
|
-
peerId: PeerId | MultiaddrInput
|
212
|
-
): PeerId | Multiaddr {
|
213
|
-
return isPeerId(peerId) ? peerId : multiaddr(peerId);
|
214
|
-
}
|