diff --git a/.gitignore b/.gitignore index 78ec179..ce702b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ */build/* node_modules/ build/ +.idea diff --git a/upgradeability_using_inherited_storage/contracts/Registry.sol b/upgradeability_using_inherited_storage/contracts/Registry.sol index f80247b..9ff476b 100644 --- a/upgradeability_using_inherited_storage/contracts/Registry.sol +++ b/upgradeability_using_inherited_storage/contracts/Registry.sol @@ -3,12 +3,13 @@ pragma solidity ^0.4.18; import './IRegistry.sol'; import './Upgradeable.sol'; import './UpgradeabilityProxy.sol'; +import './ownership/Ownable.sol'; /** * @title Registry * @dev This contract works as a registry of versions, it holds the implementations for the registered versions. */ -contract Registry is IRegistry { +contract Registry is IRegistry, Ownable { // Mapping of versions to implementations of different functions mapping (string => address) internal versions; @@ -43,4 +44,13 @@ contract Registry is IRegistry { ProxyCreated(proxy); return proxy; } + + /** + * @dev Creates an upgradeable proxy + * @param proxy value returned from createProxy + * @param newowner proxy contract's new owner + */ + function transferProxyOwnership(address proxy, address newowner) onlyOwner public payable { + Ownable(proxy).transferOwnership(newowner); + } } diff --git a/upgradeability_using_inherited_storage/contracts/UpgradeabilityProxy.sol b/upgradeability_using_inherited_storage/contracts/UpgradeabilityProxy.sol index e279e79..0bf036b 100644 --- a/upgradeability_using_inherited_storage/contracts/UpgradeabilityProxy.sol +++ b/upgradeability_using_inherited_storage/contracts/UpgradeabilityProxy.sol @@ -22,7 +22,7 @@ contract UpgradeabilityProxy is Proxy, UpgradeabilityStorage { * @dev Upgrades the implementation to the requested version * @param _version representing the version name of the new implementation to be set */ - function upgradeTo(string _version) public { + function upgradeTo(string _version) public onlyOwner { _implementation = registry.getVersion(_version); } diff --git a/upgradeability_using_inherited_storage/contracts/UpgradeabilityStorage.sol b/upgradeability_using_inherited_storage/contracts/UpgradeabilityStorage.sol index 08ad454..876ad40 100644 --- a/upgradeability_using_inherited_storage/contracts/UpgradeabilityStorage.sol +++ b/upgradeability_using_inherited_storage/contracts/UpgradeabilityStorage.sol @@ -1,12 +1,13 @@ pragma solidity ^0.4.18; import './IRegistry.sol'; +import './ownership/Ownable.sol'; /** * @title UpgradeabilityStorage * @dev This contract holds all the necessary state variables to support the upgrade functionality */ -contract UpgradeabilityStorage { +contract UpgradeabilityStorage is Ownable { // Versions registry IRegistry internal registry; diff --git a/upgradeability_using_inherited_storage/contracts/ownership/Ownable.sol b/upgradeability_using_inherited_storage/contracts/ownership/Ownable.sol new file mode 100644 index 0000000..93f82d7 --- /dev/null +++ b/upgradeability_using_inherited_storage/contracts/ownership/Ownable.sol @@ -0,0 +1,41 @@ +pragma solidity ^0.4.18; + +import './OwnableStorage.sol'; + +/** + * @title Ownable + * @dev This contract has the owner address providing basic authorization control + */ +contract Ownable is OwnableStorage { + /** + * @dev Event to show ownership has been transferred + * @param previousOwner representing the address of the previous owner + * @param newOwner representing the address of the new owner + */ + event OwnershipTransferred(address previousOwner, address newOwner); + + /** + * @dev The constructor sets the original owner of the contract to the sender account. + */ + function Ownable() public { + setOwner(msg.sender); + } + + /** + * @dev Throws if called by any account other than the owner. + */ + modifier onlyOwner() { + require(msg.sender == owner()); + _; + } + + /** + * @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 { + require(newOwner != address(0)); + OwnershipTransferred(owner(), newOwner); + setOwner(newOwner); + } +} diff --git a/upgradeability_using_inherited_storage/contracts/ownership/OwnableStorage.sol b/upgradeability_using_inherited_storage/contracts/ownership/OwnableStorage.sol new file mode 100644 index 0000000..33f2c30 --- /dev/null +++ b/upgradeability_using_inherited_storage/contracts/ownership/OwnableStorage.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.4.18; + +/** + * @title OwnableStorage + * @dev This contract keeps track of the owner's address + */ +contract OwnableStorage { + // Owner of the contract + address private _owner; + + /** + * @dev Tells the address of the owner + * @return the address of the owner + */ + function owner() public view returns (address) { + return _owner; + } + + /** + * @dev Sets a new owner address + */ + function setOwner(address newOwner) internal { + _owner = newOwner; + } +}