MetaMask / MetaMask/snaps

fix: Improve Snap Controller typing

Open
#2,029 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

team-accounts
Dominant language
TypeScript
Stars
853
Forks
662
Avg merge
2d 8h
Merged PRs (30d)
8

Description

### Description
while implementing [the snaps controllers on mobile](https://github.com/MetaMask/metamask-mobile/pull/7942) 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](https://github.com/MetaMask/snaps/pull/2023).

## Testing
- checkout [this](https://github.com/MetaMask/metamask-mobile/pull/7942) 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

```ts
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,
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,
},
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Engine.ts in the MetaMask mobile integration branch, then run yarn setup followed by yarn lint:tsc. Inspect the SnapController endowment and restricted-method callbacks, allowedActions, and featureFlags entries shown in the issue; done means lint:tsc passes with the listed @ts-ignore comments removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.