github.com/pvitto98/fabric@v2.1.1+incompatible/msp/idemix_roles.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package msp
     7  
     8  import (
     9  	m "github.com/hyperledger/fabric-protos-go/msp"
    10  )
    11  
    12  // Role : Represents a IdemixRole
    13  type Role int32
    14  
    15  // The expected roles are 4; We can combine them using a bitmask
    16  const (
    17  	MEMBER Role = 1
    18  	ADMIN  Role = 2
    19  	CLIENT Role = 4
    20  	PEER   Role = 8
    21  	// Next role values: 16, 32, 64 ...
    22  )
    23  
    24  func (role Role) getValue() int {
    25  	return int(role)
    26  }
    27  
    28  // checkRole Prove that the desired role is contained or not in the bitmask
    29  func checkRole(bitmask int, role Role) bool {
    30  	return (bitmask & role.getValue()) == role.getValue()
    31  }
    32  
    33  // getRoleMaskFromIdemixRoles Receive a list of roles to combine in a single bitmask
    34  func getRoleMaskFromIdemixRoles(roles []Role) int {
    35  	mask := 0
    36  	for _, role := range roles {
    37  		mask = mask | role.getValue()
    38  	}
    39  	return mask
    40  }
    41  
    42  // GetRoleMaskFromIdemixRole return a bitmask for one role
    43  func GetRoleMaskFromIdemixRole(role Role) int {
    44  	return getRoleMaskFromIdemixRoles([]Role{role})
    45  }
    46  
    47  // getIdemixRoleFromMSPRole gets a MSP Role type and returns the integer value
    48  func getIdemixRoleFromMSPRole(role *m.MSPRole) int {
    49  	return getIdemixRoleFromMSPRoleType(role.GetRole())
    50  }
    51  
    52  // GetIdemixRoleFromMSPRoleType gets a MSP role type and returns the integer value
    53  func getIdemixRoleFromMSPRoleType(rtype m.MSPRole_MSPRoleType) int {
    54  	return getIdemixRoleFromMSPRoleValue(int(rtype))
    55  }
    56  
    57  // getIdemixRoleFromMSPRoleValue Receives a MSP role value and returns the idemix equivalent
    58  func getIdemixRoleFromMSPRoleValue(role int) int {
    59  	switch role {
    60  	case int(m.MSPRole_ADMIN):
    61  		return ADMIN.getValue()
    62  	case int(m.MSPRole_CLIENT):
    63  		return CLIENT.getValue()
    64  	case int(m.MSPRole_MEMBER):
    65  		return MEMBER.getValue()
    66  	case int(m.MSPRole_PEER):
    67  		return PEER.getValue()
    68  	default:
    69  		return MEMBER.getValue()
    70  	}
    71  }