Prediction markets need autonomous payout agents, but those agents can't safely hold keys. Hardware is the missing layer. RangeGuard uses Ledger's Device Management Kit (DMK) to route every payout through a hardware-enforced signing flow — even when the agent runs unattended.
Dashboard (Next.js) ──→ POST /api/trigger
↓
state.json ←─────────── agent.ts (polls every 10s)
↓
Ledger DMK (5-step flow)
Init → Session → State → App → Sign
↓
Speculos (emulated Nano S Plus)
[tx appears on VNC screen]
↓
Signed tx → Ethereum Sepolia
↓
logs.jsonl → SSE → Dashboard live feed
- Docker (for Speculos emulator)
- Node.js 20+ with pnpm
- bash
cp .env.example .envEdit .env to match your setup — all values have sensible defaults for local development.
pnpm build-elfThis clones app-ethereum, applies an NBGL header patch, builds inside the Ledger App Builder Docker container (target: nanos2), and places apps/eth_nanos2.elf.
docker compose up -dSpeculos runs the Nano S Plus emulator at:
- noVNC web client →
localhost:41000 - REST API (buttons) →
localhost:5000
The setup uses patched_seproxyhal.py — a patched MCU seproxyhal for Speculos compatibility.
pnpm installThe postinstall hook patches @ledgerhq ESM packages for compatibility.
pnpm devThis runs both the agent (polls state.json every 10s) and the Next.js dev server (Turbopack) concurrently. You can also run them separately:
pnpm dev:agent # agent only
pnpm dev:web # dashboard onlyOpen http://localhost:3000 in your browser. Click Trigger Payout — watch the DMK steps light up and the Speculos screen show the transaction for confirmation.
You can also trigger from the CLI:
pnpm trigger --winner 0x6136C315631F8BF6c0DdC2C948e4908a63B26F55 --amount 0.01The sender address is derived from the Ledger device at the configured derivation path (DERIVATION_PATH in .env). Fund it with testnet ETH before broadcasting:
- Run the agent:
pnpm dev:agent
- Look for the log line
Sender address: 0x…— that's your funded wallet - Go to https://sepoliafaucet.com and paste that address
- Set
BROADCAST=truein.env - Trigger a payout — the agent will submit the signed tx and log an explorer link
To generate a different address, change the derivation path index in .env:
DERIVATION_PATH=44'/60'/0'/0/0 # first account
DERIVATION_PATH=44'/60'/0'/0/1 # second account
DERIVATION_PATH=44'/60'/0'/0/2 # third account| Component | Role |
|---|---|
src/agent.ts |
Polls state.json, orchestrates the DMK flow, writes logs.jsonl |
src/ledger.ts |
DMK integration — 5-step: Init → Session → Device State → App Open → Sign |
src/speculos.ts |
REST API wrapper to press buttons on the emulated device |
src/trigger.ts |
CLI tool to write a payout trigger to state.json |
web/ |
Next.js 15 (Turbopack) dashboard — SSE live feed, trigger panel, VNC embed |
state.json |
Shared state file — the agent polls this; the dashboard and trigger write to it |
logs.jsonl |
Append-only log — agent writes, SSE endpoint tails |
.agents/skills/ |
DMK agent skills (business logic, intent vocabulary, implementation patterns, SDK reference) |
docker-compose.yml |
Speculos emulator service definition |
automation.json |
Speculos automation rules (currently empty — DMK handles app lifecycle) |
patched_seproxyhal.py |
Patched Speculos MCU seproxyhal for compatibility |
build-elf.sh |
Builds the Ethereum app ELF binary via Docker |
scripts/patch-ledger-packages.sh |
Post-install hook — patches @ledgerhq ESM packages |
All configuration lives in .env. See .env.example for the full reference. Key variables:
| Variable | Default | Description |
|---|---|---|
POLL_INTERVAL |
10000 |
Agent poll interval in ms |
RPC_URL |
https://eth-sepolia.g.alchemy.com/v2/YOUR_API_KEY |
Ethereum Sepolia JSON-RPC endpoint for broadcast |
BROADCAST |
false |
Submit signed txs to the RPC |
MAX_RETRIES |
3 |
Max payout retries before giving up |
BACKOFF_BASE_MS |
15000 |
Base backoff delay between retries (capped at 60s) |
SPECULOS_API_URL |
http://localhost:5000 |
Speculos HTTP REST API (DMK transport + button automation) |
DERIVATION_PATH |
44'/60'/0'/0/0 |
HD wallet derivation path (no m/ prefix) |
CHAIN_ID |
11155111 |
Ethereum Sepolia chain ID |
BUTTON_DELAY_MS |
2000 |
Delay before confirming on device |
SCROLL_PAGES |
6 |
Number of right-presses to scroll through tx details |
Gas fees (maxFeePerGas, maxPriorityFeePerGas) and nonce are fetched dynamically from the network — no manual configuration needed.
- Derivation path: no
m/prefix. DMK'sDerivationPathUtils.splitPathsplits on/and callsparseInton each segment.parseInt("m")returnsNaNand throws. Use44'/60'/0'/0/0, notm/44'/60'/0'/0/0. - Speculos transport uses the HTTP API port (5000), not the raw APDU port (40000). The
speculosTransportFactoryposts APDUs via HTTP REST. Pointing it at port 40000 causesconnect()to hang indefinitely. automation.jsonshould stay empty. The DMK handles app lifecycle (opening the Ethereum app). Speculos OCR automation rules fire on every match with no state guard — they interfere with the signing flow by pressing buttons at the wrong time.- No concurrent signing. The agent uses a
_busylock to prevent overlappingtick()calls from hitting the device simultaneously. - Fund the derived address. The sender address comes from the Ledger device via
signerEth.getAddress(). Check the agent logs forSender address: 0x…and fund it at the Sepolia faucet before broadcasting.
- The agent is a participant, not a custodian. Private keys never leave the device. The agent can request a signature but cannot extract the key.
- Prompt injections end at the screen. Even if the agent is compromised, the human (or the emulator in this demo) must physically confirm the transaction — what you see on the Speculos screen is what gets signed.
- Compromised runtimes can't move funds. The DMK session validates device state before every operation. An attacker who controls the host machine still can't sign without the device's consent.
- Ledger Agent Stack Overview
- Ledger Agent Skills
- Device Management Kit
- Speculos Emulator
- Ethereum Sepolia Testnet
MIT — built for a sponsored content piece about Ledger's Agent Stack.