-
-
Save twoneks/10b12b36033584857b48230aedec4a91 to your computer and use it in GitHub Desktop.
go file to install chaincode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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