Files
temetro/backend/src/routes/signing.ts
T
Khalid Abdi 2d47abcc42 feat: patient wallet — real Signing, encrypted share relay, import-from-app
Backend
- clinic Ed25519 signing key (services/signing.ts, routes/signing.ts,
  clinic_signing_keys table) — Settings → Signing is now real
- @noble wallet-crypto (lib/wallet-crypto.ts): ed25519 identity, base58check
  wallet numbers, sealed-box (x25519 + xchacha20poly1305)
- /wallet Socket.io relay namespace (challenge-signed device auth) forwarding
  only ciphertext; emitToWallet helper
- import-from-app flow (routes/patients-wallet.ts, services/wallet-share.ts):
  request-share → patient approval → decrypt + verify → review draft → commit
- temporary shares: patients.share_expires_at + 5-min auto-delete sweep; revoke

Frontend
- SigningPanel wired to live key/fingerprint/rotate + shared-records list
- "Import from a patient app" dialog (lib/signing.ts, import-from-wallet-dialog)
  reusing the draft-review path; temporary badge on the patient list

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 18:19:57 +03:00

64 lines
1.7 KiB
TypeScript

import { Router } from "express";
import {
requireAuth,
requireOrg,
requirePermission,
} from "../middleware/auth.js";
import { recordActivity } from "../services/activity.js";
import * as signing from "../services/signing.js";
import * as walletShare from "../services/wallet-share.js";
export const signingRouter = Router();
signingRouter.use(requireAuth, requireOrg);
// The clinic's Ed25519 signing key (public key + fingerprint). Created lazily on
// first read so the panel always shows a real key. Readable by any clinician.
signingRouter.get(
"/key",
requirePermission({ patient: ["read"] }),
async (req, res, next) => {
try {
res.json(await signing.getOrCreateKey(req.organizationId!));
} catch (err) {
next(err);
}
},
);
// Rotate the signing key — owner/admin only (gated on the org-update statement,
// which only owner/admin hold).
signingRouter.post(
"/key/rotate",
requirePermission({ organization: ["update"] }),
async (req, res, next) => {
try {
const key = await signing.rotateKey(req.organizationId!);
await recordActivity({
orgId: req.organizationId!,
actor: { id: req.user!.id, name: req.user!.name },
action: "Rotated the clinic signing key",
entityType: "settings",
});
res.json(key);
} catch (err) {
next(err);
}
},
);
// Recent records shared from patient wallets — feeds the panel's shared-records
// list.
signingRouter.get(
"/records",
requirePermission({ patient: ["read"] }),
async (req, res, next) => {
try {
res.json(await walletShare.listShareRequests(req.organizationId!));
} catch (err) {
next(err);
}
},
);