github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/chaincode/signpackage.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 chaincode 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 23 "github.com/spf13/cobra" 24 25 "github.com/hyperledger/fabric/core/common/ccpackage" 26 "github.com/hyperledger/fabric/protos/utils" 27 ) 28 29 // signpackageCmd returns the cobra command for signing a package 30 func signpackageCmd(cf *ChaincodeCmdFactory) *cobra.Command { 31 spCmd := &cobra.Command{ 32 Use: "signpackage", 33 Short: "Sign the specified chaincode package", 34 Long: "Sign the specified chaincode package", 35 ValidArgs: []string{"2"}, 36 RunE: func(cmd *cobra.Command, args []string) error { 37 if len(args) < 2 { 38 return fmt.Errorf("peer chaincode signpackage <inputpackage> <outputpackage>") 39 } 40 return signpackage(cmd, args[0], args[1], cf) 41 }, 42 } 43 44 return spCmd 45 } 46 47 func signpackage(cmd *cobra.Command, ipackageFile string, opackageFile string, cf *ChaincodeCmdFactory) error { 48 var err error 49 if cf == nil { 50 cf, err = InitCmdFactory(false, false) 51 if err != nil { 52 return err 53 } 54 } 55 56 b, err := ioutil.ReadFile(ipackageFile) 57 if err != nil { 58 return err 59 } 60 61 env := utils.UnmarshalEnvelopeOrPanic(b) 62 63 env, err = ccpackage.SignExistingPackage(env, cf.Signer) 64 if err != nil { 65 return err 66 } 67 68 b = utils.MarshalOrPanic(env) 69 err = ioutil.WriteFile(opackageFile, b, 0700) 70 if err != nil { 71 return err 72 } 73 74 fmt.Printf("Wrote signed package to %s successfully\n", opackageFile) 75 76 return nil 77 }