Waku v2 Protocol TypeScript Implementation

repository·master·Indexed 41 days ago

https://github.com/logos-messaging/logos-delivery-js

A 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.

Tokens
59.1K
Snippets
218
Records
266
Agent score
52%

What's inside Waku

  1. Run reliability and longevity tests

    master

    The @waku/reliability-tests package 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 <-> nwaku filter 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:longevity
  2. Quick Start: Build and Run Waku Browser Tests

    master

    To 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

    1. Build the project: npm run build
    2. Start the server on port 8080: npm run start:server

    Docker Execution

    Use Docker to run a containerized simulation of a Waku light node:

    1. Build the image: npm run docker:build
    2. 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
  3. Run Waku Browser Tests with Docker Configuration

    master

    When using Docker, you can pass configuration via environment variables using the -e flag.

    # 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:local
  4. Use symmetric encryption with @waku/message-encryption

    master

    Symmetric encryption uses a single shared key for both encrypting and decrypting messages. Use generateSymmetricKey to create a new key, createEncoder with that key to encrypt messages for sending, and createDecoder with 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) => {
      // ...
    });
  5. Quick Start with @waku/run

    master

    Use @waku/run to spin up a local, isolated Waku network for development, hackathons, or offline testing. This provides 2 nwaku nodes 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 start

    2. Connect Your js-waku App

    Copy the connection configuration provided in the terminal output and use it when initializing your node (e.g., using createLightNode from @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 stop
  6. Run Waku Browser Test Suites

    master

    The 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) using nwaku nodes.
    • npm run test:integration: Runs integration tests (integration.spec.ts) involving Docker container integration.
    • npm run test:all: Runs all available tests.
  7. Generate API documentation locally

    master

    To 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 develop to 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 doc
  8. Use ECIES encryption with @waku/message-encryption

    master

    ECIES (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:

    1. Generate a private key using generatePrivateKey.
    2. Derive the public key using getPublicKey(privateKey) and share it with the sender.
    3. The sender uses createEncoder(contentTopic, publicKey) to encrypt.
    4. 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) => {
      // ...
    });