# Verifying a CropPath lot or fruit record

You can check a sealed lot's public documents without a CropPath account and without any CropPath software. This page
covers customs officers, importers and auditors. It names the standards and versions involved, the steps, and what
the documents can and cannot prove.

## What is published

The documents are issued once, inside the transaction that seals the lot (`POST /v1/lots/{lot}/close`, L10 #35). They
are signed with the packing facility's issuer key, the same key that signs the seal, and they never change afterwards.
Anything recorded after the seal is on the live public page but not in these documents: for a lot sealed before it was
packed, that means the packing, the shipping and any later lab report.

| Download | Route | Media type | Content |
|---|---|---|---|
| Lot EPCIS document | `GET /v1/public/lots/{publicId}/epcis.jsonld` | `application/ld+json` | GS1 EPCIS 2.0 JSON-LD: the lot's accepted events, oldest first, minimised (see below). |
| Lot credential | `GET /v1/public/lots/{publicId}/credential` | `application/vc+jwt` | UNTP Digital Product Passport (granularity `batch`) as a VC-JOSE compact JWS. |
| Fruit credential | `GET /v1/public/fruits/{publicId}/credential` | `application/vc+jwt` | UNTP Digital Product Passport (granularity `item`) of one fruit, in the same form. |
| Lab conformity credential | `GET /v1/public/lots/{publicId}/lab-credential` | `application/vc+jwt` | UNTP Digital Conformity Credential of the lab's report on the lot, in the same form, issued in the lab's name. Only for a report the lab entered in its CropPath portal (see [Checking the lab's conformity credential](#checking-the-labs-conformity-credential)). |
| Facility record | `GET /v1/public/lots/{publicId}/facility-record` | `application/vc+jwt` | UNTP Digital Facility Record of the packing facility as it was at the seal, in the same form, signed with the same key as the lot credential (see [Checking the facility record](#checking-the-facility-record)). Lots sealed before UNTP 0.7.0 have none. |

Each route has the same visibility as the public record it belongs to. An id that is unknown, hidden or has no document
gets the record's uniform 404. A credential is withdrawn once its key is revoked for compromise (see "Revocation").
The EPCIS download's `ETag` is the SHA-256 of its bytes (the digest the lot credential cites), so a client can
revalidate with `If-None-Match` and get a 304. A lot's EPCIS document is at most 16 MiB; a lot whose document would be
larger is sealed without public documents.
The public lot record links these downloads (`downloads.epcis_url`, `downloads.credential_url`). It names the key that
signed the lot's credential in `issuer` (`did`, `key_id`) only while that credential can be downloaded. The fruit
record links its own credential. The lot credential links the others, and the [link set](#finding-the-documents-from-a-label)
of a label lists them all.

## Versions

| Standard | Version | Where it shows |
|---|---|---|
| GS1 EPCIS | 2.0.0 | `@context` `https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld`, `schemaVersion` `2.0`. The document validates against GS1's JSON schema. |
| W3C Verifiable Credentials Data Model | 2.0 (Recommendation, 2025-05-15) | `@context` `https://www.w3.org/ns/credentials/v2`, `validFrom` |
| W3C Securing Verifiable Credentials using JOSE and COSE | Recommendation, 2025-05-15 | compact JWS, header `typ` `vc+jwt`, `cty` `vc` |
| UN Transparency Protocol, Digital Product Passport and Digital Facility Record | 0.7.0, the release the UNTP site (untp.unece.org/versions) lists as maintained for pilots; 1.0 is announced but not published | `@context` `https://vocabulary.uncefact.org/untp/0.7.0/context/`, the lot and fruit credentials and the facility record of a lot sealed since this release. They validate against the UNTP 0.7.0 JSON schemas, which are GPL-3.0 and so not distributed by CropPath (see [Checking a credential against the UNTP schema](#checking-a-credential-against-the-untp-schema)). |
| UN Transparency Protocol, Digital Product Passport (earlier lots) | 0.6.1 (release of 2025-06-16) | `@context` `https://test.uncefact.org/vocabulary/untp/dpp/0.6.1/`: a credential is frozen at the seal, so a lot sealed before CropPath moved to 0.7.0 keeps its 0.6.1 credentials, served byte for byte as issued. |
| UN Transparency Protocol, Digital Conformity Credential | 0.6.1 (release of 2025-06-16) | `@context` `https://test.uncefact.org/vocabulary/untp/dcc/0.6.1/`, the lab's credential. It validates against the UNTP DCC 0.6.1 JSON schema, also GPL-3.0. |
| UN Transparency Protocol, Identity Resolver | 0.7.0 | the link set of a label: an IETF link set (RFC 9264), `application/linkset+json`. |
| JWS algorithm | ES256 (ECDSA P-256 with SHA-256, RFC 7518) | header `alg` |
| DID method | `did:web` | header `kid`, `issuer.id`: a facility `did:web:<domain>:<slug>` (no facility may take the slug `labs`), a lab `did:web:<domain>:labs:<lab code>` |

## Checking a credential

1. **Read the header.** The first segment of the compact JWS is base64url JSON:
   `{"alg":"ES256","cty":"vc","kid":"did:web:<domain>:<facility slug>#key-<n>","typ":"vc+jwt"}`.
2. **Resolve the issuer.** By the did:web method, `did:web:<domain>:<slug>` resolves to
   `https://<domain>/<slug>/did.json`. In that document, take the `verificationMethod` entry whose `id` equals the
   `kid` and use its `publicKeyJwk`, a P-256 key. The key must also be listed in `assertionMethod`.
3. **Verify the signature** over `<header>.<payload>` with any JOSE library. With `jose` in Node:

   ```js
   import { compactVerify, importJWK } from "jose";

   const did = await (await fetch("https://<domain>/<slug>/did.json")).json();
   const jws = await (await fetch("https://<domain>/v1/public/lots/<publicId>/credential")).text();
   const kid = JSON.parse(Buffer.from(jws.split(".")[0], "base64url")).kid;
   const method = did.verificationMethod.find((m) => m.id === kid);
   const { payload } = await compactVerify(jws, await importJWK(method.publicKeyJwk, "ES256"));
   const credential = JSON.parse(new TextDecoder().decode(payload));
   ```

   With the `openssl` CLI (build the key from the JWK's `x` and `y`, and the DER signature from the 64-byte r || s,
   by hand):

   ```sh
   openssl dgst -sha256 -verify key.pem -signature sig.der signing-input
   ```

4. **Read the credential.** `issuer.id` is the facility's DID and `validFrom` is the seal time in UTC. In UNTP 0.7.0
   `credentialSubject` is the product itself (type `Product`):
   - `id` is the address of its public record, `https://<domain>/l/<publicId>` for a lot and
     `https://<domain>/q/<publicId>` for a fruit, the address a label prints. `idGranularity` is `batch` or `item`.
   - `productCategory` is UN CPC 01319 (other tropical and subtropical fruits, which includes durian).
     `producedAtFacility` is the packing facility, `urn:agritrace:loc:<facility code>`.
   - `dimensions.weight` is the net mass in kilograms. `characteristics` carries the other facts of the lot or the
     fruit; `characteristics.epcClass` is the lot's class identifier in the EPCIS events (`quantityList.epcClass`).
   - `performanceClaim` lists the lab results (see [What the documents leave out](#what-the-documents-leave-out)).
   - `relatedDocument` links the public page (link type `https://gs1.org/voc/pip`), the EPCIS document (link type
     `https://gs1.org/voc/traceability`), the facility record (`https://test.uncefact.org/vocabulary/linkTypes/dfr`)
     and the lab's conformity credential (`https://test.uncefact.org/vocabulary/linkTypes/dcc`) when there is one. A
     fruit's credential links its lot's credential (`https://test.uncefact.org/vocabulary/linkTypes/dpp`).

   A credential of a lot sealed before UNTP 0.7.0 is UNTP 0.6.1: there `credentialSubject.product` carries the facts,
   `conformityClaim` the lab results and `traceabilityInformation` the events.
5. **Tie the documents to the credential.** Each link to a document issued at the seal carries `digestMultibase`:
   `u`, then the base64url form without padding of the two bytes `0x12 0x20` followed by the SHA-256 of the exact
   bytes served (a SHA-256 multihash in multibase, as VC 2.0 uses it). Download the document and compute it:

   ```sh
   printf 'u%s\n' "$( (printf '\x12\x20'; openssl dgst -sha256 -binary epcis-<lot code>.jsonld) \
     | basenc --base64url | tr -d '=\n')"
   ```

   The EPCIS bytes are canonical JSON (sorted keys, no whitespace), served unchanged, and the download's `ETag` is
   their SHA-256 in hex. In a 0.6.1 credential, `traceabilityInformation[0].traceabilityEvent[0].hashDigest` holds
   that hex SHA-256 instead: compare it with `sha256sum epcis-<lot code>.jsonld`.

## Checking the facility record

The seal also issues the packing facility's UNTP Digital Facility Record, signed with the same facility key as the lot
credential. The lot credential links it (`relatedDocument`, link type
`https://test.uncefact.org/vocabulary/linkTypes/dfr`) with its `digestMultibase`.

1. **Verify it** exactly as in [Checking a credential](#checking-a-credential): same header, same key.
2. **Read it.** `credentialSubject` is a `Facility`:
   - `id` is `urn:agritrace:loc:<facility code>`, the lot credential's `producedAtFacility.id`.
   - `address` holds the locality only (district, province and country, as the public lot page shows them): the street
     address and postal code are empty, and `locationInformation` gives no coordinates.
   - `processCategory` is ISIC Rev. 4 0163, post-harvest crop activities.
   - `facilityAlsoKnownAs` lists the facility's GLN (`https://id.gs1.org/414/<gln>`) when it has one, and each packing
     house registration code that the published code lists match for a destination market
     (`urn:agritrace:phc:<market>:<code>`). A code is listed only when it is on a current list that is not out of date,
     as an export label prints it. `description` names the list's import date and the SHA-256 of the authority's file.
   It describes the facility at the seal: a later lot carries a newer record.

## Finding the documents from a label

A label's address, `https://<domain>/l/<publicId>`, `https://<domain>/q/<publicId>` or a GS1 Digital Link
`https://<domain>/01/<gtin>/10/<lot>/21/<serial>` (also `https://<domain>/01/<gtin>/10/<lot>` for a sealed lot), opens
the public page in a browser. Asked for `application/linkset+json` it answers the record's link set instead, as UNTP's
Identity Resolver describes it:

```sh
curl -H 'Accept: application/linkset+json' https://<domain>/l/<publicId>
curl 'https://<domain>/q/<publicId>?linkType=linkset'
```

The same answer comes from `GET /v1/public/linkset?code=<the label's address>`, which takes every code
`GET /v1/public/resolve` takes. The link set (RFC 9264) has one context per record: its `anchor` is the record's
address (the credential's product `id`), and its link relations are `dpp` (the UNTP passport), `dfr` (the facility
record), `dcc` (the lab's conformity credential), `https://gs1.org/voc/traceability` (the EPCIS document) and
`https://gs1.org/voc/pip` (the public page, one per language, with `hreflang`). A fruit's link set lists the fruit's
context, then its lot's. Only documents that can be downloaded are listed: a withdrawn credential is left out. A code
that names no public record gets the resolver's 404, and a lot removed at the end of its retention period its 410.

## Checking the lab's conformity credential

A lab whose users enter their results in the CropPath lab portal has its report signed with a key that CropPath holds
for the lab's portal users (#73). For such a report, the seal also issues the lab's own UNTP Digital Conformity
Credential, signed with that same key, so the credential has the same custody as the report. A report the lab signed
with its own key gets no conformity credential: CropPath never signs in the name of a lab that holds its own key.

1. **Find it.** In the lot credential, each lab claim (`credentialSubject.performanceClaim[]`) links it as `evidence`,
   and `relatedDocument` lists it too: `linkURL` is `https://<domain>/v1/public/lots/<publicId>/lab-credential` and
   `digestMultibase` is computed from the exact text served, as in step 5 of
   [Checking a credential](#checking-a-credential). (In a 0.6.1 lot credential, each `conformityClaim[]` links it as
   `conformityEvidence` with the hex SHA-256 in `hashDigest`: compare with `sha256sum untp-dcc-<lot code>.jwt`.)
2. **Read the header.** `{"alg":"ES256","cty":"vc","kid":"did:web:<domain>:labs:<lab code>#<key id>","typ":"vc+jwt"}`.
3. **Resolve the lab.** `did:web:<domain>:labs:<lab code>` resolves to `https://<domain>/labs/<lab code>/did.json`.
   Take the `verificationMethod` whose `id` equals the `kid` and verify the signature with its `publicKeyJwk`, exactly
   as in [Checking a credential](#checking-a-credential), step 3. The facility's key does not verify it.
4. **Read it.** `issuer.id` is the lab's DID and `issuer.name` its code. `credentialSubject` is a
   `ConformityAttestation`: `assessorLevel` `3rdParty`, `attestationType` `testing`, `issuedToParty` the packing
   facility, and one `assessment` per analyte and one for the origin isotope test, each with its verdict
   (`conformance`), its measured values (`declaredValue`) and the lot's product (`assessedProduct`). The description
   states who holds the key. `assessmentLevel` is `Unspecified`: a portal entry is never the accredited lab's own
   signature, so the public lab step stops at Cross-checked for it.

## Checking a credential against the UNTP schema

The UNTP JSON schemas are published by UN/CEFACT under GPL-3.0, the licence of the uncefact/spec-untp repository.
Fetch the release v0.7.0 schemas of the Digital Product Passport and the Digital Facility Record and check their
SHA-256:

```sh
curl -fsSL -o untp-dpp-0.7.0.json https://untp.unece.org/artefacts/schema/v0.7.0/dpp/DigitalProductPassport.json
curl -fsSL -o untp-dfr-0.7.0.json https://untp.unece.org/artefacts/schema/v0.7.0/dfr/DigitalFacilityRecord.json
sha256sum -c - <<'EOF'
cd1257d14904443f98a4ccd3f84b2e9b160a331b3e9f36161a8a0b14b64e0ca2  untp-dpp-0.7.0.json
b4c86df054939943d0f89de9569817d33905de5d4f9e580009865cbdbc23845b  untp-dfr-0.7.0.json
EOF
```

Then validate the credential's payload (the JSON of step 4 of "Checking a credential") against the passport schema,
and the facility record's payload against the facility record schema, with any JSON Schema draft 2020-12 validator. A
credential sealed before UNTP 0.7.0 validates against the release v0.6.1 DPP schema:

```sh
curl -fsSL -o untp-dpp-schema.json \
  https://raw.githubusercontent.com/uncefact/spec-untp/v0.6.1/data-models/untp-dpp/artefacts/untp-dpp-schema.json
echo "079b729617744cda34fa27d1763d64de138a31b39fb9e2c11b473c531859b9bf  untp-dpp-schema.json" | sha256sum -c -
```

The lab's conformity credential validates against the UNTP DCC 0.6.1 schema the same way:

```sh
curl -fsSL -o untp-dcc-schema.json \
  https://raw.githubusercontent.com/uncefact/spec-untp/v0.6.1/data-models/untp-dcc/artefacts/untp-dcc-schema.json
echo "2ecb7a4aa86e3e85fd4fccbcb4afb408cbf7e1f6964bfafd0793bed570a0e2bc  untp-dcc-schema.json" | sha256sum -c -
```

## What the documents leave out

The documents carry only what the public lot and fruit pages may show.

- **No operator.** `agri:operator` is dropped.
- **No grower or producer, no parcel.** No consent model links an envelope's producer to a grower's public-display
  consent, and a document frozen at the seal could not follow a withdrawal of consent.
- **No attachment references.**
- **No reading that is not a scalar.** In particular there are no coordinates (`geo.position`).
- **Read point.** An event's read point is its facility, else its growing area, never a parcel.
- **No device signature and no device id.** See the next section.
- **Lab results are claims; the lab's attestation is linked only for a portal entry.** The lab is the conformity
  assessment body. It signs its report with its own registered key, or its user enters the results in the CropPath lab
  portal and CropPath signs them with a key it holds for the lab (#73). Only a portal entry gets the lab's own UNTP
  Conformity Credential (see [Checking the lab's conformity credential](#checking-the-labs-conformity-credential)),
  linked from each claim as `evidence`. The lot credential's `performanceClaim` entries (type `Claim`) are the
  facility's statement of the lab's result: one per analyte, whose topic is UNTP's sanitary and phytosanitary measures,
  whose criterion is the destination market's limit, whose first `claimedPerformance` is the score `pass` or `fail`
  and whose others are the measured values as chemical contaminant levels; and one for the origin isotope test, whose
  topic is origin tracking. Each one names, in its `description`, the lab, its key id, who holds that key (`held by the
  lab`, or `held by the platform for the lab's portal users, not by the lab` for a portal entry), the report number and
  the report's SHA-256. A portal entry is never the accredited lab's own signature, so the public lab step stops at
  Cross-checked for it. A claim is made only when the public lot page's lab step would accept the same report (ADR
  0001): its key was not revoked for compromise and it comes from the lab the sampling assigned. Otherwise the
  credential has no `performanceClaim`. (A 0.6.1 credential states the same claims as `conformityClaim` entries, type
  `Claim` and `Declaration`, linking the lab's credential as `conformityEvidence`.)
- **The facility record names the facility's locality only.** No street address, postal code or coordinates; its GLN
  and the registration codes the published lists match are identifiers the facility already shows on its labels and
  in its events.

## Device signatures and how envelopes are canonicalised

The public EPCIS document carries no device signature (`agri:deviceSignature`) and no device id
(`sensorMetadata.deviceID`):

- A device signs its full envelope, including the members the public document withholds (the operator, the producer,
  the parcel). Nobody can check such a signature from the public document, since the signed bytes are not public. But
  ECDSA lets anyone recover candidate public keys from a message and its signature, so two signatures of one device
  would let a guess of a withheld low-entropy value (an operator id, a parcel code) be confirmed offline.
- No public page lists device ids.

The private EPCIS export (`GET /v1/lots/{lot}/epcis`, for authorised users) keeps both. For reference, a device signs
the canonical JSON of its envelope without the `sig` member. The device SDK's `canonical_json` and `sign_envelope`
(Python package `agritrace_iot`) define the bytes:

- UTF-8;
- object members sorted by key;
- separators `,` and `:` with no whitespace;
- non-ASCII characters written as themselves, not escaped.

The signature is ES256 over those bytes, encoded as base64url(r || s). A receiver verifies over exactly the members it
received minus `sig`, never over a re-serialised envelope. Checking a device signature needs the full envelope and the
device's registered public key; neither is public, and authorised parties get them from the platform. Publicly, the
facility's credential, which cites the document's hash, shows that the document is the one issued at the seal.

## Revocation

A key revoked for compromise leaves the facility's `did.json`, so nothing it signed verifies any more. The platform then
withdraws the credentials that key signed:

- the download routes answer 404;
- the public records link no credential;
- the lot record's `issuer` is null.

The EPCIS document stays available because the key does not sign it, but its hash is then backed by no valid
credential. A key retired by a rotation stays in `did.json`, so what it signed stays verifiable.

The lot credential is withdrawn the same way (404, no link, `issuer` null) once the lab key whose report its
`conformityClaim` entries cite is revoked for compromise: the credential cannot drop those claims, so it is no longer
served. The fruit credentials and the EPCIS document claim nothing from the lab and stay.

A lab key is handled like a facility's issuer key. Revoked for compromise, it leaves the lab's `did.json`, and the
lab's conformity credential that it signed is withdrawn (404) together with the lot credential that links it. Revoked
for a rotation, it stays in `did.json` and what it signed stays verifiable. The facility's issuer key does not sign
the lab's conformity credential, so revoking it leaves that credential available.
