npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bitmask-rbac

v2.0.0

Published

Solidity RBAC that manages user permissions for ethereum dApps

Downloads

5

Readme

bitmask-rbac

RBAC stands for Role Based Access Control. This solidity contract can be used to control access to Ethereum smart contracts and their functions, by assigning roles to users and having functions require certain roles.

This is a work in progress, built for and used by the TruSet dApp.

This implementation provides only one role to start with: rbac_admin. Other roles can be added up to a maximum of 256 by calling addUserRole(). Once added, roles can never be removed.

A user can be granted roles or have them revoked individually using grantRole() and revokeRole(). Or multiple role changes can be applied atomically using bitmasks (getUserRoleBitmask(), setUserRoles()). For example if there are three roles: rbac_admin, publish, validate (in that order) then:

  • users with a role bitmask of 101 (5) would be able to admin and validate, but not publish
  • users with a role bitmask of 011 (3) would be able to publish and validate, but not admin
  • users with a role bitmask of 001 (1) are only able to validate

It is anticipated that solidity contracts will inherit this bas contract to extend it with additional functionality and/or restrictions.

The power of the 'rbac_admin' role

Any user with the 'rbac_admin' role must be trusted by the system. They have the ability to arbitrarily change user roles, including their own, but they also have the ability to compromise the RBAC itself in multiple and not always obvious ways:

  • they could remove the permissions of all other rbac_admins, leaving themselves in sole control of the contract
  • they could add roles until there are 256 roles, at which point no more roles can ever be added (nor deleted) and any code that tries to add roles will fail
  • they could add roles that have names that look the same as existing role names, but which use different unicode characters. This approach would result in seemingly-identical roles with very different properties, and could be used to trick smart contract users or auditors into thinking the system does something it does not.

Usage

You can check a user's permission in the RBAC with function modifiers.

contract BitmaskRBACPermissionable {
  string public constant ROLE_RBAC_ADMIN = "rbac_admin";

  modifier onlyAdmin() {
    require(rbac.hasRole(msg.sender, ROLE_RBAC_ADMIN));
    _;
  }
}

contract MyContract is BitmaskRBACPermissionable{
  constructor(address _rbac) public {
    rbac = BitmaskRBAC(_rbac);
  }

  function manageAppFunction() external
  onlyAdmin()
  {
  // something only admin can do
  }
}

To create a new user, just specify the address, name (optional), and bitmask of the required roles:

await rbac.newUser(user_address, 'Contract Creator', ADMIN | PUBLISH | VALIDATE)

Usage (Javascript)

Once you have imported your user roles into javascript (e.g. using drizzle), it is super easy to define appropriate constants (e.g. ADMIN = 1; PUBLISH = 2; VALIDATE = 4) and then check whether a user bitmask allows a given role or roles. E.g. (user.role & PUBLISH) !== 0 to check if a user can publish, or even (user.role & (VALIDATE | ADMIN)) !== 0 to check if a user can Validate or Admin.