Commit 2b21105c authored by Bijun Li's avatar Bijun Li
Browse files

Fix owner authentication

parent 349e88c6
Showing with 103 additions and 58 deletions
+103 -58
......@@ -4,7 +4,6 @@ pragma solidity ^0.8.3;
import "./Upgradeable.sol";
import "./Master.sol";
import "./MemberRoles.sol";
import "./manage/Managed.sol";
import "./interfaces/IIdentityRecovery.sol";
contract IdentityRecovery is IIdentityRecovery, Upgradeable {
......
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.3;
import "./Master.sol";
import "./ownership/Ownable.sol";
contract ManageBlocksMaster is Ownable {
address[] public implementations;
event OnBoarded (address masterAddress);
function addManageBlocksMaster() public {
Master ms = new Master();
ms.initMaster(msg.sender, implementations);
emit OnBoarded(address(ms));
}
function setImplementations(address[] memory _implementatioins) external onlyOwner {
implementations = _implementatioins;
}
}
\ No newline at end of file
......@@ -3,12 +3,12 @@ pragma solidity ^0.8.3;
import "./Upgradeable.sol";
import "./MemberRoles.sol";
import "./ManageBlocksMaster.sol";
import "./IdentityRecovery.sol";
import "./proxy/OwnedUpgradeabilityProxy.sol";
import "./ownership/Ownable.sol";
import "./manage/Managed.sol";
contract Master is Ownable, Managed {
contract Master is Ownable {
uint[] public versionDates;
bytes2[] public allContractNames;
......@@ -16,14 +16,11 @@ contract Master is Ownable, Managed {
mapping(address => bool) public contractsActive;
mapping(bytes2 => address) public contractsAddress;
Managed internal manage;
function initMaster(
address _ownerAddress,
address[] memory _implementations
) external {
_addContractNames();
masterAddress = address(this);
require(allContractNames.length == _implementations.length, "Contract length does not match implementation length");
contractsActive[address(this)] = true;
......@@ -37,18 +34,19 @@ contract Master is Ownable, Managed {
_changeMasterAddress(address(this));
_changeAllAddress();
// IdentityRecovery ir = IdentityRecovery(contractsAddress["IR"]);
// ir.identityRecoveryInitiate();
MemberRoles mr = MemberRoles(contractsAddress["MR"]);
mr.memberRolesInitiate(_ownerAddress);
IdentityRecovery ir = IdentityRecovery(contractsAddress["IR"]);
ir.identityRecoveryInitiate();
}
/**
* @dev Creates a new version of contract addresses
* @param _contractAddresses Array of contract implementations
*/
function addNewVersion(address[] memory _contractAddresses) external onlyAuthorizedToManage {
function addNewVersion(address[] memory _contractAddresses) external onlyOwner {
for (uint i = 0; i < allContractNames.length; i++) {
_replaceImplementation(allContractNames[i], _contractAddresses[i]);
}
......@@ -59,7 +57,7 @@ contract Master is Ownable, Managed {
/**
* @dev adds a new contract type to master
*/
function addNewContract(bytes2 _contractName, address _contractAddress) external onlyAuthorizedToManage {
function addNewContract(bytes2 _contractName, address _contractAddress) external onlyOwner {
allContractNames.push(_contractName);
_generateProxy(_contractName, _contractAddress);
_changeMasterAddress(address(this));
......@@ -70,7 +68,7 @@ contract Master is Ownable, Managed {
* @dev upgrades a single contract
*/
function upgradeContractImplementation(bytes2 _contractsName, address _contractsAddress)
external onlyAuthorizedToManage
external onlyOwner
{
if (_contractsName == "MS") {
_changeMasterAddress(_contractsAddress);
......@@ -85,7 +83,7 @@ contract Master is Ownable, Managed {
* @dev upgrades a single contract
*/
function upgradeContractProxy(bytes2 _contractsName, address _contractsAddress)
external onlyAuthorizedToManage
external onlyOwner
{
contractsActive[contractsAddress[_contractsName]] = false;
_generateProxy(_contractsName, _contractsAddress);
......@@ -163,9 +161,8 @@ contract Master is Ownable, Managed {
* @dev Replace contract implementation
*/
function _replaceImplementation(bytes2 _contractsName, address _contractsAddress) internal {
address payable _implementation = payable(contractsAddress[_contractsName]);
OwnedUpgradeabilityProxy tempInstance
= OwnedUpgradeabilityProxy(_implementation);
= OwnedUpgradeabilityProxy(contractsAddress[_contractsName]);
tempInstance.upgradeTo(_contractsAddress);
}
......
......@@ -2,9 +2,9 @@
pragma solidity ^0.8.3;
import "./interfaces/IMemberRoles.sol";
import "./manage/Managed.sol";
import "./ownership/Ownable.sol";
contract MemberRoles is IMemberRoles, Managed {
contract MemberRoles is IMemberRoles, Ownable {
enum Role {
Everyone,
......@@ -21,19 +21,18 @@ contract MemberRoles is IMemberRoles, Managed {
MemberRoleDetails[] internal memberRoleData;
mapping(uint => mapping(address => uint)) internal memberRoleIndex;
bool internal constructorCheck;
modifier checkRoleAuthority(uint _memberRoleId) {
if (memberRoleData[_memberRoleId].authorized != address(0))
require(msg.sender == memberRoleData[_memberRoleId].authorized);
else
require(isAuthorizedToManage(msg.sender), "Not authorized");
require(isAuthorized(msg.sender), "Not authorized");
_;
}
/**
* @dev To Initiate default settings whenever the contract is regenerated!
* @dev To initiate default settings whenever the contract is regenerated!
*/
function updateDependencyAddresses() public pure { //solhint-disable-line
}
......@@ -42,12 +41,12 @@ contract MemberRoles is IMemberRoles, Managed {
* @dev Change master address
*/
function changeMasterAddress(address _masterAddress) public { //solhint-disable-line
if (masterAddress == address(0)) {
masterAddress = _masterAddress;
} else {
require(msg.sender == masterAddress);
masterAddress = _masterAddress;
}
// if (masterAddress == address(0)) {
// masterAddress = _masterAddress;
// } else {
// require(msg.sender == masterAddress);
// masterAddress = _masterAddress;
// }
}
/**
......@@ -83,7 +82,7 @@ contract MemberRoles is IMemberRoles, Managed {
string memory _roleDescription,
address _authorized,
bool _isLimited
) external override onlyAuthorizedToManage {
) external override onlyOwner {
_addRole(_roleName, _roleDescription, _authorized, _isLimited);
}
......
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.3;
interface IMaster {
function getLatestAddress(bytes2 _module) external view returns(address);
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
......@@ -8,12 +12,16 @@ pragma solidity ^0.8.3;
*/
contract Ownable {
address public owner;
mapping(address => uint) internal ownerSet;
event OwnershipRenounced(address indexed previousOwner);
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
event AddOwnerSet(address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
......@@ -21,6 +29,7 @@ contract Ownable {
*/
constructor() {
owner = msg.sender;
ownerSet[owner] = 1;
}
/**
......@@ -30,6 +39,25 @@ contract Ownable {
require(msg.sender == owner);
_;
}
/**
* @dev Throws if called by any account not in the owner set.
*/
modifier onlyOwnerSet() {
require(ownerSet[owner] == 1);
_;
}
/**
* @dev Add new owner to the owner set.
* It will only be possible to call this function by the owner.
*/
function addOwnerSet(address newOwner) public onlyOwner {
require(newOwner != address(0));
ownerSet[newOwner] = 1;
emit AddOwnerSet(newOwner);
}
/**
* @dev Allows the current owner to relinquish control of the contract.
......@@ -37,7 +65,7 @@ contract Ownable {
* It will not be possible to call the functions with the `onlyOwner`
* modifier anymore.
*/
function renounceOwnership() public onlyOwner {
function renounceOwnership() public onlyOwnerSet {
emit OwnershipRenounced(owner);
owner = address(0);
}
......@@ -46,7 +74,7 @@ contract Ownable {
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param _newOwner The address to transfer ownership to.
*/
function transferOwnership(address _newOwner) public onlyOwner {
function transferOwnership(address _newOwner) public onlyOwnerSet {
_transferOwnership(_newOwner);
}
......@@ -58,5 +86,14 @@ contract Ownable {
require(_newOwner != address(0));
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
ownerSet[owner] = 0;
}
/**
* @dev Checks if an address is in the owner set
* @return isAuthorized if authorized
*/
function isAuthorized(address _toCheck) public view returns(bool) {
return (ownerSet[_toCheck] == 1);
}
}
......@@ -7,22 +7,17 @@ pragma solidity ^0.8.3;
*/
abstract contract Proxy {
/**
* @dev Event for received value
*/
event ValueReceived(address user, uint amount);
/**
* @dev Tells the address of the implementation where every call will be delegated.
* @return address of the implementation to which it will be delegated
*/
function implementation() public virtual view returns (address);
function implementation() public virtual view returns (address);
/**
* @dev Fallback function allowing to perform a delegatecall to the given implementation.
* This function will return whatever the implementation call returns.
*/
fallback() external payable {
fallback() external {
address _impl = implementation();
require(_impl != address(0));
......@@ -38,12 +33,4 @@ abstract contract Proxy {
default { return(ptr, size) }
}
}
/**
* * @dev Receive ether funtion, no data or returned value
*/
receive() external payable {
emit ValueReceived(msg.sender, msg.value);
}
}
\ No newline at end of file
......@@ -12,41 +12,46 @@ contract UpgradeabilityProxy is Proxy {
* @dev This event will be emitted every time the implementation gets upgraded
* @param implementation representing the address of the upgraded implementation
*/
event Upgraded(address indexed implementation);
event Upgraded(address indexed implementation);
// Storage position of the address of the current implementation
bytes32 private constant implementationPosition = keccak256("memri.identity.proxy.implementation");
/**
* @dev Constructor function
*/
constructor() {}
/**
* @dev Tells the address of the current implementation
* @return impl address of the current implementation
*/
function implementation() public override view returns (address impl) {
bytes32 position = implementationPosition;
assembly {
function implementation() public override view returns (address impl) {
bytes32 position = implementationPosition;
assembly {
impl := sload(position)
}
}
}
}
/**
* @dev Sets the address of the current implementation
* @param _newImplementation address representing the new implementation to be set
*/
function _setImplementation(address _newImplementation) internal {
bytes32 position = implementationPosition;
assembly {
sstore(position, _newImplementation)
}
}
function _setImplementation(address _newImplementation) internal {
bytes32 position = implementationPosition;
assembly {
sstore(position, _newImplementation)
}
}
/**
* @dev Upgrades the implementation address
* @param _newImplementation representing the address of the new implementation to be set
*/
function _upgradeTo(address _newImplementation) internal {
function _upgradeTo(address _newImplementation) internal {
address currentImplementation = implementation();
require(currentImplementation != _newImplementation);
_setImplementation(_newImplementation);
emit Upgraded(_newImplementation);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment