# Contributing

Bug reports, fixes, test vectors and documentation are welcome. This file says how to set up the repository, which rules
the code follows, how it is tested and what a change needs before it is merged. Vulnerabilities are not reported here:
see [SECURITY.md](SECURITY.md).

## Setup

You need Node 24 (the version in `.nvmrc`; CI also runs Node 22.13, the lowest supported) and pnpm 12 (the version in
`packageManager` of `package.json`).

```sh
git clone https://github.com/ishtvankomka/umis.git && cd umis
pnpm install
pnpm build
pnpm test
```

Nothing else is needed: no database, no container and no network service. The tests that start the relay emulator listen
on 127.0.0.1.

## Commands

| Command                                                           | What it does                                                                                          |
| ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `pnpm build`, `pnpm typecheck`, `pnpm lint`, `pnpm test`          | the four tasks over the whole workspace, through Turborepo                                            |
| `pnpm turbo run lint typecheck build test --filter=@umis/core...` | the same for one package and every package that depends on it                                         |
| `pnpm check:browser`                                              | bundles every browser-safe entry for the browser and fails on any Node built-in it reaches            |
| `pnpm check:scope`                                                | fails when a package name, workspace dependency or import uses another scope than `scope.config.json` |
| `pnpm format`, `pnpm exec prettier --check .`                     | formats, or checks the formatting of, every file Prettier knows                                       |
| `pnpm docs:api`                                                   | builds the API reference with TypeDoc into `docs/api`; a warning fails the build                      |
| `pnpm gen:llms`, `pnpm check:llms`                                | writes `llms.txt` and `llms-full.txt` from the README and the docs, or checks that they are current   |
| `pnpm check:quickstart`                                           | checks that the quickstart in `README.md` is `examples/quickstart/index.ts`, character for character  |
| `pnpm docs:benchmarks <run-dir>...`                               | writes `docs/benchmarks.md` and the benchmark table of `README.md` from `umis bench` output folders   |
| `pnpm -F umis-example-quickstart start`                           | runs an example (also `umis-example-offline-checkpoint start`, `umis-example-browser-verify check`)   |
| `pnpm -F @umis/testkit gen:vectors`                               | writes `test-vectors/umis-profile-v1.json` again, byte for byte                                       |

## Rules for the code

- TypeScript, ES modules only; names, comments, documentation and commit messages in English.
- **Browser safety.** The browser-safe packages (`core`, `status-list`, `policy`, `registry`, `issuer`, `verifier`,
  `wallet`, `wallet-rn`, `transports` and the main entry of `gateway`) never import Node built-ins and never use
  `Buffer`, `process`, `URL` or `URLSearchParams`, which React Native polyfills only in part. ESLint and
  `pnpm check:browser` enforce it. Node-only code lives in `@umis/gateway/node`, `@umis/testkit` and `@umis/cli`.
- **Cryptography** goes through a `CryptoProvider`: WebCrypto by default, `@umis/core/noble` as the JavaScript one. No
  primitive is implemented here, and JWS is assembled only in `@umis/core`.
- **Time is an input.** Every function that decides takes `now` in NumericDate seconds; durations are seconds unless the
  name ends in `Ms`. Nothing reads the wall clock to make a decision, so tests and benchmarks run on simulated clocks.
- **No identifier from signature bytes.** ES256 signatures are accepted in both forms of S, so they are malleable
  ([I-15](docs/security-model.md#i-15)); replay ids come from nonces and `jti` values.
- **Errors.** Thrown errors are `UmisError` with a code of the profile; verification functions do not throw and give one
  reason code.
- **Secrets.** Environment variables are named, never given as values, in code, documentation, tests and logs. Test keys
  are generated in the test or derived from public seeds.

## Tests

The tests are the test vectors and the security invariants; coverage is not a goal.

- **Vectors.** [`test-vectors/`](test-vectors) holds vectors from the RFCs and drafts the profile uses and the vectors
  of the profile itself, generated from the demo world. A change to a format changes the vectors and says why.
- **Invariants.** The invariants I-1 to I-20 are listed in [docs/security-model.md](docs/security-model.md#invariants),
  each under its own heading with the files of its tests. A test that guards one names it in its title, for example
  `(I-13)`, and a new invariant gets a heading there.
- **The OpenID4VP contract test.**
  [`packages/transports/test/transports.test.ts`](packages/transports/test/transports.test.ts) holds the only
  wallet-to-verifier contract test of OpenID4VP: the real `respondToVpRequest` of `@umis/wallet` posts to the real
  `verifyVpResponse` of `@umis/verifier` (accept, wrong nonce, the bare response address as audience, replay, a changed
  `state`, and the status list fetched once). `@umis/verifier` and `@umis/wallet` cannot import each other, so each of
  their own test suites checks one half against `@umis/core` only. Extend this test whenever the request or the response
  format changes.
- **Examples** run in CI and are tests too: an example that stops working fails the build.

## Making a change

1. Work on a branch and keep each commit to one subject. Commit messages follow
   [Conventional Commits](https://www.conventionalcommits.org/): `feat(core): …`, `fix(verifier): …`, `docs: …`, with
   the folder name of the package as the scope.
2. A change to a published package needs a changeset: run `pnpm changeset`, choose the packages and the kind of change,
   and write one English sentence for the changelog. All `@umis/*` packages share one version number, and until 1.0 a
   minor version may change the API.
3. Before committing, run `gitleaks git --staged --no-banner` (the rules are in `.gitleaks.toml`) and
   `pnpm exec prettier --check .`.
4. Open a pull request. CI runs on Node 22.13 and 24: lint, typecheck, build, test, browser safety, scope, the examples,
   `publint` and `attw` for every package, the quickstart check and the `llms.txt` check.

## Renaming the npm scope

`pnpm rename-scope @new-scope --dry-run` lists what would change; without `--dry-run` it rewrites package names,
dependencies, imports, Markdown, `llms*.txt`, workflows, `.changeset/*.json`, `typedoc.json` and finally
`scope.config.json`, the only place the scope is configured. Run `pnpm install` afterwards, then `pnpm check:scope`.

## Benchmarks

`umis bench` (see the [CLI README](packages/cli/README.md#benchmark)) writes `env.json`, `summary.json` and `sizes.csv`
into its output folder. `pnpm docs:benchmarks <webcrypto-run> <noble-run>` turns them into `docs/benchmarks.md` and the
benchmark table of `README.md`; no figure there is written by hand. Run the benchmark on an otherwise idle machine and
commit the generated files, not the raw output.

## Licence

Contributions are accepted under the [Apache License 2.0](LICENSE), as section 5 of the licence describes.
