Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Keyring API reference #1170

Merged
merged 9 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
@@ -1,4 +1,3 @@
/build
/.yarn
/static/js/feedback-script.js
/external/keyring-api
6 changes: 0 additions & 6 deletions .github/workflows/build-lint.yml
Expand Up @@ -12,8 +12,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Use Node.js
uses: actions/setup-node@v3
with:
Expand All @@ -29,8 +27,6 @@ jobs:
- prepare
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Use Node.js
uses: actions/setup-node@v3
with:
Expand All @@ -55,8 +51,6 @@ jobs:
- prepare
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Use Node.js
uses: actions/setup-node@v3
with:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/publish-docs.yml
Expand Up @@ -23,7 +23,6 @@ jobs:
uses: actions/checkout@v3
with:
ref: ${{ inputs.ref }}
submodules: true
- name: Use Node.js
uses: actions/setup-node@v3
with:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -8,7 +8,6 @@
.docusaurus
.cache-loader
.idea
/snaps/reference/keyring-api

# yarn v3 (w/o zero-install)
# See: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

17 changes: 0 additions & 17 deletions docusaurus.config.js
Expand Up @@ -3,7 +3,6 @@

const codeTheme = require("prism-react-renderer/themes/dracula");
const remarkCodesandbox = require("remark-codesandbox");
const path = require("path");
const isProd = process.env.NODE_ENV === "production";

/** @type {import('@docusaurus/types').Config} */
Expand Down Expand Up @@ -114,22 +113,6 @@ const config = {
breadcrumbs: false,
},
],
[
"docusaurus-plugin-typedoc",
{
entryPoints: ["./external/keyring-api/src/index.ts"],
tsconfig: "./external/keyring-api/tsconfig.json",
readme: "snaps/reference/keyring-api-index/index.md",
out: path.join(__dirname, "snaps/reference/keyring-api"),
sidebar: {
filteredIds: ["reference/keyring-api/index"],
},
useCodeBlocks: true,
expandObjects: true,
parametersFormat: "table",
hideGenerator: true,
},
],
[
"@docusaurus/plugin-client-redirects",
{
Expand Down
1 change: 0 additions & 1 deletion external/keyring-api
Submodule keyring-api deleted from 1c8eeb
8 changes: 2 additions & 6 deletions package.json
Expand Up @@ -15,8 +15,7 @@
"write-heading-ids": "docusaurus write-heading-ids",
"typecheck": "tsc",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"postinstall": "cd external/keyring-api && yarn install"
"lint:fix": "eslint . --fix"
},
"dependencies": {
"@docusaurus/core": "2.4.3",
Expand All @@ -28,17 +27,14 @@
"@metamask/docusaurus-openrpc": "^0.3.1",
"clsx": "^1.2.1",
"docusaurus-plugin-segment": "^1.0.4",
"docusaurus-plugin-typedoc": "1.0.0-next.17",
"node-polyfill-webpack-plugin": "^2.0.1",
"prettier": "^3.0.0",
"prism-react-renderer": "^1.3.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-player": "^2.13.0",
"remark-codesandbox": "^0.10.1",
"remark-docusaurus-tabs": "^0.2.0",
"typedoc": "^0.25.1",
"typedoc-plugin-markdown": "4.0.0-next.22"
"remark-docusaurus-tabs": "^0.2.0"
},
"devDependencies": {
"@docusaurus/eslint-plugin": "2.4.3",
Expand Down
51 changes: 1 addition & 50 deletions snaps-sidebar.js
Expand Up @@ -50,56 +50,7 @@ const sidebar = {
label: "Reference",
link: { type: "generated-index", slug: "/reference" },
collapsed: false,
items: [
{
type: "doc",
id: "reference/snaps-api",
},
{
type: "doc",
id: "reference/entry-points",
},
{
type: "doc",
id: "reference/permissions",
},
{
type: "doc",
id: "reference/known-errors",
},
{
type: "category",
label: "Snaps command line",
link: { type: "generated-index", slug: "reference/cli" },
items: [
{
type: "doc",
id: "reference/cli/options",
},
{
type: "doc",
id: "reference/cli/subcommands",
},
],
},
{
type: "doc",
id: "reference/jest",
},
{
type: "category",
label: "Keyring API",
link: {
type: "doc",
id: "reference/keyring-api/index",
},
items: require("./snaps/reference/keyring-api/typedoc-sidebar.cjs"),
},
{
type: "doc",
id: "reference/resources",
},
],
items: [{ type: "autogenerated", dirName: "reference" }],
},
],
};
Expand Down
108 changes: 23 additions & 85 deletions snaps/features/custom-evm-accounts/create-account-snap.md
Expand Up @@ -13,7 +13,7 @@ import TabItem from '@theme/TabItem';
Create an account management Snap to connect to custom EVM accounts.

:::tip see also
- [About custom EVM accounts](index.md)
- [Custom EVM accounts](index.md)
- [Create an account management companion dapp](create-companion-dapp.md)
- [Account management Snap security guidelines](security.md)
- [Keyring API reference](../../reference/keyring-api/index.md)
Expand Down Expand Up @@ -65,7 +65,8 @@ Add a list of dapp URLs allowed to call Keyring API methods on your Snap using t

### 3. Implement the Keyring API

