2018-03-23 02:12:12 +00:00
|
|
|
pragma solidity ^0.4.21;
|
2018-03-21 21:02:05 +00:00
|
|
|
|
2018-05-16 15:59:28 +00:00
|
|
|
import "./Identity.sol";
|
2018-04-26 05:09:03 +00:00
|
|
|
import "../common/MessageSigned.sol";
|
2018-03-21 21:02:05 +00:00
|
|
|
import "../token/ERC20Token.sol";
|
|
|
|
|
2018-03-21 22:58:19 +00:00
|
|
|
/**
|
|
|
|
* @title IdentityGasRelay
|
|
|
|
* @author Ricardo Guilherme Schmidt (Status Research & Development GmbH)
|
|
|
|
* @notice enables economic abstraction for Identity
|
|
|
|
*/
|
2018-05-16 15:59:28 +00:00
|
|
|
contract IdentityGasRelay is Identity {
|
2018-03-21 21:02:05 +00:00
|
|
|
|
2018-05-08 23:12:32 +00:00
|
|
|
bytes4 public constant MSG_CALL_PREFIX = bytes4(keccak256("callGasRelay(address,uint256,bytes32,uint256,uint256,address)"));
|
|
|
|
bytes4 public constant MSG_DEPLOY_PREFIX = bytes4(keccak256("deployGasRelay(uint256,bytes32,uint256,uint256,address)"));
|
|
|
|
bytes4 public constant MSG_APPROVEANDCALL_PREFIX = bytes4(keccak256("approveAndCallGasRelay(address,address,uint256,bytes32,uint256,uint256)"));
|
2018-03-21 21:02:05 +00:00
|
|
|
|
2018-05-08 23:12:32 +00:00
|
|
|
event ExecutedGasRelayed(bytes32 messageHash);
|
2018-05-08 22:28:24 +00:00
|
|
|
event ContractDeployed(address deployedAddress);
|
2018-03-21 21:02:05 +00:00
|
|
|
|
2018-03-21 22:58:19 +00:00
|
|
|
/**
|
2018-05-08 23:12:32 +00:00
|
|
|
* @param _messageHash that is signed
|
2018-05-16 15:49:09 +00:00
|
|
|
* @param _requiredPurpose key purpose for this type of call
|
2018-03-21 22:58:19 +00:00
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
2018-04-17 14:18:23 +00:00
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
2018-03-21 22:58:19 +00:00
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
2018-05-08 23:12:32 +00:00
|
|
|
* @param _messageSignatures rsv concatenated ethereum signed message signatures required
|
2018-03-21 22:58:19 +00:00
|
|
|
*/
|
2018-05-16 15:59:28 +00:00
|
|
|
modifier executeGasRelayed (
|
2018-05-08 23:12:32 +00:00
|
|
|
bytes32 _messageHash,
|
|
|
|
uint256 _requiredPurpose,
|
2018-03-21 21:02:05 +00:00
|
|
|
uint _nonce,
|
|
|
|
uint _gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
uint _gasLimit,
|
2018-03-23 02:12:12 +00:00
|
|
|
address _gasToken,
|
|
|
|
bytes _messageSignatures
|
2018-05-08 23:12:32 +00:00
|
|
|
) {
|
|
|
|
//query current gas available
|
|
|
|
uint startGas = gasleft();
|
|
|
|
|
2018-03-23 02:12:12 +00:00
|
|
|
//verify transaction parameters
|
2018-04-18 01:24:45 +00:00
|
|
|
require(startGas >= _gasLimit);
|
2018-03-22 16:09:02 +00:00
|
|
|
require(_nonce == nonce);
|
2018-03-23 02:12:12 +00:00
|
|
|
|
|
|
|
//verify if signatures are valid and came from correct actors;
|
2018-03-23 02:31:20 +00:00
|
|
|
verifySignatures(
|
2018-05-08 23:12:32 +00:00
|
|
|
_requiredPurpose,
|
|
|
|
_messageHash,
|
2018-03-23 02:31:20 +00:00
|
|
|
_messageSignatures
|
|
|
|
);
|
2018-03-23 02:12:12 +00:00
|
|
|
|
2018-05-08 23:12:32 +00:00
|
|
|
//increase nonce
|
2018-03-23 02:12:12 +00:00
|
|
|
nonce++;
|
2018-05-08 23:12:32 +00:00
|
|
|
|
|
|
|
//executes transaction
|
|
|
|
_;
|
|
|
|
|
2018-03-23 02:12:12 +00:00
|
|
|
//refund gas used using contract held ERC20 tokens or ETH
|
|
|
|
if (_gasPrice > 0) {
|
|
|
|
uint256 _amount = 21000 + (startGas - gasleft());
|
|
|
|
_amount = _amount * _gasPrice;
|
|
|
|
if (_gasToken == address(0)) {
|
|
|
|
address(msg.sender).transfer(_amount);
|
|
|
|
} else {
|
|
|
|
ERC20Token(_gasToken).transfer(msg.sender, _amount);
|
|
|
|
}
|
2018-05-08 23:12:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 21:02:05 +00:00
|
|
|
|
2018-05-17 20:29:27 +00:00
|
|
|
constructor(
|
|
|
|
bytes32[] _keys,
|
|
|
|
uint256[] _purposes,
|
|
|
|
uint256[] _types,
|
|
|
|
uint256 _managerThreshold,
|
|
|
|
uint256 _actorThreshold,
|
|
|
|
address _recoveryContract
|
|
|
|
)
|
|
|
|
Identity(
|
|
|
|
_keys,
|
|
|
|
_purposes,
|
|
|
|
_types,
|
|
|
|
_managerThreshold,
|
|
|
|
_actorThreshold,
|
|
|
|
_recoveryContract
|
|
|
|
)
|
|
|
|
public
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:58:19 +00:00
|
|
|
/**
|
2018-03-24 04:26:06 +00:00
|
|
|
* @notice include ethereum signed callHash in return of gas proportional amount multiplied by `_gasPrice` of `_gasToken`
|
2018-03-21 22:58:19 +00:00
|
|
|
* allows identity of being controlled without requiring ether in key balace
|
|
|
|
* @param _to destination of call
|
|
|
|
* @param _value call value (ether)
|
|
|
|
* @param _data call data
|
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
2018-04-17 14:18:23 +00:00
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
2018-03-21 22:58:19 +00:00
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
2018-03-23 02:12:12 +00:00
|
|
|
* @param _messageSignatures rsv concatenated ethereum signed message signatures required
|
2018-03-21 22:58:19 +00:00
|
|
|
*/
|
|
|
|
function callGasRelayed(
|
2018-03-21 21:02:05 +00:00
|
|
|
address _to,
|
|
|
|
uint256 _value,
|
|
|
|
bytes _data,
|
|
|
|
uint _nonce,
|
|
|
|
uint _gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
uint _gasLimit,
|
2018-03-23 02:12:12 +00:00
|
|
|
address _gasToken,
|
|
|
|
bytes _messageSignatures
|
2018-03-21 21:02:05 +00:00
|
|
|
)
|
2018-03-23 02:12:12 +00:00
|
|
|
external
|
2018-05-17 20:29:27 +00:00
|
|
|
returns (bool success)
|
|
|
|
{
|
|
|
|
|
|
|
|
//query current gas available
|
|
|
|
uint startGas = gasleft();
|
|
|
|
|
|
|
|
//verify transaction parameters
|
2018-09-01 04:44:14 +00:00
|
|
|
//require(startGas >= _gasLimit); // TODO: Tune this
|
2018-05-17 20:29:27 +00:00
|
|
|
require(_nonce == nonce);
|
|
|
|
|
|
|
|
//verify if signatures are valid and came from correct actors;
|
|
|
|
verifySignatures(
|
|
|
|
_to == address(this) ? MANAGEMENT_KEY : ACTION_KEY,
|
2018-03-23 02:12:12 +00:00
|
|
|
callGasRelayHash(
|
2018-03-21 21:02:05 +00:00
|
|
|
_to,
|
|
|
|
_value,
|
|
|
|
keccak256(_data),
|
|
|
|
_nonce,
|
|
|
|
_gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
_gasLimit,
|
2018-03-21 21:02:05 +00:00
|
|
|
_gasToken
|
2018-05-17 20:29:27 +00:00
|
|
|
),
|
2018-03-23 02:31:20 +00:00
|
|
|
_messageSignatures
|
2018-05-17 20:29:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
//increase nonce
|
|
|
|
nonce++;
|
|
|
|
|
|
|
|
//executes transaction
|
2018-05-16 15:59:28 +00:00
|
|
|
success = _commitCall(_nonce, _to, _value, _data);
|
2018-05-17 20:29:27 +00:00
|
|
|
|
|
|
|
//refund gas used using contract held ERC20 tokens or ETH
|
|
|
|
if (_gasPrice > 0) {
|
|
|
|
uint256 _amount = 21000 + (startGas - gasleft());
|
|
|
|
_amount = _amount * _gasPrice;
|
|
|
|
if (_gasToken == address(0)) {
|
|
|
|
address(msg.sender).transfer(_amount);
|
|
|
|
} else {
|
|
|
|
ERC20Token(_gasToken).transfer(msg.sender, _amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 21:02:05 +00:00
|
|
|
}
|
2018-03-23 02:31:20 +00:00
|
|
|
|
2018-05-08 22:28:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice deploys contract in return of gas proportional amount multiplied by `_gasPrice` of `_gasToken`
|
|
|
|
* allows identity of being controlled without requiring ether in key balace
|
|
|
|
* @param _value call value (ether) to be sent to newly created contract
|
|
|
|
* @param _data contract code data
|
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
|
|
|
* @param _messageSignatures rsv concatenated ethereum signed message signatures required
|
|
|
|
*/
|
|
|
|
function deployGasRelayed(
|
|
|
|
uint256 _value,
|
|
|
|
bytes _data,
|
|
|
|
uint _nonce,
|
|
|
|
uint _gasPrice,
|
|
|
|
uint _gasLimit,
|
|
|
|
address _gasToken,
|
|
|
|
bytes _messageSignatures
|
|
|
|
)
|
|
|
|
external
|
2018-05-17 20:29:27 +00:00
|
|
|
returns(address deployedAddress)
|
|
|
|
{
|
|
|
|
|
|
|
|
//query current gas available
|
|
|
|
uint startGas = gasleft();
|
|
|
|
|
|
|
|
//verify transaction parameters
|
|
|
|
require(startGas >= _gasLimit);
|
|
|
|
require(_nonce == nonce);
|
|
|
|
|
|
|
|
//verify if signatures are valid and came from correct actors;
|
|
|
|
verifySignatures(
|
|
|
|
ACTION_KEY,
|
2018-05-08 22:28:24 +00:00
|
|
|
deployGasRelayHash(
|
|
|
|
_value,
|
|
|
|
keccak256(_data),
|
|
|
|
_nonce,
|
|
|
|
_gasPrice,
|
|
|
|
_gasLimit,
|
|
|
|
_gasToken
|
2018-05-17 20:29:27 +00:00
|
|
|
),
|
2018-05-08 22:28:24 +00:00
|
|
|
_messageSignatures
|
2018-05-17 20:29:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
//increase nonce
|
2018-05-16 15:59:28 +00:00
|
|
|
nonce++;
|
2018-05-17 20:29:27 +00:00
|
|
|
|
2018-05-08 22:28:24 +00:00
|
|
|
deployedAddress = doCreate(_value, _data);
|
2018-05-17 20:29:27 +00:00
|
|
|
emit ContractDeployed(deployedAddress);
|
|
|
|
|
|
|
|
//refund gas used using contract held ERC20 tokens or ETH
|
|
|
|
if (_gasPrice > 0) {
|
|
|
|
uint256 _amount = 21000 + (startGas - gasleft());
|
|
|
|
_amount = _amount * _gasPrice;
|
|
|
|
if (_gasToken == address(0)) {
|
|
|
|
address(msg.sender).transfer(_amount);
|
|
|
|
} else {
|
|
|
|
ERC20Token(_gasToken).transfer(msg.sender, _amount);
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 21:02:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 22:28:24 +00:00
|
|
|
|
2018-03-24 04:26:06 +00:00
|
|
|
/**
|
|
|
|
* @notice include ethereum signed approve ERC20 and call hash
|
|
|
|
* (`ERC20Token(baseToken).approve(_to, _value)` + `_to.call(_data)`).
|
|
|
|
* in return of gas proportional amount multiplied by `_gasPrice` of `_gasToken`
|
|
|
|
* fixes race condition in double transaction for ERC20.
|
|
|
|
* @param _baseToken token approved for `_to`
|
|
|
|
* @param _to destination of call
|
2018-05-16 15:59:28 +00:00
|
|
|
* @param _value call value (in `_baseToken`)
|
2018-03-24 04:26:06 +00:00
|
|
|
* @param _data call data
|
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
2018-04-17 14:18:23 +00:00
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
2018-03-24 04:26:06 +00:00
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
|
|
|
* @param _messageSignatures rsv concatenated ethereum signed message signatures required
|
|
|
|
*/
|
|
|
|
function approveAndCallGasRelayed(
|
|
|
|
address _baseToken,
|
|
|
|
address _to,
|
|
|
|
uint256 _value,
|
|
|
|
bytes _data,
|
|
|
|
uint _nonce,
|
|
|
|
uint _gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
uint _gasLimit,
|
2018-03-24 04:26:06 +00:00
|
|
|
address _gasToken,
|
|
|
|
bytes _messageSignatures
|
|
|
|
)
|
|
|
|
external
|
2018-05-17 20:29:27 +00:00
|
|
|
returns(bool success)
|
|
|
|
{
|
|
|
|
|
|
|
|
//query current gas available
|
|
|
|
uint startGas = gasleft();
|
|
|
|
|
|
|
|
//verify transaction parameters
|
2018-09-01 04:44:14 +00:00
|
|
|
// require(startGas >= _gasLimit); // TODO: tune this
|
2018-05-17 20:29:27 +00:00
|
|
|
require(_nonce == nonce);
|
|
|
|
|
|
|
|
//verify if signatures are valid and came from correct actors;
|
|
|
|
verifySignatures(
|
|
|
|
ACTION_KEY,
|
|
|
|
deployGasRelayHash(
|
2018-03-24 04:26:06 +00:00
|
|
|
_value,
|
|
|
|
keccak256(_data),
|
|
|
|
_nonce,
|
|
|
|
_gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
_gasLimit,
|
2018-05-17 20:29:27 +00:00
|
|
|
_gasToken
|
|
|
|
),
|
2018-03-24 04:26:06 +00:00
|
|
|
_messageSignatures
|
2018-05-17 20:29:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
//increase nonce
|
|
|
|
nonce++;
|
|
|
|
|
2018-05-08 23:12:32 +00:00
|
|
|
require(_baseToken != address(0)); //_baseToken should be something!
|
|
|
|
require(_to != address(this)); //no management with approveAndCall
|
2018-05-16 15:59:28 +00:00
|
|
|
require(_to != address(0)); //need valid destination
|
2018-05-08 23:12:32 +00:00
|
|
|
ERC20Token(_baseToken).approve(_to, _value);
|
2018-05-16 15:59:28 +00:00
|
|
|
success = _commitCall(_nonce, _to, 0, _data);
|
2018-05-17 20:29:27 +00:00
|
|
|
|
|
|
|
//refund gas used using contract held ERC20 tokens or ETH
|
|
|
|
if (_gasPrice > 0) {
|
|
|
|
uint256 _amount = 21000 + (startGas - gasleft());
|
|
|
|
_amount = _amount * _gasPrice;
|
|
|
|
if (_gasToken == address(0)) {
|
|
|
|
address(msg.sender).transfer(_amount);
|
|
|
|
} else {
|
|
|
|
ERC20Token(_gasToken).transfer(msg.sender, _amount);
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 04:26:06 +00:00
|
|
|
}
|
2018-03-23 02:12:12 +00:00
|
|
|
|
2018-03-21 22:58:19 +00:00
|
|
|
/**
|
2018-03-23 02:12:12 +00:00
|
|
|
* @notice reverts if signatures are not valid for the signed hash and required key type.
|
|
|
|
* @param _requiredKey key required to call, if _to from payload is the identity itself, is `MANAGEMENT_KEY`, else `ACTION_KEY`
|
2018-05-08 23:12:32 +00:00
|
|
|
* @param _messageHash message hash provided for the payload
|
|
|
|
* @param _messageSignatures ethereum signed `getSignHash(_messageHash)` message
|
2018-03-23 02:12:12 +00:00
|
|
|
* @return true case valid
|
|
|
|
*/
|
|
|
|
function verifySignatures(
|
|
|
|
uint256 _requiredKey,
|
2018-05-08 23:12:32 +00:00
|
|
|
bytes32 _messageHash,
|
2018-03-21 21:02:05 +00:00
|
|
|
bytes _messageSignatures
|
|
|
|
)
|
2018-03-23 02:12:12 +00:00
|
|
|
public
|
|
|
|
view
|
|
|
|
returns(bool)
|
2018-03-21 21:02:05 +00:00
|
|
|
{
|
2018-05-08 23:12:32 +00:00
|
|
|
// calculates signHash
|
|
|
|
bytes32 signHash = getSignHash(_messageHash);
|
2018-05-02 15:57:56 +00:00
|
|
|
uint _amountSignatures = _messageSignatures.length / 65;
|
2018-04-18 01:24:45 +00:00
|
|
|
require(_amountSignatures == purposeThreshold[_requiredKey]);
|
2018-03-23 02:12:12 +00:00
|
|
|
bytes32 _lastKey = 0;
|
|
|
|
for (uint256 i = 0; i < _amountSignatures; i++) {
|
|
|
|
bytes32 _currentKey = recoverKey(
|
2018-05-08 23:12:32 +00:00
|
|
|
signHash,
|
2018-03-23 02:12:12 +00:00
|
|
|
_messageSignatures,
|
|
|
|
i
|
|
|
|
);
|
|
|
|
require(_currentKey > _lastKey); //assert keys are different
|
2018-05-16 15:59:28 +00:00
|
|
|
require(keyHasPurpose(_currentKey, _requiredKey));
|
2018-03-23 02:12:12 +00:00
|
|
|
_lastKey = _currentKey;
|
|
|
|
}
|
|
|
|
return true;
|
2018-03-21 21:02:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 02:12:12 +00:00
|
|
|
|
2018-03-21 22:58:19 +00:00
|
|
|
/**
|
|
|
|
* @notice get callHash
|
|
|
|
* @param _to destination of call
|
|
|
|
* @param _value call value (ether)
|
2018-03-22 01:13:51 +00:00
|
|
|
* @param _dataHash call data hash
|
2018-03-21 22:58:19 +00:00
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
2018-04-17 14:18:23 +00:00
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
2018-03-21 22:58:19 +00:00
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
2018-03-23 02:12:12 +00:00
|
|
|
* @return callGasRelayHash the hash to be signed by wallet
|
2018-03-21 22:58:19 +00:00
|
|
|
*/
|
2018-03-23 02:12:12 +00:00
|
|
|
function callGasRelayHash(
|
2018-03-21 21:02:05 +00:00
|
|
|
address _to,
|
|
|
|
uint256 _value,
|
|
|
|
bytes32 _dataHash,
|
|
|
|
uint _nonce,
|
|
|
|
uint256 _gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
uint256 _gasLimit,
|
2018-03-21 21:02:05 +00:00
|
|
|
address _gasToken
|
|
|
|
)
|
|
|
|
public
|
|
|
|
view
|
2018-03-23 02:12:12 +00:00
|
|
|
returns (bytes32 _callGasRelayHash)
|
2018-03-21 21:02:05 +00:00
|
|
|
{
|
2018-03-23 02:12:12 +00:00
|
|
|
_callGasRelayHash = keccak256(
|
2018-03-21 21:02:05 +00:00
|
|
|
address(this),
|
2018-05-08 23:12:32 +00:00
|
|
|
MSG_CALL_PREFIX,
|
2018-03-21 21:02:05 +00:00
|
|
|
_to,
|
|
|
|
_value,
|
|
|
|
_dataHash,
|
|
|
|
_nonce,
|
|
|
|
_gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
_gasLimit,
|
2018-03-21 21:02:05 +00:00
|
|
|
_gasToken
|
|
|
|
);
|
|
|
|
}
|
2018-03-23 02:12:12 +00:00
|
|
|
|
2018-05-08 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* @notice get callHash
|
|
|
|
* @param _value call value (ether)
|
|
|
|
* @param _dataHash call data hash
|
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
|
|
|
* @return callGasRelayHash the hash to be signed by wallet
|
|
|
|
*/
|
|
|
|
function deployGasRelayHash(
|
|
|
|
uint256 _value,
|
|
|
|
bytes32 _dataHash,
|
|
|
|
uint256 _nonce,
|
|
|
|
uint256 _gasPrice,
|
|
|
|
uint256 _gasLimit,
|
|
|
|
address _gasToken
|
|
|
|
)
|
|
|
|
public
|
|
|
|
view
|
|
|
|
returns (bytes32 _callGasRelayHash)
|
|
|
|
{
|
|
|
|
_callGasRelayHash = keccak256(
|
|
|
|
address(this),
|
2018-05-08 23:12:32 +00:00
|
|
|
MSG_DEPLOY_PREFIX,
|
2018-05-08 22:28:24 +00:00
|
|
|
_value,
|
|
|
|
_dataHash,
|
|
|
|
_nonce,
|
|
|
|
_gasPrice,
|
|
|
|
_gasLimit,
|
|
|
|
_gasToken
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-24 04:26:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @notice get callHash
|
|
|
|
* @param _to destination of call
|
|
|
|
* @param _value call value (ether)
|
|
|
|
* @param _dataHash call data hash
|
|
|
|
* @param _nonce current identity nonce
|
|
|
|
* @param _gasPrice price in SNT paid back to msg.sender for each gas unit used
|
2018-04-17 14:18:23 +00:00
|
|
|
* @param _gasLimit minimal gasLimit required to execute this call
|
2018-03-24 04:26:06 +00:00
|
|
|
* @param _gasToken token being used for paying `msg.sender`
|
|
|
|
* @return callGasRelayHash the hash to be signed by wallet
|
|
|
|
*/
|
|
|
|
function approveAndCallGasRelayHash(
|
|
|
|
address _baseToken,
|
|
|
|
address _to,
|
|
|
|
uint256 _value,
|
|
|
|
bytes32 _dataHash,
|
|
|
|
uint _nonce,
|
|
|
|
uint256 _gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
uint256 _gasLimit,
|
2018-03-24 04:26:06 +00:00
|
|
|
address _gasToken
|
|
|
|
)
|
|
|
|
public
|
|
|
|
view
|
|
|
|
returns (bytes32 _callGasRelayHash)
|
|
|
|
{
|
|
|
|
_callGasRelayHash = keccak256(
|
|
|
|
address(this),
|
2018-05-08 23:12:32 +00:00
|
|
|
MSG_APPROVEANDCALL_PREFIX,
|
2018-03-24 04:26:06 +00:00
|
|
|
_baseToken,
|
|
|
|
_to,
|
|
|
|
_value,
|
|
|
|
_dataHash,
|
|
|
|
_nonce,
|
|
|
|
_gasPrice,
|
2018-04-17 14:18:23 +00:00
|
|
|
_gasLimit,
|
2018-03-24 04:26:06 +00:00
|
|
|
_gasToken
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-23 02:12:12 +00:00
|
|
|
/**
|
2018-04-26 05:09:03 +00:00
|
|
|
* @notice recovers key who signed the message
|
2018-03-21 22:58:19 +00:00
|
|
|
* @param _signHash operation ethereum signed message hash
|
|
|
|
* @param _messageSignature message `_signHash` signature
|
|
|
|
* @param _pos which signature to read
|
|
|
|
*/
|
2018-03-21 21:02:05 +00:00
|
|
|
function recoverKey (
|
|
|
|
bytes32 _signHash,
|
|
|
|
bytes _messageSignature,
|
|
|
|
uint256 _pos
|
|
|
|
)
|
|
|
|
pure
|
|
|
|
public
|
|
|
|
returns(bytes32)
|
|
|
|
{
|
|
|
|
uint8 v;
|
|
|
|
bytes32 r;
|
|
|
|
bytes32 s;
|
|
|
|
(v,r,s) = signatureSplit(_messageSignature, _pos);
|
2018-04-23 11:14:21 +00:00
|
|
|
return keccak256(
|
2018-03-21 21:02:05 +00:00
|
|
|
ecrecover(
|
|
|
|
_signHash,
|
|
|
|
v,
|
|
|
|
r,
|
|
|
|
s
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 22:58:19 +00:00
|
|
|
* @dev divides bytes signature into `uint8 v, bytes32 r, bytes32 s`
|
|
|
|
* @param _pos which signature to read
|
|
|
|
* @param _signatures concatenated vrs signatures
|
2018-03-21 21:02:05 +00:00
|
|
|
*/
|
2018-03-21 22:58:19 +00:00
|
|
|
function signatureSplit(bytes _signatures, uint256 _pos)
|
2018-03-21 21:02:05 +00:00
|
|
|
pure
|
2018-04-26 05:09:03 +00:00
|
|
|
internal
|
2018-03-21 21:02:05 +00:00
|
|
|
returns (uint8 v, bytes32 r, bytes32 s)
|
|
|
|
{
|
|
|
|
uint pos = _pos + 1;
|
|
|
|
// The signature format is a compact form of:
|
|
|
|
// {bytes32 r}{bytes32 s}{uint8 v}
|
|
|
|
// Compact means, uint8 is not padded to 32 bytes.
|
|
|
|
assembly {
|
2018-03-21 22:58:19 +00:00
|
|
|
r := mload(add(_signatures, mul(32,pos)))
|
|
|
|
s := mload(add(_signatures, mul(64,pos)))
|
2018-03-21 21:02:05 +00:00
|
|
|
// Here we are loading the last 32 bytes, including 31 bytes
|
|
|
|
// of 's'. There is no 'mload8' to do this.
|
|
|
|
//
|
|
|
|
// 'byte' is not working due to the Solidity parser, so lets
|
|
|
|
// use the second best option, 'and'
|
2018-03-21 22:58:19 +00:00
|
|
|
v := and(mload(add(_signatures, mul(65,pos))), 0xff)
|
2018-03-21 21:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require(v == 27 || v == 28);
|
|
|
|
}
|
2018-03-24 04:26:06 +00:00
|
|
|
|
2018-05-08 22:28:24 +00:00
|
|
|
/**
|
|
|
|
* @notice creates new contract based on input `_code` and transfer `_value` ETH to this instance
|
|
|
|
* @param _value amount ether in wei to sent to deployed address at its initialization
|
2018-05-16 15:49:09 +00:00
|
|
|
* @param _code contract code
|
2018-05-08 22:28:24 +00:00
|
|
|
*/
|
|
|
|
function doCreate(
|
|
|
|
uint _value,
|
|
|
|
bytes _code
|
|
|
|
)
|
|
|
|
internal
|
|
|
|
returns (address createdContract)
|
2018-03-24 04:26:06 +00:00
|
|
|
{
|
2018-05-08 22:28:24 +00:00
|
|
|
bool failed;
|
|
|
|
assembly {
|
|
|
|
createdContract := create(_value, add(_code, 0x20), mload(_code))
|
|
|
|
failed := iszero(extcodesize(createdContract))
|
|
|
|
}
|
|
|
|
require(!failed);
|
2018-03-24 04:26:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 15:59:28 +00:00
|
|
|
}
|