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

DRAFT: SIP-26 Integration #29887

Draft
wants to merge 8 commits into
base: jl/caip-multichain-migrate-core
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
[Caip25CaveatType]: createCaip25Caveat,
});

/**

Check failure on line 39 in app/scripts/controllers/permissions/specifications.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Missing JSDoc @param "options.isNonEvmScopeSupported" declaration
* Gets the specifications for all caveats that will be recognized by the
* PermissionController.
*
* @param options - The options object.

Check failure on line 43 in app/scripts/controllers/permissions/specifications.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Missing @param "options.isNonEvmScopeSupported"
* @param options.listAccounts - A function that returns the
* `AccountsController` internalAccount objects for all evm accounts.
* @param options.findNetworkClientIdByChainId - A function that
Expand All @@ -50,11 +50,13 @@
export const getCaveatSpecifications = ({
listAccounts,
findNetworkClientIdByChainId,
isNonEvmScopeSupported

Check failure on line 53 in app/scripts/controllers/permissions/specifications.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Insert `,`
}) => {
return {
[Caip25CaveatType]: caip25CaveatBuilder({
listAccounts,
findNetworkClientIdByChainId,
isNonEvmScopeSupported,
}),
...snapsCaveatsSpecifications,
...snapsEndowmentCaveatSpecifications,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ValidPermission,
} from '@metamask/permission-controller';
import {
CaipChainId,
Hex,
isPlainObject,
Json,
Expand Down Expand Up @@ -51,7 +52,7 @@
CaveatSpecificationConstraint
>;

/**

Check failure on line 55 in app/scripts/lib/rpc-method-middleware/handlers/wallet-createSession/handler.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Missing JSDoc @param "hooks.getNonEvmSupportedMethods" declaration

Check failure on line 55 in app/scripts/lib/rpc-method-middleware/handlers/wallet-createSession/handler.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Missing JSDoc @param "hooks.isNonEvmScopeSupported" declaration
* Handler for the `wallet_createSession` RPC method which is responsible
* for prompting for approval and granting a CAIP-25 permission.
*
Expand All @@ -65,7 +66,7 @@
* @param req - The request object.
* @param res - The response object.
* @param _next - The next middleware function.
* @param end - The end function.

Check failure on line 69 in app/scripts/lib/rpc-method-middleware/handlers/wallet-createSession/handler.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Missing @param "hooks.getNonEvmSupportedMethods"

Check failure on line 69 in app/scripts/lib/rpc-method-middleware/handlers/wallet-createSession/handler.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Missing @param "hooks.isNonEvmScopeSupported"
* @param hooks - The hooks object.
* @param hooks.listAccounts - The hook that returns an array of the wallet's evm accounts.
* @param hooks.findNetworkClientIdByChainId - The hook that returns the networkClientId for a chainId.
Expand Down Expand Up @@ -103,6 +104,8 @@
grantPermissions: (
...args: Parameters<AbstractPermissionController['grantPermissions']>
) => Record<string, ValidPermission<string, Caveat<string, Json>>>;
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
},
) {
const { origin } = req;
Expand All @@ -121,9 +124,11 @@

const supportedRequiredScopesObjects = getSupportedScopeObjects(
normalizedRequiredScopes,
{ getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods },
);
const supportedOptionalScopesObjects = getSupportedScopeObjects(
normalizedOptionalScopes,
{ getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods },
);

const existsNetworkClientForChainId = (chainId: Hex) => {
Expand All @@ -138,16 +143,20 @@
const { supportedScopes: supportedRequiredScopes } = bucketScopes(
supportedRequiredScopesObjects,
{
isChainIdSupported: existsNetworkClientForChainId,
isChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties
isEvmChainIdSupported: existsNetworkClientForChainId,
isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties
getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,
},
);

const { supportedScopes: supportedOptionalScopes } = bucketScopes(
supportedOptionalScopesObjects,
{
isChainIdSupported: existsNetworkClientForChainId,
isChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties
isEvmChainIdSupported: existsNetworkClientForChainId,
isEvmChainIdSupportable: () => false, // intended for future usage with eip3085 scopedProperties
getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods,
isNonEvmScopeSupported: hooks.isNonEvmScopeSupported,
},
);

Expand Down Expand Up @@ -203,7 +212,7 @@
legacyApproval.approvedAccounts,
);

const sessionScopes = getSessionScopes(caip25CaveatValue);
const sessionScopes = getSessionScopes(caip25CaveatValue, { getNonEvmSupportedMethods: hooks.getNonEvmSupportedMethods});

Check failure on line 215 in app/scripts/lib/rpc-method-middleware/handlers/wallet-createSession/handler.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Replace `·getNonEvmSupportedMethods:·hooks.getNonEvmSupportedMethods` with `⏎······getNonEvmSupportedMethods:·hooks.getNonEvmSupportedMethods,⏎····`

hooks.grantPermissions({
subject: {
Expand Down Expand Up @@ -264,5 +273,7 @@
grantPermissions: true,
sendMetrics: true,
metamaskState: true,
getNonEvmSupportedMethods: true,
isNonEvmScopeSupported: true,
},
};
45 changes: 41 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
SnapInterfaceController,
SnapInsightsController,
OffscreenExecutionService,
MultichainRouter,
} from '@metamask/snaps-controllers';
import {
createSnapsMethodMiddleware,
Expand Down Expand Up @@ -1353,6 +1354,10 @@
this.networkController.findNetworkClientIdByChainId.bind(
this.networkController,
),
isNonEvmScopeSupported: this.controllerMessenger.call.bind(
this.controllerMessenger,
'MultichainRouter:isSupportedScope',
),
}),
permissionSpecifications: {
...getPermissionSpecifications(),
Expand Down Expand Up @@ -1637,6 +1642,25 @@
messenger: snapInsightsControllerMessenger,
});

const multichainRouterMessenger = this.controllerMessenger.getRestricted({
name: 'MultichainRouter',
allowedActions: [
`${this.snapController.name}:getAll`,
`${this.snapController.name}:handleRequest`,
`${this.permissionController.name}:getPermissions`,
`AccountsController:listMultichainAccounts`,
],
allowedEvents: [],
});

this.multichainRouter = new MultichainRouter({
messenger: multichainRouterMessenger,
// Binding the call to provide the selector only giving the controller the option to pass the operation
withSnapKeyring: this.keyringController.withKeyring.bind(this.keyringController, {

Check failure on line 1659 in app/scripts/metamask-controller.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Replace `this.keyringController,` with `⏎········this.keyringController,⏎·······`
type: 'snap',

Check failure on line 1660 in app/scripts/metamask-controller.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Insert `··`
}),
});

// Notification Controllers
this.authenticationController = new AuthenticationController.Controller({
state: initState.AuthenticationController,
Expand Down Expand Up @@ -3178,7 +3202,7 @@

// remove any existing notification subscriptions for removed authorizations
for (const [origin, authorization] of removedAuthorizations.entries()) {
const sessionScopes = getSessionScopes(authorization);
const sessionScopes = getSessionScopes(authorization, {getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this)});
// if the eth_subscription notification is in the scope and eth_subscribe is in the methods
// then remove middleware and unsubscribe
Object.entries(sessionScopes).forEach(([scope, scopeObject]) => {
Expand All @@ -3200,7 +3224,7 @@

// add new notification subscriptions for changed authorizations
for (const [origin, authorization] of changedAuthorizations.entries()) {
const sessionScopes = getSessionScopes(authorization);
const sessionScopes = getSessionScopes(authorization, {getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this)});

// if the eth_subscription notification is in the scope and eth_subscribe is in the methods
// then get the subscriptionManager going for that scope
Expand Down Expand Up @@ -3241,6 +3265,7 @@
if (previousAuthorization) {
const previousSessionScopes = getSessionScopes(
previousAuthorization,
{getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this)}
);

Object.entries(previousSessionScopes).forEach(
Expand Down Expand Up @@ -5773,6 +5798,13 @@
};
}

getNonEvmSupportedMethods(scope) {
return this.controllerMessenger.call(
'MultichainRouter:getSupportedMethods',
scope
)
}

// ---------------------------------------------------------------------------
// Identity Management (signature operations)

Expand Down Expand Up @@ -6900,6 +6932,11 @@
this.permissionController,
origin,
),
getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this),
isNonEvmScopeSupported: this.controllerMessenger.call.bind(
this.controllerMessenger,
'MultichainRouter:isSupportedScope',
),
}),
);

Expand Down Expand Up @@ -7038,7 +7075,7 @@
);

// add new notification subscriptions for changed authorizations
const sessionScopes = getSessionScopes(caip25Caveat.value);
const sessionScopes = getSessionScopes(caip25Caveat.value, {getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this)});

// if the eth_subscription notification is in the scope and eth_subscribe is in the methods
// then get the subscriptionManager going for that scope
Expand Down Expand Up @@ -7934,7 +7971,7 @@
{
method: NOTIFICATION_NAMES.sessionChanged,
params: {
sessionScopes: getSessionScopes(newAuthorization),
sessionScopes: getSessionScopes(newAuthorization, {getNonEvmSupportedMethods: this.getNonEvmSupportedMethods.bind(this)}),
},
},
API_TYPE.CAIP_MULTICHAIN,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
"@metamask/message-manager": "^11.0.0",
"@metamask/message-signing-snap": "^0.6.0",
"@metamask/metamask-eth-abis": "^3.1.1",
"@metamask/multichain": "^2.1.0",
"@metamask/multichain": "npm:@metamask-previews/multichain@2.1.0-preview-abb67026",
"@metamask/multichain-transactions-controller": "^0.0.1",
"@metamask/name-controller": "^8.0.0",
"@metamask/network-controller": "patch:@metamask/network-controller@npm%3A22.1.1#~/.yarn/patches/@metamask-network-controller-npm-22.1.1-09b6510f1e.patch",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5865,9 +5865,9 @@ __metadata:
languageName: node
linkType: hard

"@metamask/multichain@npm:^2.1.0":
version: 2.1.0
resolution: "@metamask/multichain@npm:2.1.0"
"@metamask/multichain@npm:@metamask-previews/multichain@2.1.0-preview-abb67026":
version: 2.1.0-preview-abb67026
resolution: "@metamask-previews/multichain@npm:2.1.0-preview-abb67026"
dependencies:
"@metamask/api-specs": "npm:^0.10.12"
"@metamask/controller-utils": "npm:^11.4.5"
Expand All @@ -5881,7 +5881,7 @@ __metadata:
peerDependencies:
"@metamask/network-controller": ^22.0.0
"@metamask/permission-controller": ^11.0.0
checksum: 10/762231b95fa89e25e8a06ff50161301cecc1eaa8095b8eef4b1d938e43307c98549767600b4a37482bb846026f46c66791cd99e385991704b5e9624aa6032332
checksum: 10/7f138dc6d564300dc1c583d9a57f5ad5aa8c43b310852ac48761a6ef80716588400dd19a5a72eb6ae7f4008d4e14118335f34bfbc9dc6d5bc63a74c6261f1d0d
languageName: node
linkType: hard

Expand Down Expand Up @@ -26916,7 +26916,7 @@ __metadata:
"@metamask/message-manager": "npm:^11.0.0"
"@metamask/message-signing-snap": "npm:^0.6.0"
"@metamask/metamask-eth-abis": "npm:^3.1.1"
"@metamask/multichain": "npm:^2.1.0"
"@metamask/multichain": "npm:@metamask-previews/multichain@2.1.0-preview-abb67026"
"@metamask/multichain-transactions-controller": "npm:^0.0.1"
"@metamask/name-controller": "npm:^8.0.0"
"@metamask/network-controller": "patch:@metamask/network-controller@npm%3A22.1.1#~/.yarn/patches/@metamask-network-controller-npm-22.1.1-09b6510f1e.patch"
Expand Down
Loading