11import type { PermissionResponse } from '@metamask/7715-permissions-shared/types' ;
2- import { logger } from '@metamask/7715-permissions-shared/utils' ;
2+ import { logger , logToFile } from '@metamask/7715-permissions-shared/utils' ;
3+ import { decodeDelegations , hashDelegation } from '@metamask/delegation-core' ;
34import { InvalidInputError , type Json } from '@metamask/snaps-sdk' ;
45
56import type { PermissionHandlerFactory } from '../core/permissionHandlerFactory' ;
@@ -139,37 +140,37 @@ export function createRpcHandler(config: {
139140 * @returns Success confirmation.
140141 */
141142 const submitRevocation = async ( params : Json ) : Promise < Json > => {
142- console . log ( '================================================2' ) ;
143+ logToFile ( '================================================2' ) ;
143144 logger . debug ( '=== SUBMIT REVOCATION RPC CALLED ===' ) ;
144145 logger . debug ( 'submitRevocation() called with params:' , params ) ;
145146 logger . debug ( 'Params type:' , typeof params ) ;
146147 logger . debug ( 'Params stringified:' , JSON . stringify ( params , null , 2 ) ) ;
147148
148- const { delegationHash } = validateRevocationParams ( params ) ;
149- logger . debug ( 'Validated delegationHash:' , delegationHash ) ;
149+ const { permissionContext } = validateRevocationParams ( params ) ;
150+
151+ logger . debug ( 'Validated permissionContext:' , permissionContext ) ;
150152
151153 // First, get the existing permission to validate it exists
152154 logger . debug (
153- 'Looking up existing permission for delegationHash :' ,
154- delegationHash ,
155+ 'Looking up existing permission for permissionContext :' ,
156+ permissionContext ,
155157 ) ;
156158 const existingPermission =
157- await profileSyncManager . getGrantedPermissionByDelegationHash (
158- delegationHash ,
159- ) ;
159+ await profileSyncManager . getGrantedPermission ( permissionContext ) ;
160+ console . log ( 'existingPermissionBefore:' , existingPermission ) ;
160161
161162 if ( ! existingPermission ) {
162163 logger . debug (
163- '❌ Permission not found for delegationHash :' ,
164- delegationHash ,
164+ '❌ Permission not found for permissionContext :' ,
165+ permissionContext ,
165166 ) ;
166167 throw new InvalidInputError (
167- `Permission not found for delegation hash : ${ delegationHash } ` ,
168+ `Permission not found for permission context : ${ permissionContext } ` ,
168169 ) ;
169170 }
170171
171172 logger . debug ( '✅ Found existing permission:' , {
172- delegationHash ,
173+ permissionContext ,
173174 isRevoked : existingPermission . isRevoked ,
174175 siteOrigin : existingPermission . siteOrigin ,
175176 } ) ;
@@ -182,43 +183,68 @@ export function createRpcHandler(config: {
182183 logger . debug ( 'Permission details extracted:' , {
183184 chainId : permissionChainId ,
184185 delegationManager : delegationManager ?? 'undefined' ,
185- signerMeta : signerMeta ,
186+ signerMeta,
186187 } ) ;
187188
188189 // Check if the delegation is actually disabled on-chain
189190 if ( ! delegationManager ) {
190191 logger . debug ( '❌ No delegation manager found' ) ;
191192 throw new InvalidInputError (
192- `No delegation manager found for delegation hash : ${ delegationHash } ` ,
193+ `No delegation manager found for permission context : ${ permissionContext } ` ,
193194 ) ;
194195 }
195196
196- logger . debug ( 'Checking if delegation is disabled on-chain...' , {
197- delegationHash,
198- chainId : permissionChainId ,
199- delegationManager,
200- } ) ;
197+ // For on-chain validation, we need to check each delegation in the context
198+ try {
199+ const delegations = decodeDelegations ( permissionContext ) ;
200+ logger . debug ( 'Decoded delegations from context:' , delegations . length ) ;
201+
202+ // Check if any delegation is disabled on-chain
203+ // For now, we'll check the first delegation. This might need adjustment based on business logic
204+ const firstDelegation = delegations [ 0 ] ;
205+ if ( ! firstDelegation ) {
206+ throw new InvalidInputError (
207+ `No delegations found in permission context: ${ permissionContext } ` ,
208+ ) ;
209+ }
201210
202- const isDelegationDisabled =
203- await profileSyncManager . checkDelegationDisabledOnChain (
211+ const delegationHash = hashDelegation ( firstDelegation ) ;
212+ logger . debug ( 'Checking if delegation is disabled on-chain...' , {
204213 delegationHash,
205- permissionChainId ,
214+ chainId : permissionChainId ,
206215 delegationManager,
216+ } ) ;
217+
218+ const isDelegationDisabled =
219+ await profileSyncManager . checkDelegationDisabledOnChain (
220+ delegationHash ,
221+ permissionChainId ,
222+ delegationManager ,
223+ ) ;
224+
225+ console . log (
226+ '++++++++++++++++++++isDelegationDisabled:' ,
227+ isDelegationDisabled ,
207228 ) ;
229+ logger . debug ( 'On-chain check result:' , { isDelegationDisabled } ) ;
208230
209- logger . debug ( 'On-chain check result:' , { isDelegationDisabled } ) ;
231+ if ( ! isDelegationDisabled ) {
232+ logger . debug ( '❌ Delegation is not disabled on-chain' ) ;
233+ throw new InvalidInputError (
234+ `Delegation ${ delegationHash } is not disabled on-chain. Cannot process revocation.` ,
235+ ) ;
236+ }
210237
211- if ( ! isDelegationDisabled ) {
212- logger . debug ( '❌ Delegation is not disabled on-chain' ) ;
238+ logger . debug (
239+ '✅ Delegation is disabled on-chain, proceeding with revocation' ,
240+ ) ;
241+ } catch ( error ) {
242+ logger . error ( 'Error processing delegation context:' , error ) ;
213243 throw new InvalidInputError (
214- `Delegation ${ delegationHash } is not disabled on-chain. Cannot process revocation. ` ,
244+ `Invalid permission context format: ${ permissionContext } ` ,
215245 ) ;
216246 }
217247
218- logger . debug (
219- '✅ Delegation is disabled on-chain, proceeding with revocation' ,
220- ) ;
221-
222248 // Update the permission's revocation status using the optimized method
223249 // This avoids re-fetching the permission we already have
224250 logger . debug ( 'Updating permission revocation status to true...' ) ;
@@ -227,6 +253,12 @@ export function createRpcHandler(config: {
227253 true ,
228254 ) ;
229255
256+ const existingPermissionAfter =
257+ await profileSyncManager . getGrantedPermission ( permissionContext ) ;
258+
259+ console . log ( 'existingPermissionAfter:' , existingPermissionAfter ) ;
260+ logToFile ( 'existingPermissionAfter:' , existingPermissionAfter ) ;
261+
230262 logger . debug ( '✅ Revocation completed successfully' ) ;
231263 logger . debug ( '=== SUBMIT REVOCATION RPC COMPLETED ===' ) ;
232264 return { success : true } ;
0 commit comments