> ## 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.

# Architecture

> The components of MaihueSigner and the files that hold them, the signature tables, the provider interface consumers call, and the boundary between the engine and the products that use it.

## Components

```mermaid theme={null}
flowchart LR
    subgraph consumer [Consumer, e.g. digital contracting]
        workers["Workers and services"]
        gestures["Signer routes<br/>/api/v3/signature/*/signer/*"]
    end
    subgraph engine [MaihueSigner — internal-signer]
        client["Engine client<br/>SignatureProviderClient"]
        workflow["Workflow resolution<br/>and phase gate"]
        otp["One-time codes"]
        chain["Audit chain"]
        sealing["Sealing<br/>evidence sheet + PAdES"]
        verify["Verification"]
        repo["Repository"]
    end
    admin["Back office<br/>workflow and representative admin"]
    store[("Postgres signature_* tables<br/>+ contracts bucket")]
    notify["Notification core<br/>(email)"]

    workers --> client
    workers --> workflow
    gestures --> client
    client --> workflow
    client --> otp
    client --> chain
    client --> sealing
    client --> repo
    otp --> notify
    sealing --> store
    chain --> store
    repo --> store
    verify --> chain
    verify --> sealing
    admin --> workflow
```

| Component | Responsibility | File |
| - | - | - |
| Provider interface | The contract every signature provider implements, so consumers never couple to one engine | `lib/integration/signature-provider/signature-provider-client.ts` |
| Engine client | Opens the provider request for a group the consumer created, sends codes, registers gestures, applies unattended company signatures, cancels, returns sealed PDFs | `internal-signer/internal-signer.client.ts` |
| Repository | The usual reads and writes of the provider, request, code and evidence tables | `internal-signer/internal-signer.repository.ts` |
| One-time codes | Six-digit codes from a secure generator, stored only as a salted hash | `internal-signer/otp.util.ts` |
| Hashing | SHA-256 fingerprints of PDFs, evidence and events | `internal-signer/hash.util.ts` |
| Audit chain | Append-only, hash-linked log of every engine event, and its verification | `internal-signer/audit-chain.service.ts` |
| Workflow resolution | Picks the workflow for a group, builds the snapshot the consumer stores and selects representatives for the quorum; called by the consumer | `internal-signer/signature-workflow.resolve.ts` |
| Phase gate | Decides which side may sign now and when both sides are complete | `internal-signer/signature-phase.service.ts` |
| Evidence sheet | The page appended to each PDF listing who signed, when, from where and against which hash | `internal-signer/evidence-sheet.ts` |
| Sealing | Checks each original's hash, applies the representatives' and the platform's PAdES signatures, stores the sealed PDFs | `internal-signer/sealing.service.ts`, `internal-signer/pades-sealer.ts` |
| Verification | One verdict over the PAdES seals and the audit chain together; no route calls it yet (the evidence export calls the audit-chain verification directly) | `internal-signer/signature-verification.service.ts` |
| Configuration | Every environment setting of the engine, including the certificates, read in one place. The code resend limit is fixed in the client, and workflow resolution also reads `NODE_ENV` | `internal-signer/internal-signer.config.ts` |
| Observability | Structured metrics for unattended signing and the stuck-group scan | `internal-signer/enterprise-signing.observability.ts` |

`internal-signer/` stands for
`lib/integration/signature-provider/internal-signer/`. Paths are relative to
`backend/horizon-api/`.

## The provider interface

Consumers call the engine only through `SignatureProviderClient`; the engine
instance is exported as `internalSignerClient`.

| Method | What it does |
| - | - |
| `createSignatureRequest` | Opens the provider request for a group the consumer already created, and records `request_created` |
| `sendSignerOtp` | Emails a one-time code to a seat's owner |
| `registerSignerAction` | Registers a `sign` or `reject` gesture for a seat |
| `getSignedDocument` | Returns a sealed PDF |
| `cancelSignatureRequest` | Cancels the provider request: invalidates the pending codes of the listed signers and marks the request `cancelled`; the consumer marks the group `cancelled` |

The engine client also exposes `applyUnattendedEnterpriseSignatures`, which
the unattended company-side job calls. Each group records its provider in
`signature.signature_provider`; the in-house engine is `internal_signer`.

## Tables

