github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	"path/filepath"
    24  
    25  	"github.com/hyperledger/fabric/msp"
    26  	mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
    27  	"github.com/hyperledger/fabric/msp/mgmt/testtools"
    28  	"github.com/hyperledger/fabric/protos/common"
    29  	pb "github.com/hyperledger/fabric/protos/peer"
    30  	putils "github.com/hyperledger/fabric/protos/utils"
    31  )
    32  
    33  var (
    34  	signer msp.SigningIdentity
    35  )
    36  
    37  func init() {
    38  	var err error
    39  	// setup the MSP manager so that we can sign/verify
    40  	mspMgrConfigDir, err := getMSPMgrConfigDir()
    41  	if err != nil {
    42  		fmt.Printf("Could not get location of msp manager config file")
    43  		os.Exit(-1)
    44  		return
    45  	}
    46  	err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
    47  	if err != nil {
    48  		fmt.Printf("Could not load msp config, err %s", err)
    49  		os.Exit(-1)
    50  		return
    51  	}
    52  	signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
    53  	if err != nil {
    54  		os.Exit(-1)
    55  		fmt.Printf("Could not initialize msp/signer")
    56  		return
    57  	}
    58  }
    59  
    60  func getMSPMgrConfigDir() (string, error) {
    61  	var pwd string
    62  	var err error
    63  	if pwd, err = os.Getwd(); err != nil {
    64  		return "", err
    65  	}
    66  	path := pwd
    67  	dir := ""
    68  	for {
    69  		path, dir = filepath.Split(path)
    70  		path = filepath.Clean(path)
    71  		fmt.Printf("path=%s, dir=%s\n", path, dir)
    72  		if dir == "fabric" {
    73  			break
    74  		}
    75  	}
    76  	filePath := filepath.Join(path, "fabric/msp/sampleconfig/")
    77  	fmt.Printf("filePath=%s\n", filePath)
    78  	return filePath, nil
    79  }
    80  
    81  // ConstructSingedTxEnvWithDefaultSigner constructs a transaction envelop for tests with a default signer.
    82  // This method helps other modules to construct a transaction with supplied parameters
    83  func ConstructSingedTxEnvWithDefaultSigner(chainID, ccName string, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
    84  	return ConstructSingedTxEnv(chainID, ccName, response, simulationResults, events, visibility, signer)
    85  }
    86  
    87  // ConstructSingedTxEnv constructs a transaction envelop for tests
    88  func ConstructSingedTxEnv(chainID string, ccName string, pResponse *pb.Response, simulationResults []byte, events []byte, visibility []byte, signer msp.SigningIdentity) (*common.Envelope, string, error) {
    89  	ss, err := signer.Serialize()
    90  	if err != nil {
    91  		return nil, "", err
    92  	}
    93  
    94  	prop, txid, err := putils.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: &pb.ChaincodeID{Name: ccName}}}, ss)
    95  	if err != nil {
    96  		return nil, "", err
    97  	}
    98  
    99  	presp, err := putils.CreateProposalResponse(prop.Header, prop.Payload, pResponse, simulationResults, nil, nil, signer)
   100  	if err != nil {
   101  		return nil, "", err
   102  	}
   103  
   104  	env, err := putils.CreateSignedTx(prop, signer, presp)
   105  	if err != nil {
   106  		return nil, "", err
   107  	}
   108  	return env, txid, nil
   109  }
   110  
   111  var mspLcl msp.MSP
   112  var sigId msp.SigningIdentity
   113  
   114  // ConstructUnsingedTxEnv creates a Transaction envelope from given inputs
   115  func ConstructUnsingedTxEnv(chainID string, ccName string, response *pb.Response, simulationResults []byte, events []byte, visibility []byte) (*common.Envelope, string, error) {
   116  	if mspLcl == nil {
   117  		mspLcl = msp.NewNoopMsp()
   118  		sigId, _ = mspLcl.GetDefaultSigningIdentity()
   119  	}
   120  
   121  	return ConstructSingedTxEnv(chainID, ccName, response, simulationResults, events, visibility, sigId)
   122  }