← Back to Work
Full-Stack · Backend Engineering[PRODUCT][PRODUCT BUILD][REAL BUILD SHOWN]

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.

Open the live app (opens in a new tab)

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.

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

› A signature here is a drawn or typed image. It is not a cryptographic or certificate-based signature, and Countersign makes no legal or compliance claims.
› A signing link is a bearer credential: whoever holds it can sign as the counterparty. There is no second factor.
› The audit log's append-only rule is a database trigger. It guards against application bugs and ordinary SQL mistakes, not against a database owner who can disable triggers, and there is no hash chain.
› The app sends no email. Notifications are recorded in an in-app outbox.

Proof

The real build

Real buildAgreement / Frozen copy
After sending. The document is frozen and the page shows it as sent, with its SHA-256 fingerprint in the document details. The sender copies the private link, which also appears in the outbox.
Agreement / Frozen copy · original resolution

Where it stands

What works today

› A working end-to-end flow: create, send, the counterparty signs, the designated signatory countersigns, and the executed PDF downloads.
› A verifiable history for every agreement: who did what, in order, tied to the fingerprint of the exact document.
› 324 automated tests run against a real PostgreSQL, and they passed in GitHub Actions on the merged main branch.
› No usage figures are claimed. The repository is public, so every rule above can be checked in the code.

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.

› Cause: a read-then-write race. The check and the write were separate steps, so nothing stopped two requests from both succeeding.
› Fix: every transition became a conditional update inside one transaction (WHERE the status is the expected one) that must change exactly one row. PostgreSQL serializes the row update, so a second request re-checks the committed row, matches nothing, and rolls back.
› Proof: the concurrency tests fire simultaneous attempts at a real database and assert exactly one winner and exactly one event.

Known next steps

› Real email delivery. Notifications go to an in-app outbox today.
› Unattended retries. Failed deliveries are retried after each step or by hand; a scheduled worker isn't built.
› A second factor on signing links and a hash-chained audit log are outside this project's scope.

Stack

Next.jsTypeScriptPostgreSQLPrismaAuth.js
Alsopdf-libPuppeteerVitestGitHub Actions

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.

› Which exact document was approved?
› Who signed, in what order, and were they allowed to?
› Can any of it have changed since?

Build notes

› Origin: a standalone version of the MNDA signing workflow I built at Valiant (see Valiant Operations Intelligence), with its own data model, tests and audit trail.

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.

› Lifecycle: draft, sent (awaiting the counterparty), partially signed (awaiting the countersignature), then executed or voided. A CHECK constraint keeps the status in agreement with the signing timestamps, and a trigger makes executed and voided final.
› Every step runs the same way: validate and authorize, do slow work such as rendering the PDF before the transaction so no row locks are held, then move the agreement in one transaction and write the signer, link, stored PDF and audit rows together.
› The frozen copy is rendered with headless Chromium. The executed PDF is built afterwards from the frozen bytes with pdf-lib, deterministically, so a retry produces the same bytes and the same SHA-256.
› Execution commits first. The executed PDF is generated after commit, and a failure is recorded and retried on read, so a rendering problem can never leave an execution half finished.
› Signing in uses Auth.js. Being able to countersign is a separate permission from being a user, and an agreement names exactly one designated countersigner.
› Migrations are additive and each runs in a transaction. The tests start a throwaway PostgreSQL, apply the real migration history to a template database, and give each test file its own clone.
› GitHub Actions runs typecheck, lint, the tests and a production build on every push, against a PostgreSQL 18 service container.
› The hosted app opens in one click as the company's General Counsel, with sample agreements at every stage. The sign-in runs on the server and the sample accounts have no published password. A test pins where that entry is available.
› The hosted instance is a shared sample workspace. After 30 minutes without changes it starts a fresh session instead of deleting anything, and it caps agreements and signing links per session with database triggers.
› The product site is a nine-section landing page: outcome hero, problem, product tour, use cases, trust, comparison with email, FAQ and a closing call to action. Its screenshots are captures of the running application, and one scroll-triggered timeline plays the signing order once and respects reduced motion.

Need a workflow where authorization and correctness matter?

› Get in Touch