Skip to content

Commit 66e2a31

Browse files
committed
Remove option to invoke snaps, and only support invoking wallet_requestExecutionPermissions on the walletApi
1 parent a197a06 commit 66e2a31

4 files changed

Lines changed: 193 additions & 829 deletions

File tree

packages/delegation-toolkit/src/experimental/erc7715RequestExecutionPermissionsAction.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -125,44 +125,24 @@ export type RequestExecutionPermissionsReturnType = PermissionResponse<
125125
* @template Signer - The type of the signer, either an Address or Account.
126126
* @param client - The client to use for the request.
127127
* @param parameters - The permissions requests to grant.
128-
* @param kernelSnapId - The ID of the kernel snap to invoke, undefined to request via the wallet RPC method.
129128
* @returns A promise that resolves to the permission responses.
130129
* @description
131-
* This function formats the permissions requests and invokes the wallet snap to grant permissions.
130+
* This function formats the permissions requests and invokes the wallet method to grant permissions.
132131
* It will throw an error if the permissions could not be granted.
133132
*/
134133
export async function erc7715RequestExecutionPermissionsAction(
135134
client: MetaMaskExtensionClient,
136135
parameters: RequestExecutionPermissionsParameters,
137-
kernelSnapId: string | undefined,
138136
): Promise<RequestExecutionPermissionsReturnType> {
139137
const formattedPermissionRequest = parameters.map(formatPermissionsRequest);
140138

141-
let result: PermissionResponse<AccountSigner, PermissionTypes>[] | null;
142-
143-
if (kernelSnapId) {
144-
result = await client.request(
145-
{
146-
method: 'wallet_invokeSnap',
147-
params: {
148-
snapId: kernelSnapId,
149-
request: {
150-
method: 'wallet_requestExecutionPermissions',
151-
params: formattedPermissionRequest,
152-
},
153-
},
154-
},
155-
{ retryCount: 0 },
156-
);
157-
} else {
158-
result = await client.request(
159-
{
160-
method: 'wallet_requestExecutionPermissions',
161-
params: formattedPermissionRequest,
162-
},
163-
{ retryCount: 0 },
164-
);
165-
}
139+
const result = await client.request(
140+
{
141+
method: 'wallet_requestExecutionPermissions',
142+
params: formattedPermissionRequest,
143+
},
144+
{ retryCount: 0 },
145+
);
166146

167147
if (!result) {
168148
throw new Error('Failed to grant permissions');

packages/delegation-toolkit/src/experimental/index.ts

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from './erc7710RedeemDelegationAction';
1212
import { erc7715RequestExecutionPermissionsAction } from './erc7715RequestExecutionPermissionsAction';
1313
import type { RequestExecutionPermissionsParameters } from './erc7715RequestExecutionPermissionsAction';
14-
import { ensureSnapsAuthorized } from './snapsAuthorization';
1514
import type { MetaMaskExtensionClient } from './snapsAuthorization';
1615

1716
export {
@@ -27,48 +26,17 @@ export {
2726
type DelegationStorageConfig,
2827
} from './delegationStorage';
2928

30-
type Erc7715ProviderConfig = {
31-
useWalletMethod: boolean;
32-
snapIds?: {
33-
kernelSnapId?: string;
34-
providerSnapId?: string;
35-
};
36-
};
29+
type Erc7715ProviderConfig = Record<string, never>;
3730

3831
export const erc7715ProviderActions =
39-
(erc7715ProviderConfig: Erc7715ProviderConfig = { useWalletMethod: false }) =>
32+
(_erc7715ProviderConfig: Erc7715ProviderConfig = {}) =>
4033
(client: Client) => ({
4134
requestExecutionPermissions: async (
4235
parameters: RequestExecutionPermissionsParameters,
4336
) => {
44-
const { useWalletMethod, snapIds } = erc7715ProviderConfig;
45-
46-
let kernelSnapId: string | undefined;
47-
48-
if (!useWalletMethod) {
49-
kernelSnapId =
50-
snapIds?.kernelSnapId ?? 'npm:@metamask/permissions-kernel-snap';
51-
52-
const snapIdsToAuthorize = {
53-
kernelSnapId,
54-
providerSnapId:
55-
snapIds?.providerSnapId ?? 'npm:@metamask/gator-permissions-snap',
56-
};
57-
58-
if (
59-
!(await ensureSnapsAuthorized(
60-
client as MetaMaskExtensionClient,
61-
snapIdsToAuthorize,
62-
))
63-
) {
64-
throw new Error('Snaps not authorized');
65-
}
66-
}
67-
6837
return erc7715RequestExecutionPermissionsAction(
6938
client as MetaMaskExtensionClient,
7039
parameters,
71-
kernelSnapId,
7240
);
7341
},
7442
});

packages/delegation-toolkit/src/experimental/snapsAuthorization.ts

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,14 @@ import type {
66
} from '@metamask/7715-permission-types';
77
import type { Account, Chain, Client, RpcSchema, Transport } from 'viem';
88

9-
/**
10-
* Represents the authorization status of installed MetaMask Snaps.
11-
*
12-
* @property {string} version - The version of the installed Snap.
13-
* @property {string} id - The unique identifier of the Snap.
14-
* @property {boolean} enabled - Whether the Snap is currently enabled.
15-
* @property {boolean} blocked - Whether the Snap is currently blocked.
16-
*/
17-
export type SnapAuthorizations = Record<
18-
string,
19-
{ version: string; id: string; enabled: boolean; blocked: boolean }
20-
>;
21-
229
/**
2310
* RPC schema for MetaMask related methods.
2411
*
25-
* Extends the base RPC schema with methods specific to interacting with Snaps:
12+
* Extends the base RPC schema with methods specific to interacting with EIP-7715:
2613
* - `wallet_invokeSnap`: Invokes a method on a specific Snap.
27-
* - `wallet_getSnaps`: Retrieves all installed Snaps and their authorization status.
28-
* - `wallet_requestSnaps`: Requests permission to use specific Snaps.
2914
*/
3015
export type MetaMaskExtensionSchema = RpcSchema &
3116
[
32-
{
33-
// eslint-disable-next-line @typescript-eslint/naming-convention
34-
Method: 'wallet_invokeSnap';
35-
// eslint-disable-next-line @typescript-eslint/naming-convention
36-
Params: {
37-
snapId: string;
38-
request: {
39-
method: string;
40-
params: unknown[];
41-
};
42-
};
43-
// eslint-disable-next-line @typescript-eslint/naming-convention
44-
ReturnType: unknown;
45-
},
46-
{
47-
// eslint-disable-next-line @typescript-eslint/naming-convention
48-
Method: 'wallet_getSnaps';
49-
// eslint-disable-next-line @typescript-eslint/naming-convention
50-
Params: Record<string, unknown>;
51-
// eslint-disable-next-line @typescript-eslint/naming-convention
52-
ReturnType: SnapAuthorizations;
53-
},
54-
{
55-
// eslint-disable-next-line @typescript-eslint/naming-convention
56-
Method: 'wallet_requestSnaps';
57-
// eslint-disable-next-line @typescript-eslint/naming-convention
58-
Params: Record<string, unknown>;
59-
// eslint-disable-next-line @typescript-eslint/naming-convention
60-
ReturnType: SnapAuthorizations;
61-
},
6217
{
6318
// eslint-disable-next-line @typescript-eslint/naming-convention
6419
Method: 'wallet_requestExecutionPermissions';
@@ -82,80 +37,3 @@ export type MetaMaskExtensionClient = Client<
8237
Account | undefined,
8338
MetaMaskExtensionSchema
8439
>;
85-
86-
/**
87-
* Checks if a specific snap is authorized, within the specified authorizations object..
88-
*
89-
* @param authorizations - The SnapAuthorizations object containing installed Snaps and their authorization status.
90-
* @param snapId - The ID of the snap to check.
91-
* @returns A boolean indicating whether the snap is authorized.
92-
*/
93-
const isSnapAuthorized = (
94-
authorizations: SnapAuthorizations,
95-
snapId: string,
96-
) => {
97-
const authorization = authorizations[snapId];
98-
const isAuthorized =
99-
(authorization?.enabled && !authorization?.blocked) || false;
100-
101-
return isAuthorized;
102-
};
103-
104-
/**
105-
* Requests re-authorization of a specific snap.
106-
*
107-
* @param client - The SnapClient instance used to interact with MetaMask Snaps.
108-
* @param snapId - The ID of the snap to re-authorize.
109-
* @returns A promise that resolves to a boolean indicating whether the snap was re-authorized.
110-
*/
111-
const reAuthorize = async (client: MetaMaskExtensionClient, snapId: string) => {
112-
const newAuthorizations = await client.request({
113-
method: 'wallet_requestSnaps',
114-
params: {
115-
[snapId]: {} as Record<string, unknown>,
116-
},
117-
});
118-
119-
return isSnapAuthorized(newAuthorizations, snapId);
120-
};
121-
122-
/**
123-
* Ensures that the required MetaMask Snaps for ERC-7715 permissions are authorized.
124-
*
125-
* @param client - The SnapClient instance used to interact with MetaMask Snaps.
126-
* @param snapIds - Optional object containing custom snap IDs to use.
127-
* @param snapIds.kernelSnapId - Custom ID for the permissions kernel snap (defaults to 'npm:@metamask/permissions-kernel-snap').
128-
* @param snapIds.providerSnapId - Custom ID for the permissions provider snap (defaults to 'npm:@metamask/gator-permissions-snap').
129-
* @returns A promise that resolves to a boolean indicating whether both snaps are authorized.
130-
* @description
131-
* This function attempts to authorize both the kernel and provider snaps required for ERC-7715 permissions.
132-
* It returns true only if both snaps are successfully authorized.
133-
*/
134-
export async function ensureSnapsAuthorized(
135-
client: MetaMaskExtensionClient,
136-
{
137-
kernelSnapId,
138-
providerSnapId,
139-
}: { kernelSnapId: string; providerSnapId: string },
140-
) {
141-
const existingAuthorizations = await client.request({
142-
method: 'wallet_getSnaps',
143-
params: {} as Record<string, never>,
144-
});
145-
146-
if (
147-
!isSnapAuthorized(existingAuthorizations, kernelSnapId) &&
148-
!(await reAuthorize(client, kernelSnapId))
149-
) {
150-
return false;
151-
}
152-
153-
if (
154-
!isSnapAuthorized(existingAuthorizations, providerSnapId) &&
155-
!(await reAuthorize(client, providerSnapId))
156-
) {
157-
return false;
158-
}
159-
160-
return true;
161-
}

0 commit comments

Comments
 (0)