github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 "bytes" 22 "encoding/hex" 23 "fmt" 24 "io/ioutil" 25 "os" 26 "os/exec" 27 "strings" 28 29 "errors" 30 31 "github.com/golang/protobuf/proto" 32 "github.com/hyperledger/fabric/common/util" 33 ccutil "github.com/hyperledger/fabric/core/chaincode/platforms/util" 34 pb "github.com/hyperledger/fabric/protos/peer" 35 ) 36 37 func getCodeFromHTTP(path string) (codegopath string, err error) { 38 39 codegopath, err = ioutil.TempDir("", "javachaincode") 40 41 if err != nil { 42 return "", fmt.Errorf("Error creating temporary file: %s", err) 43 } 44 var out bytes.Buffer 45 46 cmd := exec.Command("git", "clone", path, codegopath) 47 cmd.Stderr = &out 48 cmderr := cmd.Run() 49 if cmderr != nil { 50 return "", fmt.Errorf("Error cloning git repository %s", cmderr) 51 } 52 53 return codegopath, nil 54 55 } 56 57 //collectChaincodeFiles collects chaincode files and generates hashcode for the 58 //package. If path is a HTTP(s) url it downloads the code first. 59 //NOTE: for dev mode, user builds and runs chaincode manually. The name provided 60 //by the user is equivalent to the path. This method will treat the name 61 //as codebytes and compute the hash from it. ie, user cannot run the chaincode 62 //with the same (name, input, args) 63 func collectChaincodeFiles(spec *pb.ChaincodeSpec, tw *tar.Writer) (string, error) { 64 if spec == nil { 65 return "", errors.New("Cannot collect chaincode files from nil spec") 66 } 67 68 chaincodeID := spec.ChaincodeId 69 if chaincodeID == nil || chaincodeID.Path == "" { 70 return "", errors.New("Cannot collect chaincode files from empty chaincode path") 71 } 72 73 input := spec.Input 74 if input == nil || len(input.Args) == 0 { 75 return "", errors.New("Cannot collect chaincode files from empty input") 76 } 77 78 codepath := chaincodeID.Path 79 80 var ishttp bool 81 defer func() { 82 if ishttp { 83 os.RemoveAll(codepath) 84 } 85 }() 86 87 var err error 88 if strings.HasPrefix(codepath, "http://") || 89 strings.HasPrefix(codepath, "https://") { 90 ishttp = true 91 codepath, err = getCodeFromHTTP(codepath) 92 } else if !strings.HasPrefix(codepath, "/") { 93 wd := "" 94 wd, err = os.Getwd() 95 codepath = wd + "/" + codepath 96 } 97 98 if err != nil { 99 return "", fmt.Errorf("Error getting code %s", err) 100 } 101 102 if err = ccutil.IsCodeExist(codepath); err != nil { 103 return "", fmt.Errorf("code does not exist %s", err) 104 } 105 106 inputbytes, err := proto.Marshal(input) 107 if err != nil { 108 return "", fmt.Errorf("Error marshalling constructor: %s", err) 109 } 110 hash := util.GenerateHashFromSignature(codepath, inputbytes) 111 112 hash, err = ccutil.HashFilesInDir("", codepath, hash, tw) 113 if err != nil { 114 return "", fmt.Errorf("Could not get hashcode for %s - %s\n", codepath, err) 115 } 116 117 return hex.EncodeToString(hash[:]), nil 118 }