2. Guide for DApp developers

2. Guide for DApp developers

Goal: get a Hydra Head running in a few minutes to test your DApp, then move to preprod. Finish 1. Prepare your machine first.

PathWhen to useWhat you need
A. Offline on your own machineTest L2 transaction logic, Plutus scripts, DApp UIDocker, Node.js. No funds, no credentials
B. On preprodTest the real L1 flow: open, deposit, close, fanoutBlockfrost project id, test ADA from the faucet

A. Offline head on your own machine

A1. Run Hexcore and complete first-time setup

bash
hexcore

The first time, the terminal prints a setup link with a one-time token:

Hexcore is not configured yet. Open this link to finish setup:
  http://localhost:4000/setup?token=…

Open the link and answer three questions:

  1. Network: choose preprod (changeable later).
  2. How do heads connect to the chain? Choose Offline; skip the credential.
  3. Admin account: a password of at least 8 characters, or leave it empty and Hexcore generates one. A generated password is shown only once, so save it immediately; if you lose it, use hexcore password.

Everything can be changed later in Settings.

A2. Create a head

  1. Click Create a Hydra Head.
  2. Kind of head: Single machine.
  3. Enter a Name and the number of Nodes (up to 5; 1 is enough to try things, 2–3 to test several signers).
  4. Chain backend: Offline — no layer 1. hydra-node version: keep 2.4.2.
  5. Click Create head. Hexcore generates the Hydra and Cardano keys for each node.

Each node is seeded with 20,000 ADA in the starting UTxO by default, which exists only inside this head; use the initial UTxO section of the form to write your own.

A3. Start

Click Start. The first time it pulls an image of about 200 MB; an offline head opens immediately once the nodes are up. If it fails, check the Container log tab.

A4. Send commands to a node

Console in the UI. Head page → pick a node → Console. Command templates only load into the editor for you to edit; Close and SafeClose ask for confirmation.

From your code. In the same panel, open the Connect tab:

  1. Create token (give it a label). The hxn_… token is shown only once.
  2. Copy the WebSocket URL and the sample code, for example with @hydra-sdk/bridge:
ts
import { HydraBridge } from '@hydra-sdk/bridge';

const bridge = new HydraBridge({
    url: 'ws://localhost:4000/api/heads/1/nodes/0/socket?X-Api-Key=hxn_…',
    autoReconnect: true,
});

The node's HTTP API goes through the same proxy, for example to read the UTxO:

bash
curl -H "X-Api-Key: hxn_…" http://localhost:4000/api/heads/1/nodes/0/socket/snapshot/utxo

1 is the head number, 0 is the node index. A token only opens the nodes of one head; revoking it closes the connections using it immediately.

Keys for signing L2 transactions. Each node's Cardano key is at:

~/.hexcore/heads/head-<id>/keys/n<index>.cardano.sk

Use it to sign NewTx. On an offline head the key holds no real money; on preprod it is the real key of a funded address, so keep it secret.

A5. Start over

ActionResult
ResetWipes persistence; an offline head returns to its starting UTxO
EditOnly if the head never ran or failed to start; otherwise Reset first
StopRemoves containers, keeps keys and state
DeleteDeletes the head, containers and state
hexcore cleanDeletes every head and container, keeps keys

B. Head on preprod with Blockfrost

B1. Get a Blockfrost project id

Create a free project at blockfrost.io and choose Cardano preprod. The project id must start with preprod: a key for another network gives no error but creates a head that never sees its own transactions. Enter the key under Settings → Blockfrost, or per head/node on the create form.

B2. Create the head and fund the nodes

  1. Create a Hydra Head → Single machine → set Nodes → Chain backend: Blockfrost → hydra-node 2.4.2 → Create head.
  2. The head page lists each node's preprod address. Click Open the faucet and request test ADA for each address (it pays L1 transaction fees).
  3. If you already have a funded address, choose Use existing Cardano payment keys and paste its signing key. Extended BIP32 keys are rejected: hydra-node needs a plain payment key.

Start is blocked when a node has no funds; if the balance cannot be read the button stays enabled, so check it yourself first.

B3. Start, open the head, move funds in

  1. Start (slower than an offline head: Blockfrost lags the tip by 50–80 seconds).
  2. Once the nodes are running, open a node's Console and send Init. With hydra-node 2.x the head opens as soon as the transaction is confirmed on L1.
  3. Move funds in with a deposit: call POST /commit on the node's HTTP API (through the proxy, with an access token), sign the returned transaction with the wallet that owns the UTxO, then submit it to L1. See the Hydra documentation.
  4. Test your DApp as in path A.
  5. To finish: send Close (or SafeClose), wait for the contestation period to end, then send Fanout to return the UTxO to L1.

Warning: a Blockfrost head can crash-loop on a token with an empty asset name (hydra#2923); a DApp using such tokens should choose cardano-node.

B4. Use cardano-node instead of Blockfrost

If you already run a synced preprod cardano-node on the same machine: Settings → cardano-node socket → Find socket. Hexcore scans and reports which socket answers on which network; a socket owned by another user (such as root in Docker) still works, and the screen explains how to grant access.

C. Development tips

  • Run in the background: hexcore start --detach, hexcore status, hexcore logs -f, hexcore stop (heads keep running). On macOS, hexcore service install registers launchd to start on login.
  • Several hydra-node versions side by side: each head pins its own image, so you can compare 2.4.1 and 2.4.2; the form also accepts any image.
  • Protocol parameters are editable on the create form; Fetch latest reads the newest values while keeping your edits. The Resources tab measures CPU and RAM of the nodes.
  • Share read-only (Connect tab): anyone can watch frames without a token but cannot send commands.
  • Automation: every action has an HTTP API, documented at http://localhost:4000/api-docs.
  • Do not run two Hexcores on the same Docker daemon unless you must: container names (hexcore-h1-n0…) can collide between two installs (#6).