- 下载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/<orgname>/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/<orgname>/tlsca/tls-ca.pem
organizations:
<orgname>:
mspid: <orgname>
peers:
- peer0
cryptoPath: /path/to/crypto/<orgname>/users/{username}/msp
client:
organization: <orgname>
logging:
level: info
cryptoconfig:
path: /path/to/crypto
tls:
enabled: false
credentialStore:
path: /tmp/state-store
cryptostore:
path: /tmp/msp
certificateAuthorities: {}
crypto的目录结果如下:
crypto/<orgname>/users/<user1>/msp/signcerts/<orgname>-user1-cert.pem
crypto/<orgname>/users/<user1>/msp/keystore/<orgname>-user1-key.pem
crypto/<orgname>/users/<user1>/msp/cacerts/<orgname>-ca.pem
crypto/<orgname>/users/<user1>/msp/tlscerts/<orgname>-tls-ca.pem
crypto/<orgname>/users/<user2>/msp/signcerts/<orgname>-user2-cert.pem
crypto/<orgname>/users/<user2>/msp/keystore/<orgname>-user2-key.pem
crypto/<orgname>/users/<user2>/msp/cacerts/<orgname>-ca.pem
crypto/<orgname>/users/<user2>/msp/tlscerts/<orgname>-tls-ca.pem
crypto/<orgname>/users/<user3>/msp/...
crypto/<orgname>/tlsca/tlsca-jarli0709f1-cert.pem
在crypto/<orgname>根目录下面包含两个子目录users和tlsca,其中users下面为每一个用户包含其msp证书目录。
- 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 = "<channelname>"
chaincodeID = "<chaincodename>"
mspID = "<orgname>"
userID = "<username>"
)
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)