The `signature_*` tables belong to the signature domain. The consumer creates
the group, its documents, its seats and the sealed versions; the engine writes
the provider request, the codes, the evidence and the audit chain, and moves
the signing statuses; the consumer opens the group (`in_progress`) and cancels
it. Their columns are on the generated schema pages.

| Table | Role | Schema |
| - | - | - |
| `signature` | The group, its status, provider and frozen workflow snapshot | [Signature](https://docs.bbrands.io/schemas/signature/signature.md) |
| `signature_document` | One per document; points to a contract, an annex or both (at least one) | [Signature document](https://docs.bbrands.io/schemas/signature/signature-document.md) |
| `signature_document_version` | Sealed versions of a document, written by the consumer after sealing | [Signature document version](https://docs.bbrands.io/schemas/signature/signature-document-version.md) |
| `signature_signer` | One seat per signer: side (customer or enterprise), signing mode, order, status | [Signature signer](https://docs.bbrands.io/schemas/signature/signature-signer.md) |
| `signature_signer_otp` | Hashed one-time codes | [Signer OTP](https://docs.bbrands.io/schemas/signature/signature-signer-otp.md) |
| `signature_signer_evidence` | The evidence of each gesture | [Signer evidence](https://docs.bbrands.io/schemas/signature/signature-signer-evidence.md) |
| `signature_provider`, `signature_provider_request` | The provider catalogue and one request per group | [Provider](https://docs.bbrands.io/schemas/signature/signature-provider.md), [Provider request](https://docs.bbrands.io/schemas/signature/signature-provider-request.md) |
| `signature_provider_event` | The audit chain (the rows with `seq`); a consumer may also store unchained markers here | [Provider event](https://docs.bbrands.io/schemas/signature/signature-provider-event.md) |
| `signature_workflow`, `signature_enterprise_signer`, `signature_workflow_enterprise_signer` | Workflows, the representatives catalogue and which representatives each workflow uses | [Signature workflow](https://docs.bbrands.io/schemas/signature/signature-workflow.md), [Enterprise signer](https://docs.bbrands.io/schemas/signature/signature-enterprise-signer.md), [Workflow enterprise signer](https://docs.bbrands.io/schemas/signature/signature-workflow-enterprise-signer.md) |
| `signature_status`, `signature_signer_status` | Status catalogues | [Signature status](https://docs.bbrands.io/schemas/signature/signature-status.md), [Signer status](https://docs.bbrands.io/schemas/signature/signature-signer-status.md) |
| `signature_notification` | Links a group to the notifications it sent | [Signature notification](https://docs.bbrands.io/schemas/signature/signature-notification.md) |
| `signature_event` | A per-group event log kept for the back office; the engine does not write it | [Signature event](https://docs.bbrands.io/schemas/signature/signature-event.md) |

Original PDFs and sealed PDFs are stored in the private `contracts` bucket.

## Statuses

| Group (`signature`) | Seat (`signature_signer`) |
| - | - |
| `pending`, `in_progress`, `partially_signed`, `awaiting_enterprise_signature`, `signed`, `rejected`, `cancelled`, `expired` | `pending`, `viewed`, `signed`, `rejected`, `cancelled` |

The internal values are declared in
`lib/common/catalog/contract-flow.constants.ts`.

## Routes

| Routes | Purpose |
| - | - |
| `POST /api/v3/signature/{id}/signer/{signer}/otp`, `…/sign` | The signer's code request and gesture |
| `/api/v3/signature/workflow/*` | Create, resolve, update and delete workflows and their representatives |
| `/api/v3/signature/enterprise-signer/*` | The representatives catalogue, revoke, stuck-group list and retry |
| `/api/v3/signature`, `/api/v3/signature/{document,signer,event,provider,status,…}` | Back-office access to the main signature tables (groups, documents and versions, seats, events, providers, statuses), each with its own permission |

## Boundaries

| Concern | Owner |
| - | - |
| When to sign, which documents, and what happens to the business record | The consumer |
| Who may sign a seat, the gesture, evidence, audit chain, sealing and verification | MaihueSigner |
| The company-side rules (representatives, quorum, phase order) | MaihueSigner workflows, configured per consumer scope |
| The person account and identity documents of a signer | Account and identity |
| Sending the emails | The notification core |

> [!NOTE]
>
> Every `signature_document` must point to a contract or a contract annex (a
> database check enforces it), so today the engine signs contract documents
> only. A new kind of document needs that reference extended.


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