snaps icon indicating copy to clipboard operation
snaps copied to clipboard

fix: Improve Snap Controller typing

Open owencraston opened this issue 2 years ago • 0 comments

Description

while implementing the snaps controllers on mobile we came across several instances where the type checker was failing for what seemed to be invalid reasons. Some of this work has already been done by @FrederikBolding in this PR.

Testing

  • checkout this branch on mobile (feat/snaps-controllers-integration). if this PR is merged than you can do this on main
  • run yarn setup. this is only needed once.
  • remove all of the // @ts-ignore comments in the Engine.ts file
  • run yarn lint:tsc in your terminal
  • you should see the type errors
  • once fixed the yarn lint:tsc command should pass without the ts-ignore comments in the Engine.ts

Acceptance criteria

we should be able to remove all of the // @ts-ignore inside the `Engine.ts in mobile and have the type checker pass.

Affected code

const getSnapPermissionSpecifications = () => ({
      ...buildSnapEndowmentSpecifications(Object.keys(ExcludedSnapEndowments)),
      ...buildSnapRestrictedMethodSpecifications(
        Object.keys(ExcludedSnapPermissions),
        {
          encrypt: encryptor.encrypt.bind(encryptor),
          decrypt: encryptor.decrypt.bind(encryptor),
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          clearSnapState: this.controllerMessenger.call.bind(
            this.controllerMessenger,
            'SnapController:clearSnapState',
          ),
          getMnemonic: getPrimaryKeyringMnemonic.bind(this),
          getUnlockPromise: getAppState.bind(this),
          getSnap: this.controllerMessenger.call.bind(
            this.controllerMessenger,
            'SnapController:get',
          ),
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          handleSnapRpcRequest: this.controllerMessenger.call.bind(
            this.controllerMessenger,
            'SnapController:handleRequest',
          ),
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          getSnapState: this.controllerMessenger.call.bind(
            this.controllerMessenger,
            'SnapController:getSnapState',
          ),
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          updateSnapState: this.controllerMessenger.call.bind(
            this.controllerMessenger,
            'SnapController:updateSnapState',
          ),
          maybeUpdatePhishingList: this.controllerMessenger.call.bind(
            this.controllerMessenger,
            'PhishingController:maybeUpdateState',
          ),
          isOnPhishingList: (origin: string) =>
            this.controllerMessenger.call(
              'PhishingController:testOrigin',
              origin,
            ).result,
          showDialog: (
            origin: string,
            type: EnumToUnion<DialogType>,
            content: any, // should be Component from '@metamask/snaps-ui';
            placeholder?: any,
          ) =>
            approvalController.addAndShowApprovalRequest({
              origin,
              type,
              requestData: { content, placeholder },
            }),
          showInAppNotification: (origin: string, args: NotificationArgs) => {
            Logger.log(
              'Snaps/ showInAppNotification called with args: ',
              args,
              ' and origin: ',
              origin,
            );
          },
        },
      ),
    });

...
allowedActions: [...
'ExecutionService:executeSnap',
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        'ExecutionService:getRpcRequestHandler',

...
const snapController = new SnapController({
      environmentEndowmentPermissions: Object.values(EndowmentPermissions),
      featureFlags: {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        requireAllowList,
      },

owencraston avatar Dec 11 '23 22:12 owencraston