Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/sdk/src/create/libp2p.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { noise } from "@chainsafe/libp2p-noise";
import { bootstrap } from "@libp2p/bootstrap";
import { identify } from "@libp2p/identify";
import type { ConnectionGater } from "@libp2p/interface";
import { mplex } from "@libp2p/mplex";
import { ping } from "@libp2p/ping";
import { webSockets } from "@libp2p/websockets";
import { all as filterAll, wss } from "@libp2p/websockets/filters";
import { wakuMetadata } from "@waku/core";
import {
type CreateLibp2pOptions,
Expand Down Expand Up @@ -53,16 +53,22 @@
? { metadata: wakuMetadata(pubsubTopics) }
: {};

const filter =
options?.filterMultiaddrs === false || isTestEnvironment()
? filterAll
: wss;
const connectionGater: ConnectionGater = {
denyDialMultiaddr: async (multiaddr) => {
if (options?.filterMultiaddrs === false || isTestEnvironment()) {
return false;
}
const protocols = multiaddr.protos().map((proto) => proto.name);
return protocols.includes("ws") && !protocols.includes("wss");
}
};

return createLibp2p({
transports: [webSockets({ filter: filter })],
transports: [webSockets()],
streamMuxers: [mplex()],
connectionEncrypters: [noise()],
...options,
connectionGater,
services: {
identify: identify({
agentVersion: userAgent ?? DefaultUserAgent
Expand All @@ -74,7 +80,7 @@
...metadataService,
...options?.services
}
}) as any as Libp2p; // TODO: make libp2p include it;

Check warning on line 83 in packages/sdk/src/create/libp2p.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 83 in packages/sdk/src/create/libp2p.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
}

const DEFAULT_DISCOVERIES_ENABLED = {
Expand Down
22 changes: 19 additions & 3 deletions packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type Stream,
TypedEventEmitter
} from "@libp2p/interface";
import { peerIdFromString } from "@libp2p/peer-id";
import type { MultiaddrInput } from "@multiformats/multiaddr";
import { ConnectionManager, createDecoder, createEncoder } from "@waku/core";
import type {
Expand Down Expand Up @@ -88,8 +89,8 @@ export class WakuNode implements IWaku {

this.connectionManager = new ConnectionManager({
libp2p,
relay: this.relay,
events: this.events,
relay: this.relay,
pubsubTopics: pubsubTopics,
networkConfig: this.networkConfig,
config: options?.connectionManager
Expand Down Expand Up @@ -213,7 +214,22 @@ export class WakuNode implements IWaku {
public async hangUp(peer: PeerId | MultiaddrInput): Promise<boolean> {
log.info(`Hanging up peer:${peer?.toString()}.`);

return this.connectionManager.hangUp(peer);
let peerId: PeerId;
if (typeof peer === "string") {
peerId = peerIdFromString(peer);
} else if (peer && "getPeerId" in peer) {
// MultiaddrInput case
const peerIdStr = peer.getPeerId?.();
if (!peerIdStr) {
throw new Error("No peer ID in multiaddr");
}
peerId = peerIdFromString(peerIdStr);
} else {
// PeerId case
peerId = peer as PeerId;
}

return await this.connectionManager.hangUp(peerId);
}

public async start(): Promise<void> {
Expand All @@ -222,7 +238,7 @@ export class WakuNode implements IWaku {
this._nodeStateLock = true;

await this.libp2p.start();
this.connectionManager.start();
// Connection manager starts automatically
this.peerManager.start();
this.healthIndicator.start();
this.lightPush?.start();
Expand Down
Loading