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

SIP-26: Non-EVM protocol support #147

Merged
merged 26 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
df817e7
Add draft of SIP-26
danroc Aug 30, 2024
df59210
Remove optional header fields
danroc Sep 2, 2024
febeb1e
Add SIP number and remove unused optional fields
danroc Sep 2, 2024
7709ea8
Rename diagram image
danroc Sep 2, 2024
48b95a3
Rename components diagram in document
danroc Sep 2, 2024
ed17dd3
Update diagram and add components descriptions
danroc Sep 12, 2024
da3182a
Rename "Address Resolution Snaps" to "Account Address Resolution Snaps"
danroc Sep 13, 2024
1a782b5
Update components diagram
danroc Sep 13, 2024
3848c61
Update `resolveAccountAddress` signature
danroc Sep 13, 2024
d514496
Add link to CAIP-2 and remove commented text
danroc Sep 13, 2024
33194a3
Add Protocol Snaps
danroc Sep 13, 2024
0c9bb3f
Update components diagram
danroc Sep 13, 2024
b4f6618
Update `resolveAccountAddress` docs
danroc Sep 13, 2024
ea35e35
Clarify: should -> MUST
danroc Sep 13, 2024
5260fba
Update `resolveAccountAddress` method
danroc Sep 16, 2024
9696ca7
Remove mention to SIP-25
danroc Sep 16, 2024
cab089f
Add note about Snap events
danroc Sep 16, 2024
b81138d
Minor text improvements
danroc Sep 16, 2024
7a99884
Add mention to CAIP-2 chain IDs
danroc Sep 24, 2024
ddd9c7d
Update keyring-api repo URL
danroc Sep 24, 2024
a0c782c
Add note about Accounts Router and Protocol Snaps priority
danroc Sep 30, 2024
72fb0c2
chore: update SIP-26 following latest discussions
danroc Nov 12, 2024
739f165
SIP-26: Update Snaps-specific parts of the specification (#153)
FrederikBolding Nov 14, 2024
f59f3cc
chore: update title
danroc Nov 14, 2024
7b35ebc
Apply suggestions from code review
FrederikBolding Nov 14, 2024
8a0171a
Update SIPS/sip-26.md
FrederikBolding Nov 14, 2024
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
168 changes: 168 additions & 0 deletions SIPS/sip-26.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
sip: 26
title: Account Router Snaps
danroc marked this conversation as resolved.
Show resolved Hide resolved
status: Draft
author: Daniel Rocha (@danroc)
created: 2024-08-28
---

## Abstract

This SIP proposes a new API to be implemented by a new Account Router, allowing
the forwarding of signing requests to the appropriate account Snap (i.e., Snaps
that implement the [Keyring API][keyring-api]).

## Motivation

The Keyring API is being modified to support non-EVM chains. However, a
challenge arises in identifying the correct account Snap that should receive
the signing request, as this information is often only obtainable from the
request itself, which varies based on the method and chain.

## Specification

> Formal specifications are written in TypeScript.

### Language

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" written
in uppercase in this document are to be interpreted as described in [RFC
2119](https://www.ietf.org/rfc/rfc2119.txt).

### High-Level architecture

The diagram below represents a high-level architecture of how the Account
Router integrates with the RPC Router, Account Snaps, and Protocol Snaps.

![High-level architecture](../assets/sip-26/components-diagram.png)

- **Account Snaps**: Snaps that implement the Keyring API and are responsible
for signing requests and managing accounts.
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved

- **Protocol Snaps**: Snaps that implement protocol methods that do not require
an account to be executed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought per our discussions with @rekmarks we'd determined that we can't enforce separation of Account + Protocol Snaps?
Accounts SIP (1)

Copy link
Contributor

@adonesky1 adonesky1 Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I now see the language in your diagram saying that these can be combined. I think its more confusing to represent them as (sortof) separate in the diagram.

As I believe I said in our meeting I don't think we're really getting anything by having these non signing protocol snaps that implement non signing methods if the signing snaps can trivially handle those requests too.

In that case I feel like its clearer to call them something more general than "Account Snaps" since they are not just handling accounts. Maybe "Protocol Snaps" or "CAIP-2 Snaps" (though I'd prefer something that clearly states its purpose without external reference).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To restate what I said in a meeting: As I read it, they are just two separate permissions that a Snap may or may not put together, and some Snaps (e.g. hardware wallet or SCAs) may want to only ship the account part.


- **RPC Router**: Native component that forwards non-signing requests to the
appropriate Protocol Snap or native implementation.

- **Account Router**: Native component that forwards signing requests to the
appropriate Account Snap or native implementation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the Account Router is the one that registers signing methods to the RPC Router, but how are the signing methods registered into the Account Router?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are listed in the account object in the methods field.

But it made me think of another problem: the account doesn't list the supported chains (there is a method to get this information, but we should probably move it into the account object).


- **Account Address Resolution Snaps**: Snaps that implement the
`resolveAccountAddress` method to extract the account address from the
request object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these Snaps be the same as Account and Protocol Snaps, or do they have to be separate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After talking with @Mrtenz, I believe the Snaps should remain separate. Our goal is for these Snaps to operate without special permissions. Additionally, we should have only one Account Address Resolution Snap per blockchain. This will help simplify the Account (CAIP-2) Snap and prevent any confusion about which Account Address Resolution Snap to use.


### Account Router

The Account Router exposes the [`snap_manageAccounts`][snap-manage-accs] method
to allow account Snaps to register, remove, and update accounts.

For example, this code can be used by an Account Snap to register a new account
with the Account Router:

```typescript
// This will notify the Account Router that a new account was created, and the
// Account Router will register this account as available for signing requests
// using the `eth_signTypedData_v4` method.
await snap.request({
method: 'snap_manageAccounts',
params: {
method: 'notify:accountCreated',
params: {
account: {
id: '74bb3393-f267-48ee-855a-2ba575291ab0',
Copy link

@shanejonas shanejonas Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the snap itself creates the id? we probably shouldn't leave these ids up to the snap developer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, they are UUIDv4, the extension enforces that they are unique when an account is registered.

type: 'eip155:eoa',
address: '0x1234567890123456789012345678901234567890',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a caip-10 address to include the chainid which is missing context here when creating an account:

eip155:1:0xab16a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea agreed. We will need some way to associate this account with the appropriate CAIP-2 identifier for routing purposes, whether its making these CAIP-10 addresses or adding a CAIP-2 property on the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a 1:1 mapping to the blockchain account, and for EVM chains the address is the same accross all networks, so I think CAIP-2 addresses are not suitable here.

We have thought about using CAIP-2 namespaces, but it's not in the scope of this SIP, this snippet is an example of the existing API.

Copy link
Contributor

@adonesky1 adonesky1 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Do we have a plan for how to handle types that are not eip155:eoa (EVM SCAs and other non-evm chains where the account type might not provide enough to be sure that the account can be used on all subchains) then? Seems like we should include a CAIP-2 scope(s) property here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there documentation we could link to where this type property is defined/scoped?

methods: ['eth_signTypedData_v4'],
options: {},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would go in options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is part of the Keyring API and is used by Snap devs to store implementation-dependent information about the account. An example would be to use it to store the tresholds, index, and other metadata used by MPC accounts.

},
},
},
});
```

Similar events are available to notify about the removal and update of
accounts: `notify:accountRemoved` and `notify:accountUpdated`.

Additionally, the Account Router expects the Account Snap to implement the
Keyring API so it can forward signing requests to it through the
[`keyring_submitRequest`][submit-request] method.

### Account Address Resolution Snaps

The Account Address Resolution Snaps are responsible for extracting the account
address that should receive the signing request from the request object. This
is accomplished by exposing the `resolveAccountAddress` method to the Account
Router.

```typescript
/**
* Returns the address of the account that should handle the signing request.
*
* @param request - The request object.
* @returns The account address or `undefined` if the address could not be
* resolved.
*/
function resolveAccountAddress(request: MultichainRequest): string | undefined;
```

FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
There must be only one Account Resolution Snap registered per chain to prevent
ambiguity in the account resolution process.

To identify which Account Resolution Snap should be used for a given request,
Account Address Resolution Snaps MUST list the supported chains in their
manifest file:
danroc marked this conversation as resolved.
Show resolved Hide resolved

```json5
"initialPermissions": {
"endowment:account-address-resolver": {
"chains": [
"<chain_id_1>",
"<chain_id_2>",
// ...
]
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as below, should resemble caip-25 permission: https://github.com/MetaMask/SIPs/pull/147/files#r1777172390

}
```

Note that the `reference` part of the [CAIP-2][caip-2] chain ID can be a `*`
wildcard to match any chain ID of a given namespace (e.g., `eip155:*`).

The RPC Router subscribes to Snap installation events to detect new Account
Address Resolution Snaps and updates its internal routing table accordingly.

### Protocol Snaps

Protocol Snaps implement and expose methods that do not require an account to
execute and MUST list their supported methods in their manifest file:

```json5
"initialPermissions": {
"endowment:protocol-methods": {
"chains": {
"<chain_id_1>": [
// List of supported methods
]
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably resemble caip-25 and the permission we just created and migrated the old account permissions to:


   "eip155:1": {
      "methods": ["eth_sendTransaction", "eth_signTransaction", "eth_sign", "get_balance", "personal_sign"],
      "notifications": []
   },

}
}
```

The RPC Router subscribes to Snap installation events to detect new Protocol
Snaps and updates its internal routing table accordingly.

### Context object

Alongside the request object, a context object is passed along to keep internal
state related to the request. Its structure is not defined by this SIP, but a
primary use would be to keep the resolved account ID.

## Copyright

Copyright and related rights waived via [CC0](../LICENSE).

[keyring-api]: https://github.com/MetaMask/keyring-api
danroc marked this conversation as resolved.
Show resolved Hide resolved
[snap-manage-accs]: https://docs.metamask.io/snaps/reference/snaps-api/#snap_manageaccounts
[submit-request]: https://docs.metamask.io/snaps/reference/keyring-api/account-management/#keyring_submitrequest
[caip-2]: https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md
Binary file added assets/sip-26/components-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading