> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bbrands.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Evidence and sealing

> What MaihueSigner records for every signature and how it makes it tamper-evident: per-signer evidence, the hash-linked audit chain, the evidence sheet, the integrity check, PAdES sealing and verification.

A signature is only as strong as what proves it. MaihueSigner keeps three
independent proofs: the **evidence** of each gesture, an **audit chain** that
links every event by hash, and **PAdES seals** on the PDFs.

## Evidence of each gesture

Each seat gets one row in `signature_signer_evidence`. It is created when the
first code is sent and completed when the seat signs; for an unattended seat
it is created when the seat signs:

| Field | Content |
| - | - |
| Signed hash | One SHA-256 over the hashes of every document in the group |
| Document number hash | SHA-256 of the normalized document number; the number itself is not stored. For an unattended company seat it is a hash of the representative and its proof of representation |
| Identity verification | The verified identity record used for the gesture |
| Code validated at, signed at | Server times of the code check and the signature |
| IP, user agent | Where the gesture came from |

The `signer_signed` audit event repeats the signed hash, the document number
hash, the IP and the user agent and, when one exists, links the identity
stamp of the verification that was used.

## Audit chain

Every engine event for a group is appended to `signature_provider_event`. The
chain is the rows with `seq`; a consumer can store unchained markers in the
same table, and the verification ignores them:

| Event | When |
| - | - |
| `request_created` | The group opens |
| `otp_sent` | A code is sent; the time it is accepted is kept in the evidence |
| `signer_signed`, `signer_rejected` | A customer or attended seat signs or rejects |
| `enterprise_signed` | An unattended company seat signs |
| `documents_sealed` | The PDFs are sealed |
| `request_cancelled` | The group is cancelled |

Each event carries a contiguous `seq`, the SHA-256 `hash` of its canonical
content and the `prev_hash` of the event before it. Changing a past event
breaks the chain at that event, and the verification reports where.

## Sealing

Sealing runs once per group, when both sides are complete.

```mermaid theme={null}
flowchart LR
    download["Download each<br/>original PDF"] --> check{"Hash equals the one<br/>recorded at creation?"}
    check -->|no| stop["Abort: the file changed"]
    check -->|yes| reps["One PAdES signature per<br/>company representative"]
    reps --> sheet["Append the<br/>evidence sheet"]
    sheet --> platform["Platform PAdES<br/>integrity seal"]
    platform --> store["Store the sealed PDF<br/>+ documents_sealed event"]
```

1. **Integrity check.** The hash of each original PDF must match the one
   recorded when the group opened; any difference aborts the seal.
2. **Representatives' signatures.** Each completed company seat signs the
   PDF with its own certificate.
3. **Evidence sheet.** A page is appended to each PDF with the document name,
   the contract, the group, the original hash and the sealing time; for each
   customer, the signing and code-validation times, the document number hash,
   the identity record, the IP and the user agent; and the company
   representatives, listed apart from the platform seal.
4. **Platform seal.** The platform's certificate applies a final PAdES seal
   that covers everything above. Its signing time is the server time.
5. **Store.** The sealed PDFs are uploaded to the private `contracts` bucket
   and `documents_sealed` is appended to the chain. The consumer reads them
   with `getSignedDocument`.

## Verification

The available verification combines two checks into one verdict. Today the
contract evidence export verifies the audit chain only:

| Check | Passes when |
| - | - |
| PAdES seal | The PDF's last signature (the platform seal) covers the whole file, so the bytes have not changed since sealing; it validates against its embedded certificate and carries a signing time |
| Audit chain | Every event recomputes its hash, links to the previous one, and the timestamps never go backwards |

Digital contracting exposes the evidence package of a contract through
`GET /api/v3/contract/{id}/evidence`; see
[Digital contracting — Signing](https://docs.bbrands.io/digital-contracting/signing.md#evidence-export).

## Code

| Concern | Path |
| - | - |
| Evidence and gesture | `lib/integration/signature-provider/internal-signer/internal-signer.client.ts`, `internal-signer.repository.ts` |
| Audit chain | `lib/integration/signature-provider/internal-signer/audit-chain.service.ts` |
| Evidence sheet | `lib/integration/signature-provider/internal-signer/evidence-sheet.ts` |
| Sealing and PAdES | `lib/integration/signature-provider/internal-signer/sealing.service.ts`, `pades-sealer.ts` |
| Verification | `lib/integration/signature-provider/internal-signer/signature-verification.service.ts` |
| Hashing | `lib/integration/signature-provider/internal-signer/hash.util.ts` |
| Schema pages | [Signer evidence](https://docs.bbrands.io/schemas/signature/signature-signer-evidence.md), [Provider event](https://docs.bbrands.io/schemas/signature/signature-provider-event.md) |

Paths are relative to `backend/horizon-api/`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.