forked from massalabs/massa-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessControl.ts
More file actions
90 lines (78 loc) · 2.31 KB
/
accessControl.ts
File metadata and controls
90 lines (78 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Args, boolToByte } from '@massalabs/as-types';
import {
_grantRole,
_hasRole,
_members,
_onlyRole,
_revokeRole,
} from './accessControl-internal';
import { balance, transferRemaining } from '@massalabs/massa-as-sdk';
/**
* Set the role for account
*
* @param role - role name string
* @param account - account address string
*/
export function grantRole(binaryArgs: StaticArray<u8>): void {
const initBal = balance();
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
const account = args
.nextString()
.expect('account argument is missing or invalid');
_grantRole(role, account);
transferRemaining(initBal);
}
/**
* get the members for a role
*
* @param role - role name string
*/
export function members(binaryArgs: StaticArray<u8>): StaticArray<u8> {
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
return new Args().add(_members(role)).serialize();
}
/**
* Returns true if the account has the role.
*
* @param role - role name string
* @param account - account address string
* @returns boolean
*/
export function hasRole(binaryArgs: StaticArray<u8>): StaticArray<u8> {
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
const account = args
.nextString()
.expect('account argument is missing or invalid');
return boolToByte(_hasRole(role, account));
}
/**
* Revoke role for account. Must be called by the role owner or the contract admin.
*
* @param role - role name string
* @param account - account address string
*/
export function revokeRole(binaryArgs: StaticArray<u8>): void {
const initBal = balance();
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
const account = args
.nextString()
.expect('account argument is missing or invalid');
_revokeRole(role, account);
transferRemaining(initBal);
}
/**
* Assert that caller has the role.
*
* @param role - role name string
* @returns boolean
*/
export function onlyRole(binaryArgs: StaticArray<u8>): void {
const role = new Args(binaryArgs)
.nextString()
.expect('role argument is missing or invalid');
_onlyRole(role);
}