Skip to content

Commit

Permalink
feat!: use onKeyringRequest to handle keyring methods (#97)
Browse files Browse the repository at this point in the history
* feat!: use `onKeyringRequest` to handle keyring methods

* chore: update dependencies

* chore: update dependencies

* chore: remove unnecessary line

* chore: update dependencies

* chore: update snap hash

* fix: update allowed methods

* chore: update snap hash

* chore: remove resolution

* chore: update `yarn.lock`

* fix: update manifest

* chore: bump `keyring-api` to `1.0.0-rc.1`

* chore: update snap hash

---------

Co-authored-by: Monte Lai <[email protected]>
  • Loading branch information
danroc and montelaidev authored Oct 10, 2023
1 parent 33a4bb1 commit e683a07
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 209 deletions.
4 changes: 2 additions & 2 deletions packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@metamask/keyring-api": "^0.2.5",
"@metamask/keyring-api": "^1.0.0-rc.1",
"@metamask/providers": "^13.0.0",
"@mui/icons-material": "^5.14.0",
"@mui/material": "^5.14.0",
Expand All @@ -45,7 +45,7 @@
"webpack": "^5.88.2"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.1.0",
"@metamask/auto-changelog": "^3.3.0",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
Expand Down
14 changes: 7 additions & 7 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:deps": "depcheck",
"lint:eslint": "eslint . --cache --ext js,jsx,ts,tsx",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' --check",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '!snap.manifest.json' --check",
"lint:types": "tsc --noEmit",
"serve": "mm-snap serve",
"start": "mm-snap watch"
Expand All @@ -40,20 +40,20 @@
"@ethereumjs/common": "^3.1.2",
"@ethereumjs/tx": "^4.1.2",
"@ethereumjs/util": "^8.0.5",
"@metamask/eth-sig-util": "^5.0.2",
"@metamask/keyring-api": "^0.2.5",
"@metamask/snaps-types": "^0.38.2-flask.1",
"@metamask/utils": "^5.0.2",
"@metamask/eth-sig-util": "^7.0.0",
"@metamask/keyring-api": "^1.0.0-rc.1",
"@metamask/snaps-types": "^3.0.0",
"@metamask/utils": "^8.1.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^2.0.3",
"@metamask/auto-changelog": "^3.1.0",
"@metamask/auto-changelog": "^3.3.0",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@metamask/snaps-cli": "^0.38.3-flask.1",
"@metamask/snaps-cli": "^3.0.0",
"@types/node": "^20.6.2",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
Expand Down
11 changes: 8 additions & 3 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git+https://github.com/MetaMask/snap-simple-keyring.git"
},
"source": {
"shasum": "fhKGcx3qfv/93ZWnwW2Q7L0W1STExHOGHDp9VMVZk3Q=",
"shasum": "uVAbDvxL04+yb5nF3mSpyWtY3IX/pEzKplTOyf6yPVk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -19,9 +19,14 @@
},
"initialPermissions": {
"snap_dialog": {},
"endowment:keyring": {
"allowedOrigins": [
"http://localhost:8000",
"https://metamask.github.io"
]
},
"endowment:rpc": {
"dapps": true,
"snaps": false
"dapps": true
},
"snap_manageAccounts": {},
"snap_manageState": {}
Expand Down
34 changes: 26 additions & 8 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
MethodNotSupportedError,
handleKeyringRequest,
isKeyringRpcMethod,
} from '@metamask/keyring-api';
import type { OnRpcRequestHandler } from '@metamask/snaps-types';
import type {
OnKeyringRequestHandler,
OnRpcRequestHandler,
} from '@metamask/snaps-types';

import { SimpleKeyring } from './keyring';
import { InternalMethod, originPermissions } from './permissions';
Expand Down Expand Up @@ -36,8 +38,8 @@ function hasPermission(origin: string, method: string): boolean {
}

export const onRpcRequest: OnRpcRequestHandler = async ({
request,
origin,
request,
}) => {
// Log request.
console.log(
Expand All @@ -52,11 +54,6 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
);
}

// Handle keyring methods.
if (isKeyringRpcMethod(request.method)) {
return handleKeyringRequest(await getKeyring(), request as any);
}

// Handle custom methods.
switch (request.method) {
case InternalMethod.ToggleSyncApprovals: {
Expand All @@ -72,3 +69,24 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
}
}
};

export const onKeyringRequest: OnKeyringRequestHandler = async ({
origin,
request,
}) => {
// Log request.
console.log(
`Snap request (id=${request.id ?? 'null'}, origin=${origin}):`,
request,
);

// Check if origin is allowed to call method.
if (!hasPermission(origin, request.method)) {
throw new Error(
`Origin '${origin}' is not allowed to call '${request.method}'`,
);
}

// Handle keyring methods.
return handleKeyringRequest(await getKeyring(), request);
};
13 changes: 5 additions & 8 deletions packages/snap/src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ export const originPermissions = new Map<string, string[]>([
[
'metamask',
[
// Keyring methods
KeyringRpcMethod.ListAccounts,
KeyringRpcMethod.GetAccount,
KeyringRpcMethod.CreateAccount,
KeyringRpcMethod.FilterAccountChains,
KeyringRpcMethod.UpdateAccount,
KeyringRpcMethod.DeleteAccount,
KeyringRpcMethod.ExportAccount,
KeyringRpcMethod.ListRequests,
KeyringRpcMethod.GetRequest,
KeyringRpcMethod.SubmitRequest,
KeyringRpcMethod.ApproveRequest,
KeyringRpcMethod.RejectRequest,
],
],
[
'http://localhost:8000',
[
// Keyring methods
KeyringRpcMethod.ListAccounts,
KeyringRpcMethod.GetAccount,
KeyringRpcMethod.CreateAccount,
Expand All @@ -35,17 +33,17 @@ export const originPermissions = new Map<string, string[]>([
KeyringRpcMethod.ExportAccount,
KeyringRpcMethod.ListRequests,
KeyringRpcMethod.GetRequest,
KeyringRpcMethod.SubmitRequest,
KeyringRpcMethod.ApproveRequest,
KeyringRpcMethod.RejectRequest,

// Custom methods
InternalMethod.ToggleSyncApprovals,
InternalMethod.IsSynchronousMode,
],
],
[
'https://metamask.github.io',
[
// Keyring methods
KeyringRpcMethod.ListAccounts,
KeyringRpcMethod.GetAccount,
KeyringRpcMethod.CreateAccount,
Expand All @@ -55,10 +53,9 @@ export const originPermissions = new Map<string, string[]>([
KeyringRpcMethod.ExportAccount,
KeyringRpcMethod.ListRequests,
KeyringRpcMethod.GetRequest,
KeyringRpcMethod.SubmitRequest,
KeyringRpcMethod.ApproveRequest,
KeyringRpcMethod.RejectRequest,

// Custom methods
InternalMethod.ToggleSyncApprovals,
InternalMethod.IsSynchronousMode,
],
Expand Down
Loading

0 comments on commit e683a07

Please sign in to comment.