Updated and standardised the contracts.

This commit is contained in:
Sowmayjain 2019-03-19 02:44:58 +05:30
parent 037e3e7c60
commit bc40451e91
2 changed files with 188 additions and 46 deletions

View File

@ -3,29 +3,63 @@ pragma solidity ^0.5.0;
import "./UserProxy.sol"; import "./UserProxy.sol";
contract ProxyRegistry { // checking if the logic proxy is authorised
event Created(address indexed sender, address indexed owner, address proxy); contract SystemAdmin {
mapping(address => UserProxy) public proxies;
address public logicProxyAddr;
constructor(address logicProxyAddr_) public { address public logicProxyAddr;
logicProxyAddr = logicProxyAddr_;
modifier isAdmin() {
require(msg.sender == getAdmin(), "permission-denied");
_;
} }
function build(uint activeDays) public returns (UserProxy proxy) { function getAdmin() internal view returns (address) {
proxy = build(msg.sender, activeDays); AddressRegistryInterface registry = AddressRegistryInterface(logicProxyAddr);
return registry.getAddress("admin");
}
}
contract ProxyRegistry is SystemAdmin {
event Created(address indexed sender, address indexed owner, address proxy);
mapping(address => UserProxy) public proxies;
bool public guardianEnabled;
constructor(address _logicProxyAddr) public {
logicProxyAddr = _logicProxyAddr;
}
function build() public returns (UserProxy proxy) {
proxy = build(msg.sender);
} }
// deploys a new proxy instance and sets custom owner of proxy // deploys a new proxy instance and sets custom owner of proxy
function build(address owner, uint activeDays) public returns (UserProxy proxy) { function build(address owner) public returns (UserProxy proxy) {
require( require(
proxies[owner] == UserProxy(0) || proxies[owner].owner() != owner, proxies[owner] == UserProxy(0) || proxies[owner].owner() != owner,
"multiple-proxy-per-user-not-allowed" "multiple-proxy-per-user-not-allowed"
); // Not allow new proxy if the user already has one and remains being the owner ); // Not allow new proxy if the user already has one and remains being the owner
proxy = new UserProxy(owner, logicProxyAddr);
proxy = new UserProxy(logicProxyAddr, activeDays);
emit Created(msg.sender, owner, address(proxy)); emit Created(msg.sender, owner, address(proxy));
proxy.setOwner(owner);
proxies[owner] = proxy; proxies[owner] = proxy;
} }
}
// msg.sender should always be proxies created via this contract for successful execution
function updateProxyRecord(address currentOwner, address nextOwner) public {
require(msg.sender == address(proxies[currentOwner]), "invalid-proxy-or-owner");
proxies[nextOwner] = proxies[currentOwner];
proxies[currentOwner] = UserProxy(0);
}
function enableGuardian() public isAdmin {
guardianEnabled = true;
}
function disableGuardian() public isAdmin {
guardianEnabled = false;
}
}

View File

@ -9,48 +9,99 @@ library SafeMath {
} }
} }
/**
* @title ProxyRegistry Interface
*/
interface ProxyRegistryInterface {
function updateProxyRecord(address currentOwner, address nextOwner) external;
function guardianEnabled() external returns (bool);
}
contract UserAuth { /**
* @title AddressRegistryInterface Interface
*/
interface AddressRegistryInterface {
function getLogic(address logicAddr) external view returns (bool);
function getAddress(string calldata name) external view returns(address);
}
/**
* @title Proxy Record
*/
contract ProxyRecord {
address public proxyContract;
/**
* @dev this updates the internal proxy ownership on "proxy registry" contract
* @param currentOwner is the current owner
* @param nextOwner is the new assigned owner
*/
function setProxyRecordOwner(address currentOwner, address nextOwner) internal {
ProxyRegistryInterface initCall = ProxyRegistryInterface(proxyContract);
initCall.updateProxyRecord(currentOwner, nextOwner);
}
}
/**
* @title User Auth
*/
contract UserAuth is ProxyRecord {
using SafeMath for uint; using SafeMath for uint;
using SafeMath for uint256; using SafeMath for uint256;
event LogSetOwner(address indexed owner, bool isGuardian); event LogSetOwner(address indexed owner, address setter);
event LogSetGuardian(address indexed guardian); event LogSetPendingOwner(address indexed pendingOwner, address setter);
mapping(uint => address) public guardians;
address public owner; address public owner;
uint public lastActivity; // timestamp address public pendingOwner;
// guardians can set owner after owner stay inactive for certain period uint public claimOnwershipTime; // 7 days
uint public activePeriod; // timestamp
/**
* @dev defines the "proxy registry" contract and sets the owner
*/
constructor() public { constructor() public {
proxyContract = msg.sender;
owner = msg.sender; owner = msg.sender;
emit LogSetOwner(msg.sender, false); emit LogSetOwner(owner, msg.sender);
} }
/**
* @dev Throws if not called by owner or contract itself
*/
modifier auth { modifier auth {
require(isAuth(msg.sender), "permission-denied"); require(isAuth(msg.sender), "permission-denied");
_; _;
} }
function setOwner(address owner_) public auth { /**
owner = owner_; * @dev sets the "pending owner"
emit LogSetOwner(owner, false); * @param nextOwner is the assigned "pending owner"
*/
function setPendingOwner(address nextOwner) public auth {
require(block.timestamp > claimOnwershipTime, "owner-is-still-pending");
pendingOwner = nextOwner;
claimOnwershipTime = block.timestamp.add(7 days);
emit LogSetPendingOwner(nextOwner, msg.sender);
} }
function setOwnerViaGuardian(address owner_, uint num) public { /**
require(msg.sender == guardians[num], "permission-denied"); * @dev sets "pending owner" as real owner
require(block.timestamp > lastActivity.add(activePeriod), "active-period-not-over"); * Throws if called before 7 day after assigning "pending owner"
owner = owner_; */
emit LogSetOwner(owner, true); function setOwner() public {
} require(pendingOwner != address(0), "no-pending-address");
setProxyRecordOwner(owner, pendingOwner);
function setGuardian(uint num, address guardian_) public auth { owner = pendingOwner;
require(num > 0 && num < 6, "guardians-cant-exceed-five"); pendingOwner = address(0);
guardians[num] = guardian_; emit LogSetOwner(owner, msg.sender);
emit LogSetGuardian(guardian_);
} }
/**
* @dev checks if called by owner or contract itself
* @param src is the address initiating the call
*/
function isAuth(address src) internal view returns (bool) { function isAuth(address src) internal view returns (bool) {
if (src == address(this)) { if (src == address(this)) {
return true; return true;
@ -60,9 +111,69 @@ contract UserAuth {
return false; return false;
} }
} }
} }
/**
* @title User Guardians
*/
contract UserGuardian is UserAuth {
event LogSetGuardian(address indexed guardian);
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
*/
modifier guard() {
ProxyRegistryInterface initCall = ProxyRegistryInterface(proxyContract);
require(initCall.guardianEnabled());
_;
}
/**
* @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
*/
function setOwnerViaGuardian(address nextOwner, uint num) public guard {
require(msg.sender == guardians[num], "permission-denied");
require(block.timestamp > lastActivity.add(activePeriod), "active-period-not-over");
owner = nextOwner;
emit LogSetOwnerViaGuardian(nextOwner, guardians[num]);
}
/**
* @dev sets the guardian with assigned number (upto 3)
* @param num is the guardian assigned number
* @param _guardian is the new guardian address
*/
function setGuardian(uint num, address _guardian) public auth guard {
require(num > 0 && num < 4, "guardians-cant-exceed-three");
guardians[num] = _guardian;
emit LogSetGuardian(_guardian);
}
/**
* @dev sets the guardian with assigned number (upto 3)
* @param num is the guardian assigned number
* @param _guardian is the new guardian address
*/
function updateActivePeriod(uint _activePeriod) public auth guard {
activePeriod = _activePeriod;
emit LogNewActivePeriod(_activePeriod);
}
}
/**
* @dev logging the execute events
*/
contract UserNote { contract UserNote {
event LogNote( event LogNote(
bytes4 indexed sig, bytes4 indexed sig,
@ -93,27 +204,23 @@ contract UserNote {
} }
interface AddressRegistry {
function getLogic(address logicAddr) external view returns (bool);
}
// checking if the logic proxy is authorised // checking if the logic proxy is authorised
contract UserLogic { contract UserLogic {
address public logicProxyAddr; address public logicProxyAddr;
function isLogicAuthorised(address logicAddr) internal view returns (bool) { function isLogicAuthorised(address logicAddr) internal view returns (bool) {
AddressRegistry logicProxy = AddressRegistry(logicProxyAddr); AddressRegistryInterface logicProxy = AddressRegistryInterface(logicProxyAddr);
return logicProxy.getLogic(logicAddr); return logicProxy.getLogic(logicAddr);
} }
} }
contract UserProxy is UserGuardian, UserNote, UserLogic {
contract UserProxy is UserAuth, UserNote, UserLogic { constructor(address _owner, address _logicProxyAddr) public {
constructor(address _logicProxyAddr, uint _activePeriod) public {
logicProxyAddr = _logicProxyAddr; logicProxyAddr = _logicProxyAddr;
lastActivity = block.timestamp; lastActivity = block.timestamp;
activePeriod = _activePeriod; activePeriod = 30 days; // default and changeable
owner = _owner;
} }
function() external payable {} function() external payable {}
@ -139,4 +246,5 @@ contract UserProxy is UserAuth, UserNote, UserLogic {
} }
} }
} }
} }