Skip to content

Instantly share code, notes, and snippets.

@PhABC
Last active May 9, 2023 07:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhABC/3d9f865592035ffde450b59aedcbaed6 to your computer and use it in GitHub Desktop.
Save PhABC/3d9f865592035ffde450b59aedcbaed6 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.18;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() public {
owner = 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);
owner = newOwner;
}
}
/* @title ControlledAccess
* @dev The ControlledAccess contract allows function to be restricted to users
* that possess a signed authorization from the owner of the contract. This signed
* message includes the user to give permission to and the contract address to prevent
* reusing the same authorization message on different contract with same owner.
*/
contract ControlledAccess is Ownable {
/*
* @dev Requires msg.sender to have valid access message.
* @param _v ECDSA signature parameter v.
* @param _r ECDSA signature parameters r.
* @param _s ECDSA signature parameters s.
*/
modifier onlyValidAccess(uint8 _v, bytes32 _r, bytes32 _s)
{
require( isValidAccessMessage(msg.sender,_v,_r,_s) );
_;
}
/*
* @dev Verifies if message was signed by owner to give access to _add for this contract.
* Assumes Geth signature prefix.
* @param _add Address of agent with access
* @param _v ECDSA signature parameter v.
* @param _r ECDSA signature parameters r.
* @param _s ECDSA signature parameters s.
* @return Validity of access message for a given address.
*/
function isValidAccessMessage(
address _add,
uint8 _v,
bytes32 _r,
bytes32 _s)
view public returns (bool)
{
bytes32 hash = keccak256(this, _add);
return owner == ecrecover(
keccak256("\x19Ethereum Signed Message:\n32", hash),
_v,
_r,
_s
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment