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

     1  /*
     2  Copyright IBM Corp. 2016 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 msp
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/flogging"
    23  	"github.com/hyperledger/fabric/protos/msp"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  )
    27  
    28  var mspLogger = flogging.MustGetLogger("msp")
    29  
    30  type mspManagerImpl struct {
    31  	// map that contains all MSPs that we have setup or otherwise added
    32  	mspsMap map[string]MSP
    33  
    34  	// error that might have occurred at startup
    35  	up bool
    36  }
    37  
    38  // NewMSPManager returns a new MSP manager instance;
    39  // note that this instance is not initialized until
    40  // the Setup method is called
    41  func NewMSPManager() MSPManager {
    42  	return &mspManagerImpl{}
    43  }
    44  
    45  // Setup initializes the internal data structures of this manager and creates MSPs
    46  func (mgr *mspManagerImpl) Setup(msps []MSP) error {
    47  	if mgr.up {
    48  		mspLogger.Infof("MSP manager already up")
    49  		return nil
    50  	}
    51  
    52  	if msps == nil {
    53  		return fmt.Errorf("Setup error: nil config object")
    54  	}
    55  
    56  	if len(msps) == 0 {
    57  		return fmt.Errorf("Setup error: at least one MSP configuration item is required")
    58  	}
    59  
    60  	mspLogger.Debugf("Setting up the MSP manager (%d msps)", len(msps))
    61  
    62  	// create the map that assigns MSP IDs to their manager instance - once
    63  	mgr.mspsMap = make(map[string]MSP)
    64  
    65  	for _, msp := range msps {
    66  		// add the MSP to the map of active MSPs
    67  		mspID, err := msp.GetIdentifier()
    68  		if err != nil {
    69  			return fmt.Errorf("Could not extract msp identifier, err %s", err)
    70  		}
    71  		mgr.mspsMap[mspID] = msp
    72  	}
    73  
    74  	mgr.up = true
    75  
    76  	mspLogger.Debugf("MSP manager setup complete, setup %d msps", len(msps))
    77  
    78  	return nil
    79  }
    80  
    81  // GetMSPs returns the MSPs that are managed by this manager
    82  func (mgr *mspManagerImpl) GetMSPs() (map[string]MSP, error) {
    83  	return mgr.mspsMap, nil
    84  }
    85  
    86  // DeserializeIdentity returns an identity given its serialized version supplied as argument
    87  func (mgr *mspManagerImpl) DeserializeIdentity(serializedID []byte) (Identity, error) {
    88  	// We first deserialize to a SerializedIdentity to get the MSP ID
    89  	sId := &msp.SerializedIdentity{}
    90  	err := proto.Unmarshal(serializedID, sId)
    91  	if err != nil {
    92  		return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
    93  	}
    94  
    95  	// we can now attempt to obtain the MSP
    96  	msp := mgr.mspsMap[sId.Mspid]
    97  	if msp == nil {
    98  		return nil, fmt.Errorf("MSP %s is unknown", sId.Mspid)
    99  	}
   100  
   101  	switch t := msp.(type) {
   102  	case *bccspmsp:
   103  		return t.deserializeIdentityInternal(sId.IdBytes)
   104  	default:
   105  		return t.DeserializeIdentity(serializedID)
   106  	}
   107  }