feat: split kyc controller responsibility by Akaryatrh · Pull Request #10094 · MetaMask/core · GitHub
Skip to content

feat: split kyc controller responsibility - #10094

Open
Akaryatrh wants to merge 15 commits into
mainfrom
feat/slimmer-kyc-controller
Open

feat: split kyc controller responsibility#10094
Akaryatrh wants to merge 15 commits into
mainfrom
feat/slimmer-kyc-controller

Conversation

@Akaryatrh

@Akaryatrh Akaryatrh commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Explanation

KycController.ts had grown to ~2,400 lines because it carried several unrelated concerns in one file: the phase machine and flow orchestration, the full state type plus its persistence metadata and defaults, pure consent-record helpers, and the UKYC attestation / capability-token wrapping glue. That made the actual orchestration logic hard to find and hard to test in isolation — the consent helpers, for instance, were module-private and could only be exercised through the controller.

This PR extracts the cohesive pieces into dedicated modules so the controller is left as the orchestrator it's meant to be. Each extracted module is domain-named rather than a generic utils.ts, so ownership stays obvious:

  • src/KycControllerState.ts — the KycControllerState type, kycControllerMetadata, and the getDefaultKycControllerState / getDefaultKycVendorDisclaimersAccepted / getDefaultKycProviderDisclaimersAccepted factories.
  • src/consents.ts — the pure consent-record and error-shape predicates: consentRecordKey, isSessionAlreadyCompletedError, isConsentConflictError, isValidConsentRecordList, consentRecordsFromAcceptedList, isAcceptedCategoryEmpty, acceptedCategoryStillMissing, and usesConsentsFlow.
  • src/ukyc/sessionAuthorizations.ts — the attestation and capability-token wrapping glue: UKYC_CAPABILITY_TOKEN_TTL_MS, assertAttestedServerPublicKey, and wrapUkycSessionAuthorizations.

Two repeated blocks inside the controller were also folded into private helpers: #markAlreadyCompleted (the "session already completed" error branch, which appeared identically in two flows) and #refreshUserStatusSoft (the best-effort user-status refresh). Both keep the existing #generation guard so a reset() landing mid-flight still can't write onto an idle controller.

Net effect: KycController.ts goes from 2,424 to 1,954 lines.

This is a pure restructuring — no behavior change and no API change.

Documentation

ARCHITECTURE.md gains the new modules in its key-source-files appendix, along with vendorDisclaimerAcceptance.ts and src/ukyc/, which were already missing from that table. I also corrected some drift left over from #10082: the doc still described KycController as owning the frame protocol and an X25519 keypair named #keypair created at construction, when that material now lives in MoonPayFrameHandler as #frameKeypair and is minted when a MoonPay flow starts. The component diagram now routes the frame protocol and decryptCredentials() through the handler rather than the controller.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Low Risk
Internal module layout and docs only; behavior and package exports are intended to be unchanged, with added unit tests on extracted pure logic.

Overview
Refactors KycController into focused modules so the main file stays the phase machine and flow orchestration only. State shape, persistence metadata, and default factories move to KycControllerState.ts; consent catalog mapping and HTTP/error predicates move to consents.ts (with new unit tests); JWT attestation and wrapping for setAuthorizations move to ukyc/sessionAuthorizations.ts (also tested). Package root exports are rewired so public API names stay the same.

Inside the controller, duplicated “already completed KYC” handling is consolidated into #markAlreadyCompleted, and best-effort refreshKycStatus calls use #tryRefreshKycStatus so toast refresh failures still cannot rewind the flow.

ARCHITECTURE.md and the changelog document the split and correct earlier drift: MoonPay frame crypto/protocol is attributed to MoonPayFrameHandler, not KycController.

Reviewed by Cursor Bugbot for commit 2ab43a7. Bugbot is set up for automated code reviews on this repo. Configure here.

jiexi and others added 8 commits September 2, 2026 17:33
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
@Akaryatrh
Akaryatrh requested a review from a team as a code owner September 3, 2026 15:35
@Akaryatrh
Akaryatrh deployed to default-branch September 3, 2026 15:35 — with GitHub Actions Active
@Akaryatrh
Akaryatrh requested a review from a team as a code owner September 3, 2026 16:31

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 997ef28. Configure here.

Comment thread packages/kyc-controller/src/ukyc/sessionAuthorizations.ts
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
Signed-off-by: Sébastien Van Eyck <sebastien.vaneyck@consensys.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants