# Many devices: enrolment by token

A token lets each device enrol itself: no device holds the account's session or access code, and no device secret crosses the network. Calls go to https://supply.coronium.ai. The rules do not change, `terms.deviceLimit` of `GET /app/api/earnings` included.

## Needs

- The operator switches these routes on. Until then `POST /app/api/enrol-tokens`, `GET /app/api/enrol-tokens`, `POST /app/api/enrol-tokens/{tokenId}/revoke` and `PUT /app/api/devices/{deviceId}` answer 503 `enrol_tokens_off`: enrol each device as in [enrolment](/guides/device-enrol.md).
- An account signed in that signed the text in force ([enrolment](/guides/device-enrol.md), steps 1 to 3), and the yes of the devices' owner.
- Of several residential devices behind one public address only the first carries traffic (`same_exit`), and a data-centre device none (`class_not_sold`).

## Steps

1. Mint a token, with the access code as proof. A `uses` above what the account may still enrol is refused `invalid` with the limit in `most`; `expiresInHours` and `label` are optional.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
jq -c '{consent: {version, sha256}, agreed: true, uses: 1, label: "rack_a"}' coronium-consent.json > coronium-mint.json
jq -c --slurpfile m coronium-mint.json '$m[0] + {proof: {code: .accessCode}}' coronium-account.json | curl -sS -b coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/enrol-tokens" -o minted.json
if jq -e '.tokenId | strings' minted.json > /dev/null; then
  jq -r '"Authorization: Bearer \(.token)"' minted.json > token.header
  jq -c .consent minted.json > consent.json
else
  jq -r '"\(.code): \(.cause) Fix: \(.fix)"' minted.json; false
fi
```

Expect: `token.header` and `consent.json`, the files each device needs; the token is shown once. If refused, the block prints `<code>: <cause> Fix: <fix>`, ends with a non-zero status and writes neither file: `enrol_tokens_off` (enrol each device as in [enrolment](/guides/device-enrol.md)), `token_limit_reached` (revoke one), `device_cap_reached`, `agreement_unsigned`. Safe to repeat: no; each run mints another.

2. On each device, beside `token.header` and `consent.json`, make the id and secret and write `agent.env` before any call, so a lost answer is sent again with the same pair.

```sh
umask 077
[ -e agent.env ] || {
  head -c 32 /dev/urandom >secret.bin
  id=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \n' | sed -E 's/^(.{8})(.{4})(.{4})(.{4})(.{12})$/\1-\2-\3-\4-\5/')
  printf 'CORONIUM_DEVICE=%s\nCORONIUM_DEVICE_SECRET=%s\n' "$id" "$(od -An -tx1 -v secret.bin | tr -d ' \n')" >agent.env
}
```

3. Exchange the token. The body is `secretSha256`, the SHA-256 of the raw secret bytes, never of their hex text, and `consent`.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
id=$(sed -n 's/^CORONIUM_DEVICE=//p' agent.env)
printf '{"secretSha256":"%s","consent":%s}' "$(sha256sum secret.bin | cut -d' ' -f1)" "$(cat consent.json)" >exchange.json
code=$(curl -sS -o answer.json -w '%{http_code}' -X PUT -H @token.header -H 'Content-Type: application/json' \
  --data-binary @exchange.json "$SUPPLY/app/api/devices/$id")
case "$code" in 200|201) printf 'CORONIUM_RELAY=%s\n' "$(jq -r '.relay // empty' answer.json)" >>agent.env && rm -f token.header secret.bin exchange.json ;; esac
echo "$code"
```

Expect: `201` (`applied`), or `200` (`already_applied`, no use spent) for the same id, secret and token. The answer holds `deviceId`, `relay`, `state` and `consent`, never a secret. If refused: `bad_enrol_token`, `enrol_token_expired`, `enrol_token_used` or `enrol_token_revoked` (mint another), `consent_outdated` (sign the text in force, then mint again), `device_id_taken` (delete `agent.env` and `secret.bin`, start at step 2). A 429 means more than 5 requests a second from one address: wait and repeat. A null `relay` means ask the operator. Safe to repeat: yes.

Then run the agent with `agent.env` as for any device. A worked digest, with made-up bytes: the secret `000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f` has the `secretSha256` `630dcd2966c4336691125448bbb25b4ff412a49c732db2c8abc1b8581bd710dd`.

**Never bake `agent.env` into an image**: copies of one id and secret are one device, each replacing the last at the relay. Bake `token.header` and `consent.json`, and make the pair on first boot.

## Read and revoke

`GET /app/api/enrol-tokens` lists the tokens with `state` (`live`, `used`, `expired`, `revoked` or `outdated`) and the `devices` each made, never the token. `POST /app/api/enrol-tokens/{tokenId}/revoke` with `{}` revokes one; replacing the access code revokes all. Revoking withdraws no device.
