smart-contract/contracts/InstaRegistry.sol

153 lines
4.4 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-04-03 18:59:32 +00:00
function logic(address logicAddr) public view returns (bool) {
if (defaultLogicProxies[logicAddr] || logicProxies[logicAddr]) {
return true;
2019-03-11 22:52:48 +00:00
} else {
2019-04-03 18:59:32 +00:00
return 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);
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-03 09:46:56 +00:00
mapping(address => UserWallet) public proxies;
2019-03-18 22:39:43 +00:00
/**
* @dev deploys a new proxy instance and sets msg.sender as owner of 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);
}
/**
* @dev deploys a new proxy instance and sets custom owner of proxy
* Throws if the owner already have a UserWallet
*/
2019-04-03 09:46:56 +00:00
function build(address owner) public returns (UserWallet proxy) {
require(proxies[owner] == UserWallet(0), "multiple-proxy-per-user-not-allowed");
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-02 23:15:32 +00:00
proxy.setOwner(owner);
2019-03-18 22:39:43 +00:00
emit Created(msg.sender, owner, address(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
*/
2019-04-03 18:59:32 +00:00
function record(address currentOwner, address nextOwner) public {
2019-03-18 22:39:43 +00:00
require(msg.sender == address(proxies[currentOwner]), "invalid-proxy-or-owner");
2019-04-03 18:59:32 +00:00
require(proxies[nextOwner] == UserWallet(0), "multiple-proxy-per-user-not-allowed");
2019-03-18 22:39:43 +00:00
proxies[nextOwner] = proxies[currentOwner];
2019-04-03 09:46:56 +00:00
proxies[currentOwner] = UserWallet(0);
2019-04-03 18:59:32 +00:00
emit LogRecord(currentOwner, nextOwner, address(proxies[nextOwner]));
2019-03-18 22:39:43 +00:00
}
2019-03-19 19:36:11 +00:00
}
/**
* @title 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-03-11 23:09:59 +00:00
}