# Node connection and security

> Connecting the client to your Ergo node, and the two secrets that guard its own API.

Source: https://lithosprotocol.org/docs/configuration/node

**Level:** Standard

*For anyone running the Lithos client. No Ergo or smart contract experience needed.*

The `node` block tells the client where your Ergo node is and how to authenticate with it. Two more
settings guard the client's own HTTP API. This page covers both, and it is the only configuration page
where every setting is required.

It does not cover setting up the node itself. For that, read
[Setting up an Ergo node](https://lithosprotocol.org/docs/startup/ergo-node).

> **Info: Lithos Launcher sets these for you**
>
> If you use [Lithos Launcher](https://lithosprotocol.org/docs/startup-intro#setting-up-with-lithos-launcher), it writes
> `node.url`, `node.key`, `node.storagePath`, `node.pass`, `node.networkType`, `node.mempoolSorting` and
> both API secrets itself. The keys and the wallet password are stored encrypted by the operating system
> and never written to the file. Change the node or Lithos API key with **API keys** under **Settings**,
> not in `lithos.conf`.

## Node connection

Every setting here is **required**. The client refuses to start if any is missing:

| Setting | Default | Range | What it does |
|---|---|---|---|
| `node.url` | `http://127.0.0.1` | An `http` or `https` URL with a host | Your node's address. Include a port to use it, or leave it off and the client appends the default for the network |
| `node.key` | empty | Any string | The node's REST API key, matching the `apiKeyHash` in your `ergo.conf` |
| `node.storagePath` | empty | Any string | Full path to the node wallet's `keyfile.json`. The client signs transactions with it |
| `node.pass` | empty | Any string | The node wallet password. The client checks it against that keystore at startup, and unlocks the node's wallet with it |
| `node.networkType` | `MAINNET` | `MAINNET` or `TESTNET` | Decides the default node port, address prefixes and contract set. Case-insensitive |
| `node.explorerURL` | `default` | A URL, or the literal `default` | Required by the Ergo SDK the client is built on. The client reads nothing from it. `default` picks the public explorer for the network |
| `node.numAddresses` | 32 | 1 to 1000 | How many EIP-3 addresses the prover holds keys for |

### The client reads the chain from your node alone

Every box, transaction and block the client uses comes from your node. `node.explorerURL` exists only
because the Ergo SDK the client is built on requires one, and leaving it at `default` is correct. This
is also why the node must run with `extraIndex = true`: the client looks boxes up by token and script
through the node's own index.

### The port is appended, not required

The client parses `node.url` and appends a default port only when the URL does not already name one:
`9053` on mainnet, `9052` on testnet. Both of these work and mean the same thing on mainnet:

```hocon
node.url = "http://127.0.0.1"
node.url = "http://127.0.0.1:9053"
```

A URL without a scheme does not work. `127.0.0.1` alone is rejected at startup, because the client
cannot tell a host from a path without one.

### Why `numAddresses` matters more than it looks

The client builds its signing prover with one secret per address index, from zero to
`numAddresses - 1`. Any box paid to an address past that last index is one the client cannot sign for.

Lender keys are derived from these addresses, which is why `emission.maxLenderKeys` must not exceed
`numAddresses`. Startup refuses the pair and names the value to raise.

> **Warning: Raising it is safe. Lowering it strands funds**
>
> Addresses are derived in order, so raising the count only adds new ones. Lowering it below a key that
> has already received a permit return or a coinbase leaves that ERG unspendable until you raise it
> back. Raising it costs a little startup time and nothing else.

### The client unlocks the node wallet itself

The client creates new lender keys through your Ergo node's wallet, and the node refuses to create one
while its wallet is locked. The node locks its wallet every time it restarts, so the client keeps the
wallet unlocked with `node.pass`.

At startup, the client checks `node.pass` against the keystore at `node.storagePath` and refuses to
start if they do not match. It then reads the node wallet's status at startup and before it creates
any lender key. If the node reports the wallet locked, the client unlocks it and logs the following
line once:

```text
Unlocked the node wallet with node.pass, so lender keys can be derived
```

If the unlock fails, the client logs one warning with the reason, and joins can use only the lender
keys the wallet already holds. The client tries again after 60 seconds, and logs again only when the
reason changes:

```text
Lender keys cannot be derived: the node wallet is not initialized. Joins can use only keys the wallet already holds
```

The password travels to `node.url` in the body of the unlock request, the same way `node.key` travels
with every request. If your Ergo node runs on another machine, use an `https` URL. See
[Collateral and emission](https://lithosprotocol.org/docs/configuration/emission#where-lender-keys-come-from) for how the client uses the
keys.

## Matching the node's mempool rule

| Setting | Default | Range | What it does |
|---|---|---|---|
| `node.mempoolSorting` | `bySize` | `bySize` or `byExecutionCost` | Which rule your node ranks mempool transactions by |

Ergo nodes rank the mempool either by fee per serialized byte or by fee per unit of execution cost, and
**the node does not report which it uses**. The client has to be told, so that the blocks it builds are
packed against the rule your node actually applies.

Set it to match `ergo.node.mempoolSorting` in your `ergo.conf`. The node's own default is `bySize`,
which is also the client's.

This setting is **not validated at startup**. Anything the client does not recognise reads as `bySize`,
so a typo here fails silently rather than refusing to start. Check the spelling against your
`ergo.conf` by eye.

## Client API security

| Setting | Default | Range | What it does |
|---|---|---|---|
| `lithos.apiKeyHash` | hash of `hello` | Exactly 64 hexadecimal characters. Not the hash of `hello` on mainnet | BLAKE2b-256 hash of the key that guards the client's authenticated endpoints |
| `play.http.secret.key` | `changethissecret` | Any string, not validated | Signs the client's own web session cookies |

Both ship with published placeholder values and both should change before you run anything you care
about.

**On mainnet, the client refuses to start while `lithos.apiKeyHash` is still the hash of `hello`.**
The client's API listens on every network interface, so the published key would open your wallet
endpoints to anyone who can reach the port. Testnet still starts on the shipped hash. Lithos Launcher
sets a random key of its own, so this check never stops a launcher install. Startup names the
setting:

```text
lithos.apiKeyHash
    is still the shipped hash of "hello". Choose your own api key and set its BLAKE2b-256
    here before running on mainnet
```

Set your own hash as described below, then start the client again.

### Setting your own API key hash

The value in the file is the **hash**, not the key. The key is what you send with a request. To
generate a hash, use your node's own hashing endpoint:

1. Open your node's Swagger panel at `http://127.0.0.1:9053/swagger`, or port `9052` on testnet.
2. Under `/utils`, call `POST /utils/hash/blake2b` with your chosen key as the request body.
3. Copy the returned hash into `lithos.apiKeyHash`.
4. Keep the original string. That is what you authenticate with.

Startup checks that the value is 64 hexadecimal characters and nothing else, so pasting the key
instead of the hash is caught immediately.

### Remembering the key in the web panel

The web panel holds your API key in memory, so you enter it again after every page refresh. To keep
it across refreshes, turn on **Remember API key in this browser** under **Settings**. The option is
off by default.

Once it is on, the key is stored in this browser's localStorage, where any script running on the
page can read it, a malicious browser extension included. Leave it off on a shared machine. Turning
the option off deletes the stored key at once.

Every section of the web panel uses the same key, so remembering it in one section remembers it for
all of them. If the browser blocks storage, the panel says so and keeps the key in memory only.

> **Danger: Keep the client's API off the open internet**
>
> The client holds your node API key and wallet password and can sign transactions with your keys. Do
> not forward its port, and do not expose it beyond your local network. The API key is a second line of
> defence, not the first.

## Mainnet node settings

Two settings belong in your **node's** configuration rather than the client's. Mining on mainnet
requires both:

```hocon title="ergo.conf"
ergo.chain.reemission.checkReemissionRules = true
ergo.wallet.checkEIP27 = true
```

The first validates consensus. The second handles payments made by the node wallet. Neither applies on
testnet. The `ergo.conf` in [Setting up an Ergo node](https://lithosprotocol.org/docs/startup/ergo-node) already includes them.

## Next steps

- [Stratum and difficulty](https://lithosprotocol.org/docs/configuration/stratum): connecting your mining software and choosing `diff`.
- [Setting up an Ergo node](https://lithosprotocol.org/docs/startup/ergo-node): the node side of this page.
