|
| 1 | +--- |
| 2 | +title: Nim |
| 3 | +hide_table_of_contents: true |
| 4 | +displayed_sidebar: build |
| 5 | +--- |
| 6 | + |
| 7 | +Nwaku provides a native Nim implementation of the Waku protocol, allowing developers to build Waku native applications directly in Nim. |
| 8 | +This guide demonstrates how to use the Waku library API to create and configure Waku nodes. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +To use the Waku Nim SDK, you'll need to: |
| 13 | + |
| 14 | +1. Install [Nim](https://nim-lang.org/install.html) (version 2.2.4 or higher) |
| 15 | +2. Add the nwaku library to your project |
| 16 | + |
| 17 | +:::warning |
| 18 | +Waku does not currently work with Nimble package manager. You can track progress on Nimble support in [PR #3564](https://github.com/waku-org/nwaku/pull/3564). |
| 19 | +::: |
| 20 | + |
| 21 | +```bash |
| 22 | +# Install nwaku as a dependency |
| 23 | +nimble install waku |
| 24 | +``` |
| 25 | + |
| 26 | +## Creating a Waku Node |
| 27 | + |
| 28 | +Waku uses blockchain smart contract to DoS protect nodes in the network. |
| 29 | +The protocol is called [RLN relay](/learn/concepts/protocols#rln-relay). |
| 30 | + |
| 31 | +To connect to the Waku Network, you'll need a Linea Sepolia RPC endpoint. |
| 32 | + |
| 33 | +Let's create a simple CLI that takes an RPC URL as argument: |
| 34 | + |
| 35 | +`your_waku_app.nim` |
| 36 | +```nim |
| 37 | +import std/options |
| 38 | +import chronos, results, confutils, confutils/defs |
| 39 | +import waku |
| 40 | +
|
| 41 | +type CliArgs = object |
| 42 | + ethRpcEndpoint* {. |
| 43 | + defaultValue: "", |
| 44 | + desc: "ETH RPC Endpoint for RLN support" |
| 45 | + .}: string |
| 46 | +
|
| 47 | +when isMainModule: |
| 48 | + let args = CliArgs.load() |
| 49 | +
|
| 50 | + if args.ethRpcEndpoint == "": |
| 51 | + echo "Please provide a Linea Sepolia RPC endpoint to connect to the Waku Network" |
| 52 | + quit(QuitFailure) |
| 53 | +
|
| 54 | + echo "Starting Waku node with RLN..." |
| 55 | +
|
| 56 | + # Create configuration with RLN enabled |
| 57 | + let config = NodeConfig.init( |
| 58 | + ethRpcEndpoints = @[args.ethRpcEndpoint] |
| 59 | + ) |
| 60 | +
|
| 61 | + # Create the node |
| 62 | + let node = (waitFor createNode(config)).valueOr: |
| 63 | + echo "Failed to create node: ", error |
| 64 | + quit(QuitFailure) |
| 65 | +
|
| 66 | + echo "Waku node created successfully!" |
| 67 | +
|
| 68 | + # Start the node |
| 69 | + (waitFor startWaku(addr node)).isOkOr: |
| 70 | + echo "Failed to start node: ", error |
| 71 | + quit(QuitFailure) |
| 72 | +
|
| 73 | + echo "Node started successfully with RLN enabled!" |
| 74 | + runForever() |
| 75 | +``` |
| 76 | + |
| 77 | +### Running the CLI |
| 78 | + |
| 79 | +To run your CLI: |
| 80 | + |
| 81 | +```bash |
| 82 | +nim c -r your_waku_app.nim --ethRpcEndpoint="https://linea-sepolia.infura.io/v3/some-api-key" |
| 83 | +``` |
| 84 | + |
| 85 | +## Blockchain-less Development Mode |
| 86 | + |
| 87 | +For development and testing purposes, you can create a Waku node without blockchain integration. |
| 88 | +This mode wil not have DoS protection but allows you to experiment with the Waku protocol without needing an RPC endpoint. |
| 89 | + |
| 90 | +`your_waku_app.nim` |
| 91 | +```nim |
| 92 | +import std/options |
| 93 | +import chronos, results |
| 94 | +import waku |
| 95 | +
|
| 96 | +when isMainModule: |
| 97 | + echo "Starting Waku node in development mode..." |
| 98 | +
|
| 99 | + # Create a basic configuration without RLN |
| 100 | + let config = NodeConfig.init( |
| 101 | + wakuConfig = WakuConfig.init( |
| 102 | + entryNodes = @[], # Add ENRs of bootstrap nodes if needed |
| 103 | + clusterId = 42 # Use a custom cluster ID for your test network |
| 104 | + ) |
| 105 | + ) |
| 106 | +
|
| 107 | + # Create the node |
| 108 | + let node = (waitFor createNode(config)).valueOr: |
| 109 | + echo "Failed to create node: ", error |
| 110 | + quit(QuitFailure) |
| 111 | +
|
| 112 | + echo "Waku node created successfully!" |
| 113 | +
|
| 114 | + # Start the node |
| 115 | + (waitFor startWaku(addr node)).isOkOr: |
| 116 | + echo "Failed to start node: ", error |
| 117 | + quit(QuitFailure) |
| 118 | +
|
| 119 | + echo "Node started in development mode!" |
| 120 | + runForever() |
| 121 | +``` |
| 122 | + |
| 123 | +### Running in Development Mode |
| 124 | + |
| 125 | +```bash |
| 126 | +nim c -r your_waku_app.nimyour_waku_app.nim |
| 127 | +``` |
| 128 | + |
| 129 | +## Configuration Options |
| 130 | + |
| 131 | +### With Custom Bootstrap Nodes |
| 132 | + |
| 133 | +You can specify bootstrap nodes to connect to specific networks: |
| 134 | + |
| 135 | +```nim |
| 136 | +let config = NodeConfig.init( |
| 137 | + wakuConfig = WakuConfig.init( |
| 138 | + entryNodes = @[ |
| 139 | + "enr:-P-4QG_d...", # Replace with actual ENR |
| 140 | + "/ip5/1.2..." # Replace with actual multiaddr |
| 141 | + ], |
| 142 | + clusterId = 42 |
| 143 | + ) |
| 144 | +) |
| 145 | +``` |
| 146 | + |
| 147 | +## API Status |
| 148 | + |
| 149 | +:::info |
| 150 | +The Waku Nim library API is under active development. Currently, node creation and configuration are supported. Additional API verbs for message handling, subscriptions, and protocol interactions are work in progress. |
| 151 | +::: |
| 152 | + |
| 153 | +## Examples and Resources |
| 154 | + |
| 155 | +- [nwaku examples](https://github.com/waku-org/nwaku/tree/master/examples) - Official examples repository |
| 156 | +- [waku_api.nim](https://github.com/waku-org/nwaku/blob/master/examples/waku_api.nim) - Library API example |
| 157 | +- [nwaku repo](https://github.com/waku-org/nwaku) - nwaku GitHub Repository |
| 158 | + |
| 159 | +## Get Help |
| 160 | + |
| 161 | +- Visit the #help-desk channel on [Discord](https://discord.waku.org/) |
| 162 | +- Check the [nwaku GitHub repository](https://github.com/waku-org/nwaku) for issues and updates |
| 163 | +- Review the [protocol documentation](/learn/concepts/protocols) to understand Waku's capabilities |
0 commit comments