github.com/kaituanwang/hyperledger@v2.0.1+incompatible/msp/mgmt/deserializer.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  	"github.com/golang/protobuf/proto"
    21  	mspproto "github.com/hyperledger/fabric-protos-go/msp"
    22  	"github.com/hyperledger/fabric/bccsp"
    23  	"github.com/hyperledger/fabric/msp"
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  // DeserializersManager is a support interface to
    28  // access the local and channel deserializers
    29  type DeserializersManager interface {
    30  
    31  	// Deserialize receives SerializedIdentity bytes and returns the unmarshaled form
    32  	// of the SerializedIdentity, or error on failure
    33  	Deserialize(raw []byte) (*mspproto.SerializedIdentity, error)
    34  
    35  	// GetLocalMSPIdentifier returns the local MSP identifier
    36  	GetLocalMSPIdentifier() string
    37  
    38  	// GetLocalDeserializer returns the local identity deserializer
    39  	GetLocalDeserializer() msp.IdentityDeserializer
    40  
    41  	// GetChannelDeserializers returns a map of the channel deserializers
    42  	GetChannelDeserializers() map[string]msp.IdentityDeserializer
    43  }
    44  
    45  // NewDeserializersManager returns a new instance of DeserializersManager
    46  func NewDeserializersManager(cryptoProvider bccsp.BCCSP) DeserializersManager {
    47  	return &mspDeserializersManager{
    48  		cryptoProvider: cryptoProvider,
    49  	}
    50  }
    51  
    52  type mspDeserializersManager struct {
    53  	cryptoProvider bccsp.BCCSP
    54  }
    55  
    56  func (m *mspDeserializersManager) Deserialize(raw []byte) (*mspproto.SerializedIdentity, error) {
    57  	sId := &mspproto.SerializedIdentity{}
    58  	err := proto.Unmarshal(raw, sId)
    59  	if err != nil {
    60  		return nil, errors.Wrap(err, "could not deserialize a SerializedIdentity")
    61  	}
    62  	return sId, nil
    63  }
    64  
    65  func (m *mspDeserializersManager) GetLocalMSPIdentifier() string {
    66  	id, _ := GetLocalMSP(m.cryptoProvider).GetIdentifier()
    67  	return id
    68  }
    69  
    70  func (m *mspDeserializersManager) GetLocalDeserializer() msp.IdentityDeserializer {
    71  	return GetLocalMSP(m.cryptoProvider)
    72  }
    73  
    74  func (m *mspDeserializersManager) GetChannelDeserializers() map[string]msp.IdentityDeserializer {
    75  	return GetDeserializers()
    76  }