smart-contract/contracts/Registry.sol

180 lines
5.0 KiB
Solidity
Raw Normal View History

2019-03-11 22:52:48 +00:00
pragma solidity ^0.5.0;
2019-03-19 21:21:08 +00:00
import "./UserWallet.sol";
2019-03-18 22:39:43 +00:00
2019-03-11 22:52:48 +00:00
2019-03-18 22:41:14 +00:00
/**
* @title Address Registry
*/
2019-03-11 22:52:48 +00:00
contract AddressRegistry {
2019-03-18 21:57:24 +00:00
event LogSetAddress(string name, address addr);
2019-03-11 23:09:59 +00:00
2019-03-11 22:52:48 +00:00
mapping(bytes32 => address) registry;
2019-03-19 14:03:31 +00:00
modifier isAdmin() {
require(
msg.sender == getAddress("admin") ||
msg.sender == getAddress("owner"),
"permission-denied"
);
_;
}
2019-03-18 21:57:24 +00:00
/**
* @dev get the address from system registry
*/
2019-03-11 22:52:48 +00:00
function getAddress(string memory name) public view returns(address) {
return registry[keccak256(abi.encodePacked(name))];
}
2019-03-18 21:57:24 +00:00
/**
* @dev set new address in system registry
*/
2019-03-19 14:03:31 +00:00
function setAddress(string memory name, address addr) public isAdmin {
2019-03-11 22:52:48 +00:00
registry[keccak256(abi.encodePacked(name))] = addr;
2019-03-18 21:57:24 +00:00
emit LogSetAddress(name, addr);
2019-03-11 22:52:48 +00:00
}
2019-03-18 22:39:43 +00:00
}
2019-03-18 22:41:14 +00:00
/**
* @title Logic Registry
*/
2019-03-18 22:39:43 +00:00
contract LogicRegistry is AddressRegistry {
2019-03-19 14:03:31 +00:00
event LogEnableDefaultLogic(address logicAddr);
event LogEnableLogic(address logicAddr);
event LogDisableLogic(address logicAddr);
2019-03-18 22:39:43 +00:00
mapping(address => bool) public defaultLogicProxies;
mapping(address => bool) public logicProxies;
2019-03-18 21:57:24 +00:00
/**
2019-03-19 14:03:31 +00:00
* @dev get the boolean of the logic contract
2019-03-18 21:57:24 +00:00
* @param logicAddr is the logic proxy address
2019-03-19 19:08:09 +00:00
* @return bool logic proxy is authorised by system admin
* @return bool logic proxy is default proxy
2019-03-18 21:57:24 +00:00
*/
2019-03-19 14:03:31 +00:00
function isLogicAuth(address logicAddr) public view returns (bool, bool) {
2019-03-11 22:52:48 +00:00
if (defaultLogicProxies[logicAddr]) {
2019-03-19 14:03:31 +00:00
return (true, true);
2019-03-11 22:52:48 +00:00
} else if (logicProxies[logicAddr]) {
2019-03-19 14:03:31 +00:00
return (true, false);
2019-03-11 22:52:48 +00:00
} else {
2019-03-19 19:08:09 +00:00
return (false, false);
2019-03-11 22:52:48 +00:00
}
}
2019-03-18 21:57:24 +00:00
/**
* @dev this sets the default logic proxy to true
2019-03-19 14:03:31 +00:00
* default proxies mostly contains the logic for withdrawal of assets
* and can never be false to freely let user withdraw their assets
2019-03-18 21:57:24 +00:00
* @param logicAddr is the default logic proxy address
*/
2019-03-19 14:03:31 +00:00
function enableDefaultLogic(address logicAddr) public isAdmin {
2019-03-11 22:52:48 +00:00
defaultLogicProxies[logicAddr] = true;
2019-03-19 14:03:31 +00:00
emit LogEnableDefaultLogic(logicAddr);
2019-03-11 22:52:48 +00:00
}
2019-03-18 21:57:24 +00:00
/**
2019-03-19 14:03:31 +00:00
* @dev enable logic proxy address and sets true
2019-03-18 21:57:24 +00:00
* @param logicAddr is the logic proxy address
*/
2019-03-19 14:03:31 +00:00
function enableLogic(address logicAddr) public isAdmin {
2019-03-11 23:09:59 +00:00
logicProxies[logicAddr] = true;
2019-03-19 14:03:31 +00:00
emit LogEnableLogic(logicAddr);
}
/**
* @dev enable logic proxy address and sets false
* @param logicAddr is the logic proxy address
*/
function disableLogic(address logicAddr) public isAdmin {
logicProxies[logicAddr] = false;
emit LogDisableLogic(logicAddr);
2019-03-11 23:09:59 +00:00
}
2019-03-18 22:39:43 +00:00
}
2019-03-18 22:41:14 +00:00
/**
* @title User Wallet Registry
*/
2019-03-19 19:36:11 +00:00
contract WalletRegistry is LogicRegistry {
2019-03-18 22:39:43 +00:00
event Created(address indexed sender, address indexed owner, address proxy);
mapping(address => InstaWallet) public proxies;
bool public guardianEnabled; // user guardian mechanism enabled in overall system
bool public managerEnabled; // user manager mechanism enabled in overall system
2019-03-18 22:39:43 +00:00
/**
* @dev deploys a new proxy instance and sets msg.sender as owner of proxy
*/
function build() public returns (InstaWallet proxy) {
2019-03-18 22:39:43 +00:00
proxy = build(msg.sender);
}
/**
* @dev deploys a new proxy instance and sets custom owner of proxy
* Throws if the owner already have a UserWallet
*/
function build(address owner) public returns (InstaWallet proxy) {
require(proxies[owner] == InstaWallet(0), "multiple-proxy-per-user-not-allowed");
proxy = new InstaWallet();
proxy.setOwnerOnce(owner);
2019-03-18 22:39:43 +00:00
emit Created(msg.sender, owner, address(proxy));
proxies[owner] = proxy;
}
/**
* @dev update the proxy record whenever owner changed on any proxy
* Throws if msg.sender is not a proxy contract created via this contract
*/
function updateProxyRecord(address currentOwner, address nextOwner) public {
require(msg.sender == address(proxies[currentOwner]), "invalid-proxy-or-owner");
proxies[nextOwner] = proxies[currentOwner];
proxies[currentOwner] = InstaWallet(0);
2019-03-18 22:39:43 +00:00
}
/**
* @dev enable guardian in overall system
*/
function enableGuardian() public isAdmin {
guardianEnabled = true;
}
/**
* @dev disable guardian in overall system
*/
function disableGuardian() public isAdmin {
guardianEnabled = false;
}
/**
2019-03-19 14:03:31 +00:00
* @dev enable user manager in overall system
*/
function enableManager() public isAdmin {
managerEnabled = true;
}
/**
* @dev disable user manager in overall system
*/
function disableManager() public isAdmin {
managerEnabled = false;
}
2019-03-19 19:36:11 +00:00
}
2019-03-19 21:20:49 +00:00
contract InstaRegistry is WalletRegistry {
2019-03-19 19:36:11 +00:00
constructor() public {
registry[keccak256(abi.encodePacked("admin"))] = msg.sender;
registry[keccak256(abi.encodePacked("owner"))] = msg.sender;
build();
2019-03-19 19:36:11 +00:00
}
2019-03-11 23:09:59 +00:00
}