forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
225 lines (195 loc) · 6.06 KB
/
package.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"io/ioutil"
"github.com/golang/protobuf/proto"
pcommon "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/core/common/ccpackage"
"github.com/hyperledger/fabric/internal/pkg/identity"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
chaincodePackageCmd *cobra.Command
createSignedCCDepSpec bool
signCCDepSpec bool
instantiationPolicy string
)
const packageCmdName = "package"
type ccDepSpecFactory func(spec *pb.ChaincodeSpec) (*pb.ChaincodeDeploymentSpec, error)
func defaultCDSFactory(spec *pb.ChaincodeSpec) (*pb.ChaincodeDeploymentSpec, error) {
return getChaincodeDeploymentSpec(spec, true)
}
// Packager holds the dependencies needed to package
// a chaincode and write it
type Packager struct {
CDSFactory ccDepSpecFactory
ChaincodeCmdFactory *ChaincodeCmdFactory
Command *cobra.Command
Input *PackageInput
CryptoProvider bccsp.BCCSP
}
// PackageInput holds the input parameters for packaging a
// ChaincodeInstallPackage
type PackageInput struct {
Name string
Version string
InstantiationPolicy string
CreateSignedCCDepSpec bool
SignCCDepSpec bool
OutputFile string
Path string
Type string
}
// packageCmd returns the cobra command for packaging chaincode
func packageCmd(cf *ChaincodeCmdFactory, cdsFact ccDepSpecFactory, p *Packager, cryptoProvider bccsp.BCCSP) *cobra.Command {
chaincodePackageCmd = &cobra.Command{
Use: "package [outputfile]",
Short: "Package a chaincode",
Long: "Package a chaincode and write the package to a file.",
ValidArgs: []string{"1"},
RunE: func(cmd *cobra.Command, args []string) error {
if p == nil {
// UT will supply its own mock factory
if cdsFact == nil {
cdsFact = defaultCDSFactory
}
p = &Packager{
CDSFactory: cdsFact,
ChaincodeCmdFactory: cf,
CryptoProvider: cryptoProvider,
}
}
p.Command = cmd
return p.packageChaincode(args)
},
}
flagList := []string{
"lang",
"path",
"ctor",
"name",
"version",
"cc-package",
"sign",
"instantiate-policy",
}
attachFlags(chaincodePackageCmd, flagList)
return chaincodePackageCmd
}
// packageChaincode packages the chaincode.
func (p *Packager) packageChaincode(args []string) error {
if p.Command != nil {
// Parsing of the command line is done so silence cmd usage
p.Command.SilenceUsage = true
}
if len(args) != 1 {
return errors.New("output file not specified or invalid number of args (filename should be the only arg)")
}
p.setInput(args[0])
// LSCC package
return p.packageCC()
}
func (p *Packager) setInput(outputFile string) {
p.Input = &PackageInput{
Name: chaincodeName,
Version: chaincodeVersion,
InstantiationPolicy: instantiationPolicy,
CreateSignedCCDepSpec: createSignedCCDepSpec,
SignCCDepSpec: signCCDepSpec,
OutputFile: outputFile,
Path: chaincodePath,
Type: chaincodeLang,
}
}
// packageCC creates the LSCC chaincode packages
// (ChaincodeDeploymentSpec and SignedChaincodeDeploymentSpec)
func (p *Packager) packageCC() error {
if p.CDSFactory == nil {
return errors.New("chaincode deployment spec factory not specified")
}
var err error
if p.ChaincodeCmdFactory == nil {
p.ChaincodeCmdFactory, err = InitCmdFactory(p.Command.Name(), false, false, p.CryptoProvider)
if err != nil {
return err
}
}
spec, err := getChaincodeSpec(p.Command)
if err != nil {
return err
}
cds, err := p.CDSFactory(spec)
if err != nil {
return errors.WithMessagef(err, "error getting chaincode code %s", p.Input.Name)
}
var bytesToWrite []byte
if createSignedCCDepSpec {
bytesToWrite, err = getChaincodeInstallPackage(cds, p.ChaincodeCmdFactory)
if err != nil {
return err
}
} else {
bytesToWrite = protoutil.MarshalOrPanic(cds)
}
logger.Debugf("Packaged chaincode into deployment spec of size <%d>, output file ", len(bytesToWrite), p.Input.OutputFile)
err = ioutil.WriteFile(p.Input.OutputFile, bytesToWrite, 0700)
if err != nil {
logger.Errorf("failed writing deployment spec to file [%s]: [%s]", p.Input.OutputFile, err)
return err
}
return err
}
func getInstantiationPolicy(policy string) (*pcommon.SignaturePolicyEnvelope, error) {
p, err := cauthdsl.FromString(policy)
if err != nil {
return nil, errors.WithMessagef(err, "invalid policy %s", policy)
}
return p, nil
}
// getChaincodeInstallPackage returns either a raw ChaincodeDeploymentSpec or
// a Envelope with ChaincodeDeploymentSpec and (optional) signature
func getChaincodeInstallPackage(cds *pb.ChaincodeDeploymentSpec, cf *ChaincodeCmdFactory) ([]byte, error) {
var owner identity.SignerSerializer
// check if we need to sign and set the owner
if createSignedCCDepSpec && signCCDepSpec {
if cf.Signer == nil {
return nil, errors.New("signing identity not found")
}
owner = cf.Signer
}
ip := instantiationPolicy
if ip == "" {
// if an instantiation policy is not given, default
// to "admin must sign chaincode instantiation proposals"
mspid, err := mspmgmt.GetLocalMSP(factory.GetDefault()).GetIdentifier()
if err != nil {
return nil, err
}
ip = "AND('" + mspid + ".admin')"
}
sp, err := getInstantiationPolicy(ip)
if err != nil {
return nil, err
}
// we get the Envelope of type CHAINCODE_PACKAGE
objToWrite, err := ccpackage.OwnerCreateSignedCCDepSpec(cds, sp, owner)
if err != nil {
return nil, err
}
// convert the proto object to bytes
bytesToWrite, err := proto.Marshal(objToWrite)
if err != nil {
return nil, errors.Wrap(err, "error marshalling chaincode package")
}
return bytesToWrite, nil
}