Skip to content

Commit b567e9f

Browse files
committed
fix: get delegation by hash
1 parent ebb10f9 commit b567e9f

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

packages/gator-permissions-snap/src/profileSync/profileSync.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export type ProfileSyncManager = {
9696
getGrantedPermission: (
9797
permissionContext: Hex,
9898
) => Promise<StoredGrantedPermission | null>;
99+
getGrantedPermissionByDelegationHash: (
100+
delegationHash: Hex,
101+
) => Promise<StoredGrantedPermission | null>;
99102
storeGrantedPermission: (
100103
storedGrantedPermission: StoredGrantedPermission,
101104
) => Promise<void>;
@@ -150,6 +153,12 @@ export function createProfileSyncManager(
150153
'unConfiguredProfileSyncManager.getPermissionByHash not implemented',
151154
);
152155
},
156+
getGrantedPermissionByDelegationHash: async (_: Hex) => {
157+
logger.debug(
158+
'unConfiguredProfileSyncManager.getGrantedPermissionByDelegationHash()',
159+
);
160+
return null;
161+
},
153162
storeGrantedPermission: async (_: StoredGrantedPermission) => {
154163
logger.debug(
155164
'unConfiguredProfileSyncManager.storeGrantedPermissionBatch()',
@@ -268,6 +277,34 @@ export function createProfileSyncManager(
268277
}
269278
}
270279

280+
/**
281+
* Retrieve a granted permission by delegation hash using direct storage lookup.
282+
* Since delegation hashes are unique, we can use them directly as storage keys.
283+
* @param delegationHash - The delegation hash to search for.
284+
* @returns The granted permission or null if not found.
285+
*/
286+
async function getGrantedPermissionByDelegationHash(
287+
delegationHash: Hex,
288+
): Promise<StoredGrantedPermission | null> {
289+
try {
290+
await authenticate();
291+
292+
// Use the delegation hash directly as the storage key
293+
const path: UserStorageGenericPathWithFeatureAndKey = `${FEATURE}.${delegationHash}`;
294+
295+
const permission = await userStorage.getItem(path);
296+
297+
if (!permission) {
298+
return null;
299+
}
300+
301+
return safeDeserializeStoredGrantedPermission(permission);
302+
} catch (error) {
303+
logger.error('Error fetching permission by delegation hash');
304+
throw error;
305+
}
306+
}
307+
271308
/**
272309
* Store the granted permission in profile sync.
273310
*
@@ -466,6 +503,7 @@ export function createProfileSyncManager(
466503
? {
467504
getAllGrantedPermissions,
468505
getGrantedPermission,
506+
getGrantedPermissionByDelegationHash,
469507
storeGrantedPermission,
470508
storeGrantedPermissionBatch,
471509
updatePermissionRevocationStatus,

packages/gator-permissions-snap/src/rpc/rpcHandler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ export function createRpcHandler(config: {
145145

146146
// First, get the existing permission to validate it exists
147147
const existingPermission =
148-
await profileSyncManager.getGrantedPermission(delegationHash);
148+
await profileSyncManager.getGrantedPermissionByDelegationHash(
149+
delegationHash,
150+
);
149151

150152
if (!existingPermission) {
151153
throw new InvalidInputError(

packages/gator-permissions-snap/src/utils/validate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export const validateStartTimeZod = (value: number): boolean => {
3737
const zRevocationParams = z.object({
3838
delegationHash: z
3939
.string()
40-
.regex(/^0x[a-fA-F0-9]{64}$/u, 'Invalid delegation hash'),
40+
.regex(
41+
/^0x[a-fA-F0-9]{64}$/u,
42+
'Invalid delegation hash format - must be a 32-byte hex string',
43+
),
4144
});
4245

4346
/**

packages/gator-permissions-snap/test/rpc/rpcHandler.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe('RpcHandler', () => {
9696
storeGrantedPermission: jest.fn(),
9797
storeGrantedPermissionBatch: jest.fn(),
9898
getGrantedPermission: jest.fn(),
99+
getGrantedPermissionByDelegationHash: jest.fn(),
99100
getAllGrantedPermissions: jest.fn(),
100101
getUserProfile: jest.fn(),
101102
updatePermissionRevocationStatus: jest.fn(),
@@ -629,7 +630,7 @@ describe('RpcHandler', () => {
629630
isRevoked: false,
630631
};
631632

632-
mockProfileSyncManager.getGrantedPermission.mockResolvedValueOnce(
633+
mockProfileSyncManager.getGrantedPermissionByDelegationHash.mockResolvedValueOnce(
633634
mockPermission,
634635
);
635636
mockProfileSyncManager.checkDelegationDisabledOnChain.mockResolvedValueOnce(
@@ -642,9 +643,9 @@ describe('RpcHandler', () => {
642643
const result = await handler.submitRevocation(validRevocationParams);
643644

644645
expect(result).toStrictEqual({ success: true });
645-
expect(mockProfileSyncManager.getGrantedPermission).toHaveBeenCalledWith(
646-
validRevocationParams.delegationHash,
647-
);
646+
expect(
647+
mockProfileSyncManager.getGrantedPermissionByDelegationHash,
648+
).toHaveBeenCalledWith(validRevocationParams.delegationHash);
648649
expect(
649650
mockProfileSyncManager.checkDelegationDisabledOnChain,
650651
).toHaveBeenCalledWith(
@@ -730,7 +731,7 @@ describe('RpcHandler', () => {
730731
};
731732

732733
const profileSyncError = new Error('Update failed');
733-
mockProfileSyncManager.getGrantedPermission.mockResolvedValueOnce(
734+
mockProfileSyncManager.getGrantedPermissionByDelegationHash.mockResolvedValueOnce(
734735
mockPermission,
735736
);
736737
mockProfileSyncManager.checkDelegationDisabledOnChain.mockResolvedValueOnce(
@@ -778,7 +779,7 @@ describe('RpcHandler', () => {
778779
isRevoked: false,
779780
};
780781

781-
mockProfileSyncManager.getGrantedPermission.mockResolvedValueOnce(
782+
mockProfileSyncManager.getGrantedPermissionByDelegationHash.mockResolvedValueOnce(
782783
mockPermission,
783784
);
784785
mockProfileSyncManager.checkDelegationDisabledOnChain.mockResolvedValueOnce(
@@ -791,9 +792,9 @@ describe('RpcHandler', () => {
791792
const result = await handler.submitRevocation(upperCaseParams);
792793

793794
expect(result).toStrictEqual({ success: true });
794-
expect(mockProfileSyncManager.getGrantedPermission).toHaveBeenCalledWith(
795-
upperCaseParams.delegationHash,
796-
);
795+
expect(
796+
mockProfileSyncManager.getGrantedPermissionByDelegationHash,
797+
).toHaveBeenCalledWith(upperCaseParams.delegationHash);
797798
});
798799

799800
it('should handle different chain configurations', async () => {
@@ -824,7 +825,7 @@ describe('RpcHandler', () => {
824825
isRevoked: false,
825826
};
826827

827-
mockProfileSyncManager.getGrantedPermission.mockResolvedValueOnce(
828+
mockProfileSyncManager.getGrantedPermissionByDelegationHash.mockResolvedValueOnce(
828829
mockPermission,
829830
);
830831
mockProfileSyncManager.checkDelegationDisabledOnChain.mockResolvedValueOnce(
@@ -837,9 +838,9 @@ describe('RpcHandler', () => {
837838
const result = await handler.submitRevocation(testParams);
838839

839840
expect(result).toStrictEqual({ success: true });
840-
expect(mockProfileSyncManager.getGrantedPermission).toHaveBeenCalledWith(
841-
testParams.delegationHash,
842-
);
841+
expect(
842+
mockProfileSyncManager.getGrantedPermissionByDelegationHash,
843+
).toHaveBeenCalledWith(testParams.delegationHash);
843844
});
844845
});
845846
});

0 commit comments

Comments
 (0)