Skip to content

Instantly share code, notes, and snippets.

@twoneks
Forked from RealDeanZhao/main.go
Created November 29, 2018 10:58
Show Gist options
  • Save twoneks/10b12b36033584857b48230aedec4a91 to your computer and use it in GitHub Desktop.
Save twoneks/10b12b36033584857b48230aedec4a91 to your computer and use it in GitHub Desktop.
go file to install chaincode
package chainmgmt
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"themis.xyd.cn/themis/server/ricsdk"
)
// InstallChaincode 安装chaincode
func InstallChaincode(c *gin.Context) {
sdk := ricsdk.Singleton()
mspClient, err := msp.New(sdk.Context(), msp.WithOrg("xiaoyudian"))
err = mspClient.Enroll("admin-xiaoyudian", msp.WithSecret("admin-xiaoyudianpw"))
admin, err := mspClient.GetSigningIdentity("admin-xiaoyudian")
adminContext := sdk.Context(fabsdk.WithIdentity(admin))
resMgmtClient, err := resmgmt.New(adminContext)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
ccPkg, err := gopackager.NewCCPackage("themis.xyd.cn/themis/cc/example_cc.go", "")
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
req := resmgmt.InstallCCRequest{
Name: "example_cc",
Path: "/opt/gopath/src/themis.xyd.cn/themis/cc",
Version: "0",
Package: ccPkg,
}
peers := ricsdk.PeersByOrg()["xiaoyudian"]
responses, err := resMgmtClient.InstallCC(req, resmgmt.WithTargets(peers...))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.JSON(200, gin.H{
"message": responses,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment