/
P256AccountFactory.sol
55 lines (49 loc) · 1.65 KB
/
P256AccountFactory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/utils/Create2.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "./P256Account.sol";
import "../../interfaces/ISecp256r1.sol";
contract P256AccountFactory {
P256Account public immutable accountImplementation;
constructor(
IEntryPoint _entryPoint,
ISecp256r1 _validator,
EmailGuardian _guardian
) {
accountImplementation = new P256Account(_entryPoint, _validator, _guardian);
}
function createAccount(bytes calldata publicKey, uint256 salt)
public
returns (P256Account ret)
{
address addr = getAddress(publicKey, salt);
uint256 codeSize = addr.code.length;
if (codeSize > 0) {
return P256Account(payable(addr));
}
ret = P256Account(
payable(
new ERC1967Proxy{salt: bytes32(salt)}(
address(accountImplementation),
abi.encodeCall(P256Account.initialize, publicKey)
)
)
);
}
function getAddress(bytes calldata publicKey, uint256 salt) public view returns (address) {
return
Create2.computeAddress(
bytes32(salt),
keccak256(
abi.encodePacked(
type(ERC1967Proxy).creationCode,
abi.encode(
address(accountImplementation),
abi.encodeCall(P256Account.initialize, publicKey)
)
)
)
);
}
}