11import type { PermissionResponse } from '@metamask/7715-permissions-shared/types' ;
2- import { logger , logToFile } from '@metamask/7715-permissions-shared/utils' ;
2+ import { logger } from '@metamask/7715-permissions-shared/utils' ;
33import { decodeDelegations , hashDelegation } from '@metamask/delegation-core' ;
44import { InvalidInputError , type Json } from '@metamask/snaps-sdk' ;
55
@@ -36,9 +36,10 @@ export type RpcHandler = {
3636 /**
3737 * Handles get granted permissions requests.
3838 *
39- * @returns The granted permissions.
39+ * @param params - Optional parameters for filtering permissions.
40+ * @returns The granted permissions, optionally filtered.
4041 */
41- getGrantedPermissions ( ) : Promise < Json > ;
42+ getGrantedPermissions ( params ?: Json ) : Promise < Json > ;
4243
4344 /**
4445 * Handles submit revocation requests.
@@ -124,13 +125,58 @@ export function createRpcHandler(config: {
124125 /**
125126 * Handles get granted permissions requests.
126127 *
127- * @returns The granted permissions.
128+ * @param params - Optional parameters for filtering permissions.
129+ * @returns The granted permissions, optionally filtered.
128130 */
129- const getGrantedPermissions = async ( ) : Promise < Json > => {
130- logger . debug ( 'getGrantedPermissions()' ) ;
131- const grantedPermission =
132- await profileSyncManager . getAllGrantedPermissions ( ) ;
133- return grantedPermission as Json [ ] ;
131+ const getGrantedPermissions = async ( params ?: Json ) : Promise < Json > => {
132+ logger . debug ( 'getGrantedPermissions()' , params ) ;
133+
134+ // Get all permissions
135+ const allPermissions = await profileSyncManager . getAllGrantedPermissions ( ) ;
136+
137+ // If no params provided, return all permissions (backward compatibility)
138+ if ( ! params || typeof params !== 'object' ) {
139+ return allPermissions as Json [ ] ;
140+ }
141+
142+ // Parse filtering options
143+ const { isRevoked, siteOrigin, chainId, delegationManager } = params as {
144+ isRevoked ?: boolean ;
145+ siteOrigin ?: string ;
146+ chainId ?: string ;
147+ delegationManager ?: string ;
148+ } ;
149+
150+ // Apply filters
151+ let filteredPermissions = allPermissions ;
152+
153+ if ( typeof isRevoked === 'boolean' ) {
154+ filteredPermissions = filteredPermissions . filter (
155+ ( permission ) => permission . isRevoked === isRevoked ,
156+ ) ;
157+ }
158+
159+ if ( typeof siteOrigin === 'string' ) {
160+ filteredPermissions = filteredPermissions . filter (
161+ ( permission ) => permission . siteOrigin === siteOrigin ,
162+ ) ;
163+ }
164+
165+ if ( typeof chainId === 'string' ) {
166+ filteredPermissions = filteredPermissions . filter (
167+ ( permission ) => permission . permissionResponse . chainId === chainId ,
168+ ) ;
169+ }
170+
171+ if ( typeof delegationManager === 'string' ) {
172+ filteredPermissions = filteredPermissions . filter (
173+ ( permission ) =>
174+ permission . permissionResponse . signerMeta . delegationManager ===
175+ delegationManager ,
176+ ) ;
177+ }
178+
179+ return filteredPermissions as Json [ ] ;
134180 } ;
135181
136182 /**
@@ -140,41 +186,24 @@ export function createRpcHandler(config: {
140186 * @returns Success confirmation.
141187 */
142188 const submitRevocation = async ( params : Json ) : Promise < Json > => {
143- logToFile ( '================================================2' ) ;
144- logger . debug ( '=== SUBMIT REVOCATION RPC CALLED ===' ) ;
145189 logger . debug ( 'submitRevocation() called with params:' , params ) ;
146- logger . debug ( 'Params type:' , typeof params ) ;
147- logger . debug ( 'Params stringified:' , JSON . stringify ( params , null , 2 ) ) ;
148190
149191 const { permissionContext } = validateRevocationParams ( params ) ;
150192
151- logger . debug ( 'Validated permissionContext:' , permissionContext ) ;
152-
153193 // First, get the existing permission to validate it exists
154194 logger . debug (
155195 'Looking up existing permission for permissionContext:' ,
156196 permissionContext ,
157197 ) ;
158198 const existingPermission =
159199 await profileSyncManager . getGrantedPermission ( permissionContext ) ;
160- console . log ( 'existingPermissionBefore:' , existingPermission ) ;
161200
162201 if ( ! existingPermission ) {
163- logger . debug (
164- '❌ Permission not found for permissionContext:' ,
165- permissionContext ,
166- ) ;
167202 throw new InvalidInputError (
168203 `Permission not found for permission context: ${ permissionContext } ` ,
169204 ) ;
170205 }
171206
172- logger . debug ( '✅ Found existing permission:' , {
173- permissionContext,
174- isRevoked : existingPermission . isRevoked ,
175- siteOrigin : existingPermission . siteOrigin ,
176- } ) ;
177-
178207 // Extract delegationManager and chainId from the permission response for logging
179208 const { chainId : permissionChainId , signerMeta } =
180209 existingPermission . permissionResponse ;
@@ -188,7 +217,6 @@ export function createRpcHandler(config: {
188217
189218 // Check if the delegation is actually disabled on-chain
190219 if ( ! delegationManager ) {
191- logger . debug ( '❌ No delegation manager found' ) ;
192220 throw new InvalidInputError (
193221 `No delegation manager found for permission context: ${ permissionContext } ` ,
194222 ) ;
@@ -197,7 +225,6 @@ export function createRpcHandler(config: {
197225 // For on-chain validation, we need to check each delegation in the context
198226 try {
199227 const delegations = decodeDelegations ( permissionContext ) ;
200- logger . debug ( 'Decoded delegations from context:' , delegations . length ) ;
201228
202229 // Check if any delegation is disabled on-chain
203230 // For now, we'll check the first delegation. This might need adjustment based on business logic
@@ -209,27 +236,16 @@ export function createRpcHandler(config: {
209236 }
210237
211238 const delegationHash = hashDelegation ( firstDelegation ) ;
212- logger . debug ( 'Checking if delegation is disabled on-chain...' , {
213- delegationHash,
214- chainId : permissionChainId ,
215- delegationManager,
216- } ) ;
217-
218239 const isDelegationDisabled =
219240 await profileSyncManager . checkDelegationDisabledOnChain (
220241 delegationHash ,
221242 permissionChainId ,
222243 delegationManager ,
223244 ) ;
224245
225- console . log (
226- '++++++++++++++++++++isDelegationDisabled:' ,
227- isDelegationDisabled ,
228- ) ;
229246 logger . debug ( 'On-chain check result:' , { isDelegationDisabled } ) ;
230247
231248 if ( ! isDelegationDisabled ) {
232- logger . debug ( '❌ Delegation is not disabled on-chain' ) ;
233249 throw new InvalidInputError (
234250 `Delegation ${ delegationHash } is not disabled on-chain. Cannot process revocation.` ,
235251 ) ;
@@ -245,22 +261,12 @@ export function createRpcHandler(config: {
245261 ) ;
246262 }
247263
248- // Update the permission's revocation status using the optimized method
249- // This avoids re-fetching the permission we already have
250264 logger . debug ( 'Updating permission revocation status to true...' ) ;
251265 await profileSyncManager . updatePermissionRevocationStatusWithPermission (
252266 existingPermission ,
253267 true ,
254268 ) ;
255269
256- const existingPermissionAfter =
257- await profileSyncManager . getGrantedPermission ( permissionContext ) ;
258-
259- console . log ( 'existingPermissionAfter:' , existingPermissionAfter ) ;
260- logToFile ( 'existingPermissionAfter:' , existingPermissionAfter ) ;
261-
262- logger . debug ( '✅ Revocation completed successfully' ) ;
263- logger . debug ( '=== SUBMIT REVOCATION RPC COMPLETED ===' ) ;
264270 return { success : true } ;
265271 } ;
266272
0 commit comments