# @umis/cli

`umis` is the command-line tool of the UMIS SDK. It plays the hero scenario, verifies and inspects QR frames offline,
generates keys, emulates a Shelly Gen3 relay and benchmarks the verifier. Try it with `npx @umis/cli demo`.

| Command                                                                                  | What it does                                                             |
| ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
| `umis demo [--steps 1,2,3,4] [--qr] [--json]`                                            | runs the hero steps on the demo world and prints a table                 |
| `umis verify (--demo <org>:<policy> \| --context <file>) [--now <epoch>] [<frame> \| -]` | decides whether a one-way or full QR frame is accepted, offline          |
| `umis inspect [<text> \| -]`                                                             | tells what a QR text, link or token is and decodes it, verifying nothing |
| `umis keygen --out <dir>`                                                                | makes a P-256 key pair and prints the public key, key id and fingerprint |
| `umis relay-emu --trust <file> [options]`                                                | runs an emulator of a relay that obeys only signed commands              |
| `umis bench [options]`                                                                   | measures the decision latency of the verifier and plays two timelines    |

Run `umis <command> --help` for the options.

## Try it from a clone

```sh
pnpm install
pnpm build
pnpm -F cli exec umis demo
pnpm -F cli exec umis demo --steps 1 --qr
```

`umis demo` prints a row for every decision with the step, the actor, the decision, the reason, the milliseconds and
the characters of the QR text. Step 4 is a login at the Lumera Intranet over OpenID4VP, played in the same process.

`umis verify --demo lumera:door_low --now 1793606400 -` reads a one-way frame from standard input and decides on it at
the Lumera Software office door of the demo world, at the given time. To decide with a verifier context you saved with
`serializeVerifierContext`, pass `--context <file>` instead of `--demo`.

`umis verify` never reads the clock. Without `--now` it decides at the time recorded in the context. For `--demo` that
is the start of the demo world, `1793606400` or 2026-11-02T08:00:00Z, so a command line gives the same answer on any
day. Decide a live frame at the moment you run the command by passing `--now <epoch seconds>`, for example
`--now $(date +%s)`.

## Benchmark

```sh
pnpm -F cli exec umis bench --n 50 --repeats 3 --out /tmp/umis-bench-smoke    # a short run, to see that it works
pnpm -F cli exec umis bench --crypto webcrypto --out bench-results/webcrypto  # n 1000, 30 repeats, 1 warm-up
pnpm -F cli exec umis bench --crypto noble --out bench-results/noble
```

`umis bench` times how long a verifier takes to decide in the demo world. It has seven scenarios, `oneway-low`,
`full-qr-low`, `s1-accept`, `s1-reject`, `mutual`, `oid4vp` and `gateway`. Pick them with `--scenario all`, the
default, or with names separated by commas. Every repeat first makes `--n` different presentations, untimed, and then
decides them one after the other. The run measures `--repeats` repeats after throwing `--warmup` repeats away. Each
`s1` presentation answers its own request and no ledger is kept across presentations, so `s1-accept` and `s1-reject`
time independent decisions. `--crypto webcrypto` uses the WebCrypto of the runtime. `--crypto noble` uses the
JavaScript implementation that stands in for the engine of Expo Go. `--seed` fixes keys, identifiers and salts. Noble
signatures are deterministic, but WebCrypto signatures stay random, so a `--crypto webcrypto` run is not reproducible
bit for bit.

The table gives the median P50, the P95, P95 REP with its 95 % interval P95 CI95, P99, the mean, the standard deviation
and the maximum, all in milliseconds. It also gives the characters of the QR text, the presentations decided otherwise
than expected and whether the P95 is within one second. P95 is the nearest-rank percentile of all measured decisions.
P95 REP is the mean of the p95 of the repeats, and the interval is the Student-t interval of that mean, so P95 can lie
slightly outside it. One repeat gives no interval, and the column shows `n/a`.

Under the table comes a note for each row that does not time a single decision. `mutual` times both decisions of
the exchange together. `oid4vp` times the provider’s decision only. `gateway` times the decision plus a signed relay
command against a loopback emulator, without connection set-up. With fewer than 30 repeats, a line says that such
intervals are for smoke runs only. Then come the autonomy of the door for 80 hours, the autonomy of the warehouse for
30 hours and the revocation, played on a simulated clock.

