Skip to content

Commit

Permalink
Merge pull request #1 from ChronoBank/release/release-0.1.0Release/re…
Browse files Browse the repository at this point in the history
…lease 0.1.0
  • Loading branch information
Alex Rudyak committed Jul 19, 2018
2 parents 2f6fc0d + 1eda360 commit 61987b1
Show file tree
Hide file tree
Showing 31 changed files with 8,274 additions and 3,323 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build/*
contracts/*
node_modules/*
.*
*.json
truffle*
64 changes: 64 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = {
"parser": "babel-eslint",
"extends": "eslint:recommended",
"env": {
"es6": true,
"node": true,
"mocha": true,
},
"globals": {
"web3": false,
"artifacts": false,
"assert": false,
"contract": false,
},
"rules": {
"indent": ["error", "tab"],

"comma-dangle": ["error", "always"],
"no-cond-assign": ["error", "always"],
"no-await-in-loop": "off",
"block-scoped-var": "error",
"curly": "error",
"eqeqeq": ["error", "always"],
"no-alert": "error",
"no-else-return": ["error", { "allowElseIf": true }],
"no-magic-numbers": ["warn", { "ignoreArrayIndexes": true }],
"no-multi-spaces": ["error", { "exceptions": { "Property": true }}],
"no-restricted-syntax": ["error", "SequenceExpression"],
"no-useless-return": "warn",
"require-await": "warn",
"vars-on-top": "off",
"no-unused-vars": "warn",
"yoda": "error",

"global-require": "error",
"array-bracket-spacing": ["error", "always", { "singleValue": false, "objectsInArrays": false }],
"block-spacing": ["error", "always"],
"brace-style": ["error", "stroustrup", { "allowSingleLine": false }],
"camelcase": ["error", { "properties": "never" }],
"key-spacing": ["error", { "beforeColon": false, "afterColon": true, "mode": "strict" }],
"keyword-spacing": ["error", { "before": true, "after": true }],
"no-lonely-if": "error",
"no-negated-condition": "error",
"no-trailing-spaces": "error",
"no-whitespace-before-property": "error",
"object-curly-newline": ["error", { "multiline": true, "minProperties": 4 }],
"object-curly-spacing": ["error", "always"],
"object-property-newline": ["warn", { "allowAllPropertiesOnSameLine": true }],
"quote-props": ["warn", "as-needed", { "keywords": true, "numbers": true }],

"semi": ["error", "never"],

"arrow-parens": ["error", "as-needed"],
"arrow-spacing": "error",

"no-duplicate-imports": "error",
"prefer-const": "error",

"no-undef": "warn",
"no-empty": "warn",

"no-console": "off"
}
}
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contracts/helpers/*
contracts/Migrations.sol

test/
migrations/
.solcover.js
truffle.js
.gitattributes

.eslint*
.solium*
5 changes: 4 additions & 1 deletion .solcover.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module.exports = {
copyNodeModules: true,
skipFiles: [
'Migrations.sol',
'helpers/StubStorageManager.sol'
'helpers/Mock.sol',
'helpers/StubRoles2Library.sol',
'helpers/StubRoles2LibraryAdapter.sol',
'helpers/StubStorageManager.sol',
]
}
4 changes: 4 additions & 0 deletions .soliumignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
test
setup
build
24 changes: 24 additions & 0 deletions .soliumrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": "solium:recommended",
"plugins": [
"security"
],
"rules": {
"quotes": [
"error",
"double"
],
"indentation": [
"error",
4
],
"security/no-low-level-calls": [
"error", [
"callcode"
]
],
"max-line-length": "off",
"security/no-assign-params": "off",
"security/no-inline-assembly": "warning"
}
}
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ install:
script:
- npm test
before_script:
- ganache-cli > /dev/null &
- npm run testrpc > /dev/null &
- sleep 5
after_script:
- travis_wait 30 npm run coverage && cat coverage/lcov.info | coveralls
- travis_wait 50 npm run coverage && cat coverage/lcov.info | coveralls
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="0.1.0"></a>
# 0.1.0 (2018-07-17)
- add linters (eslint and solium);
- cover with more tests;
- integrate with MultiEventsHistoryAdapter;
- add migration templates;
- update to the latest dependencies.
88 changes: 87 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
# solidity-roles-lib [![Build Status](https://travis-ci.org/ChronoBank/solidity-roles-lib.svg?branch=master)](https://travis-ci.org/ChronoBank/solidity-roles-lib) [![Coverage Status](https://coveralls.io/repos/github/ChronoBank/solidity-roles-lib/badge.svg?branch=master)](https://coveralls.io/github/ChronoBank/solidity-roles-lib?branch=master)
# Roles smart contract library [![Build Status](https://travis-ci.org/ChronoBank/solidity-roles-lib.svg?branch=master)](https://travis-ci.org/ChronoBank/solidity-roles-lib) [![Coverage Status](https://coveralls.io/repos/github/ChronoBank/solidity-roles-lib/badge.svg?branch=master)](https://coveralls.io/github/ChronoBank/solidity-roles-lib?branch=master)

Part of [LaborX project](https://github.com/ChronoBank). Provides a couple of smart contracts to set up role-based access to system's contracts.

- **Roles2Library** - smart contract that is a foundation of roles-based system, organizes access to functions in secured way;
- **Roles2LibraryAdapter** - basic contract that is intended to store a reference to a roles2Library and contains protection modifier to guard functions for role-based access.

## Installation

Organized as npm package this smart contracts could be easily added to a project by

```bash
npm install -s solidity-roles-lib
```

## Usage

Right before you decided to use them add this library to package dependencies and import any contract according to this pattern, for example:

```javascript
import "solidity-shared-lib/contracts/Roles2Library.sol";
```

or

```javascript
import "solidity-shared-lib/contracts/Roles2LibraryAdapter.sol";
```

Cause you might want to use **Roles2Library** without any changes (if you want to then skip this paragraph), you will need to deploy this contract. But due to imperfection of **truffle** framework when you write in migration files `const Roles2Library = artifacts.require("Roles2Library")` this artifact will not be found. You have two options:
1. Inherit from _Roles2Library_ and **truffle** will automatically grap contract's artifact;
2. Create a solidity file, for example, **Imports.sol** and add an `import` statement of _Roles2Library_. (I would recommend this one because it will not produce one more contract name and looks more reasonable.)

## Details

Contracts that wants to adopt storage approach should do the following:

1. Define a contract that will inherit from **Roles2LibraryAdapter** contract and implement constructor:

```javascript
contract JobsController is Roles2LibraryAdapter {

constructor(address _roles2Library) Roles2LibraryAdapter(_roles2Library) public {
// TODO: initialization here
}
//...
}
```

3. Protect any of your functions with `auth` modifier to restrict an access to only authorized members:

```javascript
//...
function actionExample() external auth returns (uint) {
//...
}
//...
```

4. In your migrations or during contracts set up organize rules (roles) that should be able to access your protected functions. For more details look at **Roles2Library** documentation and migration templates.

```javascript
const Roles = {
ADMIN: 2,
MODERATOR: 4,
USER: 11,
}

const jobController = await JobsController.deployed()
// Allow only defined role to call protected function
const sig = jobsController.contract.actionExample.getData().slice(0, 10)
await roles2Library.addRoleCapability(Roles.ADMIN, jobsController.address, sig)
//...

```

## Migrations

Migration templates are presented in `./migrations_templates` folder so you can use them as a scaffolding for your own configuration. Basic scenarios covered by migration templates are:

- deploying and initializing _Roles2Library_ contract;
- deploying user's smart contract which is inherited from _Roles2LibraryAdapter_ contract;
- setuping role rules for user's contract

---

For more information and use cases look at tests.
14 changes: 6 additions & 8 deletions common/errors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const errorScope = {
roles: 20000
}
const errorScope = { roles: 20000, }

const errorCodes = {
UNAUTHORIZED: 0,
OK: 1,
UNAUTHORIZED: 0,
OK: 1,

ROLES_ALREADY_EXISTS: errorScope.roles + 1,
ROLES_INVALID_INVOCATION: errorScope.roles + 2,
ROLES_NOT_FOUND: errorScope.roles + 3
ROLES_ALREADY_EXISTS: errorScope.roles + 1,
ROLES_INVALID_INVOCATION: errorScope.roles + 2,
ROLES_NOT_FOUND: errorScope.roles + 3,
}

module.exports = errorCodes
30 changes: 15 additions & 15 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ pragma solidity ^0.4.23;

// Provided by Truffle.
contract Migrations {
address public owner;
uint public last_completed_migration;
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}
modifier restricted() {
if (msg.sender == owner) _;
}

constructor() public {
owner = msg.sender;
}
constructor() public {
owner = msg.sender;
}

function setCompleted(uint completed) restricted public {
last_completed_migration = completed;
}
function setCompleted(uint completed) restricted public {
last_completed_migration = completed;
}

function upgrade(address new_address) restricted public {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
function upgrade(address new_address) restricted public {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
Loading

0 comments on commit 61987b1

Please sign in to comment.