Skip to content

Commit

Permalink
Merge pull request #194 from skalenetwork/enhancement/add-grant-role-api
Browse files Browse the repository at this point in the history
Enhancement/add grant role api
  • Loading branch information
DmytroNazarenko authored Apr 14, 2022
2 parents 9714c4e + f1a4ec3 commit 02ac546
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 12 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,24 +256,48 @@ Reserve space for certain address in Filestorage in bytes.
- **Note**: can be called only by ALLOCATOR_ROLE (or schain owner in v1 chains)

```javascript
filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace, privateKey);
filestorage.reserveSpace(allocatorAddress, addressToReserve, reservedSpace, privateKey);
```

Reserve space using external signing (Metamask etc):

```javascript
filestorage.reserveSpace(ownerAddress, addressToReserve, reservedSpace);
filestorage.reserveSpace(allocatorAddress, addressToReserve, reservedSpace);
```

##### Parameters

| Parameter | Description |
| ------------------------- | ----------------------------------------------------------- |
| `String` ownerAddress | sChain owner address |
| `String` allocatorAddress | Address with ALLOCATOR_ROLE |
| `String` addressToReserve | Address to reserve space for |
| `String` reservedSpace | Reserved space in bytes |
| `String` privateKey | _(optional)_ sChain owner private key, to sign transactions |

#### Grant allocator role

Granting allocator role to be able to reserve space in Filestorage

- **Note**: can be called only by ADMIN_ROLE (on v2+ chains)

```javascript
filestorage.grantAllocatorRole(adminAddress, allocatorAddress, adminPrivateKey);
```

Grant allocator role using external signing (Metamask etc):

```javascript
filestorage.grantAllocatorRole(adminAddress, allocatorAddress, adminPrivateKey);
```

##### Parameters

| Parameter | Description |
| ------------------------- | ----------------------------------------------------------- |
| `String` adminAddress | DEFAULT_ADMIN address |
| `String` allocatorAddress | Address to grant role for |
| `String` adminPrivateKey | _(optional)_ DEFAULT_ADMIN private key, to sign transactions|

#### Get reserved space

Get information about reserved space for account in bytes.
Expand Down
25 changes: 21 additions & 4 deletions src/FilestorageContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,36 @@ class FilestorageContract {

/**
* Javascript wrapper for solidity function reserveSpace. Reserve space in Filestorage for certain address.
* Allowed only for sChain owner
* Allowed only for address with ALLOCATOR_ROLE
*
* @function reserveSpace
*
* @param {string} ownerAddress - sChain owner address
* @param {string} allocatorAddress - Address with ALLOCATOR_ROLE
* @param {string} addressToReserve - Address to reserve space for
* @param {number} reservedSpace - Reserved space in bytes
* @param {string} [privateKey] - sChain owner private key
* @returns {object} Transaction information
*/
async reserveSpace(ownerAddress, addressToReserve, reservedSpace, privateKey = '') {
async reserveSpace(allocatorAddress, addressToReserve, reservedSpace, privateKey = '') {
let txData = this.contract.methods.reserveSpace(addressToReserve, reservedSpace);
return await transactions.send(this.web3, ownerAddress, privateKey, txData, constants.STANDARD_GAS);
return await transactions.send(this.web3, allocatorAddress, privateKey, txData, constants.STANDARD_GAS);
}

/**
* Javascript wrapper for function granting Allocator role
* Allowed only for DEFAULT_ADMIN
*
* @function grantAllocatorRole
*
* @param {string} adminAddress - Address with DEFAULT_ADMIN_ROLE
* @param {string} allocatorAddress - Address to grant role for
* @param {string} [privateKey] - Admin private key
* @returns {object} Transaction information
*/
async grantAllocatorRole(adminAddress, allocatorAddress, privateKey = '') {
let allocatorRole = await this.contract.methods.ALLOCATOR_ROLE().call();
let txData = this.contract.methods.grantRole(allocatorRole, allocatorAddress);
return await transactions.send(this.web3, adminAddress, privateKey, txData, constants.STANDARD_GAS);
}

/**
Expand Down
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,34 @@ class FilestorageClient {
}

/**
* Reserve space in Filestorage for certain address. Allowed only for sChain owner
* Reserve space in Filestorage for certain address. Allowed only for address with ALLOCATOR_ROLE
*
* @function reserveSpace
*
* @param {string} ownerAddress - sChain owner address
* @param {string} allocatorAddress - Address with ALLOCATOR_ROLE
* @param {string} addressToReserve - Address to reserve space for
* @param {number} reservedSpace - Reserved space in bytes
* @param {string} [privateKey] - sChain owner private key
* @param {string} [privateKey] - Allocator private key
*/
async reserveSpace(ownerAddress, addressToReserve, reservedSpace, privateKey) {
await this.contract.reserveSpace(ownerAddress, addressToReserve, reservedSpace, privateKey);
async reserveSpace(allocatorAddress, addressToReserve, reservedSpace, privateKey) {
await this.contract.reserveSpace(allocatorAddress, addressToReserve, reservedSpace, privateKey);
if (this.enableLogs) console.log('Space was allocated');
}

/**
* Grant allocator role for certain address. Allowed only for DEFAULT_ADMIN
*
* @function grantAllocatorRole
*
* @param {string} adminAddress - Address with DEFAULT_ADMIN_ROLE
* @param {string} allocatorAddress - Address to grant role for
* @param {string} [privateKey] - Admin private key
*/
async grantAllocatorRole(adminAddress, allocatorAddress, privateKey) {
await this.contract.grantAllocatorRole(adminAddress, allocatorAddress, privateKey);
if (this.enableLogs) console.log('Allocator role was granted');
}

/**
* Get information about reserved space for account
*
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,24 @@ describe('Test FilestorageClient', function () {
});
});

describe.only('test grantAllocatorRole', function () {
describe('Positive tests', function () {
it('should grant allocator role for account', async function () {
let owner = testHelper.getAddress(process.env.SCHAIN_OWNER_PK);
let account = await filestorage.web3.eth.accounts.create();
await filestorage.grantAllocatorRole(
owner,
account.address,
process.env.SCHAIN_OWNER_PK);
let isGranted = await filestorage.contract.contract.methods.hasRole(
await filestorage.contract.contract.methods.ALLOCATOR_ROLE().call(),
account.address
).call();
assert(isGranted === true);
});
});
});

describe('getters', function () {
it('should return total storage space', async function () {
let space = await filestorage.getTotalSpace();
Expand Down

0 comments on commit 02ac546

Please sign in to comment.