Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
feat: add support for ERC-4337 accounts (#213)
Browse files Browse the repository at this point in the history
* docs: add EIP-4337 UserOp support

* wip: support 4337 accounts

* chore: simplify regex

* feat: add `definePattern` helper function

* chore: move types to `eth` directory

* chore: use `definePattern` to define `UuidV4` type

* chore: move user operation sequence diagram

* docs: add `eth_signUserOperation`

* feat: add `BasicTransaction` type

* fix: add index files

* feat: add `PreparedUserOperation` type

* fix: make `gasLimits` optional

* feat: add `EthUserOperationPatch` type

* feat: export base ETH types

* feat: add `EthKeyring` type

* fix: add UserOperation methods to support methods enum

* docs: add link to ERC-4337 spec
  • Loading branch information
danroc authored Dec 6, 2023
1 parent 7106e68 commit 43d90b2
Show file tree
Hide file tree
Showing 18 changed files with 709 additions and 16 deletions.
10 changes: 10 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ participant Site as Snap Companion Dapp
User ->>+ Dapp: Create new sign request
Dapp ->>+ MetaMask: ethereum.request(request)
alt Is EIP-4337 account?
MetaMask ->>+ Snap: keyring_prepareRequest(request)
Snap ->> Snap: Custom logic to prepare request
Snap -->>- MetaMask: { request }
end
MetaMask ->> MetaMask: Display request to user
User ->> MetaMask: Approve request
Expand Down Expand Up @@ -208,6 +213,11 @@ participant Snap
User ->>+ Dapp: Create new sign request
Dapp ->>+ MetaMask: ethereum.request(request)
alt Is EIP-4337 account?
MetaMask ->>+ Snap: keyring_prepareRequest(request)
Snap ->> Snap: Custom logic to prepare request
Snap -->>- MetaMask: { request }
end
MetaMask ->> MetaMask: Display request to user
User ->> MetaMask: Approve request
Expand Down
115 changes: 115 additions & 0 deletions docs/diagrams/user_operation_signing.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
@startuml "ERC-4337 Account Support"
autonumber
skinparam fontname Arial

title "ERC-4337 UserOperation Signing"

participant Dapp
participant MetaMask
participant Snap

Dapp -> MetaMask ++: ""{""\n\
"" chainId, // Ignored by MetaMask""\n\
"" from,""\n\
"" to,""\n\
"" value,""\n\
"" data,""\n\
}

note over MetaMask
Currently, only one transaction
per UserOp will be supported
end note

MetaMask -> Snap ++: ""prepareUserOperation({""\n\
"" account: account.id,""\n\
"" scope: `eip155:${chainId}`,""\n\
"" transactions: [ // List of transactions""\n\
"" {""\n\
"" to,""\n\
"" value,""\n\
"" data,""\n\
"" },""\n\
"" ]""\n\
})

Snap --> MetaMask --: ""{""\n\
"" callData,""\n\
"" initCode?,""\n\
"" nonce,""\n\
"" gasLimits?: {""\n\
"" callGasLimit,""\n\
"" verificationGasLimit,""\n\
"" preVerificationGas,""\n\
"" },""\n\
"" dummySignature?,""\n\
"" dummyPaymasterAndData?,""\n\
"" bundler?,""\n\
""}""

MetaMask -> MetaMask: Check if the 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"" isn't 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 ++: ""patchUserOperation({""\n\
"" sender,""\n\
"" nonce,""\n\
"" initCode,""\n\
"" callData,""\n\
"" callGasLimit,""\n\
"" verificationGasLimit,""\n\
"" preVerificationGas,""\n\
"" maxFeePerGas,""\n\
"" maxPriorityFeePerGas,""\n\
"" paymasterAndData, // Dummy value or empty""\n\
"" signature, // Dummy value or empty""\n\
""})""

Snap --> MetaMask --: ""{""\n\
"" paymasterAndData?,""\n\
""}""

MetaMask -> MetaMask: Update ""paymasterAndData"" and\n\
remove the dummy signature

MetaMask -> MetaMask: Display approval UI

MetaMask -> Snap ++: ""signUserOperation([""\n\
"" {""\n\
"" sender,""\n\
"" nonce,""\n\
"" initCode,""\n\
"" callData,""\n\
"" callGasLimit,""\n\
"" verificationGasLimit,""\n\
"" preVerificationGas,""\n\
"" maxFeePerGas,""\n\
"" maxPriorityFeePerGas,""\n\
"" paymasterAndData,""\n\
"" signature, // Empty""\n\
"" },""\n\
"" entrypoint, // Entrypoint deployed by""\n\
"" // the Ethereum Foundation""\n\
""])""

Snap --> MetaMask --: ""{""\n\
"" signature,""\n\
""}""

MetaMask -> MetaMask: Update UserOp's ""signature""

MetaMask -> MetaMask: Submit UserOp to bundler and\n\
wait for transaction hash

MetaMask --> Dapp --: ""txHash""
@enduml
14 changes: 7 additions & 7 deletions docs/evm-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Adds support to [`eth_sendTransaction`][eth-send-transaction].
- Pattern: `^0x[0-9a-fA-F]{1,2}$`
- `nonce`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`
- `to`
- One-of:
- Contract creation
Expand All @@ -118,22 +118,22 @@ Adds support to [`eth_sendTransaction`][eth-send-transaction].
- Pattern: `^0x[0-9a-fA-F]{40}$`
- `value`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`
- `data`
- Type: `string`
- Pattern: `^0x[0-9a-f]*$`
- `gasLimit`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`
- `gasPrice`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`
- `maxPriorityFeePerGas`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`
- `maxFeePerGas`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`
- `accessList`:
- Description: EIP-2930 access list
- Type: `array`
Expand All @@ -150,7 +150,7 @@ Adds support to [`eth_sendTransaction`][eth-send-transaction].
- Pattern: `^0x[0-9a-f]{64}$`
- `chainId`
- Type: `string`
- Pattern: `^0x([1-9a-f]+[0-9a-f]*|0)$`
- Pattern: `^0x([1-9a-f][0-9a-f]*|0)$`

### Returns

Expand Down
Loading

0 comments on commit 43d90b2

Please sign in to comment.