> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lumenwipe.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rotate the relay account

> Replace the relay account (mediator) and its key without a client change: fund the new account to its minimum balance, switch the API, drain the old account and keep watching it for 24 hours.

> [!NOTE]
>
> Rehearsed end to end for testnet on the deployed service on 2026-11-17, together with the [fee
> sponsor rotation](https://docs.lumenwipe.com/runbooks/rotate-fee-sponsor.md), with step 10 on 2026-11-18; the [measured
> times](#measured-times) are from that rehearsal. Not yet rehearsed on mainnet.
> `apps/api/tests/integration/relay-rotation.integration.test.ts` runs the same sequence against a
> local API on every pull request.

The relay account (the code calls it the mediator) co-signs the forward of an [exchange close](https://docs.lumenwipe.com/guides/exchanges-and-the-mediator.md). Clients never store its address: the API puts it into every exchange close it builds, so a rotation changes only the API's configuration, a repository variable and one row of the [monitoring plan](https://docs.lumenwipe.com/monitoring-plan.md#1-what-are-we-monitoring).

## When

* **Compromise**: the key may be exposed, or `S4a.Information.1.M.1`, `S4a.Spoofing.1.M.1` or the forward check fired. Do step 1, then the rest without pausing.
* **Planned**: a scheduled rotation, a maintainer leaving. Skip step 1.

## Blast radius

* Only exchange closes. Closes into a wallet the user controls never touch the relay account.
* A close built against the old account but not yet co-signed is refused once the new revision serves, with `400 transaction_structure_not_allowed`, because its forward names the old account. Nothing has been submitted, so the client builds it again and gets the new account.
* A close co-signed before the switch still lands while the old account exists, which is why step 8 waits before the drain. One submitted after the drain fails as a whole (`op_no_account` on the merge, `op_no_source_account` on the forward): no XLM moves, the account pays one fee and stays open, and building again succeeds.
* On mainnet, at most the old account's minimum balance, 1 XLM, is ever at risk, because it holds nothing else. A testnet account created through friendbot holds far more: the testnet accounts retired on 2026-11-17 held about 10,000 XLM each, which the drain in step 9 returned to the operator.

## Steps

`<net>` is `testnet` or `mainnet` and `<NET>` the same in capitals. Run every step in one shell, started like this, so a failed command in a pipe stops the step and the keys live only in a throwaway CLI configuration:

```bash theme={null}
set -o pipefail
export XDG_CONFIG_HOME="$(mktemp -d)"
stellar keys add operator --secret-key
```

Each step that changes the service is an env-only revision. A deploy run that starts in between deploys its candidate with the environment the service has at that moment, so it can carry a half-done rotation forward or undo a step. Hold merges that deploy the API from step 4 until the last env-only revision of the rotation serves (step 5 here, or the sponsor's switch when both rotate), and check that no run is in progress (`gh run list --workflow deploy-api.yml --status in_progress`) before each `gcloud run services update`.

`operator` is the operator account that funds the new relay account and receives the old one's balance; paste its secret at the prompt. It must not be either relay account, and it must be the address in the `Rotation operator (<net>)` row of the [inventory table](https://docs.lumenwipe.com/monitoring-plan.md#1-what-are-we-monitoring) and the `OPERATOR_ADDRESS_<NET>` repository variable: the forward check recognises the drain in step 9 only as a merge into that address.

1. **Compromise only: pause exchange closes.**

   ```bash theme={null}
   gcloud run services update lumenwipe-api --region us-central1 --update-env-vars=MEDIATOR_FORWARD_ENABLED=false
   ```

   Expected: `Done.`, and `POST $API/v1/<net>/mediator/sign` answers `503 exchange_forward_paused`.

2. **Generate the new key**, without showing it:

   ```bash theme={null}
   stellar keys generate new-relay
   stellar keys address new-relay
   ```

   Expected: a `G...` address. This is `<new>` below; `<old>` is the address in the `MEDIATOR_ADDRESS_<NET>` repository variable.

3. **Create the account at its minimum balance only**, 1 XLM (two base reserves), from the operator account. Never fund it through friendbot or with more: a relay account holds no spendable XLM ([threat model, surface 4a](https://docs.lumenwipe.com/threat-model.md)).

   ```bash theme={null}
   stellar tx new create-account --source-account operator --destination <new> --starting-balance 10000000 --network <net>
   curl -s https://horizon-testnet.stellar.org/accounts/<new> | jq -r '.balances[] | select(.asset_type=="native") | .balance'
   ```

   Expected: `1.0000000` (use `https://horizon.stellar.org` on mainnet).

4. **Check the bindings, pin them, then add the secret as a new version.** An entry with `valueFrom.secretKeyRef` comes from Secret Manager. If the relay secret is bound to `latest`, a new version would reach a fresh instance of the current revision before the public key changes, and that instance would answer `503 mediator_key_misconfiguration`: pin the current version number first. On the production service both the relay and the sponsor secret were bound to `latest` at the 2026-11-17 rehearsal, so expect to pin. When the [fee sponsor rotation](https://docs.lumenwipe.com/runbooks/rotate-fee-sponsor.md) runs in the same session, pin both secrets in one revision (23 seconds on 2026-11-17):

   ```bash theme={null}
   gcloud run services describe lumenwipe-api --region us-central1 --format='yaml(spec.template.spec.containers[0].env)'
   gcloud secrets versions list <relay-secret> --filter='state=ENABLED' --format='value(name)' --limit=1
   gcloud run services update lumenwipe-api --region us-central1 \
     --update-secrets=MEDIATOR_SECRET_<NET>=<relay-secret>:<current n>,FEE_ACCOUNT_SECRET_<NET>=<sponsor-secret>:<current sponsor n>
   stellar keys secret new-relay | tr -d '\n' | gcloud secrets versions add <relay-secret> --data-file=-
   ```

   Drop the `FEE_ACCOUNT_SECRET_<NET>` binding from the update when only the relay rotates, and skip the update when the binding already names a version. Expected: the binding names a version number, and `Created version [<n>] of the secret [<relay-secret>].` The `tr` matters: a trailing newline makes the key unreadable. With `pipefail` set, a failed `keys secret` stops here instead of storing an empty version.

5. **Switch the API in one revision.** The secret and the public key must change together; a revision where they disagree answers `503 mediator_key_misconfiguration`.

   ```bash theme={null}
   gcloud run services update lumenwipe-api --region us-central1 \
     --update-secrets=MEDIATOR_SECRET_<NET>=<relay-secret>:<n> \
     --update-env-vars=MEDIATOR_PUBLIC_<NET>=<new>
   ```

   If `MEDIATOR_PUBLIC_<NET>` is bound to a secret as well, add its version as in step 4 and bind it in the same `--update-secrets` instead. Expected: `Done.` and the new revision serving 100 percent; a `describe` now shows version `<n>` and `<new>`. Note the time: step 8 counts from it. Then `curl -s "https://api.lumenwipe.com/v1/<net>/mediator/check/<an exchange address>" -H "Authorization: Bearer $KEY" | jq .available` must print `true`; `false` means the secret and `MEDIATOR_PUBLIC_<NET>` disagree or one is unset, and for a mismatch the API logs `the mediator key does not match its configured public key`.

6. **Confirm the new account co-signs.** On testnet, run the full exchange flow through the deployed web app:

   ```bash theme={null}
   APP_URL=https://www.lumenwipe.com MEDIATOR_PUBLIC_TESTNET=<new> bun run --filter @lumenwipe/api test:mediator
   ```

   Expected: it ends with `E2E PASS`, and its forward shows on Horizon as a payment from `<new>`. On mainnet there is no scripted check, because it would close a real account: confirm instead with the first real exchange close, whose forward must be a payment from `<new>`. In a compromise, turn `MEDIATOR_FORWARD_ENABLED` back to `true` once this passes.

7. **Point the monitors at the new account and keep the old one watched**, before the drain, so the new account is watched from now on and no check reads a drained current address. Edit the repository variables and, in the same hour, merge a pull request that changes the `Mediator account (<net>)` row of the [inventory table](https://docs.lumenwipe.com/monitoring-plan.md#1-what-are-we-monitoring) to `<new>` (on mainnet, also `ACCOUNTS` in `apps/web/public/.well-known/stellar.toml`). The config step fails until the variable and the table agree. Two more places hold the address:

   * `apps/api/tests/unit/service-account-monitors.test.ts` pins the current testnet addresses as `MEDIATOR` and `SPONSOR`; change them in the same pull request. That makes the pull request deploy the API, so merge it only once the last env-only revision of the rotation serves.
   * On testnet, the playground's Vercel variable `PLAYGROUND_EXPECTED_MEDIATOR_TESTNET` (and `PLAYGROUND_EXPECTED_FEE_ACCOUNT_TESTNET` for the sponsor). After changing it, deploy the playground with `vercel deploy --prod` from a clean checkout of `main` linked to the playground project: a plain `vercel redeploy` is cancelled by the playground's ignored build step when no playground file changed.

   ```bash theme={null}
   gh variable set MEDIATOR_ADDRESS_<NET> --body <new>
   gh variable set MEDIATOR_ADDRESS_<NET>_RETIRED --body <old>
   gh workflow run onchain-monitors.yml && gh run watch "$(gh run list --workflow onchain-monitors.yml --limit 1 --json databaseId -q '.[0].databaseId')"
   ```

   Expected: every job succeeds, and the config step lists both `mediator (<net>): <new>` and `mediator (retired) (<net>): <old>, drained into <operator address>`. Check for the retired line: a misspelled variable name is never passed to the workflow and would leave the old address unwatched without an error.

8. **Wait 6 minutes** from the switch in step 5. A signed close is valid for at most 330 seconds (`MAX_SIGNED_VALIDITY_SECONDS`), so after this no close co-signed by the old key can still land.

9. **Drain the old account.** The operator is the transaction source and the old key signs only the merge, so the old address never sources a transaction and its keys check stays quiet. The old key is piped from its previous secret version into the throwaway configuration, never shown:

   ```bash theme={null}
   gcloud secrets versions access <previous n> --secret=<relay-secret> | stellar keys add old-relay --secret-key
   stellar tx new bump-sequence --source-account operator --bump-to 0 --inclusion-fee 200 --network <net> --build-only \
     | stellar tx op add account-merge --source-account operator --op-source old-relay --account <operator address> --network <net> \
     | stellar tx sign --sign-with-key operator --network <net> \
     | stellar tx sign --sign-with-key old-relay --network <net> \
     | stellar tx send --network <net>
   curl -s -o /dev/null -w '%{http_code}\n' https://horizon-testnet.stellar.org/accounts/<old>
   ```

   Expected: `stellar tx send` exits with status 0 and prints the transaction result, and Horizon answers `404` for `<old>`. The next monitor run stays green: the keys check sees no transaction sourced from `<old>`, and the forward check reads this merge into the operator as the drain. For the rest of the 24 hours, any other transaction the old key signs, or any merge or payment out of `<old>` to another address, is reported as a suspected key compromise. The `bump-sequence` to 0 is a no-op that lets the operator be the source; the fee covers its two operations. In a compromise, whoever holds the old key can make the merge fail first (`op_has_sub_entries`, by adding a trustline or a signer): the most that costs is the old account's 1 XLM, and the keys check reports the transaction that did it, whoever sourced it.

10. **After 24 hours, stop watching the old account** and clean up:

    ```bash theme={null}
    gh variable delete MEDIATOR_ADDRESS_<NET>_RETIRED
    gcloud secrets versions disable <previous n> --secret=<relay-secret>
    curl -s -o /dev/null -w '%{http_code}\n' https://api.lumenwipe.com/health
    curl -s "https://api.lumenwipe.com/v1/<net>/mediator/check/<an exchange address>" -H "Authorization: Bearer $KEY" | jq .available
    rm -rf "$XDG_CONFIG_HOME"
    ```

    Use the address of an exchange that requires the relay. Expected: `200` and `true`, since the service binds version `<n>`, not the disabled one; `false` means the key the API holds does not match `MEDIATOR_PUBLIC_<NET>`. The next monitor run lists only `<new>`. The new key now lives only in Secret Manager; keep a copy where the team keeps keys before removing the throwaway configuration, if the team's policy asks for one.

## Measured times

From the testnet rehearsal on the production service, run together with the [fee sponsor rotation](https://docs.lumenwipe.com/runbooks/rotate-fee-sponsor.md):

| Step | Time and result |
| - | - |
| 2 and 3, two new keys and two `create-account` (relay 1 XLM, sponsor 100 XLM) | About 10 seconds for the two creates (2026-11-17) |
| 4, pin both secrets from `latest` to version 1 in one revision | 23 seconds (2026-11-17) |
| 5, switch the relay (secret version 2 and `MEDIATOR_PUBLIC_TESTNET`) | 22 seconds (2026-11-17) |
| 6, `test:mediator` through `www.lumenwipe.com` | 18 seconds, `E2E PASS`, transaction `6065aa4a…4267` (2026-11-17) |
| 7, repository variables, then the pull request for the monitoring plan | Merged within 9 minutes of the switch (2026-11-17); `onchain-monitors.yml` succeeded with the new and the retired addresses |
| 9, drain of each old account | About 6 seconds each; Horizon answered `404` for both afterwards (2026-11-17) |
| 10, `_RETIRED` variables removed, version 1 of both secrets disabled | Done 2026-11-18 at 15:41 UTC; the service binds version 2 of `MEDIATOR_SECRET_TESTNET` and `FEE_ACCOUNT_SECRET_TESTNET`, `/health` answered afterwards, and `mediator/check` for an exchange that requires the relay answered `available: true` |

## Confirm the fix

* `test:mediator` passes against the new account (step 6) and Horizon shows `<old>` as `404` (step 9).
* The on-chain monitors run green with the new address, and with the retired one for 24 hours (step 7).
* To roll back before step 9, bind the previous secret version and the old public key in one `gcloud run services update`, as in step 5. After step 9 the old account is gone; rotate forward instead.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.