diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 979fbe345f..7a875413e4 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Dependency on `@waku/peer-exchange`. +### Changed + +- `Filter`, `LightPush` and `Store` classes now takes in `options` of type `ProtocolCreateOptions` as the second argument, instead of `pubSubTopic` +- `Relay` class now takes in `options` of type `Partial` as the second argument, instead of `pubSubTopic` + ## [@waku/core@0.0.10] - 2023-01-25 ### Changed diff --git a/packages/core/src/lib/relay/index.ts b/packages/core/src/lib/relay/index.ts index cf0d2b9116..8768c0209b 100644 --- a/packages/core/src/lib/relay/index.ts +++ b/packages/core/src/lib/relay/index.ts @@ -40,7 +40,7 @@ export type RelayCreateOptions = ProtocolCreateOptions & GossipsubOpts; * @implements {require('libp2p-interfaces/src/pubsub')} */ class Relay extends GossipSub implements IRelay { - options: Partial; + private pubSubTopic: string; defaultDecoder: IDecoder; public static multicodec: string = constants.RelayCodecs[0]; @@ -63,9 +63,9 @@ class Relay extends GossipSub implements IRelay { super(components, options); this.multicodecs = constants.RelayCodecs; - this.observers = new Map(); + this.pubSubTopic = options?.pubSubTopic ?? DefaultPubSubTopic; - this.options = options ?? {}; + this.observers = new Map(); // TODO: User might want to decide what decoder should be used (e.g. for RLN) this.defaultDecoder = new TopicOnlyDecoder(); @@ -79,24 +79,21 @@ class Relay extends GossipSub implements IRelay { * @returns {void} */ public async start(): Promise { - const { pubSubTopic = DefaultPubSubTopic } = this.options; await super.start(); - this.subscribe(pubSubTopic); + this.subscribe(this.pubSubTopic); } /** * Send Waku message. */ public async send(encoder: IEncoder, message: IMessage): Promise { - const { pubSubTopic = DefaultPubSubTopic } = this.options; - const msg = await encoder.toWire(message); if (!msg) { log("Failed to encode message, aborting publish"); return { recipients: [] }; } - return this.publish(pubSubTopic, msg); + return this.publish(this.pubSubTopic, msg); } /** @@ -172,8 +169,7 @@ class Relay extends GossipSub implements IRelay { } getMeshPeers(topic?: TopicStr): PeerIdStr[] { - const { pubSubTopic = DefaultPubSubTopic } = this.options; - return super.getMeshPeers(topic ?? pubSubTopic); + return super.getMeshPeers(topic ?? this.pubSubTopic); } } diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 0e8bf5165a..ecba41723c 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -14,7 +14,13 @@ import { } from "@waku/core"; import { DefaultUserAgent } from "@waku/core"; import { enrTree, wakuDnsDiscovery } from "@waku/dns-discovery"; -import type { FullNode, IRelay, LightNode, RelayNode } from "@waku/interfaces"; +import type { + FullNode, + IRelay, + LightNode, + ProtocolCreateOptions, + RelayNode, +} from "@waku/interfaces"; import { wakuPeerExchange } from "@waku/peer-exchange"; import type { Libp2p } from "libp2p"; import { createLibp2p, Libp2pOptions } from "libp2p"; @@ -29,39 +35,6 @@ const DEFAULT_NODE_REQUIREMENTS = { export { Libp2pComponents }; -export interface CreateOptions { - /** - * The PubSub Topic to use. - * - * One and only one pubsub topic is used by Waku. This is used by: - * - WakuRelay to receive, route and send messages, - * - WakuLightPush to send messages, - * - WakuStore to retrieve messages. - * - * The usage of the default pubsub topic is recommended. - * See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/) for details. - */ - pubSubTopic?: string; - /** - * You can pass options to the `Libp2p` instance used by {@link @waku/core.WakuNode} using the {@link CreateOptions.libp2p} property. - * This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) - * apart that we made the `modules` property optional and partial, - * allowing its omission and letting Waku set good defaults. - * Notes that some values are overridden by {@link @waku/core.WakuNode} to ensure it implements the Waku protocol. - */ - libp2p?: Partial; - /** - * Byte array used as key for the noise protocol used for connection encryption - * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) - * This is only used for test purposes to not run out of entropy during CI runs. - */ - staticNoiseKey?: Uint8Array; - /** - * Use recommended bootstrap method to discovery and connect to new nodes. - */ - defaultBootstrap?: boolean; -} - /** * Create a Waku node that uses Waku Light Push, Filter and Store to send and * receive messages, enabling low resource consumption. @@ -70,7 +43,7 @@ export interface CreateOptions { * @see https://github.com/status-im/nwaku/issues/1085 */ export async function createLightNode( - options?: CreateOptions & WakuOptions + options?: ProtocolCreateOptions & WakuOptions ): Promise { const libp2pOptions = options?.libp2p ?? {}; const peerDiscovery = libp2pOptions.peerDiscovery ?? []; @@ -106,7 +79,7 @@ export async function createLightNode( * enabling some privacy preserving properties. */ export async function createRelayNode( - options?: CreateOptions & WakuOptions & Partial + options?: ProtocolCreateOptions & WakuOptions & Partial ): Promise { const libp2pOptions = options?.libp2p ?? {}; const peerDiscovery = libp2pOptions.peerDiscovery ?? []; @@ -138,7 +111,7 @@ export async function createRelayNode( * @internal */ export async function createFullNode( - options?: CreateOptions & WakuOptions & Partial + options?: ProtocolCreateOptions & WakuOptions & Partial ): Promise { const libp2pOptions = options?.libp2p ?? {}; const peerDiscovery = libp2pOptions.peerDiscovery ?? []; diff --git a/packages/interfaces/src/protocols.ts b/packages/interfaces/src/protocols.ts index 10af541eba..6a94872e44 100644 --- a/packages/interfaces/src/protocols.ts +++ b/packages/interfaces/src/protocols.ts @@ -1,5 +1,6 @@ import type { PeerId } from "@libp2p/interface-peer-id"; import type { Peer, PeerStore } from "@libp2p/interface-peer-store"; +import type { Libp2pOptions } from "libp2p"; import type { IMessage } from "./message.js"; @@ -19,7 +20,7 @@ export interface PointToPointProtocol { export type ProtocolCreateOptions = { /** - * The PubSub Topic to use. Defaults to {@link @waku/core/DefaultPubSubTopic }. + * The PubSub Topic to use. Defaults to {@link @waku/core.DefaultPubSubTopic }. * * One and only one pubsub topic is used by Waku. This is used by: * - WakuRelay to receive, route and send messages, @@ -31,11 +32,26 @@ export type ProtocolCreateOptions = { * */ pubSubTopic?: string; + /** + * You can pass options to the `Libp2p` instance used by {@link @waku/core.WakuNode} using the `libp2p` property. + * This property is the same type as the one passed to [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) + * apart that we made the `modules` property optional and partial, + * allowing its omission and letting Waku set good defaults. + * Notes that some values are overridden by {@link @waku/core.WakuNode} to ensure it implements the Waku protocol. + */ + libp2p?: Partial; + /** + * Byte array used as key for the noise protocol used for connection encryption + * by [`Libp2p.create`](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create) + * This is only used for test purposes to not run out of entropy during CI runs. + */ + staticNoiseKey?: Uint8Array; + /** + * Use recommended bootstrap method to discovery and connect to new nodes. + */ + defaultBootstrap?: boolean; }; -//TODO -// we can probably move `peerId` into `ProtocolCreateOptions` and remove `ProtocolOptions` and pass it in the constructor -// however, filter protocol can use multiple peers, so we need to think about this export type ProtocolOptions = { /** * Optionally specify an PeerId for the protocol request. If not included, will use a random peer. diff --git a/packages/message-encryption/CHANGELOG.md b/packages/message-encryption/CHANGELOG.md index 0cc50bed22..f000fbc844 100644 --- a/packages/message-encryption/CHANGELOG.md +++ b/packages/message-encryption/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- `createEncoder` now take an object of type `EncoderOptions` instead of `contentTopic` and `ephemeral` +- For Ecies, `createEncoder` now take an object of type `EncoderOptions` instead of `contentTopic`, `ephemeral`, `publicKey` and `sigPrivKey` +- For Symmetric, `createEncoder` now take an object of type `EncoderOptions` instead of `contentTopic`, `ephemeral`, `symKey` and `sigPrivKey` + ## [0.0.9] - 2023-01-25 ### Fixed diff --git a/packages/message-encryption/src/ecies.ts b/packages/message-encryption/src/ecies.ts index e1f2e5fe51..b887158b52 100644 --- a/packages/message-encryption/src/ecies.ts +++ b/packages/message-encryption/src/ecies.ts @@ -1,6 +1,6 @@ import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0"; import type { - EncoderOptions, + EncoderOptions as BaseEncoderOptions, IDecoder, IEncoder, IMessage, @@ -64,7 +64,7 @@ export class Encoder implements IEncoder { } } -export interface EciesEncoderOptions extends EncoderOptions { +export interface EncoderOptions extends BaseEncoderOptions { /** The public key to encrypt the payload for. */ publicKey: Uint8Array; /** An optional private key to be used to sign the payload before encryption. */ @@ -88,7 +88,7 @@ export function createEncoder({ publicKey, sigPrivKey, ephemeral = false, -}: EciesEncoderOptions): Encoder { +}: EncoderOptions): Encoder { return new Encoder(contentTopic, publicKey, sigPrivKey, ephemeral); } diff --git a/packages/message-encryption/src/symmetric.ts b/packages/message-encryption/src/symmetric.ts index fbd3cf84e8..aae6cb7887 100644 --- a/packages/message-encryption/src/symmetric.ts +++ b/packages/message-encryption/src/symmetric.ts @@ -1,6 +1,6 @@ import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0"; import type { - EncoderOptions, + EncoderOptions as BaseEncoderOptions, IDecoder, IEncoder, IMessage, @@ -62,7 +62,7 @@ export class Encoder implements IEncoder { } } -export interface SymmetricEncoderOptions extends EncoderOptions { +export interface EncoderOptions extends BaseEncoderOptions { /** The symmetric key to encrypt the payload with. */ symKey: Uint8Array; /** An optional private key to be used to sign the payload before encryption. */ @@ -87,7 +87,7 @@ export function createEncoder({ symKey, sigPrivKey, ephemeral = false, -}: SymmetricEncoderOptions): Encoder { +}: EncoderOptions): Encoder { return new Encoder(contentTopic, symKey, sigPrivKey, ephemeral); }