MetaMask / eth-snap-keyring

Snap Keyring bridge
6 stars 2 forks source link

feat(272): add `chainId` to Keyring API requests (transaction/typed message) #231

Closed ccharly closed 6 months ago

ccharly commented 6 months ago

Description

This is the first step of adding a CAIP-2 scope to internal requests.

It's a "first step" as for now, we cannot get/extract the chainId for some methods. This would require a much bigger refactor (which will come later).

signTransaction requests

The chainId is part of the transaction request, so we extract it from there and attach it to the internal request.

signTypedData (when applicable) requests

The chainId is part of the domain field (as defined by EIP-712), however, some older versions of the eth_signTypedData_vX might not have it! In this case, we do not provide the scope.

As of today, only those methods do not have the domain:

Related issues

Testing

Manual testing

Prerequisites

  1. Use the SSK
  2. Create a snap account (or use an existing one)
  3. Enable "Use Synchronous Approval"

1. Transaction

  1. Send a transaction using your snap account
  2. DO NOT APPROVE YET
  3. List requests using the SSK
  4. You should see a scope like: eip155:${chainId} (depending on which chain you are using)

2. Typed data

  1. Use the E2E dapp
  2. Connect to your snap account
  3. Sign typed data (using any version you want)
  4. DO NOT APPROVE YET
  5. List requests using the SSK
  6. You should see a scope like: eip155:${chainId} (depending on which chain you are using)

Test coverage report

Coverage remains at 100%

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 100 100 100 100
CaseInsensitiveMap.ts 100 100 100 100
DeferredPromise.ts 100 100 100 100
KeyringSnapControllerClient.ts 100 100 100 100
SnapIdMap.ts 100 100 100 100
SnapKeyring.ts 100 100 100 100
caip.ts 100 100 100 100
index.ts 100 100 100 100
types.ts 100 100 100 100
util.ts 100 100 100 100

Jest

Added a new test for CAIP helpers:

yarn jest src/caip.test.ts

Updated test regarding transaction flow:

yarn jest src/SnapKeyring.test.ts