Skip to content

Upgrade/security and core - #1

Merged
SYMBAxx merged 5 commits into
mainfrom
upgrade/security-and-core
Nov 6, 2026
Merged

SYMBAxx merged 5 commits into
mainfrom
upgrade/security-and-core

Conversation

@SYMBAxx

@SYMBAxx SYMBAxx commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Threshold encryption: orders are encrypted in the browser so that any 3 of 5 validators can decrypt them and fewer learn nothing (ElGamal KEM on ristretto255 + AES-256-GCM). Ciphertexts are bound to the submitter's address.
  • Validator decryption: each validator's decryption share comes with a Chaum–Pedersen proof that it's correct, and invalid shares are discarded. Up to 2 faulty validators are tolerated. Decryption happens only after a batch is sealed.
  • Ciphertext-only intake: the backend no longer receives tokens or amounts in plaintext.
  • Wallet-signature auth: a SEP-10-style challenge signed in Freighter replaces the spoofable x-user-address header. Order history is private to its owner.
  • Tests and CI: 31 backend, 8 frontend and 12 contract tests run in GitHub Actions on every PR, plus a source-integrity check that blocks hidden or obfuscated code.
  • Docs: threshold encryption design doc, SECURITY.md, updated README and contributor issues.

Fixes

  • Settlement called BigInt() on decimal strings like "10.5"; amounts are now converted to exact base units in the browser.
  • Freighter signTransaction was given 'TESTNET' instead of the network passphrase.

Breaking changes

  • POST /api/orders now takes only {encrypted_data, threshold_pubkey, nonce} and requires a Bearer session.
  • New env vars: AUTH_TOKEN_SECRET, VALIDATOR_COUNT, VALIDATOR_THRESHOLD (see backend/.env.example).

Still simulated

The validators run in-process, and keys come from a trusted dealer. See docs/THRESHOLD_ENCRYPTION.md. Distributed key generation and remote validators are tracked as contributor issues.

Test plan

  • CI is green (integrity, backend, frontend, contracts)
  • With the backend and frontend running locally: connect Freighter, sign in, submit a swap, and see it settle in the next batch

SYMBAxx and others added 5 commits November 6, 2026 22:23
…-GCM)

Replaces the placeholder AES/base64 scheme with real t-of-n threshold
encryption:

- orderCipher.ts: clients encrypt to the validator group key Y. Ephemeral
  r, R = rG, K = rY, HKDF-SHA256 -> AES-256-GCM, with R and the user's
  address as AAD so a ciphertext can't be replayed under another account.
  Environment-neutral (WebCrypto + @noble/curves) and shipped
  byte-identically to the frontend; a test enforces the copies match.
- threshold.ts: Shamir key sharing, per-validator partial decryption
  D_i = y_i*R with a Chaum-Pedersen DLEQ proof, proof verification, and
  Lagrange combination of any t verified shares into K = yR.

Keygen is a trusted dealer for now (simulated validator set); a DKG is
the documented next step.

Backend tests move to Vitest, since stellar-sdk 17's ESM dependencies
can't load under Jest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…only

The order pipeline now actually depends on the encryption:

- Clients submit only encrypted_data + threshold_pubkey + nonce. Tokens and
  amounts are no longer sent in plaintext alongside the ciphertext; the
  Order columns for them stay NULL until the batch is decrypted.
- validatorNetwork: a simulated t-of-n validator set (default 3-of-5),
  each node holding one key share, behind an async per-validator interface
  so remote validators can replace it. BYZANTINE_VALIDATORS injects faulty
  nodes for demos.
- validatorService.coordinateDecryption: collects shares for a sealed
  batch, verifies every DLEQ proof against the registered verification
  key, combines t good shares, decrypts, and validates the payload
  (user binding, nonce match, base-unit integer amounts). A bad order
  fails alone without blocking its batch.
- The batching loop now seals, decrypts, and settles in one pass. A
  conditional status claim prevents double settlement. The unauthenticated
  'settle with these decrypted orders' endpoint is gone; POST
  /settlement/:id/settle now only retries the honest path.
- Intake rejects malformed ciphertext, stale threshold keys, and reused
  nonces.
- Frontend converts amounts to 7-decimal base units exactly; settlement
  previously called BigInt() on strings like '10.5' and would throw.
- Express app split from server start (app.ts) for testing.

12 new end-to-end tests cover reveal-after-settle, byzantine tolerance
(2 of 5 bad nodes), failure past n-t, cross-user replay, malformed orders,
and double settlement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…0 style)

The backend trusted an x-user-address header, so anyone could act as any
address. Now:

- POST /api/auth/challenge returns a transaction sourced from the user's
  account with sequence 0 (it can never be submitted on-chain), carrying
  a manageData value expiry || random || HMAC. The HMAC makes challenges
  stateless and unforgeable by clients.
- POST /api/auth/verify checks the HMAC, the 5-minute expiry, and the
  ed25519 signature over the tx hash against the source account, then
  issues a 1-hour HMAC session token.
- validateAuth takes the address only from a verified token.
  GET /orders/user/:address now requires auth and only returns your own
  orders, since sealed orders still leak timing.
- Frontend signs the challenge in Freighter, caches the session per tab,
  and clears it on disconnect. This also fixes signTransaction passing
  'TESTNET' as the network passphrase.

8 new tests: spoofed signer, unsigned and self-forged challenges, tampered
tokens, the legacy header rejected, and order-history privacy. All 31
backend tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Frontend: Vitest + Testing Library. SwapForm test proves the POST body
  holds only {encrypted_data, threshold_pubkey, nonce}, carries the
  session header, and decrypts to the exact base-unit order. Also covers
  exact decimal-to-base-unit conversion, validation, and display.
- CI (GitHub Actions): backend typecheck/test/build, frontend test/build,
  contracts cargo test, on every push to main and every PR.
- scripts/check-source-integrity.sh, run as the first CI job: fails on
  minified-length lines, code hidden behind long whitespace runs, and
  eval/Function-constructor/large-atob usage. All three detectors fire on
  the payload removed in 61714e4, so a repeat injection can't merge
  unnoticed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d issues

- docs/THRESHOLD_ENCRYPTION.md: the scheme, each security property mapped
  to the test that checks it, and what is still simulated (trusted-dealer
  keygen, in-process validators).
- SECURITY.md: private reporting, scope, and supply-chain hygiene.
- README, backend README, CONTRIBUTING, and the in-app docs page now
  describe what is actually implemented, plus the API table and test
  commands.
- CONTRIBUTOR_ISSUES.md: the finished issues are removed; new scoped
  issues cover DKG, remote validators, on-chain ciphertext commitments,
  migrations, rate limiting, and more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SYMBAxx
SYMBAxx merged commit e3e1157 into main Nov 6, 2026
4 checks passed
SYMBAxx added a commit that referenced this pull request Nov 7, 2026
Explain the DKG rounds, map each DKG property to its test, and state its
limitations (Joint-Feldman key bias, broadcast assumption). The
'simulated' caveat is now precise: there is no dealer, but validators
sharing one process means one operator could still collect t shares
until the validators run remotely. Contributor issue #1 becomes the GJKR
unbiased-DKG upgrade.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant