github.com/lzy4123/fabric@v2.1.1+incompatible/internal/peer/lifecycle/chaincode/common.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "context" 11 "encoding/json" 12 "fmt" 13 "io" 14 15 "github.com/golang/protobuf/proto" 16 pb "github.com/hyperledger/fabric-protos-go/peer" 17 "github.com/hyperledger/fabric/common/policydsl" 18 "github.com/hyperledger/fabric/internal/peer/chaincode" 19 "github.com/hyperledger/fabric/protoutil" 20 "github.com/pkg/errors" 21 "google.golang.org/grpc" 22 ) 23 24 // EndorserClient defines the interface for sending a proposal 25 // to an endorser 26 type EndorserClient interface { 27 ProcessProposal(ctx context.Context, in *pb.SignedProposal, opts ...grpc.CallOption) (*pb.ProposalResponse, error) 28 } 29 30 // PeerDeliverClient defines the interface for a peer deliver client 31 type PeerDeliverClient interface { 32 Deliver(ctx context.Context, opts ...grpc.CallOption) (pb.Deliver_DeliverClient, error) 33 DeliverFiltered(ctx context.Context, opts ...grpc.CallOption) (pb.Deliver_DeliverClient, error) 34 } 35 36 // Signer defines the interface needed for signing messages 37 type Signer interface { 38 Sign(msg []byte) ([]byte, error) 39 Serialize() ([]byte, error) 40 } 41 42 // Writer defines the interface needed for writing a file 43 type Writer interface { 44 WriteFile(string, string, []byte) error 45 } 46 47 func signProposal(proposal *pb.Proposal, signer Signer) (*pb.SignedProposal, error) { 48 // check for nil argument 49 if proposal == nil { 50 return nil, errors.New("proposal cannot be nil") 51 } 52 53 if signer == nil { 54 return nil, errors.New("signer cannot be nil") 55 } 56 57 proposalBytes, err := proto.Marshal(proposal) 58 if err != nil { 59 return nil, errors.Wrap(err, "error marshaling proposal") 60 } 61 62 signature, err := signer.Sign(proposalBytes) 63 if err != nil { 64 return nil, err 65 } 66 67 return &pb.SignedProposal{ 68 ProposalBytes: proposalBytes, 69 Signature: signature, 70 }, nil 71 } 72 73 func createPolicyBytes(signaturePolicy, channelConfigPolicy string) ([]byte, error) { 74 if signaturePolicy == "" && channelConfigPolicy == "" { 75 // no policy, no problem 76 return nil, nil 77 } 78 79 if signaturePolicy != "" && channelConfigPolicy != "" { 80 // mo policies, mo problems 81 return nil, errors.New("cannot specify both \"--signature-policy\" and \"--channel-config-policy\"") 82 } 83 84 var applicationPolicy *pb.ApplicationPolicy 85 if signaturePolicy != "" { 86 signaturePolicyEnvelope, err := policydsl.FromString(signaturePolicy) 87 if err != nil { 88 return nil, errors.Errorf("invalid signature policy: %s", signaturePolicy) 89 } 90 91 applicationPolicy = &pb.ApplicationPolicy{ 92 Type: &pb.ApplicationPolicy_SignaturePolicy{ 93 SignaturePolicy: signaturePolicyEnvelope, 94 }, 95 } 96 } 97 98 if channelConfigPolicy != "" { 99 applicationPolicy = &pb.ApplicationPolicy{ 100 Type: &pb.ApplicationPolicy_ChannelConfigPolicyReference{ 101 ChannelConfigPolicyReference: channelConfigPolicy, 102 }, 103 } 104 } 105 106 policyBytes := protoutil.MarshalOrPanic(applicationPolicy) 107 return policyBytes, nil 108 } 109 110 func createCollectionConfigPackage(collectionsConfigFile string) (*pb.CollectionConfigPackage, error) { 111 var ccp *pb.CollectionConfigPackage 112 if collectionsConfigFile != "" { 113 var err error 114 ccp, _, err = chaincode.GetCollectionConfigFromFile(collectionsConfigFile) 115 if err != nil { 116 return nil, errors.WithMessagef(err, "invalid collection configuration in file %s", collectionsConfigFile) 117 } 118 } 119 return ccp, nil 120 } 121 122 func printResponseAsJSON(proposalResponse *pb.ProposalResponse, msg proto.Message, out io.Writer) error { 123 err := proto.Unmarshal(proposalResponse.Response.Payload, msg) 124 if err != nil { 125 return errors.Wrapf(err, "failed to unmarshal proposal response's response payload as type %T", msg) 126 } 127 128 bytes, err := json.MarshalIndent(msg, "", "\t") 129 if err != nil { 130 return errors.Wrap(err, "failed to marshal output") 131 } 132 133 fmt.Fprintf(out, "%s\n", string(bytes)) 134 135 return nil 136 }