github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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  	"fmt"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  	"github.com/hyperledger/fabric/msp"
    24  	mspproto "github.com/hyperledger/fabric/protos/msp"
    25  )
    26  
    27  // DeserializersManager is a support interface to
    28  // access the local and channel deserializers
    29  type DeserializersManager interface {
    30  	Deserialize(raw []byte) (*mspproto.SerializedIdentity, error)
    31  
    32  	// GetLocalMSPIdentifier returns the local MSP identifier
    33  	GetLocalMSPIdentifier() string
    34  
    35  	// GetLocalDeserializer returns the local identity deserializer
    36  	GetLocalDeserializer() msp.IdentityDeserializer
    37  
    38  	// GetChannelDeserializers returns a map of the channel deserializers
    39  	GetChannelDeserializers() map[string]msp.IdentityDeserializer
    40  }
    41  
    42  // DeserializersManager returns a new instance of DeserializersManager
    43  func NewDeserializersManager() DeserializersManager {
    44  	return &mspDeserializersManager{}
    45  }
    46  
    47  type mspDeserializersManager struct{}
    48  
    49  func (m *mspDeserializersManager) Deserialize(raw []byte) (*mspproto.SerializedIdentity, error) {
    50  	sId := &mspproto.SerializedIdentity{}
    51  	err := proto.Unmarshal(raw, sId)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
    54  	}
    55  	return sId, nil
    56  }
    57  
    58  func (m *mspDeserializersManager) GetLocalMSPIdentifier() string {
    59  	id, _ := GetLocalMSP().GetIdentifier()
    60  	return id
    61  }
    62  
    63  func (m *mspDeserializersManager) GetLocalDeserializer() msp.IdentityDeserializer {
    64  	return GetLocalMSP()
    65  }
    66  
    67  func (m *mspDeserializersManager) GetChannelDeserializers() map[string]msp.IdentityDeserializer {
    68  	return GetDeserializers()
    69  }