Skip to content

Commit

Permalink
Document snap and ethereum globals (MetaMask#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding authored Feb 16, 2023
1 parent ff26d40 commit 83c2497
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
7 changes: 3 additions & 4 deletions docs/guide/snaps-development-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ Whenever the snap receives a JSON-RPC request from an external entity (a dapp or
In order for the extension to call the `onRpcRequest` method of the snap, the `endowment:rpc` permission must be requested. See [Permissions](./snaps-permissions.html#endowment-rpc)
:::

In addition to being able to expose a JSON-RPC API, snaps can access the global object `wallet`.
This object exposes a very similar API to the one exposed to dapps via `window.ethereum`.
Any message sent via `wallet.request()` will be received and processed by MetaMask.
In addition to being able to expose a JSON-RPC API, snaps can access the global object `snap`.
This object can be used to make snap specific JSON-RPC requests.

If a dapp wanted to use `hello-snap`, it would do something like this:

Expand Down Expand Up @@ -285,7 +284,7 @@ if (provider && isFlask) {
### The Snap Execution Environment
Snaps execute in a sandboxed environment that's running Secure EcmaScript (SES, see [below](#secure-ecmascript-ses)).
There is no DOM, no Node.js builtins, and no platform-specific APIs other than MetaMask's `wallet` global object.
There is no DOM, no Node.js builtins, and no platform-specific APIs other than MetaMask's `snap` global object.
Almost all standard JavaScript globals contained in [this list](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) that are also in Node.js are available as normal.
This includes things like `Promise`, `Error`, `Math`, `Set`, `Reflect` etc.
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/snaps-permissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Cronjobs are specified as follows:
}
```

### `endowment:ethereum-provider`

For snaps that wish to communicate with a node via MetaMask, the snap can request the `endowment:ethereum-provider` permission. This permission will expose the global API `ethereum` to the snap execution environment. Without this permission, this global will not be available. This global is a EIP-1193 provider.

## RPC Permissions

To use any restricted RPC method, a snap will need to request permissions to access that method. For a list of available RPC methods and thus valid RPC permissions see [JSON-RPC API](./snaps-rpc-api.html#restricted-methods).
16 changes: 8 additions & 8 deletions docs/guide/snaps-rpc-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ The user can either approve or reject the confirmation, which will be indicated
#### Example

```javascript
const result = await wallet.request({
const result = await snap.request({
method: 'snap_confirm',
params: [
{
Expand Down Expand Up @@ -429,7 +429,7 @@ import { SLIP10Node } from '@metamask/key-tree';

// By way of example, we will use Dogecoin, which has a derivation path starting
// with `m/44'/3'`.
const dogecoinNode = await wallet.request({
const dogecoinNode = await snap.request({
method: 'snap_getBip32Entropy',
params: {
// Must be specified exactly in the manifest
Expand Down Expand Up @@ -561,7 +561,7 @@ Note that `@metamask/key-tree` can help you get the [extended private keys](http
import { getBIP44AddressKeyDeriver } from '@metamask/key-tree';

// By way of example, we will use Dogecoin, which has `coin_type` 3.
const dogecoinNode = await wallet.request({
const dogecoinNode = await snap.request({
method: 'snap_getBip44Entropy',
params: {
coinType: 3,
Expand Down Expand Up @@ -639,7 +639,7 @@ Note that this returns the public key, not the extended public key (`xpub`), or
```javascript
// By way of example, we will use Dogecoin, which has a derivation path starting
// with `m/44'/3'`.
const dogecoinPublicKey = await wallet.request({
const dogecoinPublicKey = await snap.request({
method: 'snap_getBip32PublicKey',
params: {
// The path and curve must be specified in the initial permissions.
Expand Down Expand Up @@ -751,13 +751,13 @@ The data is automatically encrypted using a snap-specific key and automatically

```javascript
// First, let's persist some data
await wallet.request({
await snap.request({
method: 'snap_manageState',
params: { operation: 'update', newState: { hello: 'world' } },
});

// Then, at some later time, let's get the data we stored
const persistedData = await wallet.request({
const persistedData = await snap.request({
method: 'snap_manageState',
params: { operation: 'get' },
});
Expand All @@ -766,7 +766,7 @@ console.log(persistedData);
// { hello: 'world' }

// Finally, if there's no need to store data anymore, we can clear it out
await wallet.request({
await snap.request({
method: 'snap_manageState',
params: { operation: 'clear' },
});
Expand Down Expand Up @@ -830,7 +830,7 @@ See above for their meaning and format.
#### Example

```javascript
await wallet.request({
await snap.request({
method: 'snap_notify',
params: {
type: 'inApp',
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/snaps.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ import { getMessage } from './message';
export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => {
switch (request.method) {
case 'hello':
return wallet.request({
return snap.request({
method: 'snap_confirm',
params: [
{
Expand Down

0 comments on commit 83c2497

Please sign in to comment.