Implement the [required Keyring API methods](security.md#limit-the-methods-exposed-to-dapps) in your Snap:
Implement the [Keyring Client API](../../reference/keyring-api/client-api/index.md) in your Snap.
Make sure to [limit the methods exposed to dapps](security.md#limit-the-methods-exposed-to-dapps).

```typescript
class MySnapKeyring implements Keyring {
Expand All @@ -75,11 +76,11 @@ class MySnapKeyring implements Keyring {

### 4. Handle requests submitted by MetaMask

MetaMask submits Ethereum sign requests from dapps using the
[`submitRequest`](../../reference/keyring-api/type-aliases/Keyring.md#submitrequest) method of the
Keyring API.
See the methods for [externally owned accounts](index.md#eoa-methods) and
[ERC-4337 accounts](index.md#account-abstraction-erc-4337).
MetaMask submits EVM requests from dapps using the
[`keyring_submitRequest`](../../reference/keyring-api/client-api/index.md#keyring_submitrequest)
method of the Keyring API.
See the EVM methods for [externally owned accounts](../../reference/keyring-api/interface-api.md#eoa-methods)
and [ERC-4337 accounts](../../reference/keyring-api/interface-api.md#erc-4337-methods).

The following is an example of a `personal_sign` request:

Expand Down Expand Up @@ -130,84 +131,21 @@ The redirect message and URL is displayed to the user to help them continue the

### 5. Notify MetaMask about events

Notify MetaMask when the following events take place, using the `emitSnapKeyringEvent()` helper function:

- An account is created:

```typescript
try {
emitSnapKeyringEvent(snap, KeyringEvent.AccountCreated, { account });
// Update your Snap's state...
} catch (error) {
// Handle the error...
}
```

MetaMask returns an error if the account already exists or the account object is invalid.

- An account is updated:

```typescript
try {
emitSnapKeyringEvent(snap, KeyringEvent.AccountUpdated, { account });
// Update your Snap's state...
} catch (error) {
// Handle the error...
}
```

MetaMask returns an error if the account does not exist, the account object is invalid, or the
account address changes.

- An account is deleted:

```typescript
try {
emitSnapKeyringEvent(snap, KeyringEvent.AccountDeleted, {
id: account.id,
});
// Update your Snap's state...
} catch (error) {
// Handle the error...
}
```

The delete event is idempotent, so it is safe to emit even if the account does not exist.

- A request is approved:

```typescript
try {
emitSnapKeyringEvent(snap, KeyringEvent.RequestApproved, {
id: request.id,
result,
});
// Update your Snap's state...
} catch (error) {
// Handle the error...
}
```

MetaMask returns an error if the request does not exist.
This event only applies to Snaps that implement the
[asynchronous transaction flow](index.md#asynchronous-transaction-flow).

- A request is rejected:

```typescript
try {
emitSnapKeyringEvent(snap, KeyringEvent.RequestRejected, {
id: request.id,
});
// Update your Snap's state...
} catch (error) {
// Handle the error...
}
```

MetaMask returns an error if the request does not exist.
This event only applies to Snaps that implement the
[asynchronous transaction flow](index.md#asynchronous-transaction-flow).
Notify MetaMask when [Keyring API events](../../reference/keyring-api/client-api/events.md) take
place, using the `emitSnapKeyringEvent()` helper function.

For example, when an account is created:

```typescript
try {
emitSnapKeyringEvent(snap, KeyringEvent.AccountCreated, { account });
// Update your Snap's state...
} catch (error) {
// Handle the error...
}
```

MetaMask returns an error if the account already exists or the account object is invalid.

### 6. Expose the Keyring API

Expand Down
15 changes: 7 additions & 8 deletions snaps/features/custom-evm-accounts/create-companion-dapp.md
Expand Up @@ -9,12 +9,11 @@ tags:
# Create an account management companion dapp

Create a companion dapp to provide a user interface for your account management Snap.
Use the [`KeyringSnapRpcClient`](../../reference/keyring-api/classes/KeyringSnapRpcClient.md) to
call Keyring API methods from your companion dapp, enabling users to create and interact with custom
Call Keyring API methods from your companion dapp, enabling users to create and interact with custom
EVM accounts.

:::tip see also
- [About custom EVM accounts](index.md)
- [Custom EVM accounts](index.md)
- [Create an account management Snap](create-account-snap.md)
- [Keyring API reference](../../reference/keyring-api/index.md)
:::
Expand All @@ -39,9 +38,9 @@ or
npm install @metamask/keyring-api
```

### 2. Create the KeyringSnapRpcClient
### 2. Create a KeyringSnapRpcClient

Create the [`KeyringSnapRpcClient`](../../reference/keyring-api/classes/KeyringSnapRpcClient.md):
Create a `KeyringSnapRpcClient`:

```ts
import { KeyringSnapRpcClient } from "@metamask/keyring-api";
Expand All @@ -52,10 +51,10 @@ let client = new KeyringSnapRpcClient(snapId, window.ethereum);

### 3. Call Keyring API methods

You can now use the [`KeyringSnapRpcClient`](../../reference/keyring-api/classes/KeyringSnapRpcClient.md)
to invoke Keyring API methods on your Snap.
You can now use the `KeyringSnapRpcClient` to invoke
[Keyring Client API](../../reference/keyring-api/client-api/index.md) methods on your Snap.

For example, to call [`keyring_listAccounts`](../../reference/keyring-api/classes/KeyringSnapRpcClient.md#listaccounts):
For example, to call [`keyring_listAccounts`](../../reference/keyring-api/client-api/index.md#keyringlist_accounts):

```typescript
const accounts = await client.listAccounts();
Expand Down