From 8a7a562c9b9248eefac9fa4d650e7230e7effe46 Mon Sep 17 00:00:00 2001 From: Maksym H Date: Sun, 15 Nov 2026 13:13:23 +0000 Subject: [PATCH 1/6] Serve DUB's issue-with-proof TURN route for the products a network lists --- config/ports.env | 5 ++ docs/DEVICE-UNIQUENESS-BACKEND.md | 3 +- docs/TURN.md | 13 +++ networks/README.md | 2 +- networks/previewnet.json | 4 + packages/cli/src/commands/turn.ts | 51 ++++++++++- packages/cli/tests/turn-proof.test.ts | 86 +++++++++++++++++++ packages/network-config/src/dub.ts | 12 ++- packages/network-config/src/index.ts | 2 +- packages/network-config/src/networks.ts | 18 +++- packages/network-config/tests/dub.test.ts | 12 +++ .../network-config/tests/networks.test.ts | 33 +++++++ scripts/dub/service.sh | 5 +- 13 files changed, 234 insertions(+), 12 deletions(-) create mode 100644 packages/cli/tests/turn-proof.test.ts diff --git a/config/ports.env b/config/ports.env index b445948..22d138c 100644 --- a/config/ports.env +++ b/config/ports.env @@ -133,6 +133,11 @@ PPN_STACK_EXPIRES_AT= PPN_STACK_VALID_FOR= PPN_STACK_REFRESH_SECONDS= +# The dotNS products whose personhood proofs buy TURN credentials at the backend's +# /api/v1/turn/issue-with-proof, comma-separated, such as dim2.testnet. Empty uses the network +# file's services.dub.turnProofProducts. Here for the same reason as PPN_PUBLIC_URL. +PPN_TURN_PROOF_PRODUCTS= + # This instance's name, replacing the network file's displayName in the dashboard and the # Trinity stack descriptor, so instances of one network can be told apart: "Previewnet Staging". PPN_DISPLAY_NAME= diff --git a/docs/DEVICE-UNIQUENESS-BACKEND.md b/docs/DEVICE-UNIQUENESS-BACKEND.md index 7daf4e8..15f6c1b 100644 --- a/docs/DEVICE-UNIQUENESS-BACKEND.md +++ b/docs/DEVICE-UNIQUENESS-BACKEND.md @@ -262,7 +262,8 @@ And the secrets, in `scripts/dub/service.sh` rather than the TOML (see `docs/PROFILES.md`): `CHAIN_WRITER_SIGNER_SURI`, `INVITER_SIGNER_SURI`, `JWT_ED25519_SECRET` with `JWT_ED25519_PUBLIC_KEY` derived from it, and `TURN_SECRET` with `ICE_SERVERS`, which `ppn service dub-turn-env` resolves together with the eturnal config -(see [TURN.md](TURN.md)). +(see [TURN.md](TURN.md)). It also resolves the `TURN_PROOF_*` settings, which are not secret +but need the People chain's genesis hash. The rest stay dormant under the dev defaults: `PAYMENT_MASTER_ACCOUNT` / `PAYMENT_AMOUNT_PLANCK` are only read when `PAYMENT_LANE_ENABLED=true` (default false, diff --git a/docs/TURN.md b/docs/TURN.md index 744b213..cb611a0 100644 --- a/docs/TURN.md +++ b/docs/TURN.md @@ -11,6 +11,19 @@ whose `turn-api` mints the short-lived credentials the relay accepts. `{ servers, username, password, ttl }`. 3. Pass `servers` as the ICE server URLs, with `username` and `password` as the credential. +A product without a DUB sign-in, such as Jollity, sends a personhood proof instead: +`POST /dub/api/v1/turn/issue-with-proof` with a ring-VRF proof made under its own product id. +turn-api serves this route only for the products it is told to accept: + +- `services.dub.turnProofProducts` in the network file, such as `["dim2.testnet"]` on + previewnet. +- `PPN_TURN_PROOF_PRODUCTS` in `config/ports.env`, comma-separated, which replaces that list for + one deployment. + +With neither, the route answers 404. The proofs are verified against the network's own People +chain: `ppn service dub-turn-env` reads its genesis hash when `dub-api` starts, and waits up to +10 minutes for People to answer. + Clients never configure a password themselves. The dashboard lists the same servers under **TURN / STUN**, and so does `/api/network` under `ice`. diff --git a/networks/README.md b/networks/README.md index e18ced3..74da31c 100644 --- a/networks/README.md +++ b/networks/README.md @@ -40,7 +40,7 @@ or `PPN_NETWORK=` in the environment of the scripts. | `relay.extraArgs`, `parachains[].extraArgs` | Extra node flags for that chain. A flag the shared per-key table also sets takes this value (`--rpc-max-connections=5000` replaces the table's 500), and a `-l` arg replaces the default log targets whole; the rest are appended. | | `parachains[].rpcNodes`, `parachains[].rpcNodeArgs` | Non-authoring full nodes (0..9) that serve the chain's public path instead of the collator, on the chain's port + 1, + 2, …, with nginx balancing across them. They run the collator's flags without the authoring ones, with `rpcNodeArgs` over them. | | *(network overlay)* | `PPN_NETWORK_OVERLAY=` changes `extraArgs`, `rpcNodes` and `rpcNodeArgs` for one deployment without a release: `{ "previewnet": { "relay": { "extraArgs": [...] }, "parachains": { "people": { "rpcNodes": 2, "rpcNodeArgs": [...] } } } }`. Every command applies it, so the zombienet config, nginx routes and dashboard agree. An unknown network, chain or field is an error. | -| `services` | Per-service switches and binaries: `false` disables a service; `{ "binary": { … } }` says what it runs. `eth-rpc` must match the revive pallet in the network's Asset Hub runtime, which is exactly why it is pinned per network rather than shared. | +| `services` | Per-service switches and binaries: `false` disables a service; `{ "binary": { … } }` says what it runs. `eth-rpc` must match the revive pallet in the network's Asset Hub runtime, which is exactly why it is pinned per network rather than shared. `dub` takes `turnProofProducts`, the dotNS products whose personhood proofs buy TURN credentials (see `docs/TURN.md`). | | `tools` | Local tools this network needs that are neither a chain nor a service — previewnet's `chain-spec-builder`, for instance, which only a genesis network uses. | | `bite.doppelganger` | `{repo, tag}` of the bite tool. Per network, because the build has to be able to execute the runtimes of the network being bitten. | | *(bin layout)* | Only node binaries are network-versioned: previewnet's live in `bin/`, every other network's in `bin//` (plus `dg/` for bites). Shared tooling with no per-network dimension — zombie-cli, ipfs, postgres, identity, design families — lives in plain `bin/` and is pinned in `config/versions.env`. Collators run through `scripts/omni-node.sh` regardless of binary; it carries the libp2p fix and picks binary/dir from `PPN_COLLATOR_BINARY`/`PPN_BIN_DIR`. | diff --git a/networks/previewnet.json b/networks/previewnet.json index c6c69b5..cfb1114 100644 --- a/networks/previewnet.json +++ b/networks/previewnet.json @@ -163,6 +163,10 @@ } ], "services": { + "dub": { + "turnProofProducts": ["dim2.testnet"], + "_turnProofProducts": "Jollity's product account, which proves as dim2. for TURN credentials (paritytech/individuality-apps packages/jollity-protocol/src/config.ts)." + }, "eth-rpc": { "binary": { "name": "eth-rpc", diff --git a/packages/cli/src/commands/turn.ts b/packages/cli/src/commands/turn.ts index dd9a247..9eab5b4 100644 --- a/packages/cli/src/commands/turn.ts +++ b/packages/cli/src/commands/turn.ts @@ -1,15 +1,25 @@ // `ppn service turn`: the TURN/STUN relay (eturnal) whose credentials DUB's turn-api mints. -// `ppn service dub-turn-env`: the matching secret and ICE servers, as shell exports for -// scripts/dub/service.sh. Both resolve from the same inputs, so they cannot disagree. +// `ppn service dub-turn-env`: the matching secret and ICE servers, plus the proof route's +// settings, as shell exports for scripts/dub/service.sh. Both resolve from the same inputs, so +// they cannot disagree. // See docs/TURN.md. import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { spawn, type ChildProcess } from 'node:child_process'; -import { LOCAL_TURN_SECRET, eturnalConfig, iceServers, runsTurnRelay, turnSecret } from '@parity/ppn-network-config'; +import { + LOCAL_TURN_SECRET, + asHttp, + eturnalConfig, + iceServers, + runsTurnRelay, + turnProofProducts, + turnSecret, +} from '@parity/ppn-network-config'; import type { ServiceContext } from './service-context.js'; import { secretsFile } from '../lib/secrets.js'; +import { rpc } from '../fork/rpc.js'; import { ETURNAL_BREW_INSTALL, eturnalCtl, eturnalDist } from '../lib/eturnal.js'; /** The host clients reach the network under; only its hostname and scheme matter here. */ @@ -35,8 +45,41 @@ export function dubTurnEnv(ctx: ServiceContext): Record { }; } +/** + * The People chain's genesis hash, which turn-api's proof route requires as + * TURN_PROOF_GENESIS. Waits up to 10 minutes for the chain's RPC, then fails. + */ +export async function peopleGenesis(rpcUrl: string, attempts = 300, delayMs = 2000): Promise { + for (let attempt = 1; ; attempt++) { + try { + return await rpc(asHttp(rpcUrl), 'chain_getBlockHash', [0]); + } catch (error) { + if (attempt >= attempts) throw new Error(`no genesis hash from ${rpcUrl}: ${(error as Error).message}`); + await new Promise((resolve) => setTimeout(resolve, delayMs)); + } + } +} + +/** + * turn-api's settings for `/api/v1/turn/issue-with-proof`, verified against the local People + * chain: the products from PPN_TURN_PROOF_PRODUCTS, else the network's + * `services.dub.turnProofProducts`. Empty when there are none, which leaves the route off. + */ +export async function turnProofEnv(ctx: ServiceContext): Promise> { + const setting = process.env.PPN_TURN_PROOF_PRODUCTS || ctx.ports.PPN_TURN_PROOF_PRODUCTS; + const products = setting ? setting.split(',').map((p) => p.trim()).filter(Boolean) : turnProofProducts(ctx.net); + if (!products.length) return {}; + const rpcUrl = `ws://127.0.0.1:${ctx.ports.PEOPLE_PORT}`; + return { + TURN_PROOF_ENABLED: 'true', + TURN_PROOF_RPC_URL: rpcUrl, + TURN_PROOF_PRODUCTS: products.join(','), + TURN_PROOF_GENESIS: await peopleGenesis(rpcUrl), + }; +} + export async function printDubTurnEnv(ctx: ServiceContext): Promise { - for (const [k, v] of Object.entries(dubTurnEnv(ctx))) { + for (const [k, v] of Object.entries({ ...dubTurnEnv(ctx), ...(await turnProofEnv(ctx)) })) { console.log(`export ${k}='${v.replace(/'/g, `'\\''`)}'`); } } diff --git a/packages/cli/tests/turn-proof.test.ts b/packages/cli/tests/turn-proof.test.ts new file mode 100644 index 0000000..03fbb8f --- /dev/null +++ b/packages/cli/tests/turn-proof.test.ts @@ -0,0 +1,86 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import http from 'node:http'; +import type { AddressInfo } from 'node:net'; + +import { loadNetwork } from '@parity/ppn-network-config'; +import { peopleGenesis, turnProofEnv } from '../src/commands/turn.js'; +import type { ServiceContext } from '../src/commands/service-context.js'; + +const GENESIS = `0x${'ab'.repeat(32)}`; + +/** A People RPC that fails `failures` requests before answering chain_getBlockHash(0). */ +async function people(failures: number): Promise<{ url: string; port: number; close: () => void }> { + let seen = 0; + const server = http.createServer((req, res) => { + let body = ''; + req.on('data', (chunk) => (body += chunk)); + req.on('end', () => { + if (seen++ < failures) return res.destroy(); + const { method, params } = JSON.parse(body); + assert.deepEqual([method, params], ['chain_getBlockHash', [0]]); + res.setHeader('content-type', 'application/json'); + res.end(JSON.stringify({ jsonrpc: '2.0', id: 1, result: GENESIS })); + }); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + const port = (server.address() as AddressInfo).port; + return { url: `ws://127.0.0.1:${port}`, port, close: () => server.close() }; +} + +describe('peopleGenesis', () => { + // dub-api starts beside the chains, so People may not answer yet; turn-api refuses to boot + // with proof issuance on and no genesis. + it('waits for People to answer', async () => { + const rpc = await people(2); + try { + assert.equal(await peopleGenesis(rpc.url, 5, 10), GENESIS); + } finally { + rpc.close(); + } + }); + + it('fails once People has not answered in time', async () => { + const rpc = await people(10); + try { + await assert.rejects(peopleGenesis(rpc.url, 3, 10), /no genesis hash from ws:\/\/127\.0\.0\.1/); + } finally { + rpc.close(); + } + }); +}); + +describe('turnProofEnv', () => { + const ctx = (network: string, ports: Record) => + ({ net: loadNetwork(network), ports }) as unknown as ServiceContext; + + // Jollity proves as dim2.testnet on previewnet; without these settings turn-api answers its + // issue-with-proof call with a 404. + it("enables the proof route for the network file's products, against local People", async () => { + const rpc = await people(0); + try { + assert.deepEqual(await turnProofEnv(ctx('previewnet', { PEOPLE_PORT: String(rpc.port), PPN_TURN_PROOF_PRODUCTS: '' })), { + TURN_PROOF_ENABLED: 'true', + TURN_PROOF_RPC_URL: rpc.url, + TURN_PROOF_PRODUCTS: 'dim2.testnet', + TURN_PROOF_GENESIS: GENESIS, + }); + } finally { + rpc.close(); + } + }); + + it('takes the products a deployment sets in ports.env over the network file', async () => { + const rpc = await people(0); + try { + const env = await turnProofEnv(ctx('previewnet', { PEOPLE_PORT: String(rpc.port), PPN_TURN_PROOF_PRODUCTS: 'dim2.testnet, vox.testnet' })); + assert.equal(env.TURN_PROOF_PRODUCTS, 'dim2.testnet,vox.testnet'); + } finally { + rpc.close(); + } + }); + + it('leaves the route off for a network without products', async () => { + assert.deepEqual(await turnProofEnv(ctx('polkadot', { PEOPLE_PORT: '1' })), {}); + }); +}); diff --git a/packages/network-config/src/dub.ts b/packages/network-config/src/dub.ts index d582245..bc7d387 100644 --- a/packages/network-config/src/dub.ts +++ b/packages/network-config/src/dub.ts @@ -26,6 +26,8 @@ // deployable profile already draws: public identifiers at generate time, // private keys at runtime only. +import type { NetworkDef } from './networks.js'; + /** Well-known dev account. Also what PPN grants attestation allowance to. */ export const ALICE_SS58 = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; @@ -176,8 +178,8 @@ export function dubServices( ...common, BIND_ADDR: `127.0.0.1:${ports.gateway}`, // Credentials are HMAC-signed for our own eturnal relay, not minted by Cloudflare - // (turn-api's default). The realm is public, so it belongs here; the signing secret - // and ICE_SERVERS depend on the host, so service.sh supplies them. + // (turn-api's default). The realm is public, so it belongs here; the signing secret, + // ICE_SERVERS and the TURN_PROOF_* settings depend on the host, so service.sh supplies them. TURN_PROVIDER: 'coturn', TURN_REALM, }, @@ -198,6 +200,12 @@ export function dubServices( ]; } +/** The products `services.dub.turnProofProducts` names, or none. */ +export function turnProofProducts(net: NetworkDef): string[] { + const dub = net.services['dub']; + return typeof dub === 'object' ? (dub.turnProofProducts ?? []) : []; +} + function tomlEnv(env: Record): string { const entries = Object.entries(env).map( ([name, value]) => ` { name = "${name}", value = "${value}" },` diff --git a/packages/network-config/src/index.ts b/packages/network-config/src/index.ts index 7a4b9d2..adc441d 100644 --- a/packages/network-config/src/index.ts +++ b/packages/network-config/src/index.ts @@ -57,7 +57,7 @@ export { hrmpChannels } from './toml-generator.js'; export type { HrmpChannel } from './toml-generator.js'; export { generateForkToml, FORK_PROCESSES } from './fork-toml.js'; export type { GenerateForkTomlOptions } from './fork-toml.js'; -export { dubCustomProcesses, ALICE_SS58, BOB_SS58, TURN_REALM } from './dub.js'; +export { dubCustomProcesses, turnProofProducts, ALICE_SS58, BOB_SS58, TURN_REALM } from './dub.js'; export { iceServers, runsTurnRelay, diff --git a/packages/network-config/src/networks.ts b/packages/network-config/src/networks.ts index 65e9f01..9f83747 100644 --- a/packages/network-config/src/networks.ts +++ b/packages/network-config/src/networks.ts @@ -151,7 +151,16 @@ export type NetworkChain = ChainFields & { }; /** false disables a service; an object configures it (its binary, when it has one). */ -export type ServiceConfig = boolean | { binary?: BinaryRef }; +export type ServiceConfig = + | boolean + | { + binary?: BinaryRef; + /** + * `dub` only: the dotNS products whose personhood proofs buy TURN credentials at + * `/api/v1/turn/issue-with-proof`, such as `dim2.testnet`. Without it the route is absent. + */ + turnProofProducts?: string[]; + }; /** * A Trinity stack descriptor's facts about the live network that the rest of the file does @@ -428,6 +437,13 @@ export function loadDescriptor(name: string): NetworkDef { const services: Record = raw.services ?? {}; for (const [svc, cfg] of Object.entries(services)) { if (cfg && typeof cfg === 'object' && cfg.binary) checkBinary(`services."${svc}"`, cfg.binary); + const products = cfg && typeof cfg === 'object' ? cfg.turnProofProducts : undefined; + if (products !== undefined) { + if (svc !== 'dub') bad(`services."${svc}".turnProofProducts: only dub issues TURN credentials`); + if (!Array.isArray(products) || !products.length || !products.every((p) => /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(p))) { + bad('services."dub".turnProofProducts must be a non-empty list of dotNS names, such as "dim2.testnet"'); + } + } } const tools: Record = Object.fromEntries( Object.entries(raw.tools ?? {}).filter(([k]) => !k.startsWith('_')) diff --git a/packages/network-config/tests/dub.test.ts b/packages/network-config/tests/dub.test.ts index 8006bec..d7ed292 100644 --- a/packages/network-config/tests/dub.test.ts +++ b/packages/network-config/tests/dub.test.ts @@ -11,7 +11,9 @@ import { dubDatabaseUrl, dubServices, peopleRpcUrl, + turnProofProducts, } from '../src/dub.js'; +import { loadNetwork } from '../src/load.js'; const PORTS = { postgres: 5433, people: 10010, assetHub: 10020, gateway: 8092 }; @@ -170,6 +172,16 @@ describe('dubServices', () => { }); }); +describe('turnProofProducts', () => { + it('accepts Jollity on previewnet', () => { + assert.deepEqual(turnProofProducts(loadNetwork('previewnet')), ['dim2.testnet']); + }); + + it('is empty for a network that lists none', () => { + assert.deepEqual(turnProofProducts(loadNetwork('polkadot')), []); + }); +}); + describe('dubCustomProcesses', () => { const toml = dubCustomProcesses(PORTS); diff --git a/packages/network-config/tests/networks.test.ts b/packages/network-config/tests/networks.test.ts index 8cf3839..7fd0ac9 100644 --- a/packages/network-config/tests/networks.test.ts +++ b/packages/network-config/tests/networks.test.ts @@ -3,6 +3,9 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; import { listNetworks, currentNetworkName, @@ -16,6 +19,7 @@ import { DEFAULT_NETWORK, } from '../src/networks.js'; import { loadNetwork } from '../src/load.js'; +import { repoRoot } from '../src/repo-root.js'; import { pinsProducts } from '../src/fork-toml.js'; import { paraIds, VALID_PARACHAINS } from '../src/toml-generator.js'; @@ -281,3 +285,32 @@ describe('product import', () => { assert.equal(pinsProducts(loadNetwork('devnet')), false); }); }); + +describe('services.dub.turnProofProducts', () => { + function loadWith(services: Record): () => unknown { + return () => { + const saved = process.env.PPN_HOME; + const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ppn-home-')); + fs.mkdirSync(path.join(home, 'networks')); + const raw = JSON.parse(fs.readFileSync(path.join(repoRoot(), 'networks/previewnet.json'), 'utf-8')); + fs.writeFileSync(path.join(home, 'networks', 'proof-test.json'), JSON.stringify({ ...raw, name: 'proof-test', services })); + process.env.PPN_HOME = home; + try { + return loadNetwork('proof-test'); + } finally { + if (saved === undefined) delete process.env.PPN_HOME; + else process.env.PPN_HOME = saved; + fs.rmSync(home, { recursive: true, force: true }); + } + }; + } + + it('rejects a product that is not a dotNS name', () => { + assert.throws(loadWith({ dub: { turnProofProducts: ['dim2'] } }), /dotNS names/); + assert.throws(loadWith({ dub: { turnProofProducts: [] } }), /dotNS names/); + }); + + it('rejects products on a service other than dub', () => { + assert.throws(loadWith({ 'eth-rpc': { turnProofProducts: ['dim2.testnet'] } }), /only dub/); + }); +}); diff --git a/scripts/dub/service.sh b/scripts/dub/service.sh index d929ece..fff5874 100755 --- a/scripts/dub/service.sh +++ b/scripts/dub/service.sh @@ -112,12 +112,13 @@ export CHAIN_WRITER_SIGNER_SURI="${PPN_DUB_ATTESTER_URI:-${PPN_IDENTITY_ATTESTER # signing into a proxy call that People Chain has no registration for. export INVITER_SIGNER_SURI="${PPN_INVITER_SURI:-//Bob}" -# turn-api signs credentials for the eturnal relay and returns ICE_SERVERS with them. Resolved +# turn-api signs credentials for the eturnal relay and returns ICE_SERVERS with them, and its +# proof route binds to the People genesis, which waits for People to answer. Resolved # by the same code that configures eturnal, so the secret and hosts match (docs/TURN.md). if [[ "$ROLE" == "all-in-one" ]]; then ENTRY="$PROJECT_DIR/bin/ppn.mjs" [[ -f "$ENTRY" ]] || ENTRY="$PROJECT_DIR/dist/bin.js" - TURN_ENV=$(node "$ENTRY" service dub-turn-env) || { echo "[$ROLE] could not resolve TURN_SECRET/ICE_SERVERS" >&2; exit 1; } + TURN_ENV=$(node "$ENTRY" service dub-turn-env) || { echo "[$ROLE] could not resolve the TURN environment" >&2; exit 1; } eval "$TURN_ENV" fi From e703e89ed3306cba6dacc73d0aef87134ca0065e Mon Sep 17 00:00:00 2001 From: Maksym H Date: Sun, 15 Nov 2026 13:23:27 +0000 Subject: [PATCH 2/6] Take TURN proof products from the network file and its overlay --- config/ports.env | 5 -- docs/DEVICE-UNIQUENESS-BACKEND.md | 4 +- docs/TURN.md | 22 ++++--- networks/README.md | 5 +- networks/previewnet.json | 6 +- packages/cli/src/commands/turn.ts | 51 ++++----------- packages/cli/src/fork/rpc.ts | 4 +- packages/cli/tests/turn-proof.test.ts | 63 +++---------------- packages/network-config/src/dub.ts | 26 ++++---- packages/network-config/src/fork-toml.ts | 3 +- packages/network-config/src/index.ts | 2 +- packages/network-config/src/networks.ts | 39 +++++++----- packages/network-config/src/overlay.ts | 14 +++-- packages/network-config/src/toml-generator.ts | 2 +- packages/network-config/tests/dub.test.ts | 18 +++--- .../network-config/tests/networks.test.ts | 21 ++++--- packages/network-config/tests/overlay.test.ts | 21 +++++++ scripts/dub/service.sh | 12 ++-- tests/scripts/test-turn.ts | 13 ++++ zombienet-configs/local-dev.toml | 3 + 20 files changed, 158 insertions(+), 176 deletions(-) diff --git a/config/ports.env b/config/ports.env index 22d138c..b445948 100644 --- a/config/ports.env +++ b/config/ports.env @@ -133,11 +133,6 @@ PPN_STACK_EXPIRES_AT= PPN_STACK_VALID_FOR= PPN_STACK_REFRESH_SECONDS= -# The dotNS products whose personhood proofs buy TURN credentials at the backend's -# /api/v1/turn/issue-with-proof, comma-separated, such as dim2.testnet. Empty uses the network -# file's services.dub.turnProofProducts. Here for the same reason as PPN_PUBLIC_URL. -PPN_TURN_PROOF_PRODUCTS= - # This instance's name, replacing the network file's displayName in the dashboard and the # Trinity stack descriptor, so instances of one network can be told apart: "Previewnet Staging". PPN_DISPLAY_NAME= diff --git a/docs/DEVICE-UNIQUENESS-BACKEND.md b/docs/DEVICE-UNIQUENESS-BACKEND.md index 15f6c1b..390aaac 100644 --- a/docs/DEVICE-UNIQUENESS-BACKEND.md +++ b/docs/DEVICE-UNIQUENESS-BACKEND.md @@ -262,8 +262,8 @@ And the secrets, in `scripts/dub/service.sh` rather than the TOML (see `docs/PROFILES.md`): `CHAIN_WRITER_SIGNER_SURI`, `INVITER_SIGNER_SURI`, `JWT_ED25519_SECRET` with `JWT_ED25519_PUBLIC_KEY` derived from it, and `TURN_SECRET` with `ICE_SERVERS`, which `ppn service dub-turn-env` resolves together with the eturnal config -(see [TURN.md](TURN.md)). It also resolves the `TURN_PROOF_*` settings, which are not secret -but need the People chain's genesis hash. +(see [TURN.md](TURN.md)). It also reads `TURN_PROOF_GENESIS` from People when the network +lists TURN proof products. The rest stay dormant under the dev defaults: `PAYMENT_MASTER_ACCOUNT` / `PAYMENT_AMOUNT_PLANCK` are only read when `PAYMENT_LANE_ENABLED=true` (default false, diff --git a/docs/TURN.md b/docs/TURN.md index cb611a0..9aa26d9 100644 --- a/docs/TURN.md +++ b/docs/TURN.md @@ -12,17 +12,19 @@ whose `turn-api` mints the short-lived credentials the relay accepts. 3. Pass `servers` as the ICE server URLs, with `username` and `password` as the credential. A product without a DUB sign-in, such as Jollity, sends a personhood proof instead: -`POST /dub/api/v1/turn/issue-with-proof` with a ring-VRF proof made under its own product id. -turn-api serves this route only for the products it is told to accept: - -- `services.dub.turnProofProducts` in the network file, such as `["dim2.testnet"]` on - previewnet. -- `PPN_TURN_PROOF_PRODUCTS` in `config/ports.env`, comma-separated, which replaces that list for - one deployment. +`POST /dub/api/v1/turn/issue-with-proof` with a ring-VRF proof made under its own product name. +turn-api serves this route only for the products the network lists in `turnProofProducts`, such +as `["dim2.testnet"]` on previewnet, and answers 404 when it lists none. Each product must be a +dotNS name under the network's TLD, since that is the name a host proves under. A deployment +replaces the list in its `PPN_NETWORK_OVERLAY` file: + +```json +{ "previewnet": { "turnProofProducts": ["dim2.testnet", "vox.testnet"] } } +``` -With neither, the route answers 404. The proofs are verified against the network's own People -chain: `ppn service dub-turn-env` reads its genesis hash when `dub-api` starts, and waits up to -10 minutes for People to answer. +The proofs are checked against the network's own People chain, whose genesis hash +`ppn service dub-turn-env` reads when `dub-api` starts. `dub-api` waits for People to answer +before it starts. Clients never configure a password themselves. The dashboard lists the same servers under **TURN / STUN**, and so does `/api/network` under `ice`. diff --git a/networks/README.md b/networks/README.md index 74da31c..c18afb8 100644 --- a/networks/README.md +++ b/networks/README.md @@ -39,8 +39,9 @@ or `PPN_NETWORK=` in the environment of the scripts. | *(para ids)* | `parachains[].paraId` is the single source: the genesis TOML, the fork TOML, `ppn generate` and `assign-cores.sh` all read it from here. `config/ports.env` deliberately holds no para ids — those belong to the network, not the machine. | | `relay.extraArgs`, `parachains[].extraArgs` | Extra node flags for that chain. A flag the shared per-key table also sets takes this value (`--rpc-max-connections=5000` replaces the table's 500), and a `-l` arg replaces the default log targets whole; the rest are appended. | | `parachains[].rpcNodes`, `parachains[].rpcNodeArgs` | Non-authoring full nodes (0..9) that serve the chain's public path instead of the collator, on the chain's port + 1, + 2, …, with nginx balancing across them. They run the collator's flags without the authoring ones, with `rpcNodeArgs` over them. | -| *(network overlay)* | `PPN_NETWORK_OVERLAY=` changes `extraArgs`, `rpcNodes` and `rpcNodeArgs` for one deployment without a release: `{ "previewnet": { "relay": { "extraArgs": [...] }, "parachains": { "people": { "rpcNodes": 2, "rpcNodeArgs": [...] } } } }`. Every command applies it, so the zombienet config, nginx routes and dashboard agree. An unknown network, chain or field is an error. | -| `services` | Per-service switches and binaries: `false` disables a service; `{ "binary": { … } }` says what it runs. `eth-rpc` must match the revive pallet in the network's Asset Hub runtime, which is exactly why it is pinned per network rather than shared. `dub` takes `turnProofProducts`, the dotNS products whose personhood proofs buy TURN credentials (see `docs/TURN.md`). | +| *(network overlay)* | `PPN_NETWORK_OVERLAY=` changes `extraArgs`, `rpcNodes`, `rpcNodeArgs` and `turnProofProducts` for one deployment without a release: `{ "previewnet": { "relay": { "extraArgs": [...] }, "parachains": { "people": { "rpcNodes": 2, "rpcNodeArgs": [...] } } } }`. Every command applies it, so the zombienet config, nginx routes and dashboard agree. An unknown network, chain or field is an error. | +| `services` | Per-service switches and binaries: `false` disables a service; `{ "binary": { … } }` says what it runs. `eth-rpc` must match the revive pallet in the network's Asset Hub runtime, which is exactly why it is pinned per network rather than shared. | +| `turnProofProducts` | The dotNS products, under the network's TLD, whose personhood proofs buy TURN credentials at DUB's `/api/v1/turn/issue-with-proof`, such as `["dim2.testnet"]`. Absent leaves the route off. A deployment replaces it in `PPN_NETWORK_OVERLAY` (see `docs/TURN.md`). | | `tools` | Local tools this network needs that are neither a chain nor a service — previewnet's `chain-spec-builder`, for instance, which only a genesis network uses. | | `bite.doppelganger` | `{repo, tag}` of the bite tool. Per network, because the build has to be able to execute the runtimes of the network being bitten. | | *(bin layout)* | Only node binaries are network-versioned: previewnet's live in `bin/`, every other network's in `bin//` (plus `dg/` for bites). Shared tooling with no per-network dimension — zombie-cli, ipfs, postgres, identity, design families — lives in plain `bin/` and is pinned in `config/versions.env`. Collators run through `scripts/omni-node.sh` regardless of binary; it carries the libp2p fix and picks binary/dir from `PPN_COLLATOR_BINARY`/`PPN_BIN_DIR`. | diff --git a/networks/previewnet.json b/networks/previewnet.json index cfb1114..06c1c81 100644 --- a/networks/previewnet.json +++ b/networks/previewnet.json @@ -162,11 +162,9 @@ } } ], + "turnProofProducts": ["dim2.testnet"], + "_turnProofProducts": "Jollity's product account, which proves as dim2. for TURN credentials (paritytech/individuality-apps packages/jollity-protocol/src/config.ts).", "services": { - "dub": { - "turnProofProducts": ["dim2.testnet"], - "_turnProofProducts": "Jollity's product account, which proves as dim2. for TURN credentials (paritytech/individuality-apps packages/jollity-protocol/src/config.ts)." - }, "eth-rpc": { "binary": { "name": "eth-rpc", diff --git a/packages/cli/src/commands/turn.ts b/packages/cli/src/commands/turn.ts index 9eab5b4..6290445 100644 --- a/packages/cli/src/commands/turn.ts +++ b/packages/cli/src/commands/turn.ts @@ -1,22 +1,13 @@ // `ppn service turn`: the TURN/STUN relay (eturnal) whose credentials DUB's turn-api mints. -// `ppn service dub-turn-env`: the matching secret and ICE servers, plus the proof route's -// settings, as shell exports for scripts/dub/service.sh. Both resolve from the same inputs, so -// they cannot disagree. +// `ppn service dub-turn-env`: the matching secret and ICE servers, plus the People genesis the +// proof route is bound to, as shell exports for scripts/dub/service.sh. Both resolve from the same inputs, so they cannot disagree. // See docs/TURN.md. import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { spawn, type ChildProcess } from 'node:child_process'; -import { - LOCAL_TURN_SECRET, - asHttp, - eturnalConfig, - iceServers, - runsTurnRelay, - turnProofProducts, - turnSecret, -} from '@parity/ppn-network-config'; +import { LOCAL_TURN_SECRET, asHttp, eturnalConfig, iceServers, runsTurnRelay, turnSecret } from '@parity/ppn-network-config'; import type { ServiceContext } from './service-context.js'; import { secretsFile } from '../lib/secrets.js'; import { rpc } from '../fork/rpc.js'; @@ -45,41 +36,25 @@ export function dubTurnEnv(ctx: ServiceContext): Record { }; } -/** - * The People chain's genesis hash, which turn-api's proof route requires as - * TURN_PROOF_GENESIS. Waits up to 10 minutes for the chain's RPC, then fails. - */ -export async function peopleGenesis(rpcUrl: string, attempts = 300, delayMs = 2000): Promise { +/** The chain's genesis hash, once its RPC answers; it waits as long as that takes. */ +export async function genesisHash(rpcUrl: string, delayMs = 2000): Promise { for (let attempt = 1; ; attempt++) { try { - return await rpc(asHttp(rpcUrl), 'chain_getBlockHash', [0]); + return await rpc(asHttp(rpcUrl), 'chain_getBlockHash', [0], 5000); } catch (error) { - if (attempt >= attempts) throw new Error(`no genesis hash from ${rpcUrl}: ${(error as Error).message}`); + if (attempt % 15 === 1) console.error(`dub-turn-env: waiting for ${rpcUrl}: ${(error as Error).message}`); await new Promise((resolve) => setTimeout(resolve, delayMs)); } } } -/** - * turn-api's settings for `/api/v1/turn/issue-with-proof`, verified against the local People - * chain: the products from PPN_TURN_PROOF_PRODUCTS, else the network's - * `services.dub.turnProofProducts`. Empty when there are none, which leaves the route off. - */ -export async function turnProofEnv(ctx: ServiceContext): Promise> { - const setting = process.env.PPN_TURN_PROOF_PRODUCTS || ctx.ports.PPN_TURN_PROOF_PRODUCTS; - const products = setting ? setting.split(',').map((p) => p.trim()).filter(Boolean) : turnProofProducts(ctx.net); - if (!products.length) return {}; - const rpcUrl = `ws://127.0.0.1:${ctx.ports.PEOPLE_PORT}`; - return { - TURN_PROOF_ENABLED: 'true', - TURN_PROOF_RPC_URL: rpcUrl, - TURN_PROOF_PRODUCTS: products.join(','), - TURN_PROOF_GENESIS: await peopleGenesis(rpcUrl), - }; -} - export async function printDubTurnEnv(ctx: ServiceContext): Promise { - for (const [k, v] of Object.entries({ ...dubTurnEnv(ctx), ...(await turnProofEnv(ctx)) })) { + const env = dubTurnEnv(ctx); + // turn-api refuses to start with the proof route on and no genesis to check People against. + if (process.env.TURN_PROOF_ENABLED === 'true' && process.env.TURN_PROOF_RPC_URL) { + env.TURN_PROOF_GENESIS = await genesisHash(process.env.TURN_PROOF_RPC_URL); + } + for (const [k, v] of Object.entries(env)) { console.log(`export ${k}='${v.replace(/'/g, `'\\''`)}'`); } } diff --git a/packages/cli/src/fork/rpc.ts b/packages/cli/src/fork/rpc.ts index 8b77ca6..ceb710a 100644 --- a/packages/cli/src/fork/rpc.ts +++ b/packages/cli/src/fork/rpc.ts @@ -4,12 +4,12 @@ import { Metadata, TypeRegistry } from '@polkadot/types'; import { keyOf } from './codec.js'; -export async function rpc(url: string, method: string, params: unknown[] = []): Promise { +export async function rpc(url: string, method: string, params: unknown[] = [], timeoutMs = 60_000): Promise { const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }), - signal: AbortSignal.timeout(60_000), + signal: AbortSignal.timeout(timeoutMs), }); const j = (await r.json()) as { result?: T; error?: unknown }; if (j.error) throw new Error(`${method}: ${JSON.stringify(j.error)}`); diff --git a/packages/cli/tests/turn-proof.test.ts b/packages/cli/tests/turn-proof.test.ts index 03fbb8f..df08b80 100644 --- a/packages/cli/tests/turn-proof.test.ts +++ b/packages/cli/tests/turn-proof.test.ts @@ -3,14 +3,12 @@ import assert from 'node:assert/strict'; import http from 'node:http'; import type { AddressInfo } from 'node:net'; -import { loadNetwork } from '@parity/ppn-network-config'; -import { peopleGenesis, turnProofEnv } from '../src/commands/turn.js'; -import type { ServiceContext } from '../src/commands/service-context.js'; +import { genesisHash } from '../src/commands/turn.js'; const GENESIS = `0x${'ab'.repeat(32)}`; /** A People RPC that fails `failures` requests before answering chain_getBlockHash(0). */ -async function people(failures: number): Promise<{ url: string; port: number; close: () => void }> { +async function people(failures: number): Promise<{ url: string; close: () => void }> { let seen = 0; const server = http.createServer((req, res) => { let body = ''; @@ -24,63 +22,18 @@ async function people(failures: number): Promise<{ url: string; port: number; cl }); }); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); - const port = (server.address() as AddressInfo).port; - return { url: `ws://127.0.0.1:${port}`, port, close: () => server.close() }; + return { url: `ws://127.0.0.1:${(server.address() as AddressInfo).port}`, close: () => server.close() }; } -describe('peopleGenesis', () => { - // dub-api starts beside the chains, so People may not answer yet; turn-api refuses to boot - // with proof issuance on and no genesis. +describe('genesisHash', () => { + // dub-api starts beside the chains, so People may not answer yet; turn-api refuses to start + // with the proof route on and no genesis, and nothing restarts dub-api if it exits. it('waits for People to answer', async () => { - const rpc = await people(2); + const rpc = await people(3); try { - assert.equal(await peopleGenesis(rpc.url, 5, 10), GENESIS); + assert.equal(await genesisHash(rpc.url, 10), GENESIS); } finally { rpc.close(); } }); - - it('fails once People has not answered in time', async () => { - const rpc = await people(10); - try { - await assert.rejects(peopleGenesis(rpc.url, 3, 10), /no genesis hash from ws:\/\/127\.0\.0\.1/); - } finally { - rpc.close(); - } - }); -}); - -describe('turnProofEnv', () => { - const ctx = (network: string, ports: Record) => - ({ net: loadNetwork(network), ports }) as unknown as ServiceContext; - - // Jollity proves as dim2.testnet on previewnet; without these settings turn-api answers its - // issue-with-proof call with a 404. - it("enables the proof route for the network file's products, against local People", async () => { - const rpc = await people(0); - try { - assert.deepEqual(await turnProofEnv(ctx('previewnet', { PEOPLE_PORT: String(rpc.port), PPN_TURN_PROOF_PRODUCTS: '' })), { - TURN_PROOF_ENABLED: 'true', - TURN_PROOF_RPC_URL: rpc.url, - TURN_PROOF_PRODUCTS: 'dim2.testnet', - TURN_PROOF_GENESIS: GENESIS, - }); - } finally { - rpc.close(); - } - }); - - it('takes the products a deployment sets in ports.env over the network file', async () => { - const rpc = await people(0); - try { - const env = await turnProofEnv(ctx('previewnet', { PEOPLE_PORT: String(rpc.port), PPN_TURN_PROOF_PRODUCTS: 'dim2.testnet, vox.testnet' })); - assert.equal(env.TURN_PROOF_PRODUCTS, 'dim2.testnet,vox.testnet'); - } finally { - rpc.close(); - } - }); - - it('leaves the route off for a network without products', async () => { - assert.deepEqual(await turnProofEnv(ctx('polkadot', { PEOPLE_PORT: '1' })), {}); - }); }); diff --git a/packages/network-config/src/dub.ts b/packages/network-config/src/dub.ts index bc7d387..779a823 100644 --- a/packages/network-config/src/dub.ts +++ b/packages/network-config/src/dub.ts @@ -26,8 +26,6 @@ // deployable profile already draws: public identifiers at generate time, // private keys at runtime only. -import type { NetworkDef } from './networks.js'; - /** Well-known dev account. Also what PPN grants attestation allowance to. */ export const ALICE_SS58 = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'; @@ -127,7 +125,8 @@ export function peopleRpcUrl(peoplePort: number): string { export function dubServices( ports: DubPorts, attesterAccount: string = ALICE_SS58, - inviterAccount: string = BOB_SS58 + inviterAccount: string = BOB_SS58, + turnProofProducts: string[] = [] ): DubService[] { const rpc = peopleRpcUrl(ports.people); const db = (name: (typeof DUB_DATABASES)[number]) => dubDatabaseUrl(name, ports.postgres); @@ -178,10 +177,16 @@ export function dubServices( ...common, BIND_ADDR: `127.0.0.1:${ports.gateway}`, // Credentials are HMAC-signed for our own eturnal relay, not minted by Cloudflare - // (turn-api's default). The realm is public, so it belongs here; the signing secret, - // ICE_SERVERS and the TURN_PROOF_* settings depend on the host, so service.sh supplies them. + // (turn-api's default). The realm is public, so it belongs here; the signing secret + // and ICE_SERVERS depend on the host, so service.sh supplies them. TURN_PROVIDER: 'coturn', TURN_REALM, + // /api/v1/turn/issue-with-proof. service.sh adds TURN_PROOF_GENESIS, read from People. + ...(turnProofProducts.length && { + TURN_PROOF_ENABLED: 'true', + TURN_PROOF_RPC_URL: rpc, + TURN_PROOF_PRODUCTS: turnProofProducts.join(','), + }), }, }, { @@ -200,12 +205,6 @@ export function dubServices( ]; } -/** The products `services.dub.turnProofProducts` names, or none. */ -export function turnProofProducts(net: NetworkDef): string[] { - const dub = net.services['dub']; - return typeof dub === 'object' ? (dub.turnProofProducts ?? []) : []; -} - function tomlEnv(env: Record): string { const entries = Object.entries(env).map( ([name, value]) => ` { name = "${name}", value = "${value}" },` @@ -237,13 +236,14 @@ export function dubCustomProcesses( // fork mode writes a standalone TOML with absolute paths already resolved. scriptsDir: string = '{{SCRIPTS}}', // The TURN/STUN relay whose credentials turn-api mints (docs/TURN.md). - turn: boolean = true + turn: boolean = true, + turnProofProducts: string[] = [] ): string { const wait = `127.0.0.1:${ports.postgres}`; // zombienet validates custom_process args as CLI arguments — each must parse // as a flag or --key=value, so bare positionals are rejected outright. - const blocks = dubServices(ports, attesterAccount).map( + const blocks = dubServices(ports, attesterAccount, undefined, turnProofProducts).map( (svc) => ` [[custom_processes]] name = "${svc.name}" diff --git a/packages/network-config/src/fork-toml.ts b/packages/network-config/src/fork-toml.ts index 5535ba4..a35bba3 100644 --- a/packages/network-config/src/fork-toml.ts +++ b/packages/network-config/src/fork-toml.ts @@ -389,7 +389,8 @@ export function generateForkToml(options: GenerateForkTomlOptions): string { }, undefined, scriptsDir, - net.services['turn'] !== false + net.services['turn'] !== false, + net.turnProofProducts ) : ''; diff --git a/packages/network-config/src/index.ts b/packages/network-config/src/index.ts index adc441d..7a4b9d2 100644 --- a/packages/network-config/src/index.ts +++ b/packages/network-config/src/index.ts @@ -57,7 +57,7 @@ export { hrmpChannels } from './toml-generator.js'; export type { HrmpChannel } from './toml-generator.js'; export { generateForkToml, FORK_PROCESSES } from './fork-toml.js'; export type { GenerateForkTomlOptions } from './fork-toml.js'; -export { dubCustomProcesses, turnProofProducts, ALICE_SS58, BOB_SS58, TURN_REALM } from './dub.js'; +export { dubCustomProcesses, ALICE_SS58, BOB_SS58, TURN_REALM } from './dub.js'; export { iceServers, runsTurnRelay, diff --git a/packages/network-config/src/networks.ts b/packages/network-config/src/networks.ts index 9f83747..5561fae 100644 --- a/packages/network-config/src/networks.ts +++ b/packages/network-config/src/networks.ts @@ -151,16 +151,7 @@ export type NetworkChain = ChainFields & { }; /** false disables a service; an object configures it (its binary, when it has one). */ -export type ServiceConfig = - | boolean - | { - binary?: BinaryRef; - /** - * `dub` only: the dotNS products whose personhood proofs buy TURN credentials at - * `/api/v1/turn/issue-with-proof`, such as `dim2.testnet`. Without it the route is absent. - */ - turnProofProducts?: string[]; - }; +export type ServiceConfig = boolean | { binary?: BinaryRef }; /** * A Trinity stack descriptor's facts about the live network that the rest of the file does @@ -216,6 +207,11 @@ export interface NetworkDef { parachains: NetworkParachain[]; /** Per-service switches and binaries for the custom processes. */ services: Record; + /** + * The dotNS products whose personhood proofs buy TURN credentials at DUB's + * `/api/v1/turn/issue-with-proof`, such as `dim2.testnet`. Empty leaves the route off. + */ + turnProofProducts: string[]; /** Local tools this network needs that are neither a chain nor a service. */ tools: Record; /** @@ -437,13 +433,6 @@ export function loadDescriptor(name: string): NetworkDef { const services: Record = raw.services ?? {}; for (const [svc, cfg] of Object.entries(services)) { if (cfg && typeof cfg === 'object' && cfg.binary) checkBinary(`services."${svc}"`, cfg.binary); - const products = cfg && typeof cfg === 'object' ? cfg.turnProofProducts : undefined; - if (products !== undefined) { - if (svc !== 'dub') bad(`services."${svc}".turnProofProducts: only dub issues TURN credentials`); - if (!Array.isArray(products) || !products.length || !products.every((p) => /^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(p))) { - bad('services."dub".turnProofProducts must be a non-empty list of dotNS names, such as "dim2.testnet"'); - } - } } const tools: Record = Object.fromEntries( Object.entries(raw.tools ?? {}).filter(([k]) => !k.startsWith('_')) @@ -490,6 +479,7 @@ export function loadDescriptor(name: string): NetworkDef { rpcNodeArgs: parseArgs(`parachains.${p.key}.rpcNodeArgs`, p.rpcNodeArgs, bad), })), services, + turnProofProducts: parseTurnProofProducts('turnProofProducts', raw.turnProofProducts, stack?.tld ?? raw.genesisConfig?.networkSuffix, bad), tools, dotns: raw.dotns, stack, @@ -497,6 +487,21 @@ export function loadDescriptor(name: string): NetworkDef { }; } +/** + * `products` as TURN proof products, or `bad` unless each is a dotNS name under the network's + * `tld`: a host proves under the product's name on this network, so any other name never matches. + * Absent is none. + */ +export function parseTurnProofProducts(at: string, products: unknown, tld: string | undefined, bad: (why: string) => never): string[] { + if (products === undefined) return []; + if (!tld) bad(`${at}: the network has no dotNS TLD (stack.tld or genesisConfig.networkSuffix)`); + const name = new RegExp(`^[a-z0-9-]+\\.${tld}$`); + if (!Array.isArray(products) || !products.length || products.some((p) => typeof p !== 'string' || !name.test(p))) { + bad(`${at} must be a non-empty array of dotNS names under .${tld}, such as "dim2.${tld}"`); + } + return products as string[]; +} + /** `args` as node flags, or `bad` when it is not a list of "--flag" strings. Absent is none. */ export function parseArgs(at: string, args: unknown, bad: (why: string) => never): string[] { if (args === undefined) return []; diff --git a/packages/network-config/src/overlay.ts b/packages/network-config/src/overlay.ts index d89c83f..c945d8a 100644 --- a/packages/network-config/src/overlay.ts +++ b/packages/network-config/src/overlay.ts @@ -1,5 +1,5 @@ // A deployment's own shape for a network, without a release of the engine: RPC nodes in front of -// a chain, or different flags on its nodes. Keyed by network, so loading another network (as +// a chain, different flags on its nodes, or the products its TURN proof route accepts. Keyed by network, so loading another network (as // `ppn networks` does) is unaffected by a section written for this one. // // PPN_NETWORK_OVERLAY=/etc/ppn/network-overlay.json @@ -9,14 +9,15 @@ import fs from 'node:fs'; import { overrideArgs } from './args.js'; -import { listNetworks, parseArgs, parseRpcNodes, type NetworkDef } from './networks.js'; +import { listNetworks, parseArgs, parseRpcNodes, parseTurnProofProducts, type NetworkDef } from './networks.js'; const RELAY_FIELDS = ['extraArgs']; const PARACHAIN_FIELDS = ['extraArgs', 'rpcNodes', 'rpcNodeArgs']; /** * `net` with the overlay file's section for it applied: its extraArgs and rpcNodeArgs override - * the descriptor's flag by flag, and rpcNodes replaces the count. Throws on an unreadable file, + * the descriptor's flag by flag, and rpcNodes and turnProofProducts replace the descriptor's. + * Throws on an unreadable file, * an unknown network, chain or field, or a value the descriptor itself would reject. */ export function applyOverlay(net: NetworkDef, file: string): NetworkDef { @@ -42,7 +43,7 @@ export function applyOverlay(net: NetworkDef, file: string): NetworkDef { if (strangers.length) bad(`unknown networks ${strangers.join(', ')} (known: ${networks.join(', ')})`); if (raw[net.name] === undefined) return net; - const section = object(net.name, raw[net.name], ['relay', 'parachains']); + const section = object(net.name, raw[net.name], ['relay', 'parachains', 'turnProofProducts']); const relay = object(`${net.name}.relay`, section.relay ?? {}, RELAY_FIELDS); const parachains = object(`${net.name}.parachains`, section.parachains ?? {}); const parachainKeys: string[] = net.parachains.map((parachain) => parachain.key); @@ -51,8 +52,13 @@ export function applyOverlay(net: NetworkDef, file: string): NetworkDef { bad(`${net.name} has no parachains ${unknownKeys.join(', ')} (declared: ${parachainKeys.join(', ')})`); } + const tld = net.stack?.tld ?? net.genesisConfig?.networkSuffix; return { ...net, + turnProofProducts: + section.turnProofProducts === undefined + ? net.turnProofProducts + : parseTurnProofProducts(`${net.name}.turnProofProducts`, section.turnProofProducts, tld, bad), relay: { ...net.relay, extraArgs: overrideArgs(net.relay.extraArgs, parseArgs(`${net.name}.relay.extraArgs`, relay.extraArgs, bad)), diff --git a/packages/network-config/src/toml-generator.ts b/packages/network-config/src/toml-generator.ts index af92339..bd34995 100644 --- a/packages/network-config/src/toml-generator.ts +++ b/packages/network-config/src/toml-generator.ts @@ -584,7 +584,7 @@ command = "{{SCRIPTS}}/grant-invites.sh" people: PORTS.people, assetHub: PORTS['asset-hub'], gateway: requiredPort('DUB_PORT'), - }, undefined, undefined, serviceEnabled('turn')) + }, undefined, undefined, serviceEnabled('turn'), genesisNet().turnProofProducts) )} ${section(serviceEnabled('patch-bootnodes'), ` [[custom_processes]] diff --git a/packages/network-config/tests/dub.test.ts b/packages/network-config/tests/dub.test.ts index d7ed292..a3489b1 100644 --- a/packages/network-config/tests/dub.test.ts +++ b/packages/network-config/tests/dub.test.ts @@ -11,9 +11,7 @@ import { dubDatabaseUrl, dubServices, peopleRpcUrl, - turnProofProducts, } from '../src/dub.js'; -import { loadNetwork } from '../src/load.js'; const PORTS = { postgres: 5433, people: 10010, assetHub: 10020, gateway: 8092 }; @@ -172,13 +170,19 @@ describe('dubServices', () => { }); }); -describe('turnProofProducts', () => { - it('accepts Jollity on previewnet', () => { - assert.deepEqual(turnProofProducts(loadNetwork('previewnet')), ['dim2.testnet']); +describe('TURN proof issuance', () => { + // turn-api mounts /api/v1/turn/issue-with-proof only with these set, and refuses a product + // missing from the list. Jollity fetches its relay credentials there. + it('enables the proof route on dub-api for the given products, against local People', () => { + const [api, ...workers] = dubServices(PORTS, undefined, undefined, ['dim2.testnet', 'vox.testnet']); + assert.equal(api.env.TURN_PROOF_ENABLED, 'true'); + assert.equal(api.env.TURN_PROOF_RPC_URL, api.env.PEOPLE_RPC_URL); + assert.equal(api.env.TURN_PROOF_PRODUCTS, 'dim2.testnet,vox.testnet'); + for (const svc of workers) assert.ok(!('TURN_PROOF_ENABLED' in svc.env)); }); - it('is empty for a network that lists none', () => { - assert.deepEqual(turnProofProducts(loadNetwork('polkadot')), []); + it('leaves the route off without products', () => { + for (const svc of dubServices(PORTS)) assert.ok(!Object.keys(svc.env).some((k) => k.startsWith('TURN_PROOF_'))); }); }); diff --git a/packages/network-config/tests/networks.test.ts b/packages/network-config/tests/networks.test.ts index 7fd0ac9..26b6eb5 100644 --- a/packages/network-config/tests/networks.test.ts +++ b/packages/network-config/tests/networks.test.ts @@ -286,14 +286,14 @@ describe('product import', () => { }); }); -describe('services.dub.turnProofProducts', () => { - function loadWith(services: Record): () => unknown { +describe('turnProofProducts', () => { + function loadWith(fields: Record): () => ReturnType { return () => { const saved = process.env.PPN_HOME; const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ppn-home-')); fs.mkdirSync(path.join(home, 'networks')); const raw = JSON.parse(fs.readFileSync(path.join(repoRoot(), 'networks/previewnet.json'), 'utf-8')); - fs.writeFileSync(path.join(home, 'networks', 'proof-test.json'), JSON.stringify({ ...raw, name: 'proof-test', services })); + fs.writeFileSync(path.join(home, 'networks', 'proof-test.json'), JSON.stringify({ ...raw, name: 'proof-test', ...fields })); process.env.PPN_HOME = home; try { return loadNetwork('proof-test'); @@ -305,12 +305,17 @@ describe('services.dub.turnProofProducts', () => { }; } - it('rejects a product that is not a dotNS name', () => { - assert.throws(loadWith({ dub: { turnProofProducts: ['dim2'] } }), /dotNS names/); - assert.throws(loadWith({ dub: { turnProofProducts: [] } }), /dotNS names/); + it('accepts Jollity on previewnet, under its TLD', () => { + assert.deepEqual(loadNetwork('previewnet').turnProofProducts, ['dim2.testnet']); }); - it('rejects products on a service other than dub', () => { - assert.throws(loadWith({ 'eth-rpc': { turnProofProducts: ['dim2.testnet'] } }), /only dub/); + it('is empty for a network that lists none', () => { + assert.deepEqual(loadNetwork('polkadot').turnProofProducts, []); + }); + + it('rejects a product that is not a name under the network TLD', () => { + assert.throws(loadWith({ turnProofProducts: ['dim2'] }), /dotNS names under \.testnet/); + assert.throws(loadWith({ turnProofProducts: ['dim2.paseo'] }), /dotNS names under \.testnet/); + assert.throws(loadWith({ turnProofProducts: [] }), /dotNS names under \.testnet/); }); }); diff --git a/packages/network-config/tests/overlay.test.ts b/packages/network-config/tests/overlay.test.ts index 4de6d86..bd37ad1 100644 --- a/packages/network-config/tests/overlay.test.ts +++ b/packages/network-config/tests/overlay.test.ts @@ -108,6 +108,27 @@ describe('network overlay: RPC nodes in front of People', () => { }); }); +describe('network overlay: TURN proof products', () => { + const net = loadDescriptorOnly('previewnet'); + + // A deployment accepts its own products' proofs without an engine release, and dub-api is + // started with that list rather than the network file's. + it("replaces the network file's products in dub-api's environment", () => { + const overlaid = applyOverlay(net, write('turn.json', { previewnet: { turnProofProducts: ['dim2.testnet', 'vox.testnet'] } })); + assert.deepEqual(overlaid.turnProofProducts, ['dim2.testnet', 'vox.testnet']); + }); + + it('keeps the network file\'s products when the section does not set them', () => { + assert.deepEqual(applyOverlay(net, write('none.json', { previewnet: {} })).turnProofProducts, ['dim2.testnet']); + }); + + it('rejects a product under another TLD, which no host on this network proves as', () => + assert.throws( + () => applyOverlay(net, write('tld.json', { previewnet: { turnProofProducts: ['dim2.paseo'] } })), + /previewnet\.turnProofProducts must be a non-empty array of dotNS names under \.testnet/ + )); +}); + describe('network overlay: a mistake fails instead of deploying defaults', () => { const net = loadDescriptorOnly('previewnet'); const rejects = (body: unknown, message: RegExp) => diff --git a/scripts/dub/service.sh b/scripts/dub/service.sh index fff5874..dcb4af2 100755 --- a/scripts/dub/service.sh +++ b/scripts/dub/service.sh @@ -112,13 +112,13 @@ export CHAIN_WRITER_SIGNER_SURI="${PPN_DUB_ATTESTER_URI:-${PPN_IDENTITY_ATTESTER # signing into a proxy call that People Chain has no registration for. export INVITER_SIGNER_SURI="${PPN_INVITER_SURI:-//Bob}" -# turn-api signs credentials for the eturnal relay and returns ICE_SERVERS with them, and its -# proof route binds to the People genesis, which waits for People to answer. Resolved -# by the same code that configures eturnal, so the secret and hosts match (docs/TURN.md). +# turn-api signs credentials for the eturnal relay and returns ICE_SERVERS with them. Resolved +# by the same code that configures eturnal, so the secret and hosts match (docs/TURN.md). When +# the network lists TURN proof products, dub-turn-env also waits for People to read its genesis. if [[ "$ROLE" == "all-in-one" ]]; then ENTRY="$PROJECT_DIR/bin/ppn.mjs" [[ -f "$ENTRY" ]] || ENTRY="$PROJECT_DIR/dist/bin.js" - TURN_ENV=$(node "$ENTRY" service dub-turn-env) || { echo "[$ROLE] could not resolve the TURN environment" >&2; exit 1; } + TURN_ENV=$(node "$ENTRY" service dub-turn-env) || { echo "[$ROLE] could not resolve TURN_SECRET/ICE_SERVERS" >&2; exit 1; } eval "$TURN_ENV" fi @@ -169,8 +169,8 @@ fi # ("pool timed out while waiting for an open connection"). custom_processes # have no ordering, so waiting here is the only sequencing available. # -# No equivalent wait for the chain: the services block on the People Chain -# connect indefinitely and pick it up whenever it appears. They do not bind +# The services themselves do not wait for the chain: they block on the People +# Chain connect indefinitely and pick it up whenever it appears. They do not bind # their HTTP port until that succeeds, so /livez and /readyz being # unreachable while the parachain starts is expected, not a fault. WAIT_HOST="${WAIT_TARGET%:*}" diff --git a/tests/scripts/test-turn.ts b/tests/scripts/test-turn.ts index d411d39..8daff14 100644 --- a/tests/scripts/test-turn.ts +++ b/tests/scripts/test-turn.ts @@ -184,6 +184,19 @@ export async function run(nodeName: string, _networkInfo: NetworkInfo, _args: st } console.log(`[TEST] ok relay allocated at ${xorAddress(relayed)}`); + // Previewnet lists TURN proof products, so turn-api serves the proof route. An empty body + // is rejected as malformed; without the route it is a 404. + const proof = await fetch(`${DUB_BASE}/api/v1/turn/issue-with-proof`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: "{}", + }); + if (proof.status !== 400) { + console.error(`[TEST] FAIL turn/issue-with-proof: ${proof.status} ${await proof.text()}`); + return FAILURE; + } + console.log("[TEST] ok turn/issue-with-proof is served"); + const refused = await allocate(creds.username, `${creds.password}x`); if (errorCode(refused) !== 401) { console.error(`[TEST] FAIL a wrong password was answered ${refused.type.toString(16)}, not 401`); diff --git a/zombienet-configs/local-dev.toml b/zombienet-configs/local-dev.toml index ab4df9a..a36f075 100644 --- a/zombienet-configs/local-dev.toml +++ b/zombienet-configs/local-dev.toml @@ -201,6 +201,9 @@ env = [ { name = "BIND_ADDR", value = "127.0.0.1:8092" }, { name = "TURN_PROVIDER", value = "coturn" }, { name = "TURN_REALM", value = "previewnet.local" }, + { name = "TURN_PROOF_ENABLED", value = "true" }, + { name = "TURN_PROOF_RPC_URL", value = "ws://127.0.0.1:10010" }, + { name = "TURN_PROOF_PRODUCTS", value = "dim2.testnet" }, ] [[custom_processes]] From a328cbb0b9da9f8c5c2e13f65346ce45ff88eb73 Mon Sep 17 00:00:00 2001 From: Maksym H Date: Sun, 15 Nov 2026 13:26:09 +0000 Subject: [PATCH 3/6] Resolve a network's dotNS TLD in one place --- packages/cli/src/commands/trinity-stack.ts | 3 ++- packages/network-config/src/networks.ts | 19 +++++++++++++++++-- packages/network-config/src/overlay.ts | 10 +++++----- packages/network-config/src/trinity-stack.ts | 4 ++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/commands/trinity-stack.ts b/packages/cli/src/commands/trinity-stack.ts index 8f17481..025544b 100644 --- a/packages/cli/src/commands/trinity-stack.ts +++ b/packages/cli/src/commands/trinity-stack.ts @@ -13,6 +13,7 @@ import { chainKey, dashboardModel, dotnsFromContracts, + networkTld, trinityStackModel, trinityStackUrl, type ChainKey, @@ -262,7 +263,7 @@ export async function trinityStack(ctx: ServiceContext): Promise { console.log('trinity-stack: an ephemeral run has no data directory to publish from'); return; } - if (!ctx.net.stack?.tld && !ctx.net.genesisConfig?.networkSuffix) { + if (!networkTld(ctx.net)) { console.log(`trinity-stack: networks/${ctx.net.name}.json names no TLD, so there is no descriptor to write`); return; } diff --git a/packages/network-config/src/networks.ts b/packages/network-config/src/networks.ts index 5561fae..e31a6db 100644 --- a/packages/network-config/src/networks.ts +++ b/packages/network-config/src/networks.ts @@ -479,7 +479,12 @@ export function loadDescriptor(name: string): NetworkDef { rpcNodeArgs: parseArgs(`parachains.${p.key}.rpcNodeArgs`, p.rpcNodeArgs, bad), })), services, - turnProofProducts: parseTurnProofProducts('turnProofProducts', raw.turnProofProducts, stack?.tld ?? raw.genesisConfig?.networkSuffix, bad), + turnProofProducts: parseTurnProofProducts( + 'turnProofProducts', + raw.turnProofProducts, + networkTld({ stack, genesisConfig: raw.genesisConfig }), + bad + ), tools, dotns: raw.dotns, stack, @@ -487,12 +492,22 @@ export function loadDescriptor(name: string): NetworkDef { }; } +/** The dotNS TLD: `stack.tld`, else a genesis network's `genesisConfig.networkSuffix`. */ +export function networkTld(net: Pick): string | undefined { + return net.stack?.tld ?? net.genesisConfig?.networkSuffix; +} + /** * `products` as TURN proof products, or `bad` unless each is a dotNS name under the network's * `tld`: a host proves under the product's name on this network, so any other name never matches. * Absent is none. */ -export function parseTurnProofProducts(at: string, products: unknown, tld: string | undefined, bad: (why: string) => never): string[] { +export function parseTurnProofProducts( + at: string, + products: unknown, + tld: string | undefined, + bad: (why: string) => never +): string[] { if (products === undefined) return []; if (!tld) bad(`${at}: the network has no dotNS TLD (stack.tld or genesisConfig.networkSuffix)`); const name = new RegExp(`^[a-z0-9-]+\\.${tld}$`); diff --git a/packages/network-config/src/overlay.ts b/packages/network-config/src/overlay.ts index c945d8a..fe08208 100644 --- a/packages/network-config/src/overlay.ts +++ b/packages/network-config/src/overlay.ts @@ -1,6 +1,7 @@ // A deployment's own shape for a network, without a release of the engine: RPC nodes in front of -// a chain, different flags on its nodes, or the products its TURN proof route accepts. Keyed by network, so loading another network (as -// `ppn networks` does) is unaffected by a section written for this one. +// a chain, different flags on its nodes, or the products its TURN proof route accepts. Keyed by +// network, so loading another network (as `ppn networks` does) is unaffected by a section written +// for this one. // // PPN_NETWORK_OVERLAY=/etc/ppn/network-overlay.json // { "previewnet": { @@ -9,7 +10,7 @@ import fs from 'node:fs'; import { overrideArgs } from './args.js'; -import { listNetworks, parseArgs, parseRpcNodes, parseTurnProofProducts, type NetworkDef } from './networks.js'; +import { listNetworks, networkTld, parseArgs, parseRpcNodes, parseTurnProofProducts, type NetworkDef } from './networks.js'; const RELAY_FIELDS = ['extraArgs']; const PARACHAIN_FIELDS = ['extraArgs', 'rpcNodes', 'rpcNodeArgs']; @@ -52,13 +53,12 @@ export function applyOverlay(net: NetworkDef, file: string): NetworkDef { bad(`${net.name} has no parachains ${unknownKeys.join(', ')} (declared: ${parachainKeys.join(', ')})`); } - const tld = net.stack?.tld ?? net.genesisConfig?.networkSuffix; return { ...net, turnProofProducts: section.turnProofProducts === undefined ? net.turnProofProducts - : parseTurnProofProducts(`${net.name}.turnProofProducts`, section.turnProofProducts, tld, bad), + : parseTurnProofProducts(`${net.name}.turnProofProducts`, section.turnProofProducts, networkTld(net), bad), relay: { ...net.relay, extraArgs: overrideArgs(net.relay.extraArgs, parseArgs(`${net.name}.relay.extraArgs`, relay.extraArgs, bad)), diff --git a/packages/network-config/src/trinity-stack.ts b/packages/network-config/src/trinity-stack.ts index 88c3744..9a2d733 100644 --- a/packages/network-config/src/trinity-stack.ts +++ b/packages/network-config/src/trinity-stack.ts @@ -6,7 +6,7 @@ import type { Block, Chain, Model, Service, SpecFile } from '@parity/trinity-stack'; import type { DashboardModel } from './dashboard-model.js'; -import type { ChainKey, NetworkDef } from './networks.js'; +import { networkTld, type ChainKey, type NetworkDef } from './networks.js'; import { CHAIN_ARGS } from './toml-generator.js'; /** RFC 0026 roles, plus Web3Storage, which serves tools. */ @@ -94,7 +94,7 @@ export function trinityStackUrl(baseUrl: string): string { * source publishes its own descriptor: `fork.of` is that pointer. */ export function trinityStackModel(net: NetworkDef, model: DashboardModel, facts: StackFacts): Model { - const tld = net.stack?.tld ?? net.genesisConfig?.networkSuffix; + const tld = networkTld(net); if (!tld) throw new Error(`networks/${net.name}.json has no stack.tld`); // `fork` requires `forkedAt` on every chain, so it is all or nothing. const everyForkedAt = Object.values(facts.chains).every((c) => c?.forkedAt); From df81c7c1eacb40df098fda297a2e8f20ec7590fa Mon Sep 17 00:00:00 2001 From: Maksym H Date: Sun, 15 Nov 2026 13:26:09 +0000 Subject: [PATCH 4/6] Wait for People's genesis with the shared sleep helper --- packages/cli/src/commands/turn.ts | 10 ++++++---- packages/cli/src/fork/rpc.ts | 4 ++-- packages/cli/tests/turn-proof.test.ts | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/commands/turn.ts b/packages/cli/src/commands/turn.ts index 6290445..9e676e4 100644 --- a/packages/cli/src/commands/turn.ts +++ b/packages/cli/src/commands/turn.ts @@ -1,6 +1,7 @@ // `ppn service turn`: the TURN/STUN relay (eturnal) whose credentials DUB's turn-api mints. // `ppn service dub-turn-env`: the matching secret and ICE servers, plus the People genesis the -// proof route is bound to, as shell exports for scripts/dub/service.sh. Both resolve from the same inputs, so they cannot disagree. +// proof route is bound to, as shell exports for scripts/dub/service.sh. Both resolve from the +// same inputs, so they cannot disagree. // See docs/TURN.md. import fs from 'node:fs'; @@ -11,6 +12,7 @@ import { LOCAL_TURN_SECRET, asHttp, eturnalConfig, iceServers, runsTurnRelay, tu import type { ServiceContext } from './service-context.js'; import { secretsFile } from '../lib/secrets.js'; import { rpc } from '../fork/rpc.js'; +import { sleep, type Sleep } from '../fork/sleep.js'; import { ETURNAL_BREW_INSTALL, eturnalCtl, eturnalDist } from '../lib/eturnal.js'; /** The host clients reach the network under; only its hostname and scheme matter here. */ @@ -37,13 +39,13 @@ export function dubTurnEnv(ctx: ServiceContext): Record { } /** The chain's genesis hash, once its RPC answers; it waits as long as that takes. */ -export async function genesisHash(rpcUrl: string, delayMs = 2000): Promise { +export async function genesisHash(rpcUrl: string, wait: Sleep = sleep): Promise { for (let attempt = 1; ; attempt++) { try { - return await rpc(asHttp(rpcUrl), 'chain_getBlockHash', [0], 5000); + return await rpc(asHttp(rpcUrl), 'chain_getBlockHash', [0]); } catch (error) { if (attempt % 15 === 1) console.error(`dub-turn-env: waiting for ${rpcUrl}: ${(error as Error).message}`); - await new Promise((resolve) => setTimeout(resolve, delayMs)); + await wait(2000); } } } diff --git a/packages/cli/src/fork/rpc.ts b/packages/cli/src/fork/rpc.ts index ceb710a..8b77ca6 100644 --- a/packages/cli/src/fork/rpc.ts +++ b/packages/cli/src/fork/rpc.ts @@ -4,12 +4,12 @@ import { Metadata, TypeRegistry } from '@polkadot/types'; import { keyOf } from './codec.js'; -export async function rpc(url: string, method: string, params: unknown[] = [], timeoutMs = 60_000): Promise { +export async function rpc(url: string, method: string, params: unknown[] = []): Promise { const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }), - signal: AbortSignal.timeout(timeoutMs), + signal: AbortSignal.timeout(60_000), }); const j = (await r.json()) as { result?: T; error?: unknown }; if (j.error) throw new Error(`${method}: ${JSON.stringify(j.error)}`); diff --git a/packages/cli/tests/turn-proof.test.ts b/packages/cli/tests/turn-proof.test.ts index df08b80..a623b1b 100644 --- a/packages/cli/tests/turn-proof.test.ts +++ b/packages/cli/tests/turn-proof.test.ts @@ -31,7 +31,7 @@ describe('genesisHash', () => { it('waits for People to answer', async () => { const rpc = await people(3); try { - assert.equal(await genesisHash(rpc.url, 10), GENESIS); + assert.equal(await genesisHash(rpc.url, async () => {}), GENESIS); } finally { rpc.close(); } From 61b18523d547decbc81c896d980a340bc6d50c11 Mon Sep 17 00:00:00 2001 From: Maksym H Date: Sun, 15 Nov 2026 13:33:48 +0000 Subject: [PATCH 5/6] Let an empty TURN proof product list turn the route off --- docs/TURN.md | 2 +- packages/network-config/src/networks.ts | 8 ++++---- packages/network-config/tests/networks.test.ts | 1 - packages/network-config/tests/overlay.test.ts | 8 ++++++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/TURN.md b/docs/TURN.md index 9aa26d9..85d94f3 100644 --- a/docs/TURN.md +++ b/docs/TURN.md @@ -16,7 +16,7 @@ A product without a DUB sign-in, such as Jollity, sends a personhood proof inste turn-api serves this route only for the products the network lists in `turnProofProducts`, such as `["dim2.testnet"]` on previewnet, and answers 404 when it lists none. Each product must be a dotNS name under the network's TLD, since that is the name a host proves under. A deployment -replaces the list in its `PPN_NETWORK_OVERLAY` file: +replaces the list in its `PPN_NETWORK_OVERLAY` file, where `[]` turns the route off: ```json { "previewnet": { "turnProofProducts": ["dim2.testnet", "vox.testnet"] } } diff --git a/packages/network-config/src/networks.ts b/packages/network-config/src/networks.ts index e31a6db..4855785 100644 --- a/packages/network-config/src/networks.ts +++ b/packages/network-config/src/networks.ts @@ -500,7 +500,7 @@ export function networkTld(net: Pick): st /** * `products` as TURN proof products, or `bad` unless each is a dotNS name under the network's * `tld`: a host proves under the product's name on this network, so any other name never matches. - * Absent is none. + * Absent or empty is none, which leaves the route off. */ export function parseTurnProofProducts( at: string, @@ -508,11 +508,11 @@ export function parseTurnProofProducts( tld: string | undefined, bad: (why: string) => never ): string[] { - if (products === undefined) return []; + if (products === undefined || (Array.isArray(products) && !products.length)) return []; if (!tld) bad(`${at}: the network has no dotNS TLD (stack.tld or genesisConfig.networkSuffix)`); const name = new RegExp(`^[a-z0-9-]+\\.${tld}$`); - if (!Array.isArray(products) || !products.length || products.some((p) => typeof p !== 'string' || !name.test(p))) { - bad(`${at} must be a non-empty array of dotNS names under .${tld}, such as "dim2.${tld}"`); + if (!Array.isArray(products) || products.some((product) => typeof product !== 'string' || !name.test(product))) { + bad(`${at} must be an array of dotNS names under .${tld}, such as "dim2.${tld}"`); } return products as string[]; } diff --git a/packages/network-config/tests/networks.test.ts b/packages/network-config/tests/networks.test.ts index 26b6eb5..bc03c5a 100644 --- a/packages/network-config/tests/networks.test.ts +++ b/packages/network-config/tests/networks.test.ts @@ -316,6 +316,5 @@ describe('turnProofProducts', () => { it('rejects a product that is not a name under the network TLD', () => { assert.throws(loadWith({ turnProofProducts: ['dim2'] }), /dotNS names under \.testnet/); assert.throws(loadWith({ turnProofProducts: ['dim2.paseo'] }), /dotNS names under \.testnet/); - assert.throws(loadWith({ turnProofProducts: [] }), /dotNS names under \.testnet/); }); }); diff --git a/packages/network-config/tests/overlay.test.ts b/packages/network-config/tests/overlay.test.ts index bd37ad1..276e008 100644 --- a/packages/network-config/tests/overlay.test.ts +++ b/packages/network-config/tests/overlay.test.ts @@ -118,15 +118,19 @@ describe('network overlay: TURN proof products', () => { assert.deepEqual(overlaid.turnProofProducts, ['dim2.testnet', 'vox.testnet']); }); - it('keeps the network file\'s products when the section does not set them', () => { + it('leaves the network file\'s products unchanged when the section omits them', () => { assert.deepEqual(applyOverlay(net, write('none.json', { previewnet: {} })).turnProofProducts, ['dim2.testnet']); }); it('rejects a product under another TLD, which no host on this network proves as', () => assert.throws( () => applyOverlay(net, write('tld.json', { previewnet: { turnProofProducts: ['dim2.paseo'] } })), - /previewnet\.turnProofProducts must be a non-empty array of dotNS names under \.testnet/ + /previewnet\.turnProofProducts must be an array of dotNS names under \.testnet/ )); + + it('turns the route off with an empty list', () => { + assert.deepEqual(applyOverlay(net, write('off.json', { previewnet: { turnProofProducts: [] } })).turnProofProducts, []); + }); }); describe('network overlay: a mistake fails instead of deploying defaults', () => { From 710ce28fd9378ebfc341f33e451d632fa7fac813 Mon Sep 17 00:00:00 2001 From: Maksym H Date: Sun, 15 Nov 2026 13:33:48 +0000 Subject: [PATCH 6/6] Name the genesis wait for what it does --- packages/cli/src/commands/turn.ts | 6 +++--- packages/cli/tests/turn-proof.test.ts | 14 +++++++------- packages/network-config/tests/dub.test.ts | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/commands/turn.ts b/packages/cli/src/commands/turn.ts index 9e676e4..ef35b56 100644 --- a/packages/cli/src/commands/turn.ts +++ b/packages/cli/src/commands/turn.ts @@ -39,7 +39,7 @@ export function dubTurnEnv(ctx: ServiceContext): Record { } /** The chain's genesis hash, once its RPC answers; it waits as long as that takes. */ -export async function genesisHash(rpcUrl: string, wait: Sleep = sleep): Promise { +export async function waitForGenesisHash(rpcUrl: string, wait: Sleep = sleep): Promise { for (let attempt = 1; ; attempt++) { try { return await rpc(asHttp(rpcUrl), 'chain_getBlockHash', [0]); @@ -52,9 +52,9 @@ export async function genesisHash(rpcUrl: string, wait: Sleep = sleep): Promise< export async function printDubTurnEnv(ctx: ServiceContext): Promise { const env = dubTurnEnv(ctx); - // turn-api refuses to start with the proof route on and no genesis to check People against. + // turn-api exits at start when the proof route is on and no genesis is set. if (process.env.TURN_PROOF_ENABLED === 'true' && process.env.TURN_PROOF_RPC_URL) { - env.TURN_PROOF_GENESIS = await genesisHash(process.env.TURN_PROOF_RPC_URL); + env.TURN_PROOF_GENESIS = await waitForGenesisHash(process.env.TURN_PROOF_RPC_URL); } for (const [k, v] of Object.entries(env)) { console.log(`export ${k}='${v.replace(/'/g, `'\\''`)}'`); diff --git a/packages/cli/tests/turn-proof.test.ts b/packages/cli/tests/turn-proof.test.ts index a623b1b..0f6a59e 100644 --- a/packages/cli/tests/turn-proof.test.ts +++ b/packages/cli/tests/turn-proof.test.ts @@ -3,12 +3,12 @@ import assert from 'node:assert/strict'; import http from 'node:http'; import type { AddressInfo } from 'node:net'; -import { genesisHash } from '../src/commands/turn.js'; +import { waitForGenesisHash } from '../src/commands/turn.js'; const GENESIS = `0x${'ab'.repeat(32)}`; /** A People RPC that fails `failures` requests before answering chain_getBlockHash(0). */ -async function people(failures: number): Promise<{ url: string; close: () => void }> { +async function startPeopleRpc(failures: number): Promise<{ url: string; close: () => void }> { let seen = 0; const server = http.createServer((req, res) => { let body = ''; @@ -25,13 +25,13 @@ async function people(failures: number): Promise<{ url: string; close: () => voi return { url: `ws://127.0.0.1:${(server.address() as AddressInfo).port}`, close: () => server.close() }; } -describe('genesisHash', () => { - // dub-api starts beside the chains, so People may not answer yet; turn-api refuses to start - // with the proof route on and no genesis, and nothing restarts dub-api if it exits. +describe('waitForGenesisHash', () => { + // dub-api starts beside the chains, so People may not answer yet; turn-api exits at start + // when the proof route is on and no genesis is set, and nothing restarts dub-api if it exits. it('waits for People to answer', async () => { - const rpc = await people(3); + const rpc = await startPeopleRpc(3); try { - assert.equal(await genesisHash(rpc.url, async () => {}), GENESIS); + assert.equal(await waitForGenesisHash(rpc.url, async () => {}), GENESIS); } finally { rpc.close(); } diff --git a/packages/network-config/tests/dub.test.ts b/packages/network-config/tests/dub.test.ts index a3489b1..1ef8da2 100644 --- a/packages/network-config/tests/dub.test.ts +++ b/packages/network-config/tests/dub.test.ts @@ -171,9 +171,9 @@ describe('dubServices', () => { }); describe('TURN proof issuance', () => { - // turn-api mounts /api/v1/turn/issue-with-proof only with these set, and refuses a product + // turn-api mounts /api/v1/turn/issue-with-proof only with these set, and rejects a product // missing from the list. Jollity fetches its relay credentials there. - it('enables the proof route on dub-api for the given products, against local People', () => { + it('enables the proof route on dub-api for the listed products, against local People', () => { const [api, ...workers] = dubServices(PORTS, undefined, undefined, ['dim2.testnet', 'vox.testnet']); assert.equal(api.env.TURN_PROOF_ENABLED, 'true'); assert.equal(api.env.TURN_PROOF_RPC_URL, api.env.PEOPLE_RPC_URL);