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

Add information about ERC-4337 #1167

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion snaps/features/custom-evm-accounts/create-account-snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class MySnapKeyring implements Keyring {
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 [supported signing methods](index.md#supported-signing-methods).
See the methods for [externally owned accounts](index.md#eoa-methods) and
[ERC-4337 accounts](index.md#account-abstraction-erc-4337).

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

Expand Down
95 changes: 84 additions & 11 deletions snaps/features/custom-evm-accounts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ tags:

The Keyring API integrates custom EVM accounts inside MetaMask.
You can use the Keyring API to display custom accounts, such as multi-party computation (MPC)
accounts, alongside regular MetaMask accounts in the user interface:
accounts and [ERC-4337 accounts](#account-abstraction-erc-4337), alongside regular MetaMask accounts
in the user interface:

<p align="center">
<img src={require('../../assets/keyring/accounts-ui.png').default} alt="Account management Snap accounts in Metamask UI" width="360" style={{border: '1px solid gray'}} />
Expand Down Expand Up @@ -273,17 +274,89 @@ It resolves the request using
and the Snap resolves the request using [`snap_manageAccounts`](../../reference/snaps-api.md#snap_manageaccounts),
notifying MetaMask of the result.

## Supported signing methods
## EOA methods

An account management Snap can implement support for handling the [`personal_sign`](/wallet/reference/personal_sign),
[`eth_signTypedData_v4`](/wallet/reference/eth_signtypeddata_v4), and
[`eth_signTransaction`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_signtransaction)
Ethereum signing methods.
An account management Snap can implement the following methods to support dapp requests from
externally owned accounts (EOAs):

The Snap can also implement support for [deprecated signing
methods](/wallet/concepts/signing-methods/#deprecated-signing-methods) that some dapps still
might use.
- [`personal_sign`](/wallet/reference/personal_sign)
- [`eth_signTypedData_v4`](/wallet/reference/eth_signtypeddata_v4)
- [`eth_signTransaction`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_signtransaction)
- [Deprecated signing methods](/wallet/concepts/signing-methods/#deprecated-signing-methods)

## Example
## Account abstraction (ERC-4337)

See the [example account management Snap source code](https://github.com/MetaMask/snap-simple-keyring) for more information.
:::flaskOnly
:::

Account abstraction, specified by [EIP-4337](https://eips.ethereum.org/EIPS/eip-4337), introduces
*user operations* and enables users to manage smart contract accounts containing arbitrary
verification logic.
Users can use these ERC-4337 accounts instead of externally owned accounts as primary accounts.

An account management Snap can implement the following methods to support dapp requests from
ERC-4337 accounts:

- [`eth_prepareUserOperation`](https://github.com/MetaMask/keyring-api/blob/main/docs/evm_methods_userOp.md#eth_prepareuseroperation)
- [`eth_patchUserOperation`](https://github.com/MetaMask/keyring-api/blob/main/docs/evm_methods_userOp.md#eth_patchuseroperation)
- [`eth_signUserOperation`](https://github.com/MetaMask/keyring-api/blob/main/docs/evm_methods_userOp.md#eth_signuseroperation)

The user operation signing flow in an ERC-4337 compatible account Snap looks like the following:

```mermaid
%%{
init: {
'sequence': {
'actorMargin': 60,
'width': 300
}
}
}%%

sequenceDiagram
autonumber

participant Dapp
participant MetaMask
participant Snap

Dapp ->>+ MetaMask: Transaction intents
note over MetaMask: Currently, only one transaction per userOp is supported
MetaMask ->>+ Snap: eth_prepareUserOperation(transaction intents)
Snap -->>- MetaMask: userOp details
MetaMask ->> MetaMask: Check if account is already deployed

alt The account is already deployed
MetaMask ->> MetaMask: Remove the initCode if set
else The account is not deployed and the initCode is not present
MetaMask ->> Dapp: Throw an error (without the exact reason)
end

alt The gas isn't set
MetaMask ->> MetaMask: Estimate and set gas values
end

MetaMask ->> MetaMask: Estimate and set gas fees
MetaMask ->>+ Snap: eth_patchUserOperation(userOp object)
Snap -->>- MetaMask: Partial userOp object
MetaMask ->> MetaMask: Update paymasterAndData and remove dummy signature
MetaMask ->>+ Snap: eth_signUserOperation(userOp object, entry point)
Snap -->>- MetaMask: Signature
MetaMask ->> MetaMask: Update userOp's signature

MetaMask ->> MetaMask: Submit userOp to bundler and wait for transaction hash
MetaMask -->>- Dapp: Transaction hash
```

See the [ERC-4337 methods](https://github.com/MetaMask/keyring-api/blob/main/docs/evm_methods_userOp.md)
for more information about their parameters and response details.

## Examples

See the following example account management Snap implementations:

- [Simple Account Snap](https://github.com/MetaMask/snap-simple-keyring)
- [Simple Account Abstraction Snap](https://github.com/MetaMask/snap-account-abstraction-keyring/tree/main) (ERC-4337)
- [Silent Shard Snap](https://github.com/silence-laboratories/silent-shard-snap)
- [Safeheron MPC Snap](https://github.com/Safeheron/multi-mpc-snap-monorepo)
- [Capsule Keyring Snap](https://github.com/capsule-org/mm-snap-keyring)
7 changes: 7 additions & 0 deletions snaps/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ the canary distribution of MetaMask:
title: "Home pages",
description: "Present a dedicated UI page in MetaMask for your Snap.",
flaskOnly: true
},
{
icon: require("./assets/features/network.png").default,
href: "features/custom-evm-accounts#account-abstraction-erc-4337",
title: "Account abstraction",
description: "Connect to ERC-4337 accounts in MetaMask.",
flaskOnly: true
}
]}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/theme/Admonition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function AdmonitionWrapper(props: Props): JSX.Element {
icon={<FlaskIcon />}
title="Flask Only"
>
This feature is only available in <a href="https://docs.metamask.io/snaps/get-started/install-flask/">MetaMask Flask</a>, the canary distribution of MetaMask.
This feature is experimental and only available in <a href="https://docs.metamask.io/snaps/get-started/install-flask/">MetaMask Flask</a>, the canary distribution of MetaMask.
</Admonition>
);
}
Expand Down
Loading