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 ERC-4337 and ERC-7702 account implementations #25

Merged
merged 58 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
a7b170a
WIP: Migrate Account code
ernestognw Nov 26, 2024
1859385
Merge branch 'master' into aa/accounts
ernestognw Nov 29, 2024
7847e80
Merge branch 'master' into aa/accounts
ernestognw Dec 3, 2024
25de3ac
Checkpoint
ernestognw Dec 5, 2024
292dae1
Fix lint
ernestognw Dec 5, 2024
f8657e7
Checkpoint
ernestognw Dec 5, 2024
8699232
up
ernestognw Dec 6, 2024
7bdf69e
up
ernestognw Dec 6, 2024
efd52cd
Adjust
ernestognw Dec 6, 2024
87fcd0e
up
ernestognw Dec 6, 2024
549ab8e
Simplify CallReceiverMock
ernestognw Dec 6, 2024
90c7f5e
Fix slither + Codespell
ernestognw Dec 6, 2024
2e345c3
Fix coverage
ernestognw Dec 6, 2024
af200e4
Merge branch 'master' into aa/accounts
ernestognw Dec 6, 2024
4d03f45
Remove entrypoint
ernestognw Dec 6, 2024
9caded9
Readd entrypoint
ernestognw Dec 6, 2024
f6b4454
Run --ir-minimum in forge coverage
ernestognw Dec 6, 2024
2d2300f
up
ernestognw Dec 7, 2024
d6ba190
Make Accounts initializable
ernestognw Dec 7, 2024
208386f
Finish docs
ernestognw Dec 7, 2024
49fc47d
Merge branch 'master' into aa/accounts
ernestognw Dec 7, 2024
d715e4c
rewrite helpers/signers as alternative to ethers.SigningKey and
Amxx Dec 9, 2024
8ad95a1
Rename _validateNestedEIP712Signature -> _validateSignature
ernestognw Dec 9, 2024
885efdd
Read virtual to ERC7739Signer functions
ernestognw Dec 9, 2024
d5f0dac
lint
ernestognw Dec 9, 2024
130ce04
Implement review recommendations
ernestognw Dec 9, 2024
dcdae8d
Include signer into account factory hash
ernestognw Dec 9, 2024
03d935d
Update Account inheritance order
ernestognw Dec 10, 2024
5456f26
up
ernestognw Dec 10, 2024
9412b65
Merge branch 'master' into aa/accounts
ernestognw Dec 10, 2024
524bf15
Remove ERC1155HolderLean
ernestognw Dec 10, 2024
4718b87
Abstract AccountSignerDomain
ernestognw Dec 10, 2024
c8ad19d
up
ernestognw Dec 10, 2024
ed66cca
Merge branch 'master' into aa/accounts
ernestognw Dec 10, 2024
c41956c
Remove signed hash fn
ernestognw Dec 11, 2024
7e51cd2
Add standalone example of usage
ernestognw Dec 13, 2024
459d594
Merge branch 'master' into aa/accounts
ernestognw Dec 13, 2024
f497fd1
Remove docs
ernestognw Dec 13, 2024
6409cb3
ERC4337 userOp validation should not be 7739 wrapped
Amxx Dec 13, 2024
97b33df
documentation
Amxx Dec 13, 2024
ddd17e9
Rename `_validateSignature` to `_rawSignatureValidation` and remove _…
ernestognw Dec 13, 2024
e8ef6d1
errata
ernestognw Dec 13, 2024
d706876
Default _signableUserOpHash to a typed userop signature
ernestognw Dec 13, 2024
188e71d
Remove docs mocks
ernestognw Dec 13, 2024
6ace1a7
Remove ERC7739 from AccountBase
ernestognw Dec 14, 2024
9793e49
Make ERC7739Signer validations private
ernestognw Dec 14, 2024
9e34432
Move EIP712 userop signing to Accountbase
ernestognw Dec 14, 2024
ae6a665
Split AccountCore / Account
Amxx Dec 16, 2024
07ca067
remove intermediary variable
Amxx Dec 16, 2024
6aa5597
doc
Amxx Dec 16, 2024
0513f3a
spelling
Amxx Dec 16, 2024
e3ce6b5
abstract signer
Amxx Dec 16, 2024
69cc3da
docs
Amxx Dec 16, 2024
d0bd34f
ERC7702 signer
Amxx Dec 16, 2024
3b1c1f4
fix
Amxx Dec 16, 2024
190b5a5
doc example for ERC7739 use signers
Amxx Dec 16, 2024
a6ab43b
Complete minimal documentation
ernestognw Dec 17, 2024
5c210cf
Update CHANGELOG.md
ernestognw Dec 17, 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
8 changes: 8 additions & 0 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module.exports = {
skipFiles: ['mocks'],
istanbulReporter: ['html', 'lcov', 'text-summary'],
// Work around stack too deep for coverage
configureYulOptimizer: true,
solcOptimizerDetails: {
yul: true,
yulDetails: {
optimizerSteps: '',
},
},
};
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## XX-XX-2024

