Skip to content

Commit

Permalink
refactor: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Feb 2, 2024
1 parent 70a7699 commit af4f5c9
Show file tree
Hide file tree
Showing 18 changed files with 4,000 additions and 815 deletions.
5 changes: 2 additions & 3 deletions contracts/RelayHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ contract RelayHub is IRelayHub {
function deployCall(
EnvelopingTypes.DeployRequest calldata deployRequest,
bytes calldata signature
) external override returns (bool destinationCallSuccess) {
) external override {
(signature);

bytes32 managerEntry = workerToManager[msg.sender];
Expand All @@ -164,8 +164,7 @@ contract RelayHub is IRelayHub {
"Invalid gas price"
);

bytes memory ret;
(destinationCallSuccess, ret) = Eip712Library.deploy(
(bool destinationCallSuccess, bytes memory ret) = Eip712Library.deploy(
deployRequest,
signature
);
Expand Down
323 changes: 323 additions & 0 deletions contracts/factory/BoltzSmartWalletFactory.sol
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

View workflow job for this annotation

GitHub Actions / lint_and_test

global import of path @openzeppelin/contracts/cryptography/ECDSA.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "../interfaces/ISmartWalletFactory.sol";

Check warning on line 6 in contracts/factory/BoltzSmartWalletFactory.sol

View workflow job for this annotation

GitHub Actions / lint_and_test

global import of path ../interfaces/ISmartWalletFactory.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "../utils/RSKAddrValidator.sol";

Check warning on line 7 in contracts/factory/BoltzSmartWalletFactory.sol

View workflow job for this annotation

GitHub Actions / lint_and_test

global import of path ../utils/RSKAddrValidator.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

/* 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,

Check warning on line 168 in contracts/factory/BoltzSmartWalletFactory.sol

View workflow job for this annotation

GitHub Actions / lint_and_test

Avoid making time-based decisions in your business logic
"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"
);
}
}
Loading

0 comments on commit af4f5c9

Please sign in to comment.