-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathlibp2p.ts
More file actions
128 lines (109 loc) · 3.53 KB
/
Copy pathlibp2p.ts
File metadata and controls
128 lines (109 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { noise } from "@chainsafe/libp2p-noise";
import { bootstrap } from "@libp2p/bootstrap";
import { identify } from "@libp2p/identify";
import { mplex } from "@libp2p/mplex";
import { ping } from "@libp2p/ping";
import { webSockets } from "@libp2p/websockets";
import type { Multiaddr } from "@multiformats/multiaddr";
import { WebSockets, WebSocketsSecure } from "@multiformats/multiaddr-matcher";
import { wakuMetadata } from "@waku/core";
import {
type ClusterId,
type CreateLibp2pOptions,
type CreateNodeOptions,
DEFAULT_CLUSTER_ID,
DefaultNetworkConfig,
type Libp2p
} from "@waku/interfaces";
import { Logger } from "@waku/utils";
import { createLibp2p } from "libp2p";
import { isTestEnvironment } from "../env.js";
import { getPeerDiscoveries } from "./discovery.js";
const log = new Logger("sdk:create");
const DefaultUserAgent = "js-waku";
const DefaultPingMaxInboundStreams = 10;
export async function defaultLibp2p(
clusterId: ClusterId,
options?: Partial<CreateLibp2pOptions>,
userAgent?: string
): Promise<Libp2p> {
if (!options?.hideWebSocketInfo && !isTestEnvironment()) {
/* eslint-disable no-console */
console.info(
"%cIgnore WebSocket connection failures",
"background: gray; color: white; font-size: x-large"
);
console.info(
"%cWaku tries to discover peers and some of them are expected to fail",
"background: gray; color: white; font-size: x-large"
);
/* eslint-enable no-console */
}
const denyDialMultiaddr = async (ma: Multiaddr): Promise<boolean> => {
const userDeny = options?.connectionGater?.denyDialMultiaddr;
if (options?.filterMultiaddrs === false || isTestEnvironment()) {
return (await userDeny?.(ma)) ?? false;
}
if (WebSockets.matches(ma) && !WebSocketsSecure.matches(ma)) {
return true;
}
return (await userDeny?.(ma)) ?? false;
};
const connectionGater = {
...options?.connectionGater,
denyDialMultiaddr
};
return createLibp2p({
transports: [webSockets()],
streamMuxers: [mplex()],
connectionEncrypters: [noise()],
...options,
connectionGater,
services: {
identify: identify({
agentVersion: userAgent ?? DefaultUserAgent
}),
ping: ping({
maxInboundStreams:
options?.pingMaxInboundStreams ?? DefaultPingMaxInboundStreams
}),
metadata: wakuMetadata(clusterId),
...options?.services
}
}) as any as Libp2p; // TODO: make libp2p include it;
}
export async function createLibp2pAndUpdateOptions(
options: CreateNodeOptions
): Promise<Libp2p> {
const networkConfig = options.networkConfig ?? DefaultNetworkConfig;
const clusterId = networkConfig.clusterId ?? DEFAULT_CLUSTER_ID;
log.info("Creating Waku node with cluster id: ", clusterId);
const libp2pOptions = options?.libp2p ?? {};
const peerDiscovery = libp2pOptions.peerDiscovery ?? [];
if (options?.defaultBootstrap) {
peerDiscovery.push(
...getPeerDiscoveries(
{
dns: true,
peerExchange: true,
peerCache: true,
...options.discovery
},
options.peerCache
)
);
} else {
peerDiscovery.push(
...getPeerDiscoveries(options.discovery, options.peerCache)
);
}
const bootstrapPeers = [
...(options.bootstrapPeers || []),
...(options.store?.peers || [])
];
if (bootstrapPeers.length) {
peerDiscovery.push(bootstrap({ list: bootstrapPeers }));
}
libp2pOptions.peerDiscovery = peerDiscovery;
return defaultLibp2p(clusterId, libp2pOptions, options?.userAgent);
}