- `AccountECDSA`, `AccountP256` and `AccountRSA`: Add implementations of `AccountBase` based on the available signature schemes (i.e. ECDSA, P256, RSA).
- `AccountBase`: Added a simple ERC-4337 account implementation with the minimal logic to process user operations.
ernestognw marked this conversation as resolved.
Show resolved Hide resolved

## 06-11-2024

- `ERC7739Utils`: Add a library that implements a defensive rehashing mechanism to prevent replayability of smart contract signatures based on the ERC-7739.
Expand Down
19 changes: 19 additions & 0 deletions contracts/account/draft-Account.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {ERC721Holder} from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import {ERC7739Signer} from "../utils/cryptography/draft-ERC7739Signer.sol";
import {AccountCore} from "./draft-AccountCore.sol";

/**
* @dev Extension of {AccountCore} with recommended feature that most account abstraction implementation will want:
*
* * {ERC721Holder} for ERC-721 token handling
* * {ERC1155Holder} for ERC-1155 token handling
* * {ERC7739Signer} for ERC-1271 signature support with ERC-7739 replay protection
*
* NOTE: This needs to be combine with a signer implementation such as {SignerECDSA}, {SignerP256} or {SignerRSA}
*/
abstract contract Account is AccountCore, ERC721Holder, ERC1155Holder, ERC7739Signer {}
164 changes: 164 additions & 0 deletions contracts/account/draft-AccountCore.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {PackedUserOperation, IAccount, IEntryPoint, IAccountExecute} from "@openzeppelin/contracts/interfaces/draft-IERC4337.sol";
import {ERC4337Utils} from "@openzeppelin/contracts/account/utils/draft-ERC4337Utils.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {AbstractSigner} from "../utils/cryptography/AbstractSigner.sol";

