I'm testing below chaincode using first network setup and fabric:v1.4
package main
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
peer "github.com/hyperledger/fabric/protos/peer"
shell "github.com/ipfs/go-ipfs-api"
)
// SimpleAsset implements a simple chaincode to manage an asset
type SimpleAsset struct {
}
func (t *SimpleAsset) Init(APIstub shim.ChaincodeStubInterface) peer.Response {
return shim.Success(nil)
}
func (t *SimpleAsset) Invoke(APIstub shim.ChaincodeStubInterface) peer.Response {
// Extract the function and args from the transaction proposal
fn, args := APIstub.GetFunctionAndParameters()
var result string
var err error
if fn == "set-to-ipfs" {
result, err = setToIpfs(APIstub, args)
} else if fn == "get-from-ipfs" {
result, err = getFromIpfs(APIstub, args)
}
if err != nil {
return shim.Error(err.Error())
}
return shim.Success([]byte(result))
}
func setToIpfs(APIstub shim.ChaincodeStubInterface, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("Incorrect arguments. Expecting a value")
}
sh := shell.NewShell("ipfs_host:5001")
// ipfs add
cid, err := sh.Add(strings.NewReader(args[0]))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return "", err
}
// show hash
fmt.Println("added ", cid)
if err != nil {
return "", fmt.Errorf("Failed to set asset: %s", args[0])
}
return cid, nil
}
func getFromIpfs(APIstub shim.ChaincodeStubInterface, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("Incorrect arguments. Expecting a hash")
}
sh := shell.NewShell("ipfs_host:5001")
hashKey := args[0]
// ipfs cat
catResult, err := sh.Cat(hashKey)
defer catResult.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return "", err
}
// read string from buffer
buf := new(bytes.Buffer)
buf.ReadFrom(catResult)
message := buf.String()
return message, nil
}
// main function starts up the chaincode in the container during instantiate
func main() {
if err := shim.Start(new(SimpleAsset)); err != nil {
fmt.Printf("Error starting SimpleAsset chaincode: %s", err)
}
}
But when I tried to execute the above chaincode on first network and on docker-devmode(can't even build) but then I receive below error:
cannot find package github.com/hyperledger/fabric/core/chaincode/shim in any of: /usr/local/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOROOT) /go/src/github.com/hyperledger/fabric/core/chaincode/shim (from $GOPATH) ipfsfabric.go:29:5: cannot find package "github.com/hyperledger/fabric/protos/peer" in any of: /usr/local/go/src/github.com/hyperledger/fabric/protos/peer (from $GOROOT) /go/src/github.com/github.com/hyperledger/fabric/protos/peer (from $GOPATH) can't load package: package github.com/ipfs/go-ipfs-api: cannot find package "github.com/ipfs/go-ipfs-api" in any of: /opt/go/src/github.com/ipfs/go-ipfs-api (from $GOROOT) /opt/gopath/src/github.com/ipfs/go-ipfs-api (from $GOPATH)
commands to get the packages
go get -u github.com/ipfs/go-ipfs-api
go get -u github.com/hyperledger/fabric/protos/peer
go get -u github.com/hyperledger/fabric/core/chaincode/shim
When I installed go I added it in ~./profile file:
snippet from file
export PATH=$PATH:/usr/local/go/bin
go version
go version go1.14.8 linux/amd64