2019-03-11 12:30:45 +00:00
|
|
|
pragma solidity ^0.5.0;
|
2019-03-09 20:42:05 +00:00
|
|
|
|
2019-03-10 08:38:03 +00:00
|
|
|
|
2019-03-18 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* @dev because math is not safe
|
|
|
|
*/
|
2019-03-10 19:38:53 +00:00
|
|
|
library SafeMath {
|
|
|
|
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
|
uint256 c = a + b;
|
|
|
|
require(c >= a, "math-not-safe");
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @title AddressRegistryInterface Interface
|
|
|
|
*/
|
|
|
|
interface AddressRegistryInterface {
|
2019-03-19 14:03:31 +00:00
|
|
|
function isLogicAuth(address logicAddr) external view returns (bool, bool);
|
2019-03-18 22:39:43 +00:00
|
|
|
function updateProxyRecord(address currentOwner, address nextOwner) external;
|
2019-03-24 21:49:14 +00:00
|
|
|
function guardianEnabled() external view returns (bool);
|
|
|
|
function managerEnabled() external view returns (bool);
|
2019-03-18 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 19:38:53 +00:00
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
2019-03-19 20:20:40 +00:00
|
|
|
* @title Address Registry Record
|
2019-03-18 21:14:58 +00:00
|
|
|
*/
|
2019-03-18 22:39:43 +00:00
|
|
|
contract AddressRecord {
|
|
|
|
|
|
|
|
/**
|
2019-03-19 20:20:40 +00:00
|
|
|
* @dev address registry of system, logic and wallet addresses
|
2019-03-18 22:39:43 +00:00
|
|
|
*/
|
|
|
|
address public registry;
|
2019-03-24 21:49:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param logicAddr is the logic proxy contract address
|
|
|
|
* @return the true boolean for logic proxy if authorised otherwise false
|
|
|
|
*/
|
|
|
|
function isLogicAuthorised(address logicAddr) public view returns (bool, bool) {
|
|
|
|
AddressRegistryInterface logicProxy = AddressRegistryInterface(registry);
|
|
|
|
(bool isLogic, bool isDefault) = logicProxy.isLogicAuth(logicAddr);
|
|
|
|
return (isLogic, isDefault);
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
2019-03-18 22:39:43 +00:00
|
|
|
* @dev this updates the internal proxy ownership on "registry" contract
|
2019-03-18 21:14:58 +00:00
|
|
|
* @param currentOwner is the current owner
|
|
|
|
* @param nextOwner is the new assigned owner
|
|
|
|
*/
|
|
|
|
function setProxyRecordOwner(address currentOwner, address nextOwner) internal {
|
2019-03-18 22:39:43 +00:00
|
|
|
AddressRegistryInterface initCall = AddressRegistryInterface(registry);
|
2019-03-18 21:14:58 +00:00
|
|
|
initCall.updateProxyRecord(currentOwner, nextOwner);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-18 22:39:43 +00:00
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
|
|
|
* @title User Auth
|
|
|
|
*/
|
2019-03-18 22:39:43 +00:00
|
|
|
contract UserAuth is AddressRecord {
|
2019-03-10 19:38:53 +00:00
|
|
|
using SafeMath for uint;
|
|
|
|
using SafeMath for uint256;
|
2019-03-11 12:30:45 +00:00
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
event LogSetOwner(address indexed owner, address setter);
|
|
|
|
event LogSetPendingOwner(address indexed pendingOwner, address setter);
|
2019-03-10 11:23:11 +00:00
|
|
|
address public owner;
|
2019-03-18 21:14:58 +00:00
|
|
|
address public pendingOwner;
|
2019-03-18 22:12:36 +00:00
|
|
|
uint public claimOnwershipTime; // now + 7 days
|
2019-03-19 14:03:31 +00:00
|
|
|
uint public gracePeriod; // to set the new owner - defaults to 3 days
|
2019-03-09 20:42:05 +00:00
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
|
|
|
* @dev defines the "proxy registry" contract and sets the owner
|
|
|
|
*/
|
2019-03-09 20:42:05 +00:00
|
|
|
constructor() public {
|
2019-03-18 22:12:36 +00:00
|
|
|
gracePeriod = 3 days;
|
2019-03-09 20:42:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
|
|
|
* @dev Throws if not called by owner or contract itself
|
|
|
|
*/
|
2019-03-09 20:42:05 +00:00
|
|
|
modifier auth {
|
2019-03-10 11:23:11 +00:00
|
|
|
require(isAuth(msg.sender), "permission-denied");
|
2019-03-09 20:42:05 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
2019-03-18 22:12:36 +00:00
|
|
|
* @dev sets the "pending owner" and provide 3 days grace period to set the new owner via setOwner()
|
|
|
|
* Throws if called before 10 (i.e. 7 + 3) day after assigning "pending owner"
|
2019-03-18 21:14:58 +00:00
|
|
|
* @param nextOwner is the assigned "pending owner"
|
|
|
|
*/
|
|
|
|
function setPendingOwner(address nextOwner) public auth {
|
2019-03-18 22:12:36 +00:00
|
|
|
require(block.timestamp > claimOnwershipTime.add(gracePeriod), "owner-is-still-pending");
|
2019-03-18 21:14:58 +00:00
|
|
|
pendingOwner = nextOwner;
|
|
|
|
claimOnwershipTime = block.timestamp.add(7 days);
|
|
|
|
emit LogSetPendingOwner(nextOwner, msg.sender);
|
2019-03-10 11:23:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
|
|
|
* @dev sets "pending owner" as real owner
|
2019-03-18 22:12:36 +00:00
|
|
|
* Throws if no "pending owner"
|
2019-03-18 21:14:58 +00:00
|
|
|
* Throws if called before 7 day after assigning "pending owner"
|
|
|
|
*/
|
|
|
|
function setOwner() public {
|
|
|
|
require(pendingOwner != address(0), "no-pending-address");
|
2019-03-18 22:12:36 +00:00
|
|
|
require(block.timestamp > claimOnwershipTime, "owner-is-still-pending");
|
2019-03-18 21:14:58 +00:00
|
|
|
setProxyRecordOwner(owner, pendingOwner);
|
|
|
|
owner = pendingOwner;
|
|
|
|
pendingOwner = address(0);
|
|
|
|
emit LogSetOwner(owner, msg.sender);
|
2019-03-10 11:23:11 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:20:40 +00:00
|
|
|
/**
|
|
|
|
* @dev sets owner and function is only be called once by registry on build()
|
|
|
|
* and this hack verifiy the contract on etherscan automatically
|
|
|
|
* as no dynamic owner address is sent in the constructor
|
|
|
|
* @param _owner is the new owner of this contract wallet
|
|
|
|
*/
|
|
|
|
function setOwnerOnce(address _owner) public auth {
|
|
|
|
require(msg.sender == registry, "permission-denied");
|
|
|
|
owner = _owner;
|
|
|
|
emit LogSetOwner(owner, msg.sender);
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
|
|
|
* @dev checks if called by owner or contract itself
|
|
|
|
* @param src is the address initiating the call
|
|
|
|
*/
|
2019-03-24 21:49:14 +00:00
|
|
|
function isAuth(address src) public view returns (bool) {
|
2019-03-11 12:30:45 +00:00
|
|
|
if (src == address(this)) {
|
|
|
|
return true;
|
|
|
|
} else if (src == owner) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 21:14:58 +00:00
|
|
|
|
2019-03-09 20:42:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 22:39:43 +00:00
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
/**
|
|
|
|
* @title User Guardians
|
2019-03-19 14:03:31 +00:00
|
|
|
* @dev the assigned guardian addresses (upto 3) can set new owners
|
|
|
|
* but only after certain period of owner's inactivity (i.e. activePeriod)
|
2019-03-18 21:14:58 +00:00
|
|
|
*/
|
|
|
|
contract UserGuardian is UserAuth {
|
|
|
|
|
2019-03-19 14:03:31 +00:00
|
|
|
event LogSetGuardian(uint num, address indexed prevGuardian, address indexed newGuardian);
|
2019-03-18 21:14:58 +00:00
|
|
|
event LogNewActivePeriod(uint newActivePeriod);
|
|
|
|
event LogSetOwnerViaGuardian(address nextOwner, address indexed guardian);
|
|
|
|
|
|
|
|
mapping(uint => address) public guardians;
|
|
|
|
uint public lastActivity; // time when called "execute" last time
|
|
|
|
uint public activePeriod; // the period over lastActivity when guardians have no rights
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Throws if guardians not enabled by system admin
|
|
|
|
*/
|
2019-03-19 14:03:31 +00:00
|
|
|
modifier isGuardianEnabled() {
|
2019-03-18 22:39:43 +00:00
|
|
|
AddressRegistryInterface initCall = AddressRegistryInterface(registry);
|
|
|
|
require(initCall.guardianEnabled(), "guardian-not-enabled");
|
2019-03-18 21:14:58 +00:00
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev guardians can set "owner" after owner stay inactive for minimum "activePeriod"
|
|
|
|
* @param nextOwner is the new owner
|
|
|
|
* @param num is the assigned guardian number
|
|
|
|
*/
|
2019-03-19 14:03:31 +00:00
|
|
|
function setOwnerViaGuardian(address nextOwner, uint num) public isGuardianEnabled {
|
2019-03-24 21:49:14 +00:00
|
|
|
require(isGuardian(msg.sender), "not-guardian");
|
2019-03-18 21:14:58 +00:00
|
|
|
require(msg.sender == guardians[num], "permission-denied");
|
|
|
|
require(block.timestamp > lastActivity.add(activePeriod), "active-period-not-over");
|
|
|
|
owner = nextOwner;
|
|
|
|
emit LogSetOwnerViaGuardian(nextOwner, guardians[num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-25 19:06:24 +00:00
|
|
|
* @dev sets the guardian with assigned number (upto 5)
|
2019-03-18 21:14:58 +00:00
|
|
|
* @param num is the guardian assigned number
|
|
|
|
* @param _guardian is the new guardian address
|
|
|
|
*/
|
2019-03-19 14:03:31 +00:00
|
|
|
function setGuardian(uint num, address _guardian) public auth isGuardianEnabled {
|
2019-03-25 19:06:24 +00:00
|
|
|
require(num > 0 && num < 6, "guardians-cant-exceed-three");
|
2019-03-19 14:03:31 +00:00
|
|
|
emit LogSetGuardian(num, guardians[num], _guardian);
|
2019-03-18 21:14:58 +00:00
|
|
|
guardians[num] = _guardian;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-25 19:06:24 +00:00
|
|
|
* @dev sets the guardian with assigned number (upto 5)
|
2019-03-18 21:49:39 +00:00
|
|
|
* @param _activePeriod is the period when guardians have no rights to dethrone the owner
|
2019-03-18 21:14:58 +00:00
|
|
|
*/
|
2019-03-19 14:03:31 +00:00
|
|
|
function updateActivePeriod(uint _activePeriod) public auth isGuardianEnabled {
|
2019-03-18 21:14:58 +00:00
|
|
|
activePeriod = _activePeriod;
|
|
|
|
emit LogNewActivePeriod(_activePeriod);
|
|
|
|
}
|
2019-03-10 11:23:11 +00:00
|
|
|
|
2019-03-19 14:03:31 +00:00
|
|
|
/**
|
|
|
|
* @dev Throws if the msg.sender is not guardian
|
|
|
|
*/
|
2019-03-24 21:49:14 +00:00
|
|
|
function isGuardian(address _guardian) public view returns (bool) {
|
2019-03-25 19:06:24 +00:00
|
|
|
if (
|
|
|
|
_guardian == guardians[1] || _guardian == guardians[2] ||
|
|
|
|
_guardian == guardians[3] || _guardian == guardians[4] ||
|
|
|
|
_guardian == guardians[5]
|
|
|
|
)
|
|
|
|
{
|
2019-03-19 14:03:31 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title User Manager
|
|
|
|
* @dev the assigned manager addresses (upto 3) can manage the wealth in contract to contract fashion
|
|
|
|
* but can't withdraw the assets on their personal address
|
|
|
|
*/
|
|
|
|
contract UserManager is UserGuardian {
|
|
|
|
|
|
|
|
event LogSetManager(uint num, address indexed prevManager, address indexed newManager);
|
|
|
|
|
|
|
|
mapping(uint => address) public managers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Throws if manager not enabled by system admin
|
|
|
|
*/
|
|
|
|
modifier isManagerEnabled() {
|
|
|
|
AddressRegistryInterface initCall = AddressRegistryInterface(registry);
|
|
|
|
require(initCall.managerEnabled(), "admin-not-enabled");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-25 19:06:24 +00:00
|
|
|
* @dev sets the manager with assigned number (upto 5)
|
2019-03-19 14:03:31 +00:00
|
|
|
* @param num is the assigned number of manager
|
|
|
|
* @param _manager is the new admin address
|
|
|
|
*/
|
|
|
|
function setManager(uint num, address _manager) public auth isManagerEnabled {
|
2019-03-25 19:06:24 +00:00
|
|
|
require(num > 0 && num < 6, "guardians-cant-exceed-three");
|
2019-03-19 14:03:31 +00:00
|
|
|
emit LogSetManager(num, managers[num], _manager);
|
|
|
|
managers[num] = _manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Throws if the msg.sender is not manager
|
|
|
|
*/
|
2019-03-24 21:49:14 +00:00
|
|
|
function isManager(address _manager) public view returns (bool) {
|
2019-03-25 19:06:24 +00:00
|
|
|
if (
|
|
|
|
_manager == managers[1] || _manager == managers[2] ||
|
|
|
|
_manager == managers[3] || _manager == managers[4] ||
|
|
|
|
_manager == managers[5]
|
|
|
|
)
|
|
|
|
{
|
2019-03-19 14:03:31 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 22:39:43 +00:00
|
|
|
|
2019-03-18 21:14:58 +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,
|
|
|
|
msg.value,
|
|
|
|
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
|
|
|
*/
|
2019-03-24 00:16:50 +00:00
|
|
|
contract InstaWallet is UserManager, UserNote {
|
2019-03-11 12:30:45 +00:00
|
|
|
|
2019-03-22 22:25:46 +00:00
|
|
|
event LogExecute(address sender, 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
|
|
|
|
*/
|
2019-03-19 20:20:40 +00:00
|
|
|
constructor() public {
|
2019-03-18 22:39:43 +00:00
|
|
|
registry = msg.sender;
|
2019-03-19 20:20:40 +00:00
|
|
|
owner = msg.sender; // will be changed in initial call itself
|
2019-03-10 11:23:11 +00:00
|
|
|
lastActivity = block.timestamp;
|
2019-03-19 14:03:31 +00:00
|
|
|
activePeriod = 30 days; // default on deployment and changeable afterwards
|
2019-03-09 20:42:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:38:03 +00:00
|
|
|
function() external payable {}
|
2019-03-09 20:42:05 +00:00
|
|
|
|
2019-03-18 21:49:39 +00:00
|
|
|
/**
|
|
|
|
* @dev execute authorised calls via delegate call
|
|
|
|
* @param _target logic proxy address
|
|
|
|
* @param _data delegate call data
|
|
|
|
* @param srcNum to find the source
|
|
|
|
* @param sessionNum to find the session
|
|
|
|
*/
|
|
|
|
function execute(
|
|
|
|
address _target,
|
|
|
|
bytes memory _data,
|
|
|
|
uint srcNum,
|
|
|
|
uint sessionNum
|
|
|
|
)
|
|
|
|
public
|
|
|
|
payable
|
|
|
|
note
|
2019-03-25 18:47:45 +00:00
|
|
|
isExecutable(_target)
|
2019-03-18 21:49:39 +00:00
|
|
|
returns (bytes memory response)
|
|
|
|
{
|
2019-03-10 19:38:53 +00:00
|
|
|
lastActivity = block.timestamp;
|
2019-03-22 22:25:46 +00:00
|
|
|
emit LogExecute(
|
|
|
|
msg.sender,
|
|
|
|
_target,
|
|
|
|
srcNum,
|
|
|
|
sessionNum
|
|
|
|
);
|
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-18 21:14:58 +00:00
|
|
|
|
2019-03-19 14:03:31 +00:00
|
|
|
/**
|
|
|
|
* @dev checks if the proxy is authorised
|
|
|
|
* and if the sender is owner or contract itself or manager
|
|
|
|
* and if manager then Throws if target is default proxy address
|
|
|
|
*/
|
2019-03-25 18:47:45 +00:00
|
|
|
modifier isExecutable(address proxyTarget) {
|
2019-03-24 21:49:14 +00:00
|
|
|
require(proxyTarget != address(0), "logic-proxy-address-required");
|
2019-03-25 19:06:24 +00:00
|
|
|
|
2019-03-19 14:03:31 +00:00
|
|
|
(bool isLogic, bool isDefault) = isLogicAuthorised(proxyTarget);
|
|
|
|
require(isLogic, "logic-proxy-address-not-allowed");
|
2019-03-25 19:06:24 +00:00
|
|
|
|
|
|
|
bool enact = false;
|
2019-03-19 14:03:31 +00:00
|
|
|
if (isAuth(msg.sender)) {
|
2019-03-25 19:06:24 +00:00
|
|
|
enact = true;
|
2019-03-24 21:49:14 +00:00
|
|
|
} else if (isManager(msg.sender) && !isDefault) {
|
2019-03-25 19:06:24 +00:00
|
|
|
enact = true;
|
2019-03-19 14:03:31 +00:00
|
|
|
}
|
2019-03-25 19:06:24 +00:00
|
|
|
|
|
|
|
require(enact, "not-executable");
|
2019-03-25 18:47:45 +00:00
|
|
|
_;
|
2019-03-19 14:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|