@@ -6,59 +6,14 @@ import type {
66} from '@metamask/7715-permission-types' ;
77import 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 */
3015export 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