Skip to content

Commit 4ebe1b2

Browse files
committed
Refactor to allow more thorough testing. Improve test coverage to 100%
1 parent 4bc812a commit 4ebe1b2

File tree

10 files changed

+1040
-610
lines changed

10 files changed

+1040
-610
lines changed

packages/gator-permissions-controller/src/GatorPermissionsController.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import type { HandleSnapRequest, HasSnap } from '@metamask/snaps-controllers';
1313
import type { SnapId } from '@metamask/snaps-sdk';
1414
import { hexToBigInt, numberToHex, type Hex } from '@metamask/utils';
1515

16-
import { DELEGATION_FRAMEWORK_VERSION } from './decodePermission';
1716
import type { GatorPermissionsControllerMessenger } from './GatorPermissionsController';
18-
import GatorPermissionsController from './GatorPermissionsController';
17+
import GatorPermissionsController, {
18+
DELEGATION_FRAMEWORK_VERSION,
19+
} from './GatorPermissionsController';
1920
import {
2021
mockCustomPermissionStorageEntry,
2122
mockErc20TokenPeriodicStorageEntry,
@@ -487,6 +488,22 @@ describe('GatorPermissionsController', () => {
487488
});
488489
});
489490

491+
it('throws if contracts are not found', async () => {
492+
await expect(
493+
controller.decodePermissionFromPermissionContextForOrigin({
494+
origin: controller.permissionsProviderSnapId,
495+
chainId: 999999,
496+
delegation: {
497+
caveats: [],
498+
delegator: '0x1111111111111111111111111111111111111111',
499+
delegate: '0x2222222222222222222222222222222222222222',
500+
authority: ROOT_AUTHORITY as Hex,
501+
},
502+
metadata: buildMetadata(''),
503+
}),
504+
).rejects.toThrow('Contracts not found for chainId: 999999');
505+
});
506+
490507
it('decodes a native-token-stream permission successfully', async () => {
491508
const {
492509
TimestampEnforcer,

packages/gator-permissions-controller/src/GatorPermissionsController.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
deserializeGatorPermissionsMap,
3737
serializeGatorPermissionsMap,
3838
} from './utils';
39+
import { DELEGATOR_CONTRACTS } from '@metamask/delegation-deployments';
3940

4041
// === GENERAL ===
4142

@@ -54,6 +55,14 @@ const defaultGatorPermissionsMap: GatorPermissionsMap = {
5455
other: {},
5556
};
5657

58+
/**
59+
* Delegation framework version used to select the correct deployed enforcer
60+
* contract addresses from `@metamask/delegation-deployments`.
61+
*/
62+
export const DELEGATION_FRAMEWORK_VERSION = '1.3.0';
63+
64+
const contractsByChainId = DELEGATOR_CONTRACTS[DELEGATION_FRAMEWORK_VERSION];
65+
5766
// === STATE ===
5867

5968
/**
@@ -550,16 +559,22 @@ export default class GatorPermissionsController extends BaseController<
550559
throw new OriginNotAllowedError({ origin });
551560
}
552561

562+
const contracts = contractsByChainId[chainId];
563+
564+
if (!contracts) {
565+
throw new Error(`Contracts not found for chainId: ${chainId}`);
566+
}
567+
553568
try {
554569
const enforcers = caveats.map((caveat) => caveat.enforcer);
555570

556571
const permissionType = identifyPermissionByEnforcers({
557572
enforcers,
558-
chainId,
573+
contracts,
559574
});
560575

561576
const { expiry, data } = getPermissionDataAndExpiry({
562-
chainId,
577+
contracts,
563578
caveats,
564579
permissionType,
565580
});

0 commit comments

Comments
 (0)