From 71fe0472548d009e10af6036d81352304ef99078 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 10 Nov 2022 22:44:53 +0530 Subject: [PATCH] add: support for user agent to identify libp2p --- packages/core/src/lib/waku.ts | 6 ++++++ packages/create/src/index.ts | 27 ++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/core/src/lib/waku.ts b/packages/core/src/lib/waku.ts index f792eb6c56..a8accc7202 100644 --- a/packages/core/src/lib/waku.ts +++ b/packages/core/src/lib/waku.ts @@ -19,6 +19,7 @@ import { StoreCodec, WakuStore } from "./waku_store"; export const DefaultPingKeepAliveValueSecs = 0; export const DefaultRelayKeepAliveValueSecs = 5 * 60; +export const DefaultUserAgent = "waku-js"; const log = debug("waku:waku"); @@ -37,6 +38,11 @@ export interface WakuOptions { * @default {@link DefaultRelayKeepAliveValueSecs} */ relayKeepAlive?: number; + /** + * Set the user agent string to be used in identification of the node. + * @default {@link DefaultUserAgent} + */ + userAgent?: string; } export class WakuNode implements Waku { diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 89a6015f4e..52d3c00915 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -14,6 +14,7 @@ import { } from "@waku/core"; import { PeerDiscoveryStaticPeers } from "@waku/core/lib/peer_discovery_static_list"; import { getPredefinedBootstrapNodes } from "@waku/core/lib/predefined_bootstrap_nodes"; +import { DefaultUserAgent } from "@waku/core/lib/waku"; import type { WakuFull, WakuLight, WakuPrivacy } from "@waku/interfaces"; import type { Libp2p } from "libp2p"; import { createLibp2p, Libp2pOptions } from "libp2p"; @@ -71,7 +72,11 @@ export async function createLightNode( Object.assign(libp2pOptions, { peerDiscovery }); } - const libp2p = await defaultLibp2p(undefined, libp2pOptions); + const libp2p = await defaultLibp2p( + undefined, + libp2pOptions, + options?.userAgent + ); const wakuStore = new WakuStore(libp2p, options); const wakuLightPush = new WakuLightPush(libp2p, options); @@ -100,7 +105,11 @@ export async function createPrivacyNode( Object.assign(libp2pOptions, { peerDiscovery }); } - const libp2p = await defaultLibp2p(new WakuRelay(options), libp2pOptions); + const libp2p = await defaultLibp2p( + new WakuRelay(options), + libp2pOptions, + options?.userAgent + ); return new WakuNode(options ?? {}, libp2p) as WakuPrivacy; } @@ -128,7 +137,11 @@ export async function createFullNode( Object.assign(libp2pOptions, { peerDiscovery }); } - const libp2p = await defaultLibp2p(new WakuRelay(options), libp2pOptions); + const libp2p = await defaultLibp2p( + new WakuRelay(options), + libp2pOptions, + options?.userAgent + ); const wakuStore = new WakuStore(libp2p, options); const wakuLightPush = new WakuLightPush(libp2p, options); @@ -149,14 +162,18 @@ export function defaultPeerDiscovery(): PeerDiscovery { export async function defaultLibp2p( wakuRelay?: WakuRelay, - options?: Partial + options?: Partial, + userAgent?: string ): Promise { const libp2pOpts = Object.assign( { transports: [new WebSockets({ filter: filterAll })], streamMuxers: [new Mplex()], connectionEncryption: [new Noise()], - }, + identify: { + host: userAgent || DefaultUserAgent, + }, + } as Libp2pOptions, wakuRelay ? { pubsub: wakuRelay } : {}, options ?? {} );