# Connect a device: account, agreement and enrolment

Calls go to https://supply.coronium.ai: each block sets `SUPPLY` to it unless you exported `SUPPLY`, and enters one folder, `~/coronium` unless you exported `CORONIUM_DIR`. The blocks use curl, jq, sha256sum and python3 and keep every answer in a file only you can read: check a file by its keys, never by printing it. Enrol a device only when its owner, and whoever pays for its connection, agreed.

On a hosting or data-centre network a device is withheld as `class_not_sold` and earns nothing: tell your person before they sign.

## Steps

1. Sign up once, then sign in.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
mkdir -p "${CORONIUM_DIR:=$HOME/coronium}" && cd "$CORONIUM_DIR" || exit
[ ! -e coronium-account.json ] || jq -e 'has("accessCode")' coronium-account.json >/dev/null || { echo "coronium-account.json holds no accessCode: correct or move it" >&2; exit 1; }
if [ ! -e coronium-account.json ]; then
  curl -sS "$SUPPLY/app/api/signup/challenge" -o coronium-challenge.json
  jq -e 'has("challenge") or error("\(.code): \(.cause) Fix: \(.fix) \(.retryAt // "")")' coronium-challenge.json >/dev/null || exit 1
  python3 -c 'import hashlib,json,sys,itertools;a=json.load(sys.stdin);c=a["challenge"];print(json.dumps({"challenge":c,"nonce":str(next(n for n in itertools.count() if int.from_bytes(hashlib.sha256(f"{c}:{n}".encode()).digest(),"big")>>256-a["difficulty"]==0))}))' < coronium-challenge.json | curl -sS -c coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/signup" -o coronium-signup.json
  jq -e 'has("accessCode") or error("\(.code): \(.cause) Fix: \(.fix) \(.retryAt // "")")' coronium-signup.json >/dev/null || exit 1
  mv coronium-signup.json coronium-account.json
fi
jq -c '{code: .accessCode}' coronium-account.json | curl -sS -c coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/login"
```

Expect: `{"signedIn":true}`. A new account's file holds `accessCode`, `accountId` and `credential`, shown this once. A refusal prints its code, cause and fix, and no account is made: on `registration_closed`, your person makes `coronium-account.json` in this folder with `chmod 600`, holding `{"accessCode": "<access code>"}`, then rerun; on `signups_paused` or `rate_limited`, wait until the time named. A file there without an access code stops the block: correct it or move it. Safe to repeat: yes; it signs up only when the file is absent.

2. Read the partner agreement, and show it.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS -b coronium.cookies "$SUPPLY/app/api/consent" -o coronium-consent.json
jq -j '.text // empty' coronium-consent.json > coronium-agreement.md
jq '{version, sha256, approved, status, code}' coronium-consent.json
```

Expect: `approved` true, and `status` `unsigned`, `signed` or `countersigned`; `sha256sum coronium-agreement.md` prints `sha256`. Show your person `coronium-agreement.md` whole, with `version` and `sha256`, and wait for their yes. While `approved` is false nobody can sign. Safe to repeat: yes.

3. Sign, after the yes. Write `coronium-signer.json` as `{"name": "<full legal name>", "organisation": "<only when they sign for one>"}`: the person or organisation that controls the account, never you, trimmed, without `|` or a line break. The block keeps the signed body in `coronium-signature.json`.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
[ -s coronium-signature.json ] || {
  now=$(date -u +%Y-%m-%dT%H:%M:%S.000Z)
  sig=$(jq -j --arg now "$now" --slurpfile s coronium-signer.json '"coronium-partner-agreement|\(.version)|\(.accountId)|\($s[0].name)|\($s[0].organisation // "")|\(.sha256)|\($now)"' coronium-consent.json | sha256sum | cut -d' ' -f1)
  jq -c --arg now "$now" --arg sig "$sig" --slurpfile s coronium-signer.json '{consent: {version, sha256}, agreed: true, signer: $s[0], signedAt: $now, signature: $sig}' coronium-consent.json > coronium-signature.tmp && mv coronium-signature.tmp coronium-signature.json || exit
}
curl -sS -b coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/consent" < coronium-signature.json | jq '{signatureId, version, code}'
```

Expect: a `signatureId`. [`/kit/SIGNING.md`](/kit/SIGNING.md) has the fingerprint with a worked example, and what is stored with a signature and each enrolment. If refused: `bad_signed_at` (more than 10 minutes from the server's clock: fix the clock, delete `coronium-signature.json`, repeat), `already_signed` (signed already with other values; that stands, go on), `consent_outdated` (step 2, then delete `coronium-signature.json`). Safe to repeat: yes, with the same file. A signature is never withdrawn.

4. Enrol one device.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
[ -s coronium-enrol.json ] || { jq -c --arg id "$(od -An -tx1 -N8 /dev/urandom | tr -d ' \n')" '{consent: {version, sha256}, agreed: true, requestId: $id}' coronium-consent.json > coronium-enrol.tmp && mv coronium-enrol.tmp coronium-enrol.json || exit; }
[ -s coronium-device.json ] || {
  curl -sS -b coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/devices" < coronium-enrol.json -o coronium-device.tmp
  jq -e 'has("deviceSecret")' coronium-device.tmp >/dev/null && mv coronium-device.tmp coronium-device.json || { jq '{code, cause, fix, deviceId}' coronium-device.tmp; exit 1; }
}
jq '{deviceId, relay, state}' coronium-device.json
```

Expect: `deviceId`, `relay` and `state` `new`; the file also holds `deviceSecret`, shown this once. A refusal prints its code, cause and fix and keeps no device file; act on `fix` and repeat: `agreement_unsigned` (step 3), `device_cap_reached` (the account holds `terms.deviceLimit` of `GET /app/api/earnings`), `enrolment_repeated` (this `requestId` enrolled `deviceId` already and its secret is gone: withdraw it, delete `coronium-enrol.json`, enrol again). Safe to repeat: yes; one `requestId` enrols once. For a second device, in this same folder, first move `coronium-enrol.json` and `coronium-device.json` aside.

5. Write the settings file.

```sh
umask 077
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
jq -er 'if (.relay and .deviceId and .deviceSecret) then "CORONIUM_RELAY=\(.relay)\nCORONIUM_DEVICE=\(.deviceId)\nCORONIUM_DEVICE_SECRET=\(.deviceSecret)" else error("no relay, device or secret in coronium-device.json") end' coronium-device.json > agent.env.tmp && mv agent.env.tmp agent.env
```

Expect: no output; `cut -d= -f1 agent.env` prints the three names. A null `relay` means ask the operator. Safe to repeat: yes.

## Then

[Choose and check the agent file](/guides/device-files.md), run it one way, and [read the device's state](/guides/device-states.md). Tell the person sharing the device [what it refuses](/guides/device-states.md#what-a-device-refuses).
