@@ -9,9 +9,47 @@ import type {
99 PermissionTypes ,
1010 Rule ,
1111} from '@metamask/7715-permission-types' ;
12- import { isHex , toHex , type Address } from 'viem' ;
12+ import { isHex , toHex } from 'viem' ;
13+ import type {
14+ Client ,
15+ Account ,
16+ RpcSchema ,
17+ Transport ,
18+ Chain ,
19+ Address ,
20+ } from 'viem' ;
1321
14- import type { MetaMaskExtensionClient } from './snapsAuthorization.js' ;
22+ /**
23+ * RPC schema for MetaMask related methods.
24+ *
25+ * Extends the base RPC schema with methods specific to interacting with EIP-7715:
26+ * - `wallet_invokeSnap`: Invokes a method on a specific Snap.
27+ */
28+ export type MetaMaskExtensionSchema = RpcSchema &
29+ [
30+ {
31+ // eslint-disable-next-line @typescript-eslint/naming-convention
32+ Method : 'wallet_requestExecutionPermissions' ;
33+ // eslint-disable-next-line @typescript-eslint/naming-convention
34+ Params : PermissionRequest < AccountSigner , PermissionTypes > [ ] ;
35+ // eslint-disable-next-line @typescript-eslint/naming-convention
36+ ReturnType : PermissionResponse < AccountSigner , PermissionTypes > [ ] ;
37+ } ,
38+ ] ;
39+
40+ /**
41+ * A Viem client extended with MetaMask Snap-specific RPC methods.
42+ *
43+ * This client type allows for interaction with MetaMask Snaps through
44+ * the standard Viem client interface, with added type safety for
45+ * Snap-specific methods.
46+ */
47+ export type MetaMaskExtensionClient = Client <
48+ Transport ,
49+ Chain | undefined ,
50+ Account | undefined ,
51+ MetaMaskExtensionSchema
52+ > ;
1553
1654type PermissionParameter = {
1755 type : string ;
@@ -125,44 +163,24 @@ export type RequestExecutionPermissionsReturnType = PermissionResponse<
125163 * @template Signer - The type of the signer, either an Address or Account.
126164 * @param client - The client to use for the request.
127165 * @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.
129166 * @returns A promise that resolves to the permission responses.
130167 * @description
131- * This function formats the permissions requests and invokes the wallet snap to grant permissions.
168+ * This function formats the permissions requests and invokes the wallet method to grant permissions.
132169 * It will throw an error if the permissions could not be granted.
133170 */
134171export async function erc7715RequestExecutionPermissionsAction (
135172 client : MetaMaskExtensionClient ,
136173 parameters : RequestExecutionPermissionsParameters ,
137- kernelSnapId : string | undefined ,
138174) : Promise < RequestExecutionPermissionsReturnType > {
139175 const formattedPermissionRequest = parameters . map ( formatPermissionsRequest ) ;
140176
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- }
177+ const result = await client . request (
178+ {
179+ method : 'wallet_requestExecutionPermissions' ,
180+ params : formattedPermissionRequest ,
181+ } ,
182+ { retryCount : 0 } ,
183+ ) ;
166184
167185 if ( ! result ) {
168186 throw new Error ( 'Failed to grant permissions' ) ;
0 commit comments