smart-contract/contracts/InstaRegistry.sol

157 lines
5.1 KiB
Solidity
Raw Normal View History

2019-04-06 12:49:08 +00:00
pragma solidity ^0.5.2;
2019-03-11 22:52:48 +00:00
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-04-06 12:49:08 +00:00
/// @title AddressRegistry
/// @notice
/// @dev
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-04-06 12:49:08 +00:00
/// @notice Registry of role and address
2019-03-11 22:52:48 +00:00
mapping(bytes32 => address) registry;
2019-04-06 12:49:08 +00:00
/**
* @dev Check if msg.sender is admin or owner.
*/
2019-03-19 14:03:31 +00:00
modifier isAdmin() {
require(
msg.sender == getAddress("admin") ||
msg.sender == getAddress("owner"),
"permission-denied"
);
_;
}
2019-04-06 12:49:08 +00:00
/// @dev Get the address from system registry
/// @param _name (string)
/// @return (address) Returns address based on role
function getAddress(string memory _name) public view returns(address) {
return registry[keccak256(abi.encodePacked(_name))];
2019-03-11 22:52:48 +00:00
}
2019-04-06 12:49:08 +00:00
/// @dev Set new address in system registry
/// @param _name (string) Role name
/// @param _userAddress (string) User Address
function setAddress(string memory _name, address _userAddress) public isAdmin {
registry[keccak256(abi.encodePacked(_name))] = _userAddress;
emit LogSetAddress(_name, _userAddress);
2019-03-11 22:52:48 +00:00
}
2019-03-18 22:39:43 +00:00
}
2019-04-06 12:49:08 +00:00
/// @title LogicRegistry
/// @notice
/// @dev LogicRegistry
2019-03-18 22:39:43 +00:00
contract LogicRegistry is AddressRegistry {
2019-04-07 20:21:54 +00:00
event LogEnableStaticLogic(address logicAddress);
2019-04-06 12:49:08 +00:00
event LogEnableLogic(address logicAddress);
event LogDisableLogic(address logicAddress);
2019-03-18 22:39:43 +00:00
2019-04-07 20:21:54 +00:00
/// @notice Map of static proxy state
mapping(address => bool) public logicProxiesStatic;
2019-04-06 12:49:08 +00:00
/// @notice Map of logic proxy state
2019-03-18 22:39:43 +00:00
mapping(address => bool) public logicProxies;
2019-04-06 12:49:08 +00:00
/// @dev
/// @param _logicAddress (address)
/// @return (bool)
function logic(address _logicAddress) public view returns (bool) {
2019-04-07 20:21:54 +00:00
if (logicProxiesStatic[_logicAddress] || logicProxies[_logicAddress]) {
2019-04-03 18:59:32 +00:00
return true;
2019-03-11 22:52:48 +00:00
}
2019-04-07 13:04:51 +00:00
return false;
}
/// @dev
/// @param _logicAddress (address)
/// @return (bool)
2019-04-07 20:21:54 +00:00
function logicStatic(address _logicAddress) public view returns (bool) {
if (logicProxiesStatic[_logicAddress]) {
2019-04-07 13:04:51 +00:00
return true;
}
return false;
2019-03-11 22:52:48 +00:00
}
2019-04-07 20:21:54 +00:00
/// @dev Sets the static logic proxy to true
/// static proxies mostly contains the logic for withdrawal of assets
2019-04-06 12:49:08 +00:00
/// and can never be false to freely let user withdraw their assets
/// @param _logicAddress (address)
2019-04-07 20:21:54 +00:00
function enableStaticLogic(address _logicAddress) public isAdmin {
logicProxiesStatic[_logicAddress] = true;
emit LogEnableStaticLogic(_logicAddress);
2019-03-11 22:52:48 +00:00
}
2019-04-06 12:49:08 +00:00
/// @dev Enable logic proxy address
/// @param _logicAddress (address)
function enableLogic(address _logicAddress) public isAdmin {
logicProxies[_logicAddress] = true;
emit LogEnableLogic(_logicAddress);
2019-03-19 14:03:31 +00:00
}
2019-04-06 12:49:08 +00:00
/// @dev Disable logic proxy address
/// @param _logicAddress (address)
function disableLogic(address _logicAddress) public isAdmin {
logicProxies[_logicAddress] = false;
emit LogDisableLogic(_logicAddress);
2019-03-11 23:09:59 +00:00
}
2019-03-18 22:39:43 +00:00
}
2019-03-18 22:41:14 +00:00
/**
2019-04-06 12:49:08 +00:00
* @dev Deploys a new proxy instance and sets msg.sender as owner of proxy
2019-03-18 22:41:14 +00:00
*/
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);
2019-04-03 18:59:32 +00:00
event LogRecord(address indexed currentOwner, address indexed nextOwner, address proxy);
2019-03-18 22:39:43 +00:00
2019-04-06 12:49:08 +00:00
/// @notice Address to UserWallet proxy map
2019-04-03 09:46:56 +00:00
mapping(address => UserWallet) public proxies;
2019-04-06 12:49:08 +00:00
/// @dev Deploys a new proxy instance and sets custom owner of proxy
/// Throws if the owner already have a UserWallet
/// @return proxy ()
2019-04-03 09:46:56 +00:00
function build() public returns (UserWallet proxy) {
2019-03-18 22:39:43 +00:00
proxy = build(msg.sender);
}
2019-04-06 12:49:08 +00:00
/// @dev update the proxy record whenever owner changed on any proxy
/// Throws if msg.sender is not a proxy contract created via this contract
/// @return proxy () UserWallet
2019-04-07 13:04:51 +00:00
function build(address _owner) public returns (UserWallet proxy) {
require(proxies[_owner] == UserWallet(0), "multiple-proxy-per-user-not-allowed");
2019-04-03 09:46:56 +00:00
proxy = new UserWallet();
2019-04-03 18:59:32 +00:00
proxies[address(this)] = proxy; // will be changed via record() in next line execution
2019-04-07 13:04:51 +00:00
proxy.setOwner(_owner);
emit Created(msg.sender, _owner, address(proxy));
2019-03-18 22:39:43 +00:00
}
2019-04-06 12:49:08 +00:00
/// @dev Transafers ownership
/// @param _currentOwner (address) Current Owner
/// @param _nextOwner (address) Next Owner
function record(address _currentOwner, address _nextOwner) public {
require(msg.sender == address(proxies[_currentOwner]), "invalid-proxy-or-owner");
require(proxies[_nextOwner] == UserWallet(0), "multiple-proxy-per-user-not-allowed");
proxies[_nextOwner] = proxies[_currentOwner];
proxies[_currentOwner] = UserWallet(0);
emit LogRecord(_currentOwner, _nextOwner, address(proxies[_nextOwner]));
2019-03-18 22:39:43 +00:00
}
2019-03-19 19:36:11 +00:00
}
2019-04-06 12:49:08 +00:00
/// @title InstaRegistry
/// @dev Initializing Registry
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;
}
2019-04-06 12:49:08 +00:00
}