Waku v2 Protocol TypeScript Implementation
repository·master·Indexed 41 days ago
https://github.com/logos-messaging/logos-delivery-jsA TypeScript implementation of the Waku v2 protocol for decentralized messaging and data delivery. Includes the @waku/sdk for node management, @waku/message-encryption for symmetric and ECIES asymmetric encryption, @waku/rln for Rate Limiting Nullifiers, and @waku/run for spinning up local isolated networks via Docker.
What's inside Waku
- js-waku is a TypeScript implementation of the Waku v2 protocol. It provides the core messaging and delivery capabilities required to interact with the Waku network.
Overview of Scalable Data Sync (SDS)
masterScalable Data Sync (SDS) is a TypeScript implementation of the Scalable Data Sync protocol. It is designed to provide message reliability for distributed logs within browser environments.Install @waku/rln via npm
masterTo use the Rate Limiting Nullifier (RLN) implementation in your Waku project, install the
@waku/rlnpackage using npm.npm install @waku/rlnRun reliability and longevity tests
masterThe
@waku/reliability-testspackage provides tests designed to identify edge cases, memory leaks, network inconsistencies, or message delivery issues by running realistic message scenarios in loops over extended periods.Longevity Test (
longevity.spec.ts)This test runs a
js-waku<->nwakufilter scenario continuously for 2 hours. It monitors:- Message ID
- Timestamp
- Send/receive status
- Transmission errors
A summary report is printed at the end of the run, and any errors encountered during the 2-hour window will cause the test to fail.
# From the project root npm run test:longevityQuick Start: Build and Run Waku Browser Tests
masterTo set up the Waku browser test environment locally, follow these steps to build the application, start the server, or run it via Docker.
Local Execution
- Build the project:
npm run build - Start the server on port 8080:
npm run start:server
Docker Execution
Use Docker to run a containerized simulation of a Waku light node:
- Build the image:
npm run docker:build - Run the container:
docker run -p 8080:8080 waku-browser-tests:local
# Build the application npm run build # Start the server (port 8080) npm run start:server # Build and run Docker container npm run docker:build docker run -p 8080:8080 waku-browser-tests:local- Build the project:
Generate Smart Contract TypeScript bindings
masterThe
@waku/rlnpackage useswagmito generate TypeScript bindings for interacting with RLN smart contracts. While these typings are typically bundled, you must manually regenerate them if the underlyingwaku-rlnv2-contractrepository has been updated.npm run setup:contract-abiRun Waku Browser Tests with Docker Configuration
masterWhen using Docker, you can pass configuration via environment variables using the
-eflag.# Run with ENR bootstrap docker run -p 8080:8080 \ -e WAKU_ENR_BOOTSTRAP="enr:-QEnuE..." \ -e WAKU_CLUSTER_ID="1" \ waku-browser-tests:local # Run with specific configuration (Static Sharding) docker run -p 8080:8080 \ -e WAKU_CLUSTER_ID="2" \ -e WAKU_SHARD="0" \ waku-browser-tests:localUse symmetric encryption with @waku/message-encryption
masterSymmetric encryption uses a single shared key for both encrypting and decrypting messages. Use
generateSymmetricKeyto create a new key,createEncoderwith that key to encrypt messages for sending, andcreateDecoderwith the same key to decrypt incoming messages.This is useful when two or more parties already share a secret key.
import { createDecoder, createEncoder, generateSymmetricKey } from "@waku/message-encryption/symmetric"; // Generate a random key const key = generateSymmetricKey(); // To send messages, create an encoder const encoder = createEncoder(contentTopic, key); // For example waku.lightPush.push(encoder, { payload }); // To receive messages, create a decoder const decoder = createDecoder(contentTopic, key); // For example await waku.store.queryOrderedCallback([decoder], (msg) => { // ... });Quick Start with @waku/run
masterUse
@waku/runto spin up a local, isolated Waku network for development, hackathons, or offline testing. This provides 2nwakunodes connected to each other with a PostgreSQL database for message persistence.Prerequisites
- Docker Desktop or Docker Engine with the Compose plugin must be installed.
1. Start the Network
Run the following command to start the nodes and database in detached mode. The command will output the connection information (bootstrap peers) required to connect your application.
npx @waku/run start2. Connect Your js-waku App
Copy the connection configuration provided in the terminal output and use it when initializing your node (e.g., using
createLightNodefrom@waku/sdk).import { createLightNode } from "@waku/sdk"; const waku = await createLightNode({ defaultBootstrap: false, bootstrapPeers: [ "/ip4/127.0.0.1/tcp/60000/ws/p2p/16Uiu2HAmF6oAsd23RMAnZb3NJgxXrExxBTPMdEoih232iAZkviU2", "/ip4/127.0.0.1/tcp/60001/ws/p2p/16Uiu2HAm5aZU47YkiUoARqivbCXwuFPzFFXXiURAorySqAQbL6EQ" ], numPeersToUse: 2, libp2p: { filterMultiaddrs: false }, networkConfig: { clusterId: 0, numShardsInCluster: 8 } }); await waku.start();3. Stop the Network
When finished, clean up the network and containers using:
npx @waku/run stopRun Waku Browser Test Suites
masterThe package includes several test suites for verifying server functionality, integration, and end-to-end flows.
Test Commands
npm test: Runs basic server functionality tests.npm run test:server: Runs server-only tests (server.spec.ts).npm run test:e2e: Runs end-to-end tests (e2e.spec.ts) usingnwakunodes.npm run test:integration: Runs integration tests (integration.spec.ts) involving Docker container integration.npm run test:all: Runs all available tests.
Generate API documentation locally
masterTo generate the API documentation on your local machine, clone the repository, install dependencies, and run the documentation build command.
Note: You can also use
nix developto set up the development environment before running these commands.# Using standard npm git clone https://github.com/waku-org/js-waku.git cd js-waku npm install npm run doc # Using Nix shell git clone https://github.com/waku-org/js-waku.git cd js-waku nix develop npm install npm run docUse ECIES encryption with @waku/message-encryption
masterECIES (Elliptic Curve Integrated Encryption Scheme) allows for asymmetric encryption. You can encrypt messages using a recipient's public key, and only the holder of the corresponding private key can decrypt them.
Workflow:
- Generate a private key using
generatePrivateKey. - Derive the public key using
getPublicKey(privateKey)and share it with the sender. - The sender uses
createEncoder(contentTopic, publicKey)to encrypt. - The recipient uses
createDecoder(contentTopic, privateKey)to decrypt.
import { createDecoder, createEncoder, generatePrivateKey, getPublicKey } from "@waku/message-encryption/ecies"; // Generate a random private key const privateKey = generatePrivateKey(); // Keep the private key secure, provide the public key to the sender const publicKey = getPublicKey(privateKey); // To send messages, create an encoder const encoder = createEncoder(contentTopic, publicKey); // For example waku.lightPush.push(encoder, { payload }); // To receive messages, create a decoder const decoder = createDecoder(contentTopic, privateKey); // For example await waku.store.queryOrderedCallback([decoder], (msg) => { // ... });- Generate a private key using