Skip to content

About

RangeGuard uses Ledger's Device Management Kit (DMK) to route every payout through a hardware-enforced signing flow

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

RangeGuard — Hardware-Enforced Treasury Payout Agent

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

Setup

Prerequisites

  • Docker (for Speculos emulator)
  • Node.js 20+ with pnpm
  • bash

1. Configure environment

cp .env.example .env

Edit .env to match your setup — all values have sensible defaults for local development.

2. Build the Ethereum .elf binary

pnpm build-elf

This clones app-ethereum, applies an NBGL header patch, builds inside the Ledger App Builder Docker container (target: nanos2), and places apps/eth_nanos2.elf.

3. Start Speculos

docker compose up -d

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

4. Install dependencies

pnpm install

The postinstall hook patches @ledgerhq ESM packages for compatibility.

5. Run the agent + dashboard

pnpm dev

This 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 only

6. Trigger a payout

Open 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.01

7. (Optional) Broadcast to Ethereum Sepolia

The sender address is derived from the Ledger device at the configured derivation path (DERIVATION_PATH in .env). Fund it with testnet ETH before broadcasting:

  1. Run the agent:
    pnpm dev:agent
  2. Look for the log line Sender address: 0x… — that's your funded wallet
  3. Go to https://sepoliafaucet.com and paste that address
  4. Set BROADCAST=true in .env
  5. 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

Architecture

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

Configuration

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.

Known Gotchas

  • Derivation path: no m/ prefix. DMK's DerivationPathUtils.splitPath splits on / and calls parseInt on each segment. parseInt("m") returns NaN and throws. Use 44'/60'/0'/0/0, not m/44'/60'/0'/0/0.
  • Speculos transport uses the HTTP API port (5000), not the raw APDU port (40000). The speculosTransportFactory posts APDUs via HTTP REST. Pointing it at port 40000 causes connect() to hang indefinitely.
  • automation.json should 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 _busy lock to prevent overlapping tick() 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 for Sender address: 0x… and fund it at the Sepolia faucet before broadcasting.

Security Model

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

Resources

License

MIT — built for a sponsored content piece about Ledger's Agent Stack.

About

RangeGuard uses Ledger's Device Management Kit (DMK) to route every payout through a hardware-enforced signing flow

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages