Skip to content

Instantly share code, notes, and snippets.

@cmditch
Last active April 6, 2018 00:37
Show Gist options
  • Save cmditch/3d0666b2970619b32a7a005844d945dd to your computer and use it in GitHub Desktop.
Save cmditch/3d0666b2970619b32a7a005844d945dd to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
//////////////////////////////
// //
// DSAuthority //
// //
//////////////////////////////
contract DSAuthority {
function canCall(
address src, address dst, bytes4 sig
) public view returns (bool);
}
contract DSAuthEvents {
event LogSetAuthority (address indexed authority);
event LogSetOwner (address indexed owner);
}
//////////////////////////////
// //
// DSAuth //
// //
//////////////////////////////
contract DSAuth is DSAuthEvents {
DSAuthority public authority;
address public owner;
function DSAuth() public {
owner = msg.sender;
emit LogSetOwner(msg.sender);
}
function setOwner(address owner_)
public
auth
{
owner = owner_;
emit LogSetOwner(owner);
}
function setAuthority(DSAuthority authority_)
public
auth
{
authority = authority_;
emit LogSetAuthority(authority);
}
modifier auth {
require(isAuthorized(msg.sender, msg.sig));
_;
}
function isAuthorized(address src, bytes4 sig) internal view returns (bool) {
if (src == address(this)) {
return true;
} else if (src == owner) {
return true;
} else if (authority == DSAuthority(0)) {
return false;
} else {
return authority.canCall(src, this, sig);
}
}
}
//////////////////////////////
// //
// DSGuard //
// //
//////////////////////////////
contract DSGuardEvents {
event LogPermit(
bytes32 indexed src,
bytes32 indexed dst,
bytes32 indexed sig
);
event LogForbid(
bytes32 indexed src,
bytes32 indexed dst,
bytes32 indexed sig
);
}
contract DSGuard is DSAuth, DSAuthority, DSGuardEvents {
bytes32 constant public ANY = bytes32(uint(-1));
mapping (bytes32 => mapping (bytes32 => mapping (bytes32 => bool))) acl;
function canCall(
address src_, address dst_, bytes4 sig
) public view returns (bool) {
var src = bytes32(src_);
var dst = bytes32(dst_);
return acl[src][dst][sig]
|| acl[src][dst][ANY]
|| acl[src][ANY][sig]
|| acl[src][ANY][ANY]
|| acl[ANY][dst][sig]
|| acl[ANY][dst][ANY]
|| acl[ANY][ANY][sig]
|| acl[ANY][ANY][ANY];
}
function permit(bytes32 src, bytes32 dst, bytes32 sig) public auth {
acl[src][dst][sig] = true;
LogPermit(src, dst, sig);
}
function forbid(bytes32 src, bytes32 dst, bytes32 sig) public auth {
acl[src][dst][sig] = false;
LogForbid(src, dst, sig);
}
function permit(address src, address dst, bytes32 sig) public {
permit(bytes32(src), bytes32(dst), sig);
}
function forbid(address src, address dst, bytes32 sig) public {
forbid(bytes32(src), bytes32(dst), sig);
}
}
contract DSGuardFactory {
mapping (address => bool) public isGuard;
function newGuard() public returns (DSGuard guard) {
guard = new DSGuard();
guard.setOwner(msg.sender);
isGuard[guard] = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment