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

feat: add contract creation execution functions #61

Merged
merged 13 commits into from
May 31, 2024
44 changes: 40 additions & 4 deletions src/common/BaseLightAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
error InvalidSignatureType();
error NotAuthorized(address caller);
error ZeroAddressNotAllowed();
error CreateFailed();
Zer0dot marked this conversation as resolved.
Show resolved Hide resolved

modifier onlyAuthorized() {
_onlyAuthorized();
Expand Down Expand Up @@ -75,6 +76,39 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
}
}

/// @notice Creates a contract, this can only be called by this account.
/// @param initCode The initCode to deploy. NOTE: This could be replaced with transient storage in the near future,
/// depending on gas savings, if any.
function create(bytes calldata initCode, uint256 value) external payable virtual onlyAuthorized {
Zer0dot marked this conversation as resolved.
Show resolved Hide resolved
assembly ("memory-safe") {
// Copy the initCode to memory, then deploy the contract
let len := initCode.length
calldatacopy(0, initCode.offset, len)
adamegyed marked this conversation as resolved.
Show resolved Hide resolved
let succ := create(value, 0, len)

// If the creation fails, revert
if iszero(succ) {
mstore(0, 0x7e16b8cd) // CreateFailed()
revert(28, 4)
}
adamegyed marked this conversation as resolved.
Show resolved Hide resolved
}
}

function create2(bytes calldata initCode, bytes32 salt, uint256 value) external payable virtual onlyAuthorized {
assembly ("memory-safe") {
// Copy the initCode to memory, then deploy the contract
let len := initCode.length
calldatacopy(0, initCode.offset, len)
let succ := create2(value, 0, len, salt)
adamegyed marked this conversation as resolved.
Show resolved Hide resolved

// If the creation fails, revert
if iszero(succ) {
mstore(0, 0x7e16b8cd) // CreateFailed()
revert(28, 4)
}
}
}

/// @notice Deposit more funds for this account in the entry point.
function addDeposit() external payable {
entryPoint().depositTo{value: msg.value}(address(this));
Expand Down Expand Up @@ -123,10 +157,12 @@ abstract contract BaseLightAccount is BaseAccount, TokenCallbackHandler, UUPSUpg
}

function _call(address target, uint256 value, bytes memory data) internal {
(bool success, bytes memory result) = target.call{value: value}(data);
if (!success) {
assembly ("memory-safe") {
revert(add(result, 32), mload(result))
assembly ("memory-safe") {
let succ := call(gas(), target, value, add(data, 32), mload(data), 0, 0)
if iszero(succ) {
// We can overwrite memory since we're going to revert out of this call frame anyway
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
adamegyed marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions test/LightAccount.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,53 @@ contract LightAccountTest is Test {
assertEq(initialized, 1);
}

function testRevertCreate_IncorrectCaller() public {
vm.expectRevert(abi.encodeWithSelector(BaseLightAccount.NotAuthorized.selector, address(this)));
account.create(hex"1234", 0);
}

function testRevertCreate_CreateFailed() public {
vm.prank(eoaAddress);
vm.expectRevert(BaseLightAccount.CreateFailed.selector);
account.execute(
address(account),
0,
abi.encodeCall(
account.create,
(hex"3d3dfd", 0) // Attempt to deploy a contract with creation code that reverts.
)
);
}

function testRevertCreate2_IncorrectCaller() public {
vm.expectRevert(abi.encodeWithSelector(BaseLightAccount.NotAuthorized.selector, address(this)));
account.create2(hex"1234", bytes32(0), 0);
}

function testRevertCreate2_CreateFailed() public {
vm.prank(eoaAddress);
vm.expectRevert(BaseLightAccount.CreateFailed.selector);
account.create2(hex"3d3dfd", bytes32(0), 0);
}

function testCreate() public {
vm.prank(eoaAddress);
address expected = vm.computeCreateAddress(address(account), vm.getNonce(address(account)));
account.create(abi.encodePacked(type(LightAccount).creationCode, abi.encode(address(entryPoint))), 0);
assertEq(address(LightAccount(payable(expected)).entryPoint()), address(entryPoint));
}

function testCreate2() public {
vm.prank(eoaAddress);
bytes memory initCode = abi.encodePacked(type(LightAccount).creationCode, abi.encode(address(entryPoint)));
bytes32 initCodeHash = keccak256(initCode);
bytes32 salt = bytes32(hex"04546b");
address expected = vm.computeCreate2Address(salt, initCodeHash, address(account));

account.create2(abi.encodePacked(type(LightAccount).creationCode, abi.encode(address(entryPoint))), salt, 0);
assertEq(address(LightAccount(payable(expected)).entryPoint()), address(entryPoint));
}

function _useContractOwner() internal {
vm.prank(eoaAddress);
account.transferOwnership(address(contractOwner));
Expand Down
Loading