From 4bdd7eeafdcede6e4dc91268146e4adce169bd68 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Wed, 10 Jan 2024 13:00:15 +0530 Subject: [PATCH 1/3] add a comment discouraging relay --- packages/sdk/src/create.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/sdk/src/create.ts b/packages/sdk/src/create.ts index 01d254f0ca..4cf88c97b3 100644 --- a/packages/sdk/src/create.ts +++ b/packages/sdk/src/create.ts @@ -80,6 +80,12 @@ export async function createLightNode( /** * Create a Waku node that uses Waku Relay to send and receive messages, * enabling some privacy preserving properties. + * * @remarks + * This function creates a Relay Node using the Waku Relay protocol. + * While it is technically possible to use this function in a browser environment, + * it is not recommended due to potential performance issues and limited browser capabilities. + * If you are developing a browser-based application, consider alternative approaches like creating a Light Node + * or use this function with caution. */ export async function createRelayNode( options?: ProtocolCreateOptions & WakuOptions & Partial From 8d8244ea78c18c7daa546b2895ba90df9b02e1d1 Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 11 Jan 2024 17:07:33 +0530 Subject: [PATCH 2/3] move createRelayNode under an explicit export --- package-lock.json | 2 +- packages/sdk/package.json | 13 +++++ packages/sdk/src/create.ts | 43 ----------------- packages/sdk/src/relay/index.ts | 47 +++++++++++++++++++ packages/tests/tests/enr.node.spec.ts | 2 +- packages/tests/tests/relay/index.node.spec.ts | 2 +- .../tests/tests/relay/interop.node.spec.ts | 2 +- .../tests/relay/multiple_pubsub.node.spec.ts | 2 +- .../tests/tests/relay/publish.node.spec.ts | 2 +- .../tests/tests/relay/subscribe.node.spec.ts | 2 +- .../tests/wait_for_remote_peer.node.spec.ts | 3 +- packages/tests/tests/waku.node.spec.ts | 4 +- 12 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 packages/sdk/src/relay/index.ts diff --git a/package-lock.json b/package-lock.json index 3f4bb5b45d..1a503e1d69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26641,6 +26641,7 @@ "license": "MIT OR Apache-2.0", "dependencies": { "@noble/hashes": "^1.3.2", + "@waku/interfaces": "0.0.20", "chai": "^4.3.10", "debug": "^4.3.4", "uint8arrays": "^4.0.4" @@ -26650,7 +26651,6 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.2.3", "@waku/build-utils": "*", - "@waku/interfaces": "0.0.20", "cspell": "^7.3.2", "npm-run-all": "^4.1.5", "rollup": "^4.6.0" diff --git a/packages/sdk/package.json b/packages/sdk/package.json index b69776715a..e01f699f91 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -8,6 +8,19 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" + }, + "./relay": { + "types": "./dist/relay/index.d.ts", + "import": "./dist/relay/index.js" + } + }, + "typesVersions": { + "*": { + "*": [ + "*", + "dist/*", + "dist/*/index" + ] } }, "type": "module", diff --git a/packages/sdk/src/create.ts b/packages/sdk/src/create.ts index 4cf88c97b3..920b42ac4f 100644 --- a/packages/sdk/src/create.ts +++ b/packages/sdk/src/create.ts @@ -22,7 +22,6 @@ import type { Libp2pComponents, LightNode, ProtocolCreateOptions, - RelayNode, ShardingParams } from "@waku/interfaces"; import { wakuPeerExchangeDiscovery } from "@waku/peer-exchange"; @@ -77,48 +76,6 @@ export async function createLightNode( ) as LightNode; } -/** - * Create a Waku node that uses Waku Relay to send and receive messages, - * enabling some privacy preserving properties. - * * @remarks - * This function creates a Relay Node using the Waku Relay protocol. - * While it is technically possible to use this function in a browser environment, - * it is not recommended due to potential performance issues and limited browser capabilities. - * If you are developing a browser-based application, consider alternative approaches like creating a Light Node - * or use this function with caution. - */ -export async function createRelayNode( - options?: ProtocolCreateOptions & WakuOptions & Partial -): Promise { - options = options ?? {}; - - const libp2pOptions = options?.libp2p ?? {}; - const peerDiscovery = libp2pOptions.peerDiscovery ?? []; - if (options?.defaultBootstrap) { - peerDiscovery.push(...defaultPeerDiscoveries()); - Object.assign(libp2pOptions, { peerDiscovery }); - } - - const libp2p = await defaultLibp2p( - options.shardInfo, - wakuGossipSub(options), - libp2pOptions, - options?.userAgent - ); - - const relay = wakuRelay(options); - - return new WakuNode( - options, - libp2p, - options.shardInfo, - undefined, - undefined, - undefined, - relay - ) as RelayNode; -} - /** * Create a Waku node that uses all Waku protocols. * diff --git a/packages/sdk/src/relay/index.ts b/packages/sdk/src/relay/index.ts new file mode 100644 index 0000000000..d88adbac13 --- /dev/null +++ b/packages/sdk/src/relay/index.ts @@ -0,0 +1,47 @@ +import { WakuNode, WakuOptions } from "@waku/core"; +import type { ProtocolCreateOptions, RelayNode } from "@waku/interfaces"; +import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay"; + +import { defaultLibp2p, defaultPeerDiscoveries } from "../create.js"; + +/** + * Create a Waku node that uses Waku Relay to send and receive messages, + * enabling some privacy preserving properties. + * * @remarks + * This function creates a Relay Node using the Waku Relay protocol. + * While it is technically possible to use this function in a browser environment, + * it is not recommended due to potential performance issues and limited browser capabilities. + * If you are developing a browser-based application, consider alternative approaches like creating a Light Node + * or use this function with caution. + */ +export async function createRelayNode( + options?: ProtocolCreateOptions & WakuOptions & Partial +): Promise { + options = options ?? {}; + + const libp2pOptions = options?.libp2p ?? {}; + const peerDiscovery = libp2pOptions.peerDiscovery ?? []; + if (options?.defaultBootstrap) { + peerDiscovery.push(...defaultPeerDiscoveries()); + Object.assign(libp2pOptions, { peerDiscovery }); + } + + const libp2p = await defaultLibp2p( + options.shardInfo, + wakuGossipSub(options), + libp2pOptions, + options?.userAgent + ); + + const relay = wakuRelay(options); + + return new WakuNode( + options, + libp2p, + options.shardInfo, + undefined, + undefined, + undefined, + relay + ) as RelayNode; +} diff --git a/packages/tests/tests/enr.node.spec.ts b/packages/tests/tests/enr.node.spec.ts index f6b7872b65..5959c31f9f 100644 --- a/packages/tests/tests/enr.node.spec.ts +++ b/packages/tests/tests/enr.node.spec.ts @@ -2,7 +2,7 @@ import { waitForRemotePeer } from "@waku/core"; import { EnrDecoder } from "@waku/enr"; import type { RelayNode } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; import { makeLogFileName, NOISE_KEY_1, tearDownNodes } from "../src/index.js"; diff --git a/packages/tests/tests/relay/index.node.spec.ts b/packages/tests/tests/relay/index.node.spec.ts index d6bb19fcff..c62e0b2a3f 100644 --- a/packages/tests/tests/relay/index.node.spec.ts +++ b/packages/tests/tests/relay/index.node.spec.ts @@ -13,7 +13,7 @@ import { createDecoder as createSymDecoder, createEncoder as createSymEncoder } from "@waku/message-encryption/symmetric"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/interop.node.spec.ts b/packages/tests/tests/relay/interop.node.spec.ts index 47cf2eeeba..1304bc9743 100644 --- a/packages/tests/tests/relay/interop.node.spec.ts +++ b/packages/tests/tests/relay/interop.node.spec.ts @@ -1,7 +1,7 @@ import type { PeerId } from "@libp2p/interface/peer-id"; import { DecodedMessage, waitForRemotePeer } from "@waku/core"; import { DefaultPubsubTopic, Protocols, RelayNode } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts index f1cfaf886f..2b3b33ac51 100644 --- a/packages/tests/tests/relay/multiple_pubsub.node.spec.ts +++ b/packages/tests/tests/relay/multiple_pubsub.node.spec.ts @@ -11,7 +11,7 @@ import { SingleShardInfo } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { contentTopicToPubsubTopic, singleShardInfoToPubsubTopic diff --git a/packages/tests/tests/relay/publish.node.spec.ts b/packages/tests/tests/relay/publish.node.spec.ts index f550bb4a9e..1f4f8fe1ae 100644 --- a/packages/tests/tests/relay/publish.node.spec.ts +++ b/packages/tests/tests/relay/publish.node.spec.ts @@ -1,6 +1,6 @@ import { createEncoder } from "@waku/core"; import { IRateLimitProof, RelayNode, SendError } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/relay/subscribe.node.spec.ts b/packages/tests/tests/relay/subscribe.node.spec.ts index bb67659cf3..324820bf89 100644 --- a/packages/tests/tests/relay/subscribe.node.spec.ts +++ b/packages/tests/tests/relay/subscribe.node.spec.ts @@ -1,6 +1,6 @@ import { createDecoder, createEncoder } from "@waku/core"; import { DefaultPubsubTopic, RelayNode } from "@waku/interfaces"; -import { createRelayNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; diff --git a/packages/tests/tests/wait_for_remote_peer.node.spec.ts b/packages/tests/tests/wait_for_remote_peer.node.spec.ts index 586d2574c0..3ca2263d94 100644 --- a/packages/tests/tests/wait_for_remote_peer.node.spec.ts +++ b/packages/tests/tests/wait_for_remote_peer.node.spec.ts @@ -1,7 +1,8 @@ import { waitForRemotePeer } from "@waku/core"; import type { LightNode, RelayNode } from "@waku/interfaces"; import { DefaultPubsubTopic, Protocols } from "@waku/interfaces"; -import { createLightNode, createRelayNode } from "@waku/sdk"; +import { createLightNode } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { expect } from "chai"; import { diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index e7aa69f176..bfd812517d 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -14,9 +14,9 @@ import { } from "@waku/message-encryption/symmetric"; import { createLightNode, - createEncoder as createPlainEncoder, - createRelayNode + createEncoder as createPlainEncoder } from "@waku/sdk"; +import { createRelayNode } from "@waku/sdk/relay"; import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; From 51c3d138766bca6eb2c1d2b6abe3c697fb12215a Mon Sep 17 00:00:00 2001 From: danisharora099 Date: Thu, 11 Jan 2024 18:59:25 +0530 Subject: [PATCH 3/3] fix: build --- packages/sdk/src/relay/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sdk/src/relay/index.ts b/packages/sdk/src/relay/index.ts index d88adbac13..d02b989d93 100644 --- a/packages/sdk/src/relay/index.ts +++ b/packages/sdk/src/relay/index.ts @@ -37,6 +37,7 @@ export async function createRelayNode( return new WakuNode( options, + options.pubsubTopics, libp2p, options.shardInfo, undefined,