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

# Signing flow

> How a signer signs with MaihueSigner: requesting a one-time code, the gesture tied to the verified identity, the guards that run before the engine, rejection, and how the group status advances.

This page follows one seat from the code request to the signed status, and
the group from opening to sealing.

## Overview

```mermaid theme={null}
sequenceDiagram
    actor S as Signer
    participant R as Signer routes
    participant E as MaihueSigner
    participant N as Notification core

    S->>R: POST /signature/{id}/signer/{signer}/otp
    R->>R: Seat owner, re-entry and order checks
    R->>E: sendSignerOtp
    E->>E: Phase gate, resend budget, new code
    E->>N: Email the code
    E-->>R: Masked email + expiry
    R-->>S: Masked email + expiry
    S->>R: POST /signature/{id}/signer/{signer}/sign (code)
    R->>R: Same checks
    R->>E: registerSignerAction (sign)
    E->>E: Validate code, resolve document number
    E->>E: Evidence + audit event
    E-->>R: Seat signed, group status, sealed?
```

## Guards before the engine

The signer routes run these checks before calling the engine:

| Check | Answer when it fails |
| - | - |
| The seat belongs to the session user | `403` |
| A recovered session has proved the identity card serial again | `422 reentry-serial-required` |
| For company administrators, the previous customer seat has signed | `409 prior-signer-pending` |

The order lock applies to customer seats only and only when there is more
than one; company representatives follow the workflow.

## Requesting a code

`sendSignerOtp`:

1. Refuses a seat that has already signed, rejected or been cancelled
   (`409`).
2. Applies the phase gate: the seat's side must be the one whose turn it is
   (`409` otherwise). See [Workflows](https://docs.bbrands.io/signature-engine/workflows.md#phase-order).
3. Applies a resend budget per seat (`429` when it is spent).
4. Creates a new six-digit code, stores only its salted hash and invalidates
   any earlier code for the seat.
5. Hands the email to the notification core, which queues it. If the core
   skips it or fails, the call fails and nothing is recorded as sent.
6. Appends `otp_sent` to the audit chain and answers the masked email and the
   expiry time.

## The signing gesture

`registerSignerAction` with `sign` and the code:

1. Applies the phase gate again.
2. Validates the code: it must not be expired, used or past its wrong-attempt
   limit.
3. Resolves the **document number** recorded for the gesture (below).
4. Computes one hash over the hashes of every document in the group: a single
   gesture signs all of them.
5. Marks the seat `signed`, stores its evidence and appends `signer_signed`
   to the audit chain.
6. Advances the group (below).

### The document number

The signer types only the code. The engine resolves the document number from
the signer's verified identity, in this order:

1. The latest verified identity of the signer, counted only if it happened
   after the contract started (when it has started). When the signer has a
   **person account**, only identity records for that account's RUT count
   (person accounts that disagree on the RUT fail with `422`). Since the card
   serial is no longer stored, the verified RUT is the document number.
2. Only for the primary signer (the seat with `signing_order = 1`) with no
   identity record at all: the RUT on their person account, or else the RUT
   of the contract's account.
3. Any other case is refused with `400`: the signer must verify their
   identity first.

The evidence keeps only a SHA-256 hash of the normalized number.

## Rejecting

`registerSignerAction` with `reject` (and an optional reason) marks the seat
and the group `rejected` and appends `signer_rejected` to the
audit chain. No other seat can sign afterwards. The phase gate does not
apply to a rejection.

## How the group advances

```mermaid theme={null}
stateDiagram-v2
    [*] --> pending: group created
    pending --> in_progress: seats opened
    in_progress --> partially_signed: a customer signed, others pending
    in_progress --> awaiting_enterprise_signature: customers complete (last)
    partially_signed --> awaiting_enterprise_signature: customers complete (last)
    awaiting_enterprise_signature --> signed: company side complete, PDFs sealed
    awaiting_enterprise_signature --> rejected: an attended company seat rejects
    in_progress --> rejected: a signer rejects
    partially_signed --> rejected: a signer rejects
    pending --> cancelled: consumer cancels
    in_progress --> cancelled: consumer cancels
    partially_signed --> cancelled: consumer cancels
    awaiting_enterprise_signature --> cancelled: consumer cancels
    signed --> [*]
```

The diagram shows `company_phase = last`. With `first`, the group waits for
the company side before any customer seat can sign, and the last customer
signature seals the group.

| After a gesture | Group status | What the engine reports |
| - | - | - |
| Customer seats still pending | `partially_signed` | How many seats are pending |
| The customer side is complete and company seats are still pending (`last`) | `awaiting_enterprise_signature`, until the company side is complete | That the company phase is due |
| Both sides are complete | `signed` | `sealed: true`; the PDFs are sealed |

The consumer decides what each outcome means for its own record. The engine
also exposes `cancelSignatureRequest`, which invalidates the pending codes of
the signers listed when the request was opened and marks the request
cancelled.

## Code

| Concern | Path |
| - | - |
| Code request and gesture | `lib/integration/signature-provider/internal-signer/internal-signer.client.ts` |
| Code generation and hashing | `lib/integration/signature-provider/internal-signer/otp.util.ts` |
| Signer routes and guards | `app/api/v3/signature/[document_id]/signer/[signer_id]/`, `lib/endpoint/signature-signing/` |
| Order lock | `lib/endpoint/signature-signing/assert-customer-sequential-sign-lock.ts` |
| Schema pages | [Signature signer](https://docs.bbrands.io/schemas/signature/signature-signer.md), [Signer OTP](https://docs.bbrands.io/schemas/signature/signature-signer-otp.md) |

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


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