`--out <dir>` names a folder that is new or empty, and its default is `bench-results/<UTC time>`. The run writes
`env.json`, `summary.json`, `samples-<scenario>.csv`, `steps-<scenario>.csv`, `sizes.csv`, `autonomy-low.csv`,
`autonomy-medium.csv` and `revocation.csv` there. `--emit-frames <file>` also writes one-way QR codes of the door with
the context to decide on them. Progress goes to standard error and the table to standard output. A full run takes
minutes with WebCrypto. It takes much longer with noble, which signs and verifies in JavaScript, so make it on a
machine that is otherwise idle.

## Relay emulator

```sh
pnpm -F cli exec umis keygen --out .tmp/station    # the key pair of the station that sends the commands
pnpm -F cli exec umis relay-emu --trust .tmp/station/key.public.jwk.json
```

`umis relay-emu` runs the emulator of a Shelly Gen3 relay from `@umis/gateway/node` until you stop it with Ctrl+C. It
prints its address, the status page at `/`, the JSON-RPC endpoint at `POST /rpc`, the fingerprint of the trusted keys
and the audiences. Then it prints one line for every command it obeys, such as `OPEN 3 s · jti … · iss …`, and one
for every command it refuses, such as `401 <reason>`.

| Option                         | Meaning                                                                                                                  |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------ |
| `--port <n>`                   | port to listen on, 8089 by default, and 0 picks a free one                                                               |
| `--host <address>`             | address to listen on, 127.0.0.1 by default                                                                               |
| `--trust <file>`               | public key of a station as a JWK, a JWKS or an array of JWKs. Repeat it for more files. Required unless `--no-signature` |
| `--cors <origin>[,<origin>]`   | browser origins that may call the emulator. Repeat the option or separate origins with commas                            |
| `--aud <id>`                   | a relay identity a command may name. Repeat it for more. It replaces the defaults                                        |
| `--digest-password-env <NAME>` | name of the environment variable that holds the Shelly digest password. `Switch.Set` then needs the digest               |
| `--no-signature`               | obeys `Switch.Set` without a signature, for a closed test network only                                                   |

Only `Switch.Set` changes the switch, and only `Switch.Set` needs the signed command. With `--digest-password-env` it
needs the digest as well. `Switch.GetStatus` and `Shelly.GetDeviceInfo` answer without credentials. The status carries
`umis_now`, the emulator’s clock, and `umis_last`, the last command. A real relay reports neither. Without `--aud`, a
command may name `urn:umis:relay:emu`, the emulator’s address, `http://127.0.0.1:<port>` or
`http://localhost:<port>`. So a client made like this needs no `--aud`.

```ts
shellyRpcRelay({ url: 'http://127.0.0.1:8089/rpc', relayAud: 'http://127.0.0.1:8089', signer, iss });
```

A page on another origin also needs `--cors <its origin>`. A trust file that holds a private key gives a warning, and
only its public part is used. `umis relay-emu` exits with 0 after a normal stop or `--help`, with 64 for a mistake in
the arguments or the trust files, and with 1 when the emulator cannot start, for example on a port that is taken.

## Exit codes

| Code | Meaning                                                                                                                                       |
| ---- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| 0    | success. Every row of the demo is as expected, the frame is accepted, the text is decoded or the benchmark finds nothing off                  |
| 2    | the answer is no. A demo row is unexpected, the frame is rejected, the text cannot be decoded, or a benchmark presentation or timeline is off |
| 64   | a mistake in the arguments or in a file they name                                                                                             |
| 1    | any other failure                                                                                                                             |

## Keys

`umis keygen` writes `key.private.jwk.json`, which only its owner can read, and `key.public.jwk.json`. It never prints
the private key and does not overwrite existing files. Where a command takes an environment variable, you pass its
name, never its value.

## Licence

Copyright 2026 Ishtvan Komonii. Licensed under the
[Apache License 2.0](https://github.com/ishtvankomka/umis/blob/main/LICENSE).
