-
Notifications
You must be signed in to change notification settings - Fork 12
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
1 parent
70a7699
commit af4f5c9
Showing
18 changed files
with
4,000 additions
and
815 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,323 @@ | ||
// SPDX-License-Identifier:MIT | ||
pragma solidity ^0.6.12; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "@openzeppelin/contracts/cryptography/ECDSA.sol"; | ||
Check warning on line 5 in contracts/factory/BoltzSmartWalletFactory.sol GitHub Actions / lint_and_test
|
||
import "../interfaces/ISmartWalletFactory.sol"; | ||
Check warning on line 6 in contracts/factory/BoltzSmartWalletFactory.sol GitHub Actions / lint_and_test
|
||
import "../utils/RSKAddrValidator.sol"; | ||
Check warning on line 7 in contracts/factory/BoltzSmartWalletFactory.sol GitHub Actions / lint_and_test
|
||
|
||
/* solhint-disable no-inline-assembly */ | ||
/* solhint-disable avoid-low-level-calls */ | ||
|
||
/** | ||
==================================================================================================================================================== | ||
Documentation of the Proxy Code being deployed | ||
==================================================================================================================================================== | ||
A simple proxy that delegates every call to an address. | ||
This ProxyA is the one instantiated per smart wallet, and it will receive the Forwarder as MC. So every call | ||
made to this proxy will end up in MC. | ||
MC is controlled by the same developer who created the factory, and . | ||
For the transaction execution (execute() call), MC will do the signature verification and payment. Then it will | ||
execute the request | ||
==================================================================================================================================================== | ||
PROXY A | ||
==================================================================================================================================================== | ||
Constructor | ||
==================================================================================================================================================== | ||
PC | OPCODE| Mnemonic | Stack [top, bottom] | Comments | ||
---------------------------------------------------------------------------------------------------------------------------------------------------- | ||
0 | 60 2D | PUSH1 2D | [45] | Size of runtime code | ||
2 | 3D | RETURNDATASIZE | [0, 45] | Before any external call, returdatasize = 0 (cheaper than PUSH1 00) | ||
3 | 81 | DUP2 | [45, 0, 45] | | ||
4 | 60 09 | PUSH1 09 | [9, 45, 0, 45] | Size of constructor code | ||
6 | 3D | RETURNDATASIZE | [0, 9, 45, 0, 45] | | ||
7 | 39 | CODECOPY | [0, 45] | Mem[0:44] = address(this).code[9:53] | ||
8 | F3 | RETURN | [] | return Mem[0:44] | ||
==================================================================================================================================================== | ||
Runtime Code | ||
==================================================================================================================================================== | ||
PC | OPCODE| Mnemonic | Stack [top, bottom] | Comments | ||
---------------------------------------------------------------------------------------------------------------------------------- | ||
0 | 36 | CALLDATASIZE | [msg.data.size] | | ||
1 | 3D | RETURNDATASIZE | [0, msg.data.size] | | ||
2 | 3D | RETURNDATASIZE | [0, 0, msg.data.size] | | ||
3 | 37 | CALLDATACOPY | [] | Mem[0:msg.data.size-1] = msg.data[0:msg.data.size-1] | ||
4 | 3D | RETURNDATASIZE | [0] | | ||
5 | 3D | RETURNDATASIZE | [0, 0] | | ||
6 | 3D | RETURNDATASIZE | [0, 0, 0] | | ||
7 | 3D | RETURNDATASIZE | [0, 0, 0, 0] | | ||
8 | 36 | CALLDATASIZE | [msg.data.size, 0, 0, 0, 0] | | ||
9 | 3D | RETURNDATASIZE | [0, msg.data.size, 0, 0, 0, 0] | | ||
10 | 73 MC | PUSH20 MC | [mcAddr,0, msg.data.size, 0, 0, 0, 0] | mcAddr = address of master Copy, injected by factory | ||
31 | 5A | GAS | [rGas, mcAddr,0, msg.data.size, 0, 0, 0, 0] | rGas = remaining gas | ||
32 | F4 | DELEGATECALL | [isSuccess, 0, 0] | isSuccess, Mem[0:0] = address(mcAddr).delegateCall.gas(rGas)(Mem[0:msg.data.size-1]) | ||
33 | 3D | RETURNDATASIZE | [rds, isSuccess, 0, 0] | rds = size of what the logic called returned | ||
34 | 92 | SWAP3 | [0, isSuccess, 0, rds] | | ||
35 | 3D | RETURNDATASIZE | [rds, 0, isSuccess, 0, rds] | | ||
36 | 90 | SWAP1 | [0, rds, isSuccess, 0, rds] | | ||
37 | 80 | DUP1 | [0, 0, rds, isSuccess, 0, rds] | | ||
38 | 3E | RETURNDATACOPY | [isSuccess, 0, rds] | Mem[0:rds-1] = RETURNDATA[0:rds-1] | ||
39 | 60 2B | PUSH1 2B | [43, isSuccess, 0, rds] | | ||
41 | 57 | JUMPI | [0, rds] | if(isSuccess) then jump to PC=43 | ||
42 | FD | REVERT | [] | revert(Mem[0, rds-1]) | ||
43 | 5B | JUMPDEST | [0, rds] | | ||
44 | F3 | RETURN | [] | return(Mem[0, rds-1]) | ||
*/ | ||
|
||
/** Factory of Proxies to the SmartWallet (Forwarder) | ||
*/ | ||
contract BoltzSmartWalletFactory is ISmartWalletFactory { | ||
using ECDSA for bytes32; | ||
|
||
bytes11 private constant _RUNTIME_START = hex"363D3D373D3D3D3D363D73"; | ||
bytes14 private constant _RUNTIME_END = hex"5AF43D923D90803E602B57FD5BF3"; | ||
address public masterCopy; // this is the ForwarderProxy contract that will be proxied | ||
bytes32 public constant DATA_VERSION_HASH = keccak256("2"); | ||
bytes32 public domainSeparator = | ||
keccak256( | ||
abi.encode( | ||
keccak256( | ||
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" | ||
), //hex"8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f", | ||
keccak256("RSK Enveloping Transaction"), //DOMAIN_NAME hex"d41b7f69f4d7734774d21b5548d74704ad02f9f1545db63927a1d58479c576c8" | ||
DATA_VERSION_HASH, | ||
_getChainID(), | ||
address(this) | ||
) | ||
); | ||
|
||
// Nonces of senders, used to prevent replay attacks | ||
mapping(address => uint256) private _nonces; | ||
|
||
/** | ||
* @param forwarderTemplate It implements all the payment and execution needs, | ||
* it pays for the deployment during initialization, and it pays for the transaction | ||
* execution on each execute() call. | ||
*/ | ||
constructor(address forwarderTemplate) public { | ||
masterCopy = forwarderTemplate; | ||
} | ||
|
||
function runtimeCodeHash() external view override returns (bytes32) { | ||
return | ||
keccak256( | ||
abi.encodePacked(_RUNTIME_START, masterCopy, _RUNTIME_END) | ||
); | ||
} | ||
|
||
function nonce(address from) public view override returns (uint256) { | ||
return _nonces[from]; | ||
} | ||
|
||
function createUserSmartWallet( | ||
address owner, | ||
address recoverer, | ||
uint256 index, | ||
bytes calldata sig | ||
) external override { | ||
bytes32 _hash = keccak256( | ||
abi.encodePacked(address(this), owner, recoverer, index) | ||
); | ||
(sig); | ||
bytes32 message = keccak256( | ||
abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash) | ||
); | ||
require( | ||
RSKAddrValidator.safeEquals(message.recover(sig), owner), | ||
"Invalid signature" | ||
); | ||
|
||
//88903efe => initialize(address owner,address tokenAddr,address tokenRecipient,uint256 tokenAmount,uint256 tokenGas,address to,uint256 value,uint256 destinationCallGas,bytes calldata data) | ||
bytes memory initData = abi.encodeWithSelector( | ||
hex"88903efe", | ||
owner, | ||
address(0), // This "gas-funded" call does not pay with tokens | ||
address(0), | ||
0, | ||
0, //No token transfer, | ||
address(0), | ||
0, | ||
0, | ||
"" | ||
); | ||
|
||
_deploy( | ||
getCreationBytecode(), | ||
keccak256( | ||
abi.encodePacked(owner, recoverer, index) // salt | ||
), | ||
initData | ||
); | ||
} | ||
|
||
function relayedUserSmartWalletCreation( | ||
IForwarder.DeployRequest memory req, | ||
bytes32 suffixData, | ||
address feesReceiver, | ||
bytes calldata sig | ||
) external override { | ||
require(msg.sender == req.relayHub, "Invalid caller"); | ||
_verifySig(req, suffixData, sig); | ||
// solhint-disable-next-line not-rely-on-time | ||
require( | ||
req.validUntilTime == 0 || req.validUntilTime > block.timestamp, | ||
"SW: request expired" | ||
); | ||
_nonces[req.from]++; | ||
|
||
//88903efe => initialize(address owner,address tokenAddr,address tokenRecipient,uint256 tokenAmount,uint256 tokenGas,address to,uint256 value,uint256 destinationCallGas,bytes calldata data) | ||
//a9059cbb = transfer(address _to, uint256 _value) public returns (bool success) | ||
/* solhint-disable avoid-tx-origin */ | ||
_deploy( | ||
getCreationBytecode(), | ||
keccak256( | ||
abi.encodePacked(req.from, req.recoverer, req.index) // salt | ||
), | ||
abi.encodeWithSelector( | ||
hex"88903efe", | ||
req.from, | ||
req.tokenContract, | ||
feesReceiver, | ||
req.tokenAmount, | ||
req.tokenGas, | ||
req.to, | ||
req.value, | ||
req.gas, | ||
req.data | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Calculates the Smart Wallet address for an owner EOA | ||
* @param owner - EOA of the owner of the smart wallet | ||
* @param recoverer - Address of that can be used by some contracts to give specific roles to the caller (e.g, a recoverer) | ||
* @param index - Allows to create many addresses for the same owner|recoverer | ||
*/ | ||
function getSmartWalletAddress( | ||
address owner, | ||
address recoverer, | ||
uint256 index | ||
) external view override returns (address) { | ||
return | ||
address( | ||
uint160( | ||
uint256( | ||
keccak256( | ||
abi.encodePacked( | ||
bytes1(0xff), | ||
address(this), | ||
keccak256( | ||
abi.encodePacked(owner, recoverer, index) | ||
), // salt | ||
keccak256(getCreationBytecode()) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
function _deploy( | ||
bytes memory code, | ||
bytes32 salt, | ||
bytes memory initdata | ||
) internal returns (address addr) { | ||
//Deployment of the Smart Wallet | ||
/* solhint-disable-next-line no-inline-assembly */ | ||
assembly { | ||
addr := create2(0, add(code, 0x20), mload(code), salt) | ||
if iszero(extcodesize(addr)) { | ||
revert(0, 0) | ||
} | ||
} | ||
|
||
//Since the init code determines the address of the smart wallet, any initialization | ||
//required is done via the runtime code, to avoid the parameters impacting on the resulting address | ||
(bool success, bytes memory ret) = addr.call(initdata); | ||
|
||
if (!success) { | ||
assembly { | ||
revert(add(ret, 32), mload(ret)) | ||
} | ||
} | ||
|
||
//No info is returned, an event is emitted to inform the new deployment | ||
emit Deployed(addr, uint256(salt)); | ||
} | ||
|
||
// Returns the proxy code to that is deployed on every Smart Wallet creation | ||
function getCreationBytecode() public view override returns (bytes memory) { | ||
//The code to install: constructor, runtime start, master copy, runtime end | ||
return | ||
abi.encodePacked( | ||
hex"602D3D8160093D39F3", | ||
_RUNTIME_START, | ||
masterCopy, | ||
_RUNTIME_END | ||
); | ||
} | ||
|
||
function _getEncoded( | ||
IForwarder.DeployRequest memory req, | ||
bytes32 suffixData | ||
) internal pure returns (bytes memory) { | ||
return | ||
abi.encodePacked( | ||
keccak256( | ||
"RelayRequest(address relayHub,address from,address to,address tokenContract,address recoverer,uint256 value,uint256 gas,uint256 nonce,uint256 tokenAmount,uint256 tokenGas,uint256 validUntilTime,uint256 index,bytes data,RelayData relayData)RelayData(uint256 gasPrice,address feesReceiver,address callForwarder,address callVerifier)" | ||
), | ||
abi.encode( | ||
req.relayHub, | ||
req.from, | ||
req.to, | ||
req.tokenContract, | ||
req.recoverer, | ||
req.value, | ||
req.gas, | ||
req.nonce, | ||
req.tokenAmount, | ||
req.tokenGas, | ||
req.validUntilTime, | ||
req.index, | ||
keccak256(req.data) | ||
), | ||
suffixData | ||
); | ||
} | ||
|
||
function _getChainID() internal pure returns (uint256 id) { | ||
/* solhint-disable no-inline-assembly */ | ||
assembly { | ||
id := chainid() | ||
} | ||
} | ||
|
||
function _verifySig( | ||
IForwarder.DeployRequest memory req, | ||
bytes32 suffixData, | ||
bytes memory sig | ||
) internal view { | ||
//Verify nonce | ||
require(_nonces[req.from] == req.nonce, "nonce mismatch"); | ||
|
||
require( | ||
RSKAddrValidator.safeEquals( | ||
keccak256( | ||
abi.encodePacked( | ||
"\x19\x01", | ||
domainSeparator, | ||
keccak256(_getEncoded(req, suffixData)) | ||
) | ||
).recover(sig), | ||
req.from | ||
), | ||
"Signature mismatch" | ||
); | ||
} | ||
} |
Oops, something went wrong.