- 下载fabric-go-sek
$ export GOPATH=/path/to/current
$ cd src/github.com/hyperledger
$ git clone -b v1.0.0-beta2 https://github.com/hyperledger/fabric-sdk-go.git
- 下载依赖
$ cd fabric-sdk-go
$ make populate
- 生成配置YAML文件和crypto目录
$ cat config.yaml
version: 1.0.0
channels:
default:
orderers:
- orderer0
peers:
peer0:
endorsingPeer: true
chaincodeQuery: true
ledgerQuery: true
eventSource: true
peers:
peer0:
url: peer.example.com:7050
grpcOptions:
allow-insecure: false
fail-fast: false
keep-alive-permit: false
keep-alive-time: 30s
keep-alive-timeout: 30s
tlsCACerts:
path: /path/to/crypto//tlsca/tls-ca.pem
orderers:
orderer0:
url: orderer.example.com:7051
grpcOptions:
allow-insecure: false
fail-fast: false
keep-alive-permit: false
keep-alive-time: 30s
keep-alive-timeout: 30s
tlsCACerts:
path: /path/to/crypto//tlsca/tls-ca.pem
organizations:
:
mspid:
peers:
- peer0
cryptoPath: /path/to/crypto//users/{username}/msp
client:
organization:
logging:
level: info
cryptoconfig:
path: /path/to/crypto
tls:
enabled: false
credentialStore:
path: /tmp/state-store
cryptostore:
path: /tmp/msp
certificateAuthorities: {}
crypto的目录结果如下:
crypto//users//msp/signcerts/-user1-cert.pem
crypto//users//msp/keystore/-user1-key.pem
crypto//users//msp/cacerts/-ca.pem
crypto//users//msp/tlscerts/-tls-ca.pem
crypto//users//msp/signcerts/-user2-cert.pem
crypto//users//msp/keystore/-user2-key.pem
crypto//users//msp/cacerts/-ca.pem
crypto//users//msp/tlscerts/-tls-ca.pem
crypto//users//msp/...
crypto//tlsca/tlsca-jarli0709f1-cert.pem
在crypto/
- client端代码
package main
import (
"log"
"os"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)
const (
channelID = ""
chaincodeID = ""
mspID = ""
userID = ""
)
func main() {
configOpt := config.FromFile("./config.yaml")
sdk, err := fabsdk.New(configOpt)
if err != nil {
log.Fatalf("Failed to create new SDK: %v\n", err)
return
}
defer sdk.Close()
clientChannelContext := sdk.ChannelContext(channelID, fabsdk.WithUser(userID), fabsdk.WithOrg(mspID))
client, err := channel.New(clientChannelContext)
if err != nil {
log.Fatalf("Failed to create new channel client: %v\n", err)
return
}
var response channel.Response
if len(os.Args) > 1 && os.Args[1] == "invoke" {
response, err = client.Execute(channel.Request{ChaincodeID: chaincodeID, Fcn: "invoke", Args: [][]byte{[]byte("a"), []byte("b"), []byte("10")}},
channel.WithRetry(retry.DefaultChannelOpts))
} else {
response, err = client.Query(channel.Request{ChaincodeID: chaincodeID, Fcn: "query", Args: [][]byte{[]byte("b")}},
channel.WithRetry(retry.DefaultChannelOpts))
}
if err != nil {
log.Fatalf("Failed to call client: %v\n", err)
return
}
log.Printf("Result=(%v)\n", string(response.Payload))
}
这段代码是从SDK的e2e.go里面提取出来的,调用balanceTransfer的query和invoke功能。
编译运行:
$ export GOPATH=/path/to/fabric-go-sdk
$ go build test.go
$ ./test
2020/07/10 10:12:55 Result=(100)