Skip to content
This repository was archived by the owner on Jul 22, 2026. It is now read-only.

feat: update to keyring v2 - #645

Open
hmalik88 wants to merge 31 commits into
mainfrom
hm/add-v2-methods
Open

feat: update to keyring v2#645
hmalik88 wants to merge 31 commits into
mainfrom
hm/add-v2-methods

Conversation

@hmalik88

@hmalik88 hmalik88 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Migrates the Bitcoin snap to the v2 Keyring API and declares the corresponding capabilities in the manifest. Also removes all v1-only keyring methods and replaces the manual routing table with the v2 dispatcher.

  • Rewrites KeyringHandler to implement KeyringSnapRpc (from @metamask/keyring-api/v2): adds getAccounts, getAccount, createAccounts, exportAccount, getAccountBalances, getAccountAssets, getAccountTransactions, deleteAccount, submitRequest, setSelectedAccounts, and resolveAccountAddress
  • Removes v1-only flows (createAccount, discoverAccounts, filterAccountChains, updateAccount) and stops emitting AccountCreated / AccountDeleted events
  • Folds bip44:discover into createAccounts: calls AccountUseCases.discover(), deletes the account from state if no transactions are found, and returns [] to signal end-of-discovery
  • Adds exportAccount with WIF private-key export (Base58Check, version byte 0x80/0xEF); uses is() not assert() to prevent the private key from appearing in a StructError message on encoding failure
  • Reads SUPPORTED_SCOPES directly from snap.manifest.json capabilities block (same pattern as Solana snap) instead of hardcoding the network
  • Switches handleKeyringRequest to the v2 dispatcher (@metamask/keyring-snap-sdk/v2)
  • Adds endowment:keyring.capabilities to snap.manifest.json: scopes, privateKey.exportFormats, and bip44 derive/discover flags
  • Sets module: "ESNext" and moduleResolution: "bundler" in packages/snap/tsconfig.json so /v2 subpath exports resolve correctly
  • Bumps @metamask/keyring-api → ^23.5.0, keyring-snap-sdk → ^9.2.0, snaps-sdk → ^11.2.0, snaps-cli → ^8.4.1, snaps-jest → ^10.2.0
  • Updates KeyringHandler.test.ts: removes v1-only describe blocks, adds exportAccount and bip44:discover coverage, mocks wif module, adds beforeEach(() => jest.resetAllMocks()) to fix mock-call accumulation between tests

References

N/A

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

High Risk
Breaking keyring RPC contract and new private-key export path are security-critical; clients must adopt v2 methods and capabilities before upgrade.

Overview
Breaking migration to MetaMask Keyring API v2: onKeyringRequest now delegates to handleKeyringRequest from @metamask/keyring-snap-sdk/v2 instead of a custom route() switch on v1 method names.

KeyringHandler implements KeyringSnapRpc with v2 names (getAccounts, getAccountTransactions, etc.) and drops v1-only paths (createAccount, discoverAccounts, filterAccountChains, updateAccount). Account creation is unified under keyring_createAccounts: bip44:derive-path (via new parseDerivationPath), index/range batching across manifest SUPPORTED_SCOPES, and bip44:discover (discover, delete if no txs, return [] when done). exportAccount adds WIF (base58) export with guarded error handling so keys do not leak in validation messages.

The snap manifest gains endowment:keyring.capabilities (scopes, BIP44 derive/discover, private-key export). Mapped accounts are exportable: true. Dependencies bump keyring/snaps packages; integration tests expect v2 methods, array results, and updated error codes/messages.

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

@socket-security

socket-security Bot commented Jul 10, 2026

Copy link
Copy Markdown

@socket-security

socket-security Bot commented Jul 10, 2026

Copy link
Copy Markdown

Warning

MetaMask internal reviewing guidelines:

  • Do not ignore-all
  • Each alert has instructions on how to review if you don't know what it means. If lost, ask your Security Liaison or the supply-chain group
  • Copy-paste ignore lines for specific packages or a group of one kind with a note on what research you did to deem it safe.
    @SocketSecurity ignore npm/PACKAGE@VERSION
Action Severity Alert  (click "▶" to expand/collapse)
Warn Low
Potential code anomaly (AI signal): npm @metamask/snaps-simulation is 62.0% likely to have a medium risk anomaly

Notes: The fragment is primarily an orchestration layer for installing/executing MetaMask Snaps and registering internal RPC/action handlers. The main high-impact risk signals are (1) direct execution of snap source code obtained from fetchSnap(), and (2) sensitive key material derivation (mnemonic/seed) from options.secretRecoveryPhrase in a clearly labeled 'withKeyringV2Unsafe' handler that passes mnemonic+seed into an externally provided operation callback. Additionally, SnapController:getSnapFile forwards caller-controlled path/encoding to getSnapFile(auxiliaryFiles, ...)—which could enable unintended file access depending on that helper’s validation. No clear malicious exfiltration or obfuscation is visible in this snippet alone.

Confidence: 0.62

Severity: 0.58

From: ?npm/@metamask/snaps-jest@10.2.0npm/@metamask/snaps-simulation@4.3.0

ℹ Read more on: This package | This alert | What is an AI-detected potential code anomaly?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support@socket.dev.

Suggestion: An AI system found a low-risk anomaly in this package. It may still be fine to use, but you should check that it is safe before proceeding.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/@metamask/snaps-simulation@4.3.0. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

Ignoring alerts on:

  • @ethersproject/web@5.8.0
  • @metamask/snaps-controllers@21.0.0
  • ses@2.2.0

View full report

@hmalik88

Copy link
Copy Markdown
Contributor Author

@SocketSecurity ignore npm/@metamask/snaps-controllers@21.0.0

@hmalik88

Copy link
Copy Markdown
Contributor Author

@SocketSecurity ignore npm/ses@2.2.0

@hmalik88

Copy link
Copy Markdown
Contributor Author

@SocketSecurity ignore npm/@ethersproject/web@5.8.0

@hmalik88
hmalik88 marked this pull request as ready for review July 13, 2026 08:36
@hmalik88
hmalik88 requested a review from a team as a code owner July 13, 2026 08:36
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/integration-test/keyring-request.test.ts
Comment thread packages/snap/integration-test/keyring.test.ts Outdated
Comment thread packages/snap/integration-test/keyring.test.ts Outdated
writeFileSync(
join(__dirname, '../locales/en.json'),
JSON.stringify(englishLocale, null, 2),
`${JSON.stringify(englishLocale, null, 2)}\n`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CI seemed to been have failing on it

Comment thread packages/snap/tsconfig.json
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/src/index.ts
Comment thread packages/snap/src/handlers/parsers.ts
Comment thread packages/snap/src/handlers/mappings.ts
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/src/handlers/KeyringHandler.ts Outdated
Comment thread packages/snap/snap.manifest.json
Comment thread packages/snap/snap.manifest.json Outdated
Comment thread packages/snap/CHANGELOG.md Outdated
Comment thread packages/snap/package.json Outdated
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants