github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/chaincode/platforms/java/hash.go (about) 1 /* 2 Copyright DTCC 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 java 18 19 import ( 20 "archive/tar" 21 "encoding/hex" 22 "fmt" 23 "os" 24 "strings" 25 26 "errors" 27 28 "github.com/golang/protobuf/proto" 29 "github.com/hyperledger/fabric/common/flogging" 30 "github.com/hyperledger/fabric/common/util" 31 ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util" 32 pb "github.com/hyperledger/fabric/protos/peer" 33 ) 34 35 var logger = flogging.MustGetLogger("java/hash") 36 37 //collectChaincodeFiles collects chaincode files and generates hashcode for the 38 //package. 39 //NOTE: for dev mode, user builds and runs chaincode manually. The name provided 40 //by the user is equivalent to the path. This method will treat the name 41 //as codebytes and compute the hash from it. ie, user cannot run the chaincode 42 //with the same (name, input, args) 43 func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, error) { 44 if spec == nil { 45 return "", errors.New("Cannot collect chaincode files from nil spec") 46 } 47 48 chaincodeID := spec.ChaincodeId 49 if chaincodeID == nil || chaincodeID.Path == "" { 50 return "", errors.New("Cannot collect chaincode files from empty chaincode path") 51 } 52 53 codepath := chaincodeID.Path 54 55 var err error 56 if !strings.HasPrefix(codepath, "/") { 57 wd := "" 58 wd, err = os.Getwd() 59 codepath = wd + "/" + codepath 60 } 61 62 if err != nil { 63 return "", fmt.Errorf("Error getting code %s", err) 64 } 65 66 if err = ccutil.IsCodeExist(codepath); err != nil { 67 return "", fmt.Errorf("code does not exist %s", err) 68 } 69 70 var hash []byte 71 72 //install will not have inputs and we don't have to collect hash for it 73 if spec.Input == nil || len(spec.Input.Args) == 0 { 74 logger.Debugf("not using input for hash computation for %v ", chaincodeID) 75 } else { 76 inputbytes, err2 := proto.Marshal(spec.Input) 77 if err2 != nil { 78 return "", fmt.Errorf("Error marshalling constructor: %s", err) 79 } 80 hash = util.GenerateHashFromSignature(codepath, inputbytes) 81 } 82 83 hash, err = ccutil.HashFilesInDir("", codepath, hash, tw) 84 if err != nil { 85 return "", fmt.Errorf("could not get hashcode for %s - %s", codepath, err) 86 } 87 88 return hex.EncodeToString(hash[:]), nil 89 }