-
Notifications
You must be signed in to change notification settings - Fork 639
wip: Add Multichain API support #3759
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
base: main
Are you sure you want to change the base?
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
6945f3c to
78ff428
Compare
917fc58 to
491f06d
Compare
| import { getMockOptions } from '../test-utils/options'; | ||
|
|
||
| const MOCK_HOOKS: RestrictedMiddlewareHooks = { | ||
| const MOCK_HOOKS = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why'd you remove the type? Can it be replaces by satisfies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make it easier to modify the mocks in each test without casting. They seem to be inferred correctly when passed in
| options, | ||
| }: GetPermissionSpecificationsOptions) { | ||
| return { | ||
| [caip25EndowmentBuilder.targetName]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be in buildSnapEndowmentSpecifications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is the multichain endowment that is specified in the clients, which is granted when using wallet_createSession. We don't want that to be part of the Snap endowments.
packages/snaps-simulation/src/middleware/internal-methods/chain-id.ts
Outdated
Show resolved
Hide resolved
| optionalScopes: request.params.optionalScopes ?? {}, | ||
| sessionProperties: {}, | ||
| isMultichainOrigin: true, | ||
| } as Caip25CaveatValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this assertion needed?
| } | ||
|
|
||
| // TODO: Struct? | ||
| const { request: wrappedRequest, scope } = request.params as any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leaving a comment so we don't forget about replacing any here.
| const mergedScopes = { | ||
| ...value.requiredScopes, | ||
| ...value.optionalScopes, | ||
| } as Record<CaipChainId, InternalScopeObject & { methods: string[] }>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this type assertion needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is slightly different from the proper types because we store methods so that we can recreate the session scopes. In the clients these values are not stored in the permission, but instead recreated whenever requested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose this way of doing it to let the Snap register any non-EVM action as long as they mock it
| const nonEvmMethods = Object.keys(mergedScopes).reduce< | ||
| Record<CaipChainId, string[]> | ||
| >((accumulator, scope) => { | ||
| const castScope = scope as CaipChainId | 'wallet'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this type assertion needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, because Object.keys just returns string[]
| return new PermissionController({ | ||
| messenger, | ||
| caveatSpecifications: { | ||
| [Caip25CaveatType]: caip25CaveatBuilder({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this isn't part of snapsEndowmentCaveatSpecifications?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * @param request - The JSON-RPC request. | ||
| * @returns The JSON-RPC response. | ||
| */ | ||
| export async function invokeMethod<ReturnType>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved to the Module class, so it can be used like this.invokeMethod, removing the need to provide the scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I feel like I had a justification for this at some point, but I can't think of one now.
Adds support for using the multichain API in Snaps. This is accomplished by setting up a separate provider and substream called
metamask-multichain-provider. Whensnap.requestis called with a multichain request, this provider and substream is used. The clients will need to route to the proper JSON-RPC pipeline based on the substream and verify that the Snap has the proper permission (TBD).Additionally this PR adds a example Snap for usage of this API, that can leverage Ethereum and Solana APIs at once. It also adds limited simulation support for the multichain API. The simulation framework implements a basic version of the multichain API where sessions are tracked and requests are routed to supported EVM providers. There are no underlying providers for non-EVM request, but they can be mocked.
TODO:
wallet_revokeSessionsupport