Countersign
Two-party agreements. One enforced signing order.Result: Two-party agreements are signed in one enforced order, and the executed PDF comes with an append-only audit trail.
A standalone version of the MNDA signing workflow I built at Valiant. The counterparty signs first through a private link, only the designated company signatory can countersign, and the database itself refuses any other order.
The landing page opens the sample workspace in one click, signed in as the company's General Counsel, with agreements at every stage.
› View the code on GitHub (opens in a new tab)› The Valiant MNDA work it grew from
- My role
- Designed the data model, enforced the signing order on the server and again in PostgreSQL, added the frozen-document fingerprint and the append-only audit trail, and built the rest of the application end to end: authentication and authorization, PDF generation, the interface, and the test suite and CI.
- Built with
- Next.js, TypeScript, PostgreSQL, Prisma, Auth.js
- Status
- Designed, built and owned by Michael Rhey Palaganas, with a public repository. Notifications are recorded in an in-app outbox.
- 2 layersthe signing order is enforced by the server and again by PostgreSQL constraints
- SHA-256fingerprint of the frozen agreement, which every signature must match
- 324 testsrun against a real PostgreSQL, including simultaneous-signature races
Before
The problem
Agreements pick up version confusion on the way to a signature. It is unclear which copy was approved, who has signed, and who is allowed to sign, and when the order slips or the wrong version is signed, there is no clear record of it.
The product
Two parties, one enforced order
Signing flow
Who signs, and when.
- Prepare and sendA company user drafts an agreement from one of four templates, names the counterparty and chooses the designated countersigner. Sending freezes the document.
- Counterparty signs firstThe counterparty signs in a private signing room reached by an expiring link. No account is needed.
- Designated countersignerOnly that signed-in user, and only after the counterparty has signed, can countersign.
Document integrity
What was approved is what is kept.
- Frozen documentSending renders the agreement to PDF and records its SHA-256. A signature is accepted only for that exact fingerprint.
- Executed PDFThe frozen pages, unchanged, plus an execution page with both signatures, their times and the fingerprint.
Control and record
Every step is traceable.
- Link lifecycleLinks expire after 14 days. Reissuing replaces the old link at once, and voiding revokes it and blocks every further step.
- Audit logEvery step is recorded with who took it, and the database rejects edits and deletes.
- OutboxEach notification is written in the same transaction as the step that causes it. Signing links appear in an in-app outbox, where the sender can open them.
Under the hood
The engineering
The workflow is simple to describe. The work is in making it hard to get wrong. Each rule below is enforced on the server, and the important ones again in PostgreSQL, so a bug in one layer can't quietly break the order.
- Signing order
The server moves an agreement with a conditional update (WHERE the status is what it expects) and requires exactly one row to change. Separately, a CHECK constraint in PostgreSQL rejects any countersignature that doesn't come after the counterparty's signature.
› Tests: signing-order.test.ts, database-invariants.test.ts
- Who may countersign
The caller's identity comes from the session, never the request. They must be the agreement's designated countersigner and still hold signatory rights, checked inside the server action rather than only at the page. A signing link cannot countersign.
› Tests: authorization.test.ts
- Concurrency
Simultaneous attempts to sign, countersign or send the same agreement produce exactly one winner. The tests race six signatures, six countersignatures and four sends against a real database, and each ends with one success and one audit event.
› Tests: concurrency.test.ts
- Document fingerprint
Sending stores the rendered PDF with its SHA-256. A signature must name that hash and is refused if it doesn't match. The executed PDF is built from the stored bytes and refuses to build if their hash has changed.
› Tests: artifacts.test.ts, signing-order.test.ts
- Stored PDFs
Once a PDF is ready, a database trigger rejects any change or deletion, and a CHECK constraint keeps the stored hash equal to the hash of the stored bytes.
› Tests: storage-lifecycle.test.ts
- Signing links
A link is 256 random bits, and only its SHA-256 is stored. It expires after 14 days, signs once, and at most one link per agreement is active. Reissuing or voiding revokes the old link immediately, even when racing a signature.
› Tests: signing-links.test.ts
- Audit history
A trigger rejects UPDATE and DELETE on audit events, and every new event must name a verifiable actor: a company user, a signing link, or the system. Counterparty actions record a keyed hash of the IP address, never the address itself.
› Tests: audit-log.test.ts
What it does not claim
Proof
The real build
Where it stands
What works today
Hardest bug
An early version checked an agreement's state in application code, then wrote. A review found that simultaneous requests all passed the check before any of them wrote: three simultaneous countersignatures produced three FULLY_EXECUTED events for one agreement.
Known next steps
Stack
For technical readers
Technical detail
The problem, in detail
A signing workflow has to answer three questions after the fact, from the system itself rather than from memory or an email thread.
Build notes
One Next.js application in TypeScript with its own PostgreSQL schema, accessed through Prisma. There is a row per agreement, signer, signing link, stored PDF and audit event.
Need a workflow where authorization and correctness matter?
› Get in Touch