github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/msp/mgmt/principal.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mgmt
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	"github.com/hyperledger/fabric/protos/msp"
    24  )
    25  
    26  const (
    27  	// Admins is the label for the local MSP admins
    28  	Admins = "Admins"
    29  
    30  	// Members is the label for the local MSP members
    31  	Members = "Members"
    32  )
    33  
    34  type MSPPrincipalGetter interface {
    35  	// Get returns an MSP principal for the given role
    36  	Get(role string) (*msp.MSPPrincipal, error)
    37  }
    38  
    39  func NewLocalMSPPrincipalGetter() MSPPrincipalGetter {
    40  	return &localMSPPrincipalGetter{}
    41  }
    42  
    43  type localMSPPrincipalGetter struct{}
    44  
    45  func (m *localMSPPrincipalGetter) Get(role string) (*msp.MSPPrincipal, error) {
    46  	mspid, err := GetLocalMSP().GetIdentifier()
    47  	if err != nil {
    48  		return nil, fmt.Errorf("Could not extract local msp identifier [%s]", err)
    49  	}
    50  
    51  	switch role {
    52  	case Admins:
    53  		principalBytes, err := proto.Marshal(&msp.MSPRole{Role: msp.MSPRole_ADMIN, MspIdentifier: mspid})
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  
    58  		return &msp.MSPPrincipal{
    59  			PrincipalClassification: msp.MSPPrincipal_ROLE,
    60  			Principal:               principalBytes}, nil
    61  	case Members:
    62  		principalBytes, err := proto.Marshal(&msp.MSPRole{Role: msp.MSPRole_MEMBER, MspIdentifier: mspid})
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  
    67  		return &msp.MSPPrincipal{
    68  			PrincipalClassification: msp.MSPPrincipal_ROLE,
    69  			Principal:               principalBytes}, nil
    70  	default:
    71  		return nil, fmt.Errorf("MSP Principal role [%s] not recognized.", role)
    72  	}
    73  }