github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/scc/lscc/support.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package lscc
     8  
     9  import (
    10  	pb "github.com/hyperledger/fabric-protos-go/peer"
    11  	"github.com/hyperledger/fabric/common/cauthdsl"
    12  	"github.com/hyperledger/fabric/core/common/ccprovider"
    13  	"github.com/hyperledger/fabric/msp/mgmt"
    14  	"github.com/hyperledger/fabric/protoutil"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  type SupportImpl struct {
    19  	GetMSPIDs MSPIDsGetter
    20  }
    21  
    22  // PutChaincodeToLocalStorage stores the supplied chaincode
    23  // package to local storage (i.e. the file system)
    24  func (s *SupportImpl) PutChaincodeToLocalStorage(ccpack ccprovider.CCPackage) error {
    25  	if err := ccpack.PutChaincodeToFS(); err != nil {
    26  		return errors.Errorf("error installing chaincode code %s:%s(%s)", ccpack.GetChaincodeData().Name, ccpack.GetChaincodeData().Version, err)
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  // GetChaincodeFromLocalStorage retrieves the chaincode package
    33  // for the requested chaincode, specified by name and version
    34  func (s *SupportImpl) GetChaincodeFromLocalStorage(ccNameVersion string) (ccprovider.CCPackage, error) {
    35  	return ccprovider.GetChaincodeFromFS(ccNameVersion)
    36  }
    37  
    38  // GetChaincodesFromLocalStorage returns an array of all chaincode
    39  // data that have previously been persisted to local storage
    40  func (s *SupportImpl) GetChaincodesFromLocalStorage() (*pb.ChaincodeQueryResponse, error) {
    41  	return ccprovider.GetInstalledChaincodes()
    42  }
    43  
    44  // GetInstantiationPolicy returns the instantiation policy for the
    45  // supplied chaincode (or the channel's default if none was specified)
    46  func (s *SupportImpl) GetInstantiationPolicy(channel string, ccpack ccprovider.CCPackage) ([]byte, error) {
    47  	var ip []byte
    48  	var err error
    49  	// if ccpack is a SignedCDSPackage, return its IP, otherwise use a default IP
    50  	sccpack, isSccpack := ccpack.(*ccprovider.SignedCDSPackage)
    51  	if isSccpack {
    52  		ip = sccpack.GetInstantiationPolicy()
    53  		if ip == nil {
    54  			return nil, errors.Errorf("instantiation policy cannot be nil for a SignedCCDeploymentSpec")
    55  		}
    56  	} else {
    57  		// the default instantiation policy allows any of the channel MSP admins
    58  		// to be able to instantiate
    59  		mspids := s.GetMSPIDs(channel)
    60  
    61  		p := cauthdsl.SignedByAnyAdmin(mspids)
    62  		ip, err = protoutil.Marshal(p)
    63  		if err != nil {
    64  			return nil, errors.Errorf("error marshalling default instantiation policy")
    65  		}
    66  
    67  	}
    68  	return ip, nil
    69  }
    70  
    71  // CheckInstantiationPolicy checks whether the supplied signed proposal
    72  // complies with the supplied instantiation policy
    73  func (s *SupportImpl) CheckInstantiationPolicy(signedProp *pb.SignedProposal, chainName string, instantiationPolicy []byte) error {
    74  	// create a policy object from the policy bytes
    75  	mgr := mgmt.GetManagerForChain(chainName)
    76  	if mgr == nil {
    77  		return errors.Errorf("error checking chaincode instantiation policy: MSP manager for channel %s not found", chainName)
    78  	}
    79  	npp := cauthdsl.NewPolicyProvider(mgr)
    80  	instPol, _, err := npp.NewPolicy(instantiationPolicy)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	proposal, err := protoutil.UnmarshalProposal(signedProp.ProposalBytes)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	// get the signature header of the proposal
    89  	header, err := protoutil.UnmarshalHeader(proposal.Header)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	shdr, err := protoutil.UnmarshalSignatureHeader(header.SignatureHeader)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	// construct signed data we can evaluate the instantiation policy against
    98  	sd := []*protoutil.SignedData{{
    99  		Data:      signedProp.ProposalBytes,
   100  		Identity:  shdr.Creator,
   101  		Signature: signedProp.Signature,
   102  	}}
   103  	err = instPol.EvaluateSignedData(sd)
   104  	if err != nil {
   105  		return errors.WithMessage(err, "instantiation policy violation")
   106  	}
   107  	return nil
   108  }