Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement CAIP25 MultiChain API and NextJS playground integration #1201

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

abretonc7s
Copy link
Collaborator

@abretonc7s abretonc7s commented Jan 17, 2025

Description

This PR implements a new MultiChain API package that follows the CAIP25 specification to provide chain-agnostic wallet interactions. It also includes a Next.js playground for testing and demonstrating the API capabilities.

Key Features

  • New @metamask/multichainapi package implementing CAIP25 spec
  • Interactive Next.js playground for testing multichain functionality
  • Integration with MetaMask extension for cross-chain operations
  • Support for session management and chain-specific method invocation
  • Rich validation and testing environment

MultiChain API Implementation

  • Implements CAIP25 session management (create/get/revoke)
  • Supports method invocation across different chains
  • Handles wallet discovery and connection
  • Event system for session changes and notifications
  • Built-in TypeScript support and full type safety

Next.js Playground Features

  • Interactive UI for testing CAIP25 functionality
  • Session lifecycle management
  • Chain selection and method invocation
  • Real-time response visualization
  • Comprehensive error handling

Implementation Details

  • Uses Turborepo for improved monorepo development
  • Implements proper validation for message sizes and payloads
  • Handles extension and deep-link communication
  • Supports different environments (extension, mobile web, desktop)
  • Includes thorough unit tests
  • Configurable logging and debugging support

Testing Instructions

  1. Build and run the playground:
yarn install
yarn dev:core
cd packages/playground-next
yarn dev
  1. Test core functionality:
  • Connect to MetaMask extension
  • Create sessions with different chain selections
  • Invoke methods across chains
  • Monitor session changes and notifications
  • Test error handling and validation

Documentation

The playground serves as living documentation for the MultiChain API. Additional package documentation will be added in follow-up PRs.

Review Checklist

  • Review CAIP25 compliance
  • Test session management across different chains
  • Verify error handling and validation
  • Check playground functionality
  • Review TypeScript types and API surface
  • Test extension integration
  • Verify build process and dependencies

Comment on lines +213 to +216
{
type: 'caip-request',
data: request,
},

Check warning

Code scanning / CodeQL

Cross-window communication with unrestricted target origin Medium

Sensitive data
is sent to another window without origin restriction.
Sensitive data
is sent to another window without origin restriction.
Sensitive data
is sent to another window without origin restriction.
Sensitive data
is sent to another window without origin restriction.
Sensitive data
is sent to another window without origin restriction.

Copilot Autofix AI 3 days ago

To fix the problem, we need to specify a target origin for the window.postMessage calls. This ensures that the message is only sent to a trusted origin, reducing the risk of exposing sensitive data to malicious sites.

  1. Identify the trusted origin(s) that should receive the messages.
  2. Replace the wildcard target origin ('*') with the specific trusted origin(s) in the window.postMessage calls.
Suggested changeset 1
packages/multichainapi/src/providers/ExtensionProvider.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/multichainapi/src/providers/ExtensionProvider.ts b/packages/multichainapi/src/providers/ExtensionProvider.ts
--- a/packages/multichainapi/src/providers/ExtensionProvider.ts
+++ b/packages/multichainapi/src/providers/ExtensionProvider.ts
@@ -186,3 +186,3 @@
       },
-      '*',
+      'https://trusted-origin.com',
     );
@@ -216,3 +216,3 @@
         },
-        '*',
+        'https://trusted-origin.com',
       );
EOF
@@ -186,3 +186,3 @@
},
'*',
'https://trusted-origin.com',
);
@@ -216,3 +216,3 @@
},
'*',
'https://trusted-origin.com',
);
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
});
}

#handleFallbackMessage(event: MessageEvent): void {

Check warning

Code scanning / CodeQL

Missing origin verification in `postMessage` handler Medium

Postmessage handler has no origin check.

Copilot Autofix AI 3 days ago

To fix the problem, we need to add an origin check in the #handleFallbackMessage function to ensure that only messages from trusted origins are processed. This involves comparing the event.origin property to a list of allowed origins and only proceeding if the origin is trusted.

  1. Identify the trusted origins for the application.
  2. Modify the #handleFallbackMessage function to include a check for the origin of the incoming message.
  3. If the origin is not trusted, the function should return early and not process the message.
Suggested changeset 1
packages/multichainapi/src/providers/ExtensionProvider.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/multichainapi/src/providers/ExtensionProvider.ts b/packages/multichainapi/src/providers/ExtensionProvider.ts
--- a/packages/multichainapi/src/providers/ExtensionProvider.ts
+++ b/packages/multichainapi/src/providers/ExtensionProvider.ts
@@ -230,2 +230,7 @@
   #handleFallbackMessage(event: MessageEvent): void {
+    const trustedOrigins = ['https://www.example.com', 'https://another-trusted-origin.com'];
+    if (!trustedOrigins.includes(event.origin)) {
+      this.#logger?.warn('[ExtensionProvider] Untrusted origin:', event.origin);
+      return;
+    }
     if (event.data?.type !== 'caip-response') {
EOF
@@ -230,2 +230,7 @@
#handleFallbackMessage(event: MessageEvent): void {
const trustedOrigins = ['https://www.example.com', 'https://another-trusted-origin.com'];
if (!trustedOrigins.includes(event.origin)) {
this.#logger?.warn('[ExtensionProvider] Untrusted origin:', event.origin);
return;
}
if (event.data?.type !== 'caip-response') {
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@metamask/[email protected] None 0 275 kB metamaskbot
npm/@metamask/[email protected] None +4 905 kB metamaskbot
npm/@open-rpc/[email protected] None 0 38.4 kB belfordz
npm/@open-rpc/[email protected] filesystem +7 242 kB belfordz
npm/@types/[email protected] None +2 843 kB types

View full report↗︎

Copy link

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteCI
New author npm/@lavamoat/[email protected] 🚫
Install scripts npm/@lavamoat/[email protected]
  • Install script: preinstall
  • Source: echo "Don't run npm lifecycle scripts by default! Create a .yarnrc or .npmrc and set enableScripts: false. Then, whitelist them with @lavamoat/allow-scripts" && exit 1
⚠︎

View full report↗︎

Next steps

What is new author?

A new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.

Scrutinize new collaborator additions to packages because they now have the ability to publish code into your dependency tree. Packages should avoid frequent or unnecessary additions or changes to publishing rights.

What is an install script?

Install scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.

Packages should not be running non-essential scripts during install and there are often solutions to problems people solve with install scripts that can be run at publish time instead.

Take a deeper look at the dependency

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 [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/[email protected] or ignore all packages with @SocketSecurity ignore-all

@abretonc7s abretonc7s changed the title Ab/multichainapi feat: implement CAIP25 MultiChain API and NextJS playground integration Jan 17, 2025
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