# Apple Wallet passes and orders

Passes and orders that live in Apple Wallet, made through the API and kept current through Apple's web service. Account: Praecise LTD, Team ID `X787J4RAP6`. Identifiers: `pass.com.praecise.skerry` (passes), `order.com.praecise.skerry` (orders), `merchant.com.praecise.skerry` (Apple Pay).

Migration: `db/053_wallet.sql`. Code: `lib/wallet.ts`, `lib/wallet-signer.ts`, and the files inside the package in `lib/pure/wallet.ts`, `lib/pure/cms.ts`, `lib/pure/zip.ts`.

## Making one

`POST /wallet/passes` (`kind`: `trip` a boarding pass, `pickup`, `receipt`, `membership`) and `POST /wallet/orders` (number, merchant, items, total, status, tracking). The answer carries `add_to_wallet_url`: it opens the signed package in Safari on an iPhone and holds a secret, so it goes to the person only. `PATCH` changes an item (the version goes up, devices holding it fetch the new one, `wallet.updated` is sent); `DELETE` voids a pass or cancels an order, which stays in Wallet marked so. A `changeMessage` on a field makes Wallet tell the person when that value changes; `relevant_date` and `locations` make a pass appear on the lock screen.

## The web service (Apple's, not the partner API)

Under `/wallet/v1`, authenticated by the token inside the package (`Authorization: ApplePass <token>` or `AppleOrder <token>`): `POST` and `DELETE /devices/{device}/registrations/{typeId}/{serial}`, `GET /devices/{device}/registrations/{typeId}` (changed serials since a tag), `GET /passes/{typeId}/{serial}` and `GET /orders/{typeId}/{id}` (the latest package, 304 when unchanged), `POST /log`. A bad token is 401 and nothing more.

## Signing and the key

Each package is signed with a detached CMS signature over `manifest.json` (SHA-1 of every file inside), RSA with SHA-256, carrying the issued certificate and Apple's intermediate (WWDR). The private keys are RSA 2048 keys made inside the server's TPM by `scripts/tpm-csr.py` (non-exportable, fixed to this TPM, persistent at `0x81010F10` for passes and `0x81010F11` for orders on server1). A key is never a file and never a `.p12`; signing is `tpm2_sign` on the handle, so the service user must be in the `tss` group. The certificate requests are in `~/rivier-handoff/apple-csr-1125/`; the owner uploads each in the Apple portal, downloads the certificate, and sets `SKERRY_WALLET_PASS_CERT` and `SKERRY_WALLET_ORDER_CERT` (public files) with `SKERRY_WALLET_WWDR_CERT`. Until then a download answers 503 `wallet_not_ready`. `SKERRY_WALLET_TEST_KEY` is a software key for tests only and signs only when `SKERRY_WALLET_ALLOW_TEST_KEY=tests-only` is also set; never set either on a server that serves people.

## Installed (25 Nov)

The pass and order certificates Apple issued (valid to 25 Dec 2027) are matched to the TPM keys: `scripts/wallet-sample.mjs` signs a sample pass and a sample order with the TPM handles and `openssl cms -verify` accepts both against Apple's WWDR G4 intermediate and root. The settings the service needs are listed with the certificates in `apple-csr-1125/INSTALL.md` of the handoff repository.

## Push

Wallet is told to fetch an updated pass by an empty push through Apple's push service using the certificate for the Pass Type ID as the client certificate of the connection, so the private key must sign inside the TLS handshake. Node cannot do that with a TPM key: it needs OpenSSL's tpm2 provider (`tpm2-openssl`, not installed on server1) in a small helper process. Until that helper exists devices get the new version when the person opens the pass, which Wallet does by itself, and `wallet.updated` is sent to developers either way.

## Checks

The package structure, manifest and signature are tested with OpenSSL (`test/wallet.test.mjs`) and the web service over HTTP (`test/integration/wallet.test.mjs`). Apple's own validators (`signpass`, the Wallet order validator) and a device check of a real pass are the last step and need the issued certificate.

Scopes: `wallet:read`, `wallet:write`. Event: `wallet.updated`.
