-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,69 @@ | ||
pragma solidity >=0.5.0 <0.7.0; | ||
|
||
import { Enum } from "@gnosis.pm/safe-contracts/contracts/common/Enum.sol"; | ||
import { Proxy } from "@gnosis.pm/safe-contracts/contracts/proxies/Proxy.sol"; | ||
import { GnosisSafeProxy } from "@gnosis.pm/safe-contracts/contracts/proxies/GnosisSafeProxy.sol"; | ||
import { GnosisSafe } from "@gnosis.pm/safe-contracts/contracts/GnosisSafe.sol"; | ||
|
||
import { ProxyImplSetter } from "./ProxyImplSetter.sol"; | ||
|
||
contract CPKFactory { | ||
event ProxyCreation(Proxy proxy); | ||
//keccak256( | ||
// "EIP712Domain(address verifyingContract)" | ||
//); | ||
bytes32 private constant DOMAIN_SEPARATOR_TYPEHASH = 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749; | ||
|
||
//keccak256( | ||
// "SafeTx(address to,uint256 value,bytes data,uint8 operation,uint256 safeTxGas,uint256 baseGas,uint256 gasPrice,address gasToken,address refundReceiver,uint256 nonce)" | ||
//); | ||
bytes32 private constant SAFE_TX_TYPEHASH = 0xbb8310d486368db6bd6f849402fdd73ad53d316b5a4b2644ad6efe0f941286d8; | ||
|
||
ProxyImplSetter public proxyImplSetter; | ||
|
||
constructor() public { | ||
proxyImplSetter = new ProxyImplSetter(); | ||
} | ||
|
||
event ProxyCreation(GnosisSafeProxy proxy); | ||
|
||
function proxyCreationCode() external pure returns (bytes memory) { | ||
return type(Proxy).creationCode; | ||
return type(GnosisSafeProxy).creationCode; | ||
} | ||
|
||
function createProxyAndExecTransaction( | ||
address masterCopy, | ||
address owner, | ||
address safeVersion, | ||
uint256 saltNonce, | ||
address fallbackHandler, | ||
address to, | ||
uint256 value, | ||
bytes calldata data, | ||
Enum.Operation operation | ||
bytes calldata execTxCalldata | ||
) | ||
external | ||
payable | ||
returns (bool execTransactionSuccess) | ||
{ | ||
GnosisSafe proxy; | ||
bytes memory deploymentData = abi.encodePacked(type(Proxy).creationCode, abi.encode(masterCopy)); | ||
bytes32 salt = keccak256(abi.encode(msg.sender, saltNonce)); | ||
// solium-disable-next-line security/no-inline-assembly | ||
assembly { | ||
proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt) | ||
bytes32 salt = keccak256(abi.encode(owner, saltNonce)); | ||
|
||
address payable proxy; | ||
{ | ||
bytes memory deploymentData = abi.encodePacked( | ||
type(GnosisSafeProxy).creationCode, | ||
abi.encode(proxyImplSetter) | ||
); | ||
// solium-disable-next-line security/no-inline-assembly | ||
assembly { | ||
proxy := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt) | ||
} | ||
require(proxy != address(0), "create2 call failed"); | ||
} | ||
require(address(proxy) != address(0), "create2 call failed"); | ||
|
||
ProxyImplSetter(proxy).setImplementation(safeVersion); | ||
|
||
{ | ||
address[] memory tmp = new address[](1); | ||
tmp[0] = address(this); | ||
proxy.setup(tmp, 1, address(0), "", fallbackHandler, address(0), 0, address(0)); | ||
tmp[0] = address(owner); | ||
GnosisSafe(proxy).setup(tmp, 1, address(0), "", fallbackHandler, address(0), 0, address(0)); | ||
} | ||
|
||
execTransactionSuccess = proxy.execTransaction(to, value, data, operation, 0, 0, 0, address(0), address(0), | ||
abi.encodePacked(uint(address(this)), uint(0), uint8(1))); | ||
proxy.call.value(msg.value)(execTxCalldata); | ||
|
||
proxy.execTransaction( | ||
address(proxy), 0, | ||
abi.encodeWithSignature("swapOwner(address,address,address)", address(1), address(this), msg.sender), | ||
Enum.Operation.Call, | ||
0, 0, 0, address(0), address(0), | ||
abi.encodePacked(uint(address(this)), uint(0), uint8(1)) | ||
); | ||
|
||
emit ProxyCreation(Proxy(address(proxy))); | ||
} | ||
emit ProxyCreation(GnosisSafeProxy(proxy)); | ||
} | ||
} |
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,57 @@ | ||
pragma solidity >=0.5.0 <0.7.0; | ||
|
||
import { Enum } from "@gnosis.pm/safe-contracts/contracts/common/Enum.sol"; | ||
import { CPKFactory } from "./CPKFactory.sol"; | ||
|
||
contract CPKFactoryFacade { | ||
CPKFactory cpkFactory; | ||
address safeVersion; | ||
uint256 saltNonce; | ||
address fallbackHandler; | ||
|
||
constructor( | ||
CPKFactory _cpkFactory, | ||
address _safeVersion, | ||
uint256 _saltNonce, | ||
address _fallbackHandler | ||
) public { | ||
cpkFactory = _cpkFactory; | ||
safeVersion = _safeVersion; | ||
saltNonce = _saltNonce; | ||
fallbackHandler = _fallbackHandler; | ||
} | ||
|
||
function execTransaction( | ||
address to, | ||
uint256 value, | ||
bytes calldata data, | ||
Enum.Operation operation, | ||
uint256 safeTxGas, | ||
uint256 baseGas, | ||
uint256 gasPrice, | ||
address gasToken, | ||
address payable refundReceiver, | ||
bytes calldata signature | ||
) | ||
external | ||
payable | ||
returns (bool) | ||
{ | ||
address owner; | ||
// solium-disable-next-line security/no-inline-assembly | ||
assembly { | ||
owner := and( | ||
calldataload(add(calldataload(0x124), 0x61)), | ||
0xffffffffffffffffffffffffffffffffffffffff | ||
) | ||
} | ||
|
||
return cpkFactory.createProxyAndExecTransaction.value(msg.value)( | ||
owner, | ||
safeVersion, | ||
saltNonce, | ||
fallbackHandler, | ||
msg.data | ||
); | ||
} | ||
} |
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,9 @@ | ||
pragma solidity >=0.5.0 <0.7.0; | ||
|
||
contract ProxyImplSetter { | ||
address implementation; | ||
|
||
function setImplementation(address _implementation) external { | ||
implementation = _implementation; | ||
} | ||
} |
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