forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 11
/
idemix_roles.go
85 lines (72 loc) · 2.18 KB
/
idemix_roles.go
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msp
import (
m "github.com/hyperledger/fabric/protos/msp"
)
// Role : Represents a IdemixRole
type Role int32
// The expected roles are 4; We can combine them using a bitmask
const (
MEMBER Role = 1
ADMIN Role = 2
CLIENT Role = 4
PEER Role = 8
// Next role values: 16, 32, 64 ...
)
func (role Role) getValue() int {
return int(role)
}
// CheckRole Prove that the desired role is contained or not in the bitmask
func checkRole(bitmask int, role Role) bool {
return (bitmask & role.getValue()) == role.getValue()
}
// GetRoleMaskFromIdemixRoles Receive a list of roles to combine in a single bitmask
func getRoleMaskFromIdemixRoles(roles []Role) int {
mask := 0
for _, role := range roles {
mask = mask | role.getValue()
}
return mask
}
// GetRoleMaskFromMSPRoles Receive a list of roles to combine in a single bitmask
func getRoleMaskFromMSPRoles(roles []*m.MSPRole) int {
mask := 0
for _, role := range roles {
mask = mask | getIdemixRoleFromMSPRole(role)
}
return mask
}
// GetRoleMaskFromIdemixRole return a bitmask for one role
func GetRoleMaskFromIdemixRole(role Role) int {
return getRoleMaskFromIdemixRoles([]Role{role})
}
// GetRoleMaskFromMSPRole return a bitmask for one role
func getRoleMaskFromMSPRole(role *m.MSPRole) int {
return getRoleMaskFromMSPRoles([]*m.MSPRole{role})
}
// GetIdemixRoleFromMSPRole gets a MSP Role type and returns the integer value
func getIdemixRoleFromMSPRole(role *m.MSPRole) int {
return getIdemixRoleFromMSPRoleType(role.GetRole())
}
// GetIdemixRoleFromMSPRoleType gets a MSP role type and returns the integer value
func getIdemixRoleFromMSPRoleType(rtype m.MSPRole_MSPRoleType) int {
return getIdemixRoleFromMSPRoleValue(int(rtype))
}
// GetIdemixRoleFromMSPRoleValue Receives a MSP role value and returns the idemix equivalent
func getIdemixRoleFromMSPRoleValue(role int) int {
switch role {
case int(m.MSPRole_ADMIN):
return ADMIN.getValue()
case int(m.MSPRole_CLIENT):
return CLIENT.getValue()
case int(m.MSPRole_MEMBER):
return MEMBER.getValue()
case int(m.MSPRole_PEER):
return PEER.getValue()
default:
return MEMBER.getValue()
}
}