# Receiving documents

The reader of a stamped document verifies the stamp, compares its domain with the domain on record for the issuer, and compares its fields with the document. This guide describes these checks, made by a person or by a system that processes incoming documents.

## Overview

The reader of a stamped document ([section 1.2](https://dkds.org/docs/specification#1-2-roles)) is a person who
receives it or a system that processes incoming documents, such as an accounts payable or claims
system. Before the document is relied on, the reader checks the outcome of verification, the domain
that signed the stamp, the fields, and the coverage of the facts acted on. The checks are the same for
both kinds of reader; their methods are compared in
[Manual and automated checking](#manual-and-automated-checking).

| Check | Condition | Section |
|---|---|---|
| Outcome | The outcome is `valid` | [Outcome](#outcome) |
| Domain | The domain is the one on record for the issuer | [Domain](#domain) |
| Fields | Every field matches the document | [Fields](#fields) |
| Coverage | Every fact acted on is among the fields | [Coverage](#coverage) |

## Outcome

Verification ends in one of seven outcomes ([section 8.1](https://dkds.org/docs/specification#8-1-outcomes)). The
further checks apply only to `valid`; for any other outcome, a verifier presents no fields.
`unavailable` indicates that the key could not be retrieved, and the stamp can be verified again
later. `unsupported` indicates a version or algorithm that the verifier does not implement. The other
outcomes are findings about the stamp.

A genuine document can fail verification, for example after its issuer has removed a key record in
error. A stamp fabricated under an existing domain with an invented key ID fails in the same way, with
`key-not-found`. A document whose stamp does not verify is therefore treated as a document without a
valid stamp, and its origin is confirmed with the issuer through a channel not taken from the
document.

## Domain

A valid stamp identifies the domain that signed it, not the organisation behind the domain. A stamp
signed under a domain registered by a forger, such as `northwind-invoices.example`, verifies as
`valid` ([Lookalike domains](https://dkds.org/docs/security#lookalike-domains)). The domain is therefore compared with
the domain on record for the issuer, taken from a source other than the document, such as a contract
or earlier correspondence. The domain of a new issuer is recorded when the relationship begins.

A domain is read from the right: `northwind.example.pay-portal.example` is a name under
`pay-portal.example`. A stamp signed under the domain of a service provider, such as an invoicing
platform, identifies the provider, not the customer on whose behalf it was signed. The customer is
identified by fields such as its registration number in the public register of companies, which the
reader checks against that register
([Stamps signed on behalf of customers](https://dkds.org/docs/issuing#stamps-signed-on-behalf-of-customers)).

## Fields

Each field is compared with the corresponding value on the document. A difference indicates that the
document or the stamp was altered after signing. Issuers write values exactly as printed
([requirement 10.5](https://dkds.org/docs/specification#10-issuer-requirements)), so the comparison is exact; an
account number is compared in full.

The fields that identify the document and its subject
([requirement 10.4](https://dkds.org/docs/specification#10-issuer-requirements)) show whether the stamp belongs to the
document. A stamp whose fields describe another document, such as an earlier invoice of the same
issuer, has been copied.

## Coverage

A stamp protects only the facts it contains. A fact printed on the document but absent from the
stamp, such as a bank account number, can be altered without a difference appearing. The reader acts
only on facts that are among the fields, and confirms other facts by other means. An issuer that
follows the [field conventions](https://dkds.org/docs/field-conventions#minimal-sets) includes the same fields in
every stamp of a type of document, with the value `(nil)` for a fact that the document does not
contain; if the document nevertheless prints a value for it, the document has been altered.

A document can carry several stamps, each verified independently
([section 3.3](https://dkds.org/docs/specification#3-3-qr-code)). A field is a statement of the domain that signed its
stamp; a field in the stamp of another domain is not a statement of the issuer.

## Missing stamps

The protocol cannot establish that a document should carry a stamp. A missing stamp is significant
only where the reader knows that the issuer stamps its documents, for example from earlier documents
or from its records of the issuer.

A notice that changes payment details is a document of its own, and the stamp of an accompanying
invoice does not cover it. A change of payment details is accepted when the notice carries a valid
stamp of the issuer's domain, or after confirmation through a channel not named in the notice.

## Choice of verifier

The verifier is chosen independently of the document. The text form of a stamp begins with `DKDS:`
and is not a web address ([section 3.1](https://dkds.org/docs/specification#3-1-text-form)), so a QR code that opens a
web page is not a DKDS stamp. dkds.js and scan.dkds.org are conforming verifiers; other
implementations are tested against the [test vectors](https://dkds.org/docs/specification#test-vectors).

## Manual and automated checking

| Check | Person | System |
|---|---|---|
| Outcome | Reads the outcome before the fields | Accepts only `valid`, verifies again after `unavailable`, and refers other outcomes for review |
| Domain | Reads the domain in full and heeds warnings about similar domains ([Building a verifier](https://dkds.org/docs/verifying#warnings)) | Compares `domain`, the ASCII form, exactly with the domain in its records of the issuer |
| Fields | Compares each value character by character | Reads the printed values from the text of a PDF file or by text recognition and compares them exactly, apart from white space; refers uncertain readings for review |
| Coverage | Looks for the facts acted on among the fields | Requires, for each type of document, the fields it acts on, such as the amount and the payee account of an invoice |
| Missing stamps | Relies on knowledge of the issuer | Records for each issuer whether its documents are stamped, and refers unstamped documents of such issuers for review |
| Verifier | Opens a verifier known in advance, such as scan.dkds.org | Uses a conforming implementation, such as dkds.js |
