github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/passthru/passthru.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 main 18 19 import ( 20 "fmt" 21 "strings" 22 23 "github.com/inklabsfoundation/inkchain/common/util" 24 "github.com/inklabsfoundation/inkchain/core/chaincode/shim" 25 pb "github.com/inklabsfoundation/inkchain/protos/peer" 26 ) 27 28 // PassthruChaincode passes thru invoke and query to another chaincode where 29 // called ChaincodeID = function 30 // called chaincode's function = args[0] 31 // called chaincode's args = args[1:] 32 type PassthruChaincode struct { 33 } 34 35 //Init func will return error if function has string "error" anywhere 36 func (p *PassthruChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { 37 function, _ := stub.GetFunctionAndParameters() 38 if strings.Index(function, "error") >= 0 { 39 return shim.Error(function) 40 } 41 return shim.Success([]byte(function)) 42 } 43 44 //helper 45 func (p *PassthruChaincode) iq(stub shim.ChaincodeStubInterface, function string, args []string) pb.Response { 46 if function == "" { 47 return shim.Error("Chaincode ID not provided") 48 } 49 chaincodeID := function 50 51 return stub.InvokeChaincode(chaincodeID, util.ToChaincodeArgs(args...), "") 52 } 53 54 // Invoke passes through the invoke call 55 func (p *PassthruChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 56 function, args := stub.GetFunctionAndParameters() 57 return p.iq(stub, function, args) 58 } 59 60 func main() { 61 err := shim.Start(new(PassthruChaincode)) 62 if err != nil { 63 fmt.Printf("Error starting Passthru chaincode: %s", err) 64 } 65 }