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