RunningSince 2023

Web · USB token

Architecture, signing, backend and dashboard

Healthy Tax

Government e-invoicing for a pharmaceutical distributor: every invoice signed on a certified USB token and submitted to Egypt's Tax Authority, with what the Authority accepted synced back.

  • In production since

  • Scheduled syncs with the Authority a day

  • Token, guarded by a global signing lock

  • Commits

Healthy Tax dashboard, figures redacted: submitted invoices per day and the token's status

01/Problem

Egypt made e-invoicing mandatory: each invoice has to be serialised, signed with a certified token in CAdES-BES and accepted by the Tax Authority. The company's invoices live in its ERP on SQL Server, which does none of that.

The goal: invoices leave the ERP, get signed and accepted every day without anyone babysitting them, and staff can see and fix the few that bounce.

02/Constraints

  • 01One physical USB token signs everything, and its vendor library is not safe across processes.
  • 02The Authority's API times out and resets connections; a retry must never create a duplicate.
  • 03The signed document must hash exactly as the Authority serialises it, key by key.
  • 04Runs on a company machine, next to the token.

03/Architecture

Staff

  • Dashboard · React 18 + MUI
  • manual invoices · XLSX import
  • Socket.io status

Service

  • Express · TypeScript
  • scheduled syncs
  • Authority API client

Signing

  • PKCS#11 worker thread
  • CAdES-BES builder
  • global lock · watchdog

Data

  • ERP · SQL Server
  • MongoDB
  • Redis
Invoices are read from the ERP, serialised the way the Authority hashes them, signed on the token in a worker thread and submitted. Syncs run at 23:00, 01:00 and 08:00 to pull back what the Authority accepted or rejected.

04/Decisions

  1. One token, one signer at a time

    A global signing lock spans users and sessions, and the dashboard shows who holds it live. USB work runs in a worker thread so a slow token never freezes the server.

  2. A single process with a watchdog, not a cluster

    The signing library is not multi-process safe, so 32 cluster workers became one. A 3-second watchdog stops a hung close from poisoning the signer, and a single release-and-reset action frees the lock, clears sessions and resets the token.

    Trade-off: Throughput is bound to one signer, so per-signature debug work was gated off to save 80 to 250 ms each.

  3. Retry safely against a flaky API

    Submissions retry on timeouts and connection resets because the Authority deduplicates by internal ID, and those IDs were made deletion-proof so a credit note can't reuse one.

  4. Hash exactly what the Authority hashes

    Manual invoices were rejected with error 4043, digest mismatch. The Authority's serialiser hashes every key, even an empty signatures array, so the builder now omits it. Manual and imported invoices reuse the same signing pipeline.

05/Stack

Node.jsExpressTypeScriptPKCS#11CAdES-BESMongoDBSQL ServerRedisSocket.ioReact 18MUI

Next systemHealthy Temp
Mohammed Yousef Ali | Systems Architect for real businesses