smart-contract/contracts/UserWallet.sol

168 lines
3.7 KiB
Solidity
Raw Normal View History

2019-04-06 12:49:08 +00:00
pragma solidity ^0.5.2;
/**
2019-04-03 18:59:32 +00:00
* @title RegistryInterface Interface
*/
2019-04-03 18:59:32 +00:00
interface RegistryInterface {
function logic(address logicAddr) external view returns (bool);
function record(address currentOwner, address nextOwner) external;
}
/**
* @title Address Registry Record
*/
2019-03-18 22:39:43 +00:00
contract AddressRecord {
/**
* @dev address registry of system, logic and wallet addresses
2019-03-18 22:39:43 +00:00
*/
address public registry;
/**
* @dev Throws if the logic is not authorised
*/
modifier logicAuth(address logicAddr) {
require(logicAddr != address(0), "logic-proxy-address-required");
2019-04-07 14:41:57 +00:00
require(RegistryInterface(registry).logic(logicAddr), "logic-not-authorised");
_;
}
}
2019-03-18 22:39:43 +00:00
/**
* @title User Auth
*/
2019-03-18 22:39:43 +00:00
contract UserAuth is AddressRecord {
2019-03-11 12:30:45 +00:00
2019-04-02 23:15:32 +00:00
event LogSetOwner(address indexed owner);
address public owner;
2019-03-09 20:42:05 +00:00
/**
* @dev Throws if not called by owner or contract itself
*/
2019-03-09 20:42:05 +00:00
modifier auth {
require(isAuth(msg.sender), "permission-denied");
2019-03-09 20:42:05 +00:00
_;
}
/**
2019-04-02 23:15:32 +00:00
* @dev sets new owner
*/
2019-04-02 23:15:32 +00:00
function setOwner(address nextOwner) public auth {
2019-04-03 18:59:32 +00:00
RegistryInterface(registry).record(owner, nextOwner);
2019-04-02 23:15:32 +00:00
owner = nextOwner;
emit LogSetOwner(nextOwner);
}
/**
* @dev checks if called by owner or contract itself
* @param src is the address initiating the call
*/
function isAuth(address src) public view returns (bool) {
2019-04-02 23:15:32 +00:00
if (src == owner) {
2019-03-11 12:30:45 +00:00
return true;
2019-04-02 23:15:32 +00:00
} else if (src == address(this)) {
2019-03-19 14:03:31 +00:00
return true;
}
2019-04-07 13:04:51 +00:00
return false;
2019-03-19 14:03:31 +00:00
}
}
2019-03-18 22:39:43 +00:00
/**
* @dev logging the execute events
*/
2019-03-10 07:40:51 +00:00
contract UserNote {
2019-03-11 22:28:00 +00:00
event LogNote(
bytes4 indexed sig,
address indexed guy,
bytes32 indexed foo,
bytes32 bar,
uint wad,
bytes fax
);
2019-03-09 20:42:05 +00:00
modifier note {
bytes32 foo;
bytes32 bar;
assembly {
foo := calldataload(4)
bar := calldataload(36)
}
2019-03-11 12:30:45 +00:00
emit LogNote(
msg.sig,
msg.sender,
foo,
bar,
2019-04-02 23:15:32 +00:00
msg.value,
2019-03-11 12:30:45 +00:00
msg.data
);
2019-03-09 20:42:05 +00:00
_;
}
}
2019-03-11 12:30:45 +00:00
2019-03-18 21:49:39 +00:00
/**
2019-03-18 22:39:43 +00:00
* @title User Owned Contract Wallet
2019-03-18 21:49:39 +00:00
*/
contract UserWallet is UserAuth, UserNote {
2019-03-11 12:30:45 +00:00
2019-04-07 14:41:57 +00:00
event LogExecute(address target, uint srcNum, uint sessionNum);
2019-03-18 21:49:39 +00:00
/**
* @dev sets the "address registry", owner's last activity, owner's active period and initial owner
*/
constructor() public {
2019-03-18 22:39:43 +00:00
registry = msg.sender;
2019-04-02 23:15:32 +00:00
owner = msg.sender;
2019-03-09 20:42:05 +00:00
}
function() external payable {}
2019-03-09 20:42:05 +00:00
2019-03-18 21:49:39 +00:00
/**
2019-04-06 12:49:08 +00:00
* @dev Execute authorised calls via delegate call
2019-03-18 21:49:39 +00:00
* @param _target logic proxy address
* @param _data delegate call data
2019-04-07 14:41:57 +00:00
* @param _src to find the source
* @param _session to find the session
2019-03-18 21:49:39 +00:00
*/
function execute(
address _target,
bytes memory _data,
2019-04-07 14:41:57 +00:00
uint _src,
uint _session
2019-03-18 21:49:39 +00:00
)
public
payable
note
2019-04-02 23:15:32 +00:00
auth
logicAuth(_target)
2019-03-18 21:49:39 +00:00
returns (bytes memory response)
{
2019-03-22 22:25:46 +00:00
emit LogExecute(
_target,
2019-04-07 14:41:57 +00:00
_src,
_session
2019-03-22 22:25:46 +00:00
);
2019-03-18 21:49:39 +00:00
2019-03-09 20:42:05 +00:00
// call contract in current context
assembly {
let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0)
let size := returndatasize
response := mload(0x40)
mstore(0x40, add(response, and(add(add(size, 0x20), 0x1f), not(0x1f))))
mstore(response, size)
returndatacopy(add(response, 0x20), 0, size)
switch iszero(succeeded)
2019-03-11 12:30:45 +00:00
case 1 {
// throw if delegatecall failed
revert(add(response, 0x20), size)
}
2019-03-09 20:42:05 +00:00
}
}
2019-03-19 14:03:31 +00:00
}