|
| 1 | +import { WakuNode, WakuOptions } from "@waku/core"; |
| 2 | +import type { ProtocolCreateOptions, RelayNode } from "@waku/interfaces"; |
| 3 | +import { RelayCreateOptions, wakuGossipSub, wakuRelay } from "@waku/relay"; |
| 4 | + |
| 5 | +import { defaultLibp2p, defaultPeerDiscoveries } from "../create.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Create a Waku node that uses Waku Relay to send and receive messages, |
| 9 | + * enabling some privacy preserving properties. |
| 10 | + * * @remarks |
| 11 | + * This function creates a Relay Node using the Waku Relay protocol. |
| 12 | + * While it is technically possible to use this function in a browser environment, |
| 13 | + * it is not recommended due to potential performance issues and limited browser capabilities. |
| 14 | + * If you are developing a browser-based application, consider alternative approaches like creating a Light Node |
| 15 | + * or use this function with caution. |
| 16 | + */ |
| 17 | +export async function createRelayNode( |
| 18 | + options?: ProtocolCreateOptions & WakuOptions & Partial<RelayCreateOptions> |
| 19 | +): Promise<RelayNode> { |
| 20 | + options = options ?? {}; |
| 21 | + |
| 22 | + const libp2pOptions = options?.libp2p ?? {}; |
| 23 | + const peerDiscovery = libp2pOptions.peerDiscovery ?? []; |
| 24 | + if (options?.defaultBootstrap) { |
| 25 | + peerDiscovery.push(...defaultPeerDiscoveries()); |
| 26 | + Object.assign(libp2pOptions, { peerDiscovery }); |
| 27 | + } |
| 28 | + |
| 29 | + const libp2p = await defaultLibp2p( |
| 30 | + options.shardInfo, |
| 31 | + wakuGossipSub(options), |
| 32 | + libp2pOptions, |
| 33 | + options?.userAgent |
| 34 | + ); |
| 35 | + |
| 36 | + const relay = wakuRelay(options); |
| 37 | + |
| 38 | + return new WakuNode( |
| 39 | + options, |
| 40 | + options.pubsubTopics, |
| 41 | + libp2p, |
| 42 | + options.shardInfo, |
| 43 | + undefined, |
| 44 | + undefined, |
| 45 | + undefined, |
| 46 | + relay |
| 47 | + ) as RelayNode; |
| 48 | +} |
0 commit comments