/**
* @dev A simple ERC4337 account implementation. This base implementation only includes the minimal logic to process
* user operations.
*
* Developers must implement the {_rawSignatureValidation} function to define the account's validation logic.
*
* IMPORTANT: Implementing a mechanism to validate signatures is a security-sensitive operation as it may allow an
* attacker to bypass the account's security measures. Check out {AccountECDSA}, {AccountP256}, or {AccountRSA} for
* digital signature validation implementations.
*/
abstract contract AccountCore is AbstractSigner, EIP712, IAccount, IAccountExecute {
using MessageHashUtils for bytes32;

bytes32 internal constant _PACKED_USER_OPERATION =
keccak256(
"PackedUserOperation(address sender,uint256 nonce,bytes initCode,bytes callData,bytes32 accountGasLimits,uint256 preVerificationGas,bytes32 gasFees,bytes paymasterAndData,address entrypoint)"
);

/**
* @dev Unauthorized call to the account.
*/
error AccountUnauthorized(address sender);

/**
* @dev Revert if the caller is not the entry point or the account itself.
*/
modifier onlyEntryPointOrSelf() {
_checkEntryPointOrSelf();
_;
}

/**
* @dev Revert if the caller is not the entry point.
*/
modifier onlyEntryPoint() {
_checkEntryPoint();
_;
}

/**
* @dev Canonical entry point for the account that forwards and validates user operations.
*/
function entryPoint() public view virtual returns (IEntryPoint) {
return IEntryPoint(0x0000000071727De22E5E9d8BAf0edAc6f37da032);
}

/**
* @dev Return the account nonce for the canonical sequence.
*/
function getNonce() public view virtual returns (uint256) {
return getNonce(0);
}

/**
* @dev Return the account nonce for a given sequence (key).
*/
function getNonce(uint192 key) public view virtual returns (uint256) {
return entryPoint().getNonce(address(this), key);
}

/**
* @inheritdoc IAccount
*/
function validateUserOp(
PackedUserOperation calldata userOp,
bytes32 userOpHash,
uint256 missingAccountFunds
) public virtual onlyEntryPoint returns (uint256) {
uint256 validationData = _rawSignatureValidation(_signableUserOpHash(userOp, userOpHash), userOp.signature)
? ERC4337Utils.SIG_VALIDATION_SUCCESS
: ERC4337Utils.SIG_VALIDATION_FAILED;
_payPrefund(missingAccountFunds);
return validationData;
}

/**
* @inheritdoc IAccountExecute
*/
function executeUserOp(
PackedUserOperation calldata userOp,
bytes32 /*userOpHash*/
) public virtual onlyEntryPointOrSelf {
(address target, uint256 value, bytes memory data) = abi.decode(userOp.callData[4:], (address, uint256, bytes));
Address.functionCallWithValue(target, data, value);
}

/**
* @dev Returns the digest used by an offchain signer instead of the opaque `userOpHash`.
*
* Given the `userOpHash` calculation is defined by ERC-4337, offchain signers
* may need to sign again this hash by rehashing it with other schemes (e.g. ERC-191).
*
* Returns a typehash following EIP-712 typed data hashing for readability.
*/
function _signableUserOpHash(
PackedUserOperation calldata userOp,
bytes32 /* userOpHash */
) internal view virtual returns (bytes32) {
return
_hashTypedDataV4(
keccak256(
abi.encode(
_PACKED_USER_OPERATION,
userOp.sender,
userOp.nonce,
keccak256(userOp.initCode),
keccak256(userOp.callData),
userOp.accountGasLimits,
userOp.preVerificationGas,
userOp.gasFees,
keccak256(userOp.paymasterAndData),
entryPoint()
)
)
);
}

/**
* @dev Sends the missing funds for executing the user operation to the {entrypoint}.
* The `missingAccountFunds` must be defined by the entrypoint when calling {validateUserOp}.
*/
function _payPrefund(uint256 missingAccountFunds) internal virtual {
if (missingAccountFunds > 0) {
(bool success, ) = payable(msg.sender).call{value: missingAccountFunds}("");
success; // Silence warning. The entrypoint should validate the result.
}
}

/**
* @dev Ensures the caller is the {entrypoint}.
*/
function _checkEntryPoint() internal view virtual {
address sender = msg.sender;
if (sender != address(entryPoint())) {
revert AccountUnauthorized(sender);
}
}

/**
* @dev Ensures the caller is the {entrypoint} or the account itself.
*/
function _checkEntryPointOrSelf() internal view virtual {
address sender = msg.sender;
if (sender != address(this) && sender != address(entryPoint())) {
revert AccountUnauthorized(sender);
}
}

/**
* @dev Receive Ether.
*/
receive() external payable virtual {}
}
19 changes: 19 additions & 0 deletions contracts/account/extensions/AccountSignerERC7702.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {AccountCore} from "../draft-AccountCore.sol";

abstract contract AccountSignerERC7702 is AccountCore {
/**
* @dev Validates the signature using the account's address.
*/
function _rawSignatureValidation(
bytes32 hash,
bytes calldata signature
) internal view virtual override returns (bool) {
(address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(hash, signature);
return address(this) == recovered && err == ECDSA.RecoverError.NoError;
}
}
13 changes: 13 additions & 0 deletions contracts/mocks/CallReceiverMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {CallReceiverMock} from "@openzeppelin/contracts/mocks/CallReceiverMock.sol";

contract CallReceiverMockExtended is CallReceiverMock {
event MockFunctionCalledExtra(address caller, uint256 value);

function mockFunctionExtra() public payable {
emit MockFunctionCalledExtra(msg.sender, msg.value);
}
}
19 changes: 19 additions & 0 deletions contracts/mocks/Create2Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Create2} from "@openzeppelin/contracts/utils/Create2.sol";

contract Create2Mock {
function $deploy(uint256 amount, bytes32 salt, bytes memory bytecode) external returns (address) {
return Create2.deploy(amount, salt, bytecode);
}

function $computeAddress(bytes32 salt, bytes32 bytecodeHash) external view returns (address) {
return Create2.computeAddress(salt, bytecodeHash, address(this));
}

function $computeAddress(bytes32 salt, bytes32 bytecodeHash, address deployer) external pure returns (address) {
return Create2.computeAddress(salt, bytecodeHash, deployer);
}
}
7 changes: 7 additions & 0 deletions contracts/mocks/ERC1155Mock.sol
Copy link
Collaborator

@Amxx Amxx Dec 9, 2024

Choose a reason for hiding this comment

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

Ideally, we would just import what we need, and the exposed plugin would take care of creating the mock.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking we could whitelist some contracts we want to expose directly in the hardhat-exposed configuration. Something like

  exposed: {
    include: [
      'contracts/**/*',
      '@openzeppelin/contracts/token/ERC721/ERC721.sol',
      '@openzeppelin/contracts/token/ERC1155/ERC1155.sol'
    ],
  },

but the plugin don't support that unless we change the config.path.sources to include "@openzeppelin/contracts/".

@frangio do you have any idea what we could do ?

Copy link
Contributor

@frangio frangio Dec 9, 2024

Choose a reason for hiding this comment

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

The simplest reason why the plugin doesn't support this is that it creates contracts-exposed/**/* files for matching contracts/**/* files.

What would be the location of the exposed files created for something under @openzeppelin/?

I'm not opposed to something like this but it requires some design.

Copy link
Collaborator

@Amxx Amxx Dec 9, 2024

Choose a reason for hiding this comment

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

The plugin already deals with that: https://github.com/frangio/hardhat-exposed/blob/master/src/core.ts#L96

From the core tests:

/home/amxx/Work/Repos/hardhat-exposed/contracts-exposed/Imported.sol
/home/amxx/Work/Repos/hardhat-exposed/contracts-exposed/$_/@openzeppelin/contracts/proxy/Clones.sol

Copy link
Collaborator

@Amxx Amxx Dec 9, 2024

Choose a reason for hiding this comment

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

feels to me like this PR would mostly take care of that frangio/hardhat-exposed#32 if this change is applied frangio/hardhat-exposed#32 (comment)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

abstract contract ERC1155Mock is ERC1155 {}
7 changes: 7 additions & 0 deletions contracts/mocks/ERC721Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

abstract contract ERC721Mock is ERC721 {}
20 changes: 0 additions & 20 deletions contracts/mocks/ERC7739SignerMock.sol

This file was deleted.

17 changes: 17 additions & 0 deletions contracts/mocks/account/AccountBaseMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {PackedUserOperation} from "@openzeppelin/contracts/interfaces/draft-IERC4337.sol";
import {ERC4337Utils} from "@openzeppelin/contracts/account/utils/draft-ERC4337Utils.sol";
import {Account} from "../../account/draft-Account.sol";

abstract contract AccountBaseMock is Account {
/// Validates a user operation with a boolean signature.
function _rawSignatureValidation(
bytes32 /* userOpHash */,
bytes calldata signature
) internal pure override returns (bool) {
return bytes1(signature[0:1]) == bytes1(0x01);
}
}
12 changes: 12 additions & 0 deletions contracts/mocks/account/AccountECDSAMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Account} from "../../account/draft-Account.sol";
import {SignerECDSA} from "../../utils/cryptography/SignerECDSA.sol";

abstract contract AccountECDSAMock is Account, SignerECDSA {
constructor(address signerAddr) {
_initializeSigner(signerAddr);
}
}
8 changes: 8 additions & 0 deletions contracts/mocks/account/AccountERC7702Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Account} from "../../account/draft-Account.sol";
import {AccountSignerERC7702} from "../../account/extensions/AccountSignerERC7702.sol";

abstract contract AccountERC7702Mock is Account, AccountSignerERC7702 {}
12 changes: 12 additions & 0 deletions contracts/mocks/account/AccountP256Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Account} from "../../account/draft-Account.sol";
import {SignerP256} from "../../utils/cryptography/SignerP256.sol";

abstract contract AccountP256Mock is Account, SignerP256 {
constructor(bytes32 qx, bytes32 qy) {
_initializeSigner(qx, qy);
}
}
12 changes: 12 additions & 0 deletions contracts/mocks/account/AccountRSAMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {Account} from "../../account/draft-Account.sol";
import {SignerRSA} from "../../utils/cryptography/SignerRSA.sol";

abstract contract AccountRSAMock is Account, SignerRSA {
constructor(bytes memory e, bytes memory n) {
_initializeSigner(e, n);
}
}
21 changes: 0 additions & 21 deletions contracts/mocks/docs/utils/cryptography/ERC7739SignerECDSA.sol

This file was deleted.

Loading
Loading