github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/scc/lscc/support.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package lscc
     8  
     9  import (
    10  	"github.com/hechain20/hechain/common/cauthdsl"
    11  	"github.com/hechain20/hechain/common/policydsl"
    12  	"github.com/hechain20/hechain/core/common/ccprovider"
    13  	"github.com/hechain20/hechain/msp"
    14  	"github.com/hechain20/hechain/protoutil"
    15  	pb "github.com/hyperledger/fabric-protos-go/peer"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  type SupportImpl struct {
    20  	GetMSPIDs               MSPIDsGetter
    21  	GetIdentityDeserializer func(chainID string) msp.IdentityDeserializer
    22  }
    23  
    24  // PutChaincodeToLocalStorage stores the supplied chaincode
    25  // package to local storage (i.e. the file system)
    26  func (s *SupportImpl) PutChaincodeToLocalStorage(ccpack ccprovider.CCPackage) error {
    27  	if err := ccpack.PutChaincodeToFS(); err != nil {
    28  		return errors.Errorf("error installing chaincode code %s:%s(%s)", ccpack.GetChaincodeData().Name, ccpack.GetChaincodeData().Version, err)
    29  	}
    30  
    31  	return nil
    32  }
    33  
    34  // GetChaincodeFromLocalStorage retrieves the chaincode package
    35  // for the requested chaincode, specified by name and version
    36  func (s *SupportImpl) GetChaincodeFromLocalStorage(ccNameVersion string) (ccprovider.CCPackage, error) {
    37  	return ccprovider.GetChaincodeFromFS(ccNameVersion)
    38  }
    39  
    40  // GetChaincodesFromLocalStorage returns an array of all chaincode
    41  // data that have previously been persisted to local storage
    42  func (s *SupportImpl) GetChaincodesFromLocalStorage() (*pb.ChaincodeQueryResponse, error) {
    43  	return ccprovider.GetInstalledChaincodes()
    44  }
    45  
    46  // GetInstantiationPolicy returns the instantiation policy for the
    47  // supplied chaincode (or the channel's default if none was specified)
    48  func (s *SupportImpl) GetInstantiationPolicy(channel string, ccpack ccprovider.CCPackage) ([]byte, error) {
    49  	var ip []byte
    50  	var err error
    51  	// if ccpack is a SignedCDSPackage, return its IP, otherwise use a default IP
    52  	sccpack, isSccpack := ccpack.(*ccprovider.SignedCDSPackage)
    53  	if isSccpack {
    54  		ip = sccpack.GetInstantiationPolicy()
    55  		if ip == nil {
    56  			return nil, errors.Errorf("instantiation policy cannot be nil for a SignedCCDeploymentSpec")
    57  		}
    58  	} else {
    59  		// the default instantiation policy allows any of the channel MSP admins
    60  		// to be able to instantiate
    61  		mspids := s.GetMSPIDs(channel)
    62  
    63  		p := policydsl.SignedByAnyAdmin(mspids)
    64  		ip, err = protoutil.Marshal(p)
    65  		if err != nil {
    66  			return nil, errors.Errorf("error marshalling default instantiation policy")
    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  	idd := s.GetIdentityDeserializer(chainName)
    77  	if idd == nil {
    78  		return errors.Errorf("error checking chaincode instantiation policy: MSP manager for channel %s not found", chainName)
    79  	}
    80  	npp := cauthdsl.NewPolicyProvider(idd)
    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  }