Skip to content

Commit

Permalink
Fix caip25 permission spec type
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi committed Aug 26, 2024
1 parent 773e11c commit d46d3bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
6 changes: 4 additions & 2 deletions app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ export const getPermissionSpecifications = ({
return {
[caip25EndowmentBuilder.targetName]:
caip25EndowmentBuilder.specificationBuilder({
findNetworkClientIdByChainId,
getInternalAccounts,
methodHooks: {
findNetworkClientIdByChainId,
getInternalAccounts,
},
}),
[PermissionNames.eth_accounts]: {
permissionType: PermissionType.RestrictedMethod,
Expand Down
36 changes: 22 additions & 14 deletions app/scripts/lib/multichain-api/caip25permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ValidPermissionSpecification,
PermissionValidatorConstraint,
PermissionConstraint,
Caveat,
} from '@metamask/permission-controller';
import {
CaveatMutatorOperation,
Expand All @@ -13,6 +14,7 @@ import {
} from '@metamask/permission-controller';
import {
CaipAccountId,
Json,
parseCaipAccountId,
type Hex,
type NonEmptyArray,
Expand All @@ -30,7 +32,7 @@ import { assertScopesSupported } from './scope/assert';
export type Caip25CaveatValue = {
requiredScopes: ScopesObject;
optionalScopes: ScopesObject;
sessionProperties?: Record<string, unknown>;
sessionProperties?: Record<string, Json>;
isMultichainOrigin: boolean;
};

Expand All @@ -45,6 +47,10 @@ export const Caip25CaveatFactoryFn = (value: Caip25CaveatValue) => {

export const Caip25EndowmentPermissionName = 'endowment:caip25';

type Caip25EndowmentMethodHooks = {
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;
};

type Caip25EndowmentSpecification = ValidPermissionSpecification<{
permissionType: PermissionType.Endowment;
targetName: typeof Caip25EndowmentPermissionName;
Expand All @@ -53,43 +59,45 @@ type Caip25EndowmentSpecification = ValidPermissionSpecification<{
allowedCaveats: Readonly<NonEmptyArray<string>> | null;
}>;

type Caip25EndowmentSpecificationBuilderOptions = {
methodHooks: Caip25EndowmentMethodHooks;
};

/**
* `endowment:caip25` returns nothing atm;
*
* @param builderOptions - The specification builder options.
* @param builderOptions.findNetworkClientIdByChainId
* @param builderOptions.methodHooks
* @param builderOptions.methodHooks.findNetworkClientIdByChainId
* @returns The specification for the `caip25` endowment.
*/
const specificationBuilder: PermissionSpecificationBuilder<
PermissionType.Endowment,
// TODO: FIX THIS
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
Caip25EndowmentSpecificationBuilderOptions,
Caip25EndowmentSpecification
> = ({
findNetworkClientIdByChainId,
}: {
findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;
}) => {
methodHooks: { findNetworkClientIdByChainId },
}: Caip25EndowmentSpecificationBuilderOptions) => {
return {
permissionType: PermissionType.Endowment,
targetName: Caip25EndowmentPermissionName,
allowedCaveats: [Caip25CaveatType],
endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,
subjectTypes: [SubjectType.Website],
validator: (permission: PermissionConstraint) => {
const caip25Caveat = permission.caveats?.[0];
const caip25Caveat = permission.caveats?.[0] as Caveat<
typeof Caip25CaveatType,
Caip25CaveatValue
>;
if (
permission.caveats?.length !== 1 ||
caip25Caveat?.type !== Caip25CaveatType
) {
throw new Error('missing required caveat'); // TODO: throw better error here
}

// TODO: FIX THIS TYPE
const { requiredScopes, optionalScopes, isMultichainOrigin } = (
caip25Caveat as unknown as { value: Caip25CaveatValue }
).value;
const { requiredScopes, optionalScopes, isMultichainOrigin } =
caip25Caveat.value;

if (
!requiredScopes ||
Expand Down

0 comments on commit d46d3bb

Please sign in to comment.