github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/protos/testutils/txtestutils.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 testutils
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	mmsp "github.com/hyperledger/fabric/common/mocks/msp"
    24  	"github.com/hyperledger/fabric/msp"
    25  	mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
    26  	"github.com/hyperledger/fabric/msp/mgmt/testtools"
    27  	"github.com/hyperledger/fabric/protos/common"
    28  	pb "github.com/hyperledger/fabric/protos/peer"
    29  	putils "github.com/hyperledger/fabric/protos/utils"
    30  )
    31  
    32  var (
    33  	signer msp.SigningIdentity
    34  )
    35  
    36  func init() {
    37  	var err error
    38  	// setup the MSP manager so that we can sign/verify
    39  	err = msptesttools.LoadMSPSetupForTesting()
    40  	if err != nil {
    41  		fmt.Printf("Could not load msp config, err %s", err)
    42  		os.Exit(-1)
    43  		return
    44  	}
    45  	signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
    46  	if err != nil {
    47  		os.Exit(-1)
    48  		fmt.Printf("Could not initialize msp/signer")
    49  		return
    50  	}
    51  }
    52  
    53  // ConstractBytesProposalResponsePayload constructs a ProposalResponsePayload byte for tests with a default signer.
    54  func ConstractBytesProposalResponsePayload(chainID string, ccid *pb.ChaincodeID, pResponse *pb.Response, simulationResults []byte) ([]byte, error) {
    55  	ss, err := signer.Serialize()
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	prop, _, err := putils.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: ccid}}, ss)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	presp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, pResponse, simulationResults, nil, ccid, nil, signer)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return presp.Payload, nil
    71  }
    72  
    73  // ConstructSingedTxEnvWithDefaultSigner constructs a transaction envelop for tests with a default signer.
    74  // This method helps other modules to construct a transaction with supplied parameters
    75  func ConstructSingedTxEnvWithDefaultSigner(chainID string, ccid *pb.ChaincodeID, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
    76  	return ConstructSingedTxEnv(chainID, ccid, response, simulationResults, events, visibility, signer)
    77  }
    78  
    79  // ConstructSingedTxEnv constructs a transaction envelop for tests
    80  func ConstructSingedTxEnv(chainID string, ccid *pb.ChaincodeID, pResponse *pb.Response, simulationResults []byte, events []byte, visibility []byte, signer msp.SigningIdentity) (*common.Envelope, string, error) {
    81  	ss, err := signer.Serialize()
    82  	if err != nil {
    83  		return nil, "", err
    84  	}
    85  
    86  	prop, txid, err := putils.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: ccid}}, ss)
    87  	if err != nil {
    88  		return nil, "", err
    89  	}
    90  
    91  	presp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, pResponse, simulationResults, nil, ccid, nil, signer)
    92  	if err != nil {
    93  		return nil, "", err
    94  	}
    95  
    96  	env, err := putils.CreateSignedTx(prop, signer, presp)
    97  	if err != nil {
    98  		return nil, "", err
    99  	}
   100  	return env, txid, nil
   101  }
   102  
   103  var mspLcl msp.MSP
   104  var sigId msp.SigningIdentity
   105  
   106  // ConstructUnsingedTxEnv creates a Transaction envelope from given inputs
   107  func ConstructUnsingedTxEnv(chainID string, ccid *pb.ChaincodeID, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
   108  	if mspLcl == nil {
   109  		mspLcl = mmsp.NewNoopMsp()
   110  		sigId, _ = mspLcl.GetDefaultSigningIdentity()
   111  	}
   112  
   113  	return ConstructSingedTxEnv(chainID, ccid, response, simulationResults, events, visibility, sigId)
   114  }