Skip to content

Commit

Permalink
Add allowedOrigins field to endowment:rpc (#1822)
Browse files Browse the repository at this point in the history
This adds an optional `allowedOrigins` field to `endowment:rpc`. For
example, a Snap can now specify this:

```json
{
  "initialPermissions": {
    "endowment:rpc": {
      "allowedOrigins": [
        "example.com"
      ]
    }
  }
}
```

Which would then only allow requests from "example.com" to the Snap.
  • Loading branch information
Mrtenz authored Oct 6, 2023
1 parent 50ce7fd commit fd5f42f
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/examples/packages/bip32/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "MPZ9ur9CDq3vW/16po2PcMSAE/0wWl4sPgjGBwqCW7g=",
"shasum": "35n/r+4qoZLl+N5q0ACMTEU2ROspQP9GTMeqsuJ99gQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
7 changes: 5 additions & 2 deletions packages/examples/packages/bip44/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "H7vpcjHBrK0/AdGj9SAxC4gnp9l6zxDYtP+V/vFX9IU=",
"shasum": "bYcRMl7jxVJlMwW9+U5IiivNK/4T/VsX+k1pGPlp9ik=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -18,7 +18,10 @@
},
"initialPermissions": {
"endowment:rpc": {
"dapps": true
"dapps": true,
"allowedOrigins": [
"npm:@metamask/json-rpc-example-snap"
]
},
"snap_dialog": {},
"snap_getBip44Entropy": [
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/dialogs/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "PiQKZ+XJWHflCsFTqI+bfOC2lv51ReaYh55NBKV7sqY=",
"shasum": "zTD155x2TSEe8zsnJRo7J+CT47RsgOgh44JsB1WuVfU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-entropy/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "WvilWKhHZPbr31IFi6GP5yw77sFlJpFELjvS5qN0QvM=",
"shasum": "QFQgPo75m1aVfrlsAvtkdoPZ7PfTS6koFPpwCLJxgr0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/get-locale/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "BYde5IQ3I0Y6sCow1IGm4mAyksC5fdP3DHnOXZFzxDw=",
"shasum": "z7DohohSRqLwkXWEiINCgaEAW9IntR9Vjhdunt4wsmY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "qQMuTp5zbcAAVNMENTQCvo5Rj825ohur3Uwld8Nxgs0=",
"shasum": "SvREdi8XCfnwdJycaa/k7rrKnYJNSdXmrDJ0+cjQ2zo=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/json-rpc/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "gKdBa0h+SZKNu6WFHFps5W1ly7WNTU2lrrOsyy7Z7VU=",
"shasum": "y2QayHgWDbm8Ax1NGQRO4885HVHviFlbus1Z7uVfOXk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
24 changes: 22 additions & 2 deletions packages/examples/packages/json-rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { OnRpcRequestHandler } from '@metamask/snaps-types';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
* `wallet_invokeSnap` method. This handler handles a single method:
* `wallet_invokeSnap` method. This handler handles two methods:
*
* - `invokeSnap`: Call the `getPublicKey` method of the
* `@metamask/bip32-example-snap`, and return the response. This demonstrates
Expand All @@ -12,12 +12,16 @@ import type { OnRpcRequestHandler } from '@metamask/snaps-types';
* extension, and it must have the `endowment:rpc` permission, with the "snaps"
* option enabled.
*
* - `invokeOtherSnap`: Call the `getPublicKey` method of the
* `@metamask/bip44-example-snap`, and return the response. This demonstrates
* that snaps can specify a list of `allowedOrigins` in their manifest, which
* restricts which snaps can invoke them through the `wallet_invokeSnap` method.
*
* @param params - The request parameters.
* @param params.request - The JSON-RPC request object.
* @returns The JSON-RPC response.
* @see https://docs.metamask.io/snaps/reference/exports/#onrpcrequest
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
* @see https://docs.metamask.io/snaps/reference/rpc-api/#snap_getentropy
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
Expand All @@ -38,6 +42,22 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});
}

case 'invokeOtherSnap': {
return snap.request({
method: 'wallet_invokeSnap',
params: {
snapId: 'npm:@metamask/bip44-example-snap',
request: {
method: 'getPublicKey',
params: {
coinType: 1,
addressIndex: 0,
},
},
},
});
}

default:
throw rpcErrors.methodNotFound({
data: {
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/manage-state/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "3l7IqA82IUC9I+tMEwQSSRKZKoTtQ3pgRwX8kyLz+MM=",
"shasum": "whwrc1YPP3PsAJzrXY2M/2akaG8pFvQhHcV62qO68M8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "z6nWAwVjLuw07zNx1RrFLPChIrr7U/UlJSFo7JXx4pM=",
"shasum": "StQFabni9psFzkxMGGPYiaXXugO1ewvMHcPXHnisCJ4=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 89.06,
"branches": 88.74,
"functions": 95.84,
"lines": 96.98,
"statements": 96.64
"statements": 96.63
}
Loading

0 comments on commit fd5f42f

Please sign in to comment.