-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to be committed: modified: contracts/core/HCHelper.sol modified: contracts/samples/HybridAccount.sol new file: contracts/samples/HybridAccountFactory.sol
- Loading branch information
1 parent
a0305bf
commit 134dee9
Showing
3 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity ^0.8.12; | ||
|
||
import "@openzeppelin/contracts/utils/Create2.sol"; | ||
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
|
||
import "./HybridAccount.sol"; | ||
|
||
/** | ||
* A sample factory contract for HybridAccount | ||
* A UserOperations "initCode" holds the address of the factory, and a method call (to createAccount, in this sample factory). | ||
* The factory's createAccount returns the target account address even if it is already installed. | ||
* This way, the entryPoint.getSenderAddress() can be called either before or after the account is created. | ||
*/ | ||
contract HybridAccountFactory { | ||
HybridAccount public immutable accountImplementation; | ||
address public Helper; | ||
|
||
constructor(IEntryPoint _entryPoint, address _helper) { | ||
accountImplementation = new HybridAccount(_entryPoint, _helper); | ||
Helper = _helper; | ||
} | ||
|
||
/** | ||
* create an account, and return its address. | ||
* returns the address even if the account is already deployed. | ||
* Note that during UserOperation execution, this method is called only if the account is not deployed. | ||
* This method returns an existing account address so that entryPoint.getSenderAddress() would work even after account creation | ||
*/ | ||
function createAccount(address owner,uint256 salt) public returns (HybridAccount ret) { | ||
address addr = getAddress(owner, salt); | ||
uint codeSize = addr.code.length; | ||
if (codeSize > 0) { | ||
return HybridAccount(payable(addr)); | ||
} | ||
ret = HybridAccount(payable(new ERC1967Proxy{salt : bytes32(salt)}( | ||
address(accountImplementation), | ||
abi.encodeCall(HybridAccount.initialize, (owner)) | ||
))); | ||
} | ||
|
||
/** | ||
* calculate the counterfactual address of this account as it would be returned by createAccount() | ||
*/ | ||
function getAddress(address owner,uint256 salt) public view returns (address) { | ||
return Create2.computeAddress(bytes32(salt), keccak256(abi.encodePacked( | ||
type(ERC1967Proxy).creationCode, | ||
abi.encode( | ||
address(accountImplementation), | ||
abi.encodeCall(HybridAccount.initialize, (owner)) | ||
) | ||
))); | ||
} | ||
} |