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