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