github.com/lzy4123/fabric@v2.1.1+incompatible/internal/peer/chaincode/signpackage.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "fmt" 11 "io/ioutil" 12 13 "github.com/hyperledger/fabric/bccsp" 14 "github.com/hyperledger/fabric/core/common/ccpackage" 15 "github.com/hyperledger/fabric/protoutil" 16 "github.com/spf13/cobra" 17 ) 18 19 // signpackageCmd returns the cobra command for signing a package 20 func signpackageCmd(cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) *cobra.Command { 21 spCmd := &cobra.Command{ 22 Use: "signpackage", 23 Short: "Sign the specified chaincode package", 24 Long: "Sign the specified chaincode package", 25 ValidArgs: []string{"2"}, 26 RunE: func(cmd *cobra.Command, args []string) error { 27 if len(args) < 2 { 28 return fmt.Errorf("peer chaincode signpackage <inputpackage> <outputpackage>") 29 } 30 return signpackage(cmd, args[0], args[1], cf, cryptoProvider) 31 }, 32 } 33 34 return spCmd 35 } 36 37 func signpackage(cmd *cobra.Command, ipackageFile string, opackageFile string, cf *ChaincodeCmdFactory, cryptoProvider bccsp.BCCSP) error { 38 // Parsing of the command line is done so silence cmd usage 39 cmd.SilenceUsage = true 40 41 var err error 42 if cf == nil { 43 cf, err = InitCmdFactory(cmd.Name(), false, false, cryptoProvider) 44 if err != nil { 45 return err 46 } 47 } 48 49 b, err := ioutil.ReadFile(ipackageFile) 50 if err != nil { 51 return err 52 } 53 54 env := protoutil.UnmarshalEnvelopeOrPanic(b) 55 56 env, err = ccpackage.SignExistingPackage(env, cf.Signer) 57 if err != nil { 58 return err 59 } 60 61 b = protoutil.MarshalOrPanic(env) 62 err = ioutil.WriteFile(opackageFile, b, 0700) 63 if err != nil { 64 return err 65 } 66 67 fmt.Printf("Wrote signed package to %s successfully\n", opackageFile) 68 69 return nil 70 }