Warning! Contract bytecode has been changed and doesn't match the verified one. Therefore, interaction with this smart contract may be risky.
- Contract name:
- AuthLib
- Optimization enabled
- false
- Compiler version
- v0.8.26+commit.8a97fa7a
- EVM Version
- paris
- Verified at
- 2024-09-24T13:14:22.291934Z
contracts/Classes/Access.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
/**
* @title AuthLib
* @dev A library for managing role-based access control (RBAC) using ranks. Allows accounts to be granted or revoked roles, and checks for required ranks.
*/
library AuthLib {
// Custom error for unauthorized account access
error AccessControlUnauthorizedAccount(address account, bytes32 neededRole);
// Enum representing different ranks in the system
enum Rank {
PRINCEPS,
GLADIATOR,
LEGATUS,
SENATOR,
CONSUL,
PREATORMAXIMUS
}
// Registry structure to store keys and their corresponding ranks
struct Registry {
uint256[] keys; // Array to hold registered keys
mapping(uint256 => uint256) indexOf; // Maps key to its index in the keys array
mapping(uint256 => bool) inserted; // Tracks whether a key is inserted
mapping(uint256 => Rank) keyRoles; // Maps key to its assigned rank
}
// RoleData structure for managing roles of accounts
struct RoleData {
Registry registry; // Holds the registry for keys and roles
mapping(address => Rank) ranks; // Maps accounts to their assigned ranks
}
// Events
event RoleGranted(address indexed account, Rank role);
event RoleRevoked(address indexed account, Rank role);
/**
* @dev Registers a key with a specified rank in the registry.
* @param _registry The registry where the key will be stored.
* @param key The key to register (typically the address cast to uint256).
* @param rank The rank to assign to the key.
*/
function Register(Registry storage _registry, uint256 key, Rank rank) public {
if (!_registry.inserted[key]) {
_registry.inserted[key] = true;
_registry.indexOf[key] = _registry.keys.length;
_registry.keys.push(key);
_registry.keyRoles[key] = rank; // Store the rank with the key
} else {
// Update the rank if already registered
_registry.keyRoles[key] = rank;
}
}
/**
* @dev Removes a key from the registry.
* @param _registry The registry from which the key will be removed.
* @param key The key to remove.
*/
function Remove(Registry storage _registry, uint256 key) public {
if (!_registry.inserted[key]) return;
delete _registry.inserted[key];
uint256 index = _registry.indexOf[key];
uint256 lastKey = _registry.keys[_registry.keys.length - 1];
_registry.keys[index] = lastKey;
_registry.indexOf[lastKey] = index;
delete _registry.indexOf[key];
_registry.keys.pop();
delete _registry.keyRoles[key]; // Remove the associated role
}
/**
* @dev Grants a role (rank) to an account. This updates the registry and emits a RoleGranted event.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param account The account to grant the role to.
* @param rank The rank to assign to the account.
*/
function grantRole(RoleData storage roleData, address account, Rank rank) public {
uint256 key = uint256(uint160(account));
roleData.ranks[account] = rank;
Register(roleData.registry, key, rank);
emit RoleGranted(account, rank); // Emit event for granting role
}
/**
* @dev Revokes a role (rank) from an account. This updates the registry and emits a RoleRevoked event.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param account The account from which the role will be revoked.
*/
function revokeRole(RoleData storage roleData, address account) public {
uint256 key = uint256(uint160(account));
Rank role = roleData.ranks[account]; // Capture the role before removal for the event
delete roleData.ranks[account];
Remove(roleData.registry, key);
emit RoleRevoked(account, role); // Emit event for revoking role
}
/**
* @dev Checks whether an account holds the required role (rank) or higher.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param requiredRank The minimum required rank for the account.
* @param account The account to check for the required rank.
*/
function checkRole(RoleData storage roleData, Rank requiredRank, address account) public view {
require(roleData.ranks[account] >= requiredRank, "AccessControlUnauthorizedAccount");
}
/**
* @dev Retrieves the highest rank assigned to an account.
* @param roleData The RoleData struct that holds the registry and ranks.
* @param account The account whose rank is being queried.
* @return The highest rank held by the account.
*/
function getHighestRankForAccount(RoleData storage roleData, address account) public view returns (Rank) {
return roleData.ranks[account];
}
}
Compiler Settings
{"outputSelection":{"*":{"*":["abi","evm.bytecode","evm.deployedBytecode","evm.methodIdentifiers","metadata","storageLayout"],"":["ast"]}},"optimizer":{"runs":200,"enabled":false},"libraries":{},"evmVersion":"paris"}
Contract ABI
[{"type":"error","name":"AccessControlUnauthorizedAccount","inputs":[{"type":"address","name":"account","internalType":"address"},{"type":"bytes32","name":"neededRole","internalType":"bytes32"}]},{"type":"event","name":"RoleGranted","inputs":[{"type":"address","name":"account","internalType":"address","indexed":true},{"type":"uint8","name":"role","internalType":"enum AuthLib.Rank","indexed":false}],"anonymous":false},{"type":"event","name":"RoleRevoked","inputs":[{"type":"address","name":"account","internalType":"address","indexed":true},{"type":"uint8","name":"role","internalType":"enum AuthLib.Rank","indexed":false}],"anonymous":false}]
Contract Creation Code
0x610bc0610052600b82828239805160001a6073146045577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe730000000000000000000000000000000000000000301460806040526004361061006c5760003560e01c806301dba04c146100715780631fe3a8a3146100a1578063337b2d82146100ca578063401726b4146100f35780637fc35a911461011c5780639d14dc6014610145575b600080fd5b61008b60048036038101906100869190610786565b610161565b604051610098919061083d565b60405180910390f35b8180156100ad57600080fd5b506100c860048036038101906100c39190610786565b6101ba565b005b8180156100d657600080fd5b506100f160048036038101906100ec919061087d565b6102da565b005b8180156100ff57600080fd5b5061011a6004803603810190610115919061093c565b6103c0565b005b81801561012857600080fd5b50610143600480360381019061013e919061097c565b61050d565b005b61015f600480360381019061015a91906109cf565b610633565b005b60008260040160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16905092915050565b60008173ffffffffffffffffffffffffffffffffffffffff16905060008360040160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1690508360040160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81549060ff021916905561028684600001836103c0565b8273ffffffffffffffffffffffffffffffffffffffff167f34a1009b84e077aee5bc8197faf7105e54f9ba6879e2a51a716e4156ea9ad769826040516102cc9190610a31565b60405180910390a250505050565b60008273ffffffffffffffffffffffffffffffffffffffff169050818460040160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690836005811115610359576103586107c6565b5b021790555061036c84600001828461050d565b8273ffffffffffffffffffffffffffffffffffffffff167faa259565575c834bc07e74dca784b4071676133ac78513b431afb6ee7edae121836040516103b29190610a31565b60405180910390a250505050565b81600201600082815260200190815260200160002060009054906101000a900460ff16156105095781600201600082815260200190815260200160002060006101000a81549060ff02191690556000826001016000838152602001908152602001600020549050600083600001600185600001805490506104419190610a7b565b8154811061045257610451610aaf565b5b906000526020600020015490508084600001838154811061047657610475610aaf565b5b9060005260206000200181905550818460010160008381526020019081526020016000208190555083600101600084815260200190815260200160002060009055836000018054806104cb576104ca610ade565b5b6001900381819060005260206000200160009055905583600301600084815260200190815260200160002060006101000a81549060ff021916905550505b5050565b82600201600083815260200190815260200160002060009054906101000a900460ff166105f057600183600201600084815260200190815260200160002060006101000a81548160ff021916908315150217905550826000018054905083600101600084815260200190815260200160002081905550826000018290806001815401808255809150506001900390600052602060002001600090919091909150558083600301600084815260200190815260200160002060006101000a81548160ff021916908360058111156105e6576105e56107c6565b5b021790555061062e565b8083600301600084815260200190815260200160002060006101000a81548160ff02191690836005811115610628576106276107c6565b5b02179055505b505050565b816005811115610646576106456107c6565b5b8360040160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1660058111156106a7576106a66107c6565b5b10156106e8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106df90610b6a565b60405180910390fd5b505050565b600080fd5b6000819050919050565b610705816106f2565b811461071057600080fd5b50565b600081359050610722816106fc565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061075382610728565b9050919050565b61076381610748565b811461076e57600080fd5b50565b6000813590506107808161075a565b92915050565b6000806040838503121561079d5761079c6106ed565b5b60006107ab85828601610713565b92505060206107bc85828601610771565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b60068110610806576108056107c6565b5b50565b6000819050610817826107f5565b919050565b600061082782610809565b9050919050565b6108378161081c565b82525050565b6000602082019050610852600083018461082e565b92915050565b6006811061086557600080fd5b50565b60008135905061087781610858565b92915050565b600080600060608486031215610896576108956106ed565b5b60006108a486828701610713565b93505060206108b586828701610771565b92505060406108c686828701610868565b9150509250925092565b6000819050919050565b6108e3816108d0565b81146108ee57600080fd5b50565b600081359050610900816108da565b92915050565b6000819050919050565b61091981610906565b811461092457600080fd5b50565b60008135905061093681610910565b92915050565b60008060408385031215610953576109526106ed565b5b6000610961858286016108f1565b925050602061097285828601610927565b9150509250929050565b600080600060608486031215610995576109946106ed565b5b60006109a3868287016108f1565b93505060206109b486828701610927565b92505060406109c586828701610868565b9150509250925092565b6000806000606084860312156109e8576109e76106ed565b5b60006109f686828701610713565b9350506020610a0786828701610868565b9250506040610a1886828701610771565b9150509250925092565b610a2b8161081c565b82525050565b6000602082019050610a466000830184610a22565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610a8682610906565b9150610a9183610906565b9250828203905081811115610aa957610aa8610a4c565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603160045260246000fd5b600082825260208201905092915050565b7f416363657373436f6e74726f6c556e617574686f72697a65644163636f756e74600082015250565b6000610b54602083610b0d565b9150610b5f82610b1e565b602082019050919050565b60006020820190508181036000830152610b8381610b47565b905091905056fea26469706673582212206cff20c4c0041a5400a966f55358f1b41f630a679f64fff77c440b6438bd945a64736f6c634300081a0033
Deployed ByteCode
0x73c4d8811f729b26847b4619da49698be6f94bb912301460806040526004361061006c5760003560e01c806301dba04c146100715780631fe3a8a3146100a1578063337b2d82146100ca578063401726b4146100f35780637fc35a911461011c5780639d14dc6014610145575b600080fd5b61008b60048036038101906100869190610786565b610161565b604051610098919061083d565b60405180910390f35b8180156100ad57600080fd5b506100c860048036038101906100c39190610786565b6101ba565b005b8180156100d657600080fd5b506100f160048036038101906100ec919061087d565b6102da565b005b8180156100ff57600080fd5b5061011a6004803603810190610115919061093c565b6103c0565b005b81801561012857600080fd5b50610143600480360381019061013e919061097c565b61050d565b005b61015f600480360381019061015a91906109cf565b610633565b005b60008260040160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16905092915050565b60008173ffffffffffffffffffffffffffffffffffffffff16905060008360040160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1690508360040160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81549060ff021916905561028684600001836103c0565b8273ffffffffffffffffffffffffffffffffffffffff167f34a1009b84e077aee5bc8197faf7105e54f9ba6879e2a51a716e4156ea9ad769826040516102cc9190610a31565b60405180910390a250505050565b60008273ffffffffffffffffffffffffffffffffffffffff169050818460040160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690836005811115610359576103586107c6565b5b021790555061036c84600001828461050d565b8273ffffffffffffffffffffffffffffffffffffffff167faa259565575c834bc07e74dca784b4071676133ac78513b431afb6ee7edae121836040516103b29190610a31565b60405180910390a250505050565b81600201600082815260200190815260200160002060009054906101000a900460ff16156105095781600201600082815260200190815260200160002060006101000a81549060ff02191690556000826001016000838152602001908152602001600020549050600083600001600185600001805490506104419190610a7b565b8154811061045257610451610aaf565b5b906000526020600020015490508084600001838154811061047657610475610aaf565b5b9060005260206000200181905550818460010160008381526020019081526020016000208190555083600101600084815260200190815260200160002060009055836000018054806104cb576104ca610ade565b5b6001900381819060005260206000200160009055905583600301600084815260200190815260200160002060006101000a81549060ff021916905550505b5050565b82600201600083815260200190815260200160002060009054906101000a900460ff166105f057600183600201600084815260200190815260200160002060006101000a81548160ff021916908315150217905550826000018054905083600101600084815260200190815260200160002081905550826000018290806001815401808255809150506001900390600052602060002001600090919091909150558083600301600084815260200190815260200160002060006101000a81548160ff021916908360058111156105e6576105e56107c6565b5b021790555061062e565b8083600301600084815260200190815260200160002060006101000a81548160ff02191690836005811115610628576106276107c6565b5b02179055505b505050565b816005811115610646576106456107c6565b5b8360040160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1660058111156106a7576106a66107c6565b5b10156106e8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106df90610b6a565b60405180910390fd5b505050565b600080fd5b6000819050919050565b610705816106f2565b811461071057600080fd5b50565b600081359050610722816106fc565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061075382610728565b9050919050565b61076381610748565b811461076e57600080fd5b50565b6000813590506107808161075a565b92915050565b6000806040838503121561079d5761079c6106ed565b5b60006107ab85828601610713565b92505060206107bc85828601610771565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602160045260246000fd5b60068110610806576108056107c6565b5b50565b6000819050610817826107f5565b919050565b600061082782610809565b9050919050565b6108378161081c565b82525050565b6000602082019050610852600083018461082e565b92915050565b6006811061086557600080fd5b50565b60008135905061087781610858565b92915050565b600080600060608486031215610896576108956106ed565b5b60006108a486828701610713565b93505060206108b586828701610771565b92505060406108c686828701610868565b9150509250925092565b6000819050919050565b6108e3816108d0565b81146108ee57600080fd5b50565b600081359050610900816108da565b92915050565b6000819050919050565b61091981610906565b811461092457600080fd5b50565b60008135905061093681610910565b92915050565b60008060408385031215610953576109526106ed565b5b6000610961858286016108f1565b925050602061097285828601610927565b9150509250929050565b600080600060608486031215610995576109946106ed565b5b60006109a3868287016108f1565b93505060206109b486828701610927565b92505060406109c586828701610868565b9150509250925092565b6000806000606084860312156109e8576109e76106ed565b5b60006109f686828701610713565b9350506020610a0786828701610868565b9250506040610a1886828701610771565b9150509250925092565b610a2b8161081c565b82525050565b6000602082019050610a466000830184610a22565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610a8682610906565b9150610a9183610906565b9250828203905081811115610aa957610aa8610a4c565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603160045260246000fd5b600082825260208201905092915050565b7f416363657373436f6e74726f6c556e617574686f72697a65644163636f756e74600082015250565b6000610b54602083610b0d565b9150610b5f82610b1e565b602082019050919050565b60006020820190508181036000830152610b8381610b47565b905091905056fea26469706673582212206cff20c4c0041a5400a966f55358f1b41f630a679f64fff77c440b6438bd945a64736f6c634300081a0033