我正在尝试通过导入使用hyperledger go sdk 模块的
https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/msp
。
我想使用超级账本结构go-sdk创建CA客户端实例,我需要导入哪个模块以及如何导入它,有人可以建议吗?
我正在使用以下cmd来生成交换矩阵服务器CA客户端配置文件。
./bin/fabric-ca-client enroll admin:adminpws localhost:7054
下面的代码使用通过上述cmd生成的fabric-ca-client.yaml文件创建Fabric sdk上下文。如果我做错了任何事,请告诉我。
package main
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
clientmsp "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
mspid "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/pkg/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp"
)
var (DefaultHome = os.ExpandEnv("$PWD/CONFIG"))
var caServerURL string
var caServer = &mockmsp.MockFabricCAServer{}
const (
caServerURLListen = "http://localhost:7054"
configFile = "fabric-ca-server-config.yaml"
)
type nwConfig struct {
CertificateAuthorities map[string]msp.CAConfig
}
type clientFixture struct {
cryptoSuiteConfig core.CryptoSuiteConfig
identityConfig mspid.IdentityConfig
}
func main() {
// Initiate the sdk using the config file
client := clientFixture{}
sdk := client.setup()
//create the CA instance
c, err := clientmsp.New(sdk.Context())
if err != nil {
fmt.Println("failed to create msp client", err)
return
}
fmt.Println("New client instance created", c)
}
func (f *clientFixture) setup() *fabsdk.FabricSDK {
var lis net.Listener
var err error
if !caServer.Running() {
lis, err = net.Listen("tcp", strings.TrimPrefix(caServerURLListen,
"http://"))
if err != nil {
panic(fmt.Sprintf("Error starting CA Server %s", err))
}
caServerURL = "http://" + lis.Addr().String()
}
configPath := filepath.Join(DefaultHome, configFile)
backend, err := config.FromFile(configPath)()
if err != nil {
fmt.Println(err)
}
configProvider := func() ([]core.ConfigBackend, error) {
return backend, nil
}
// Instantiate the SDK
sdk, err := fabsdk.New(configProvider)
if err != nil {
fmt.Println(err)
}
configBackend, err := sdk.Config()
if err != nil {
panic(fmt.Sprintf("Failed to get config: %s", err))
}
f.cryptoSuiteConfig = cryptosuite.ConfigFromBackend(configBackend)
f.identityConfig, _ = msp.ConfigFromBackend(configBackend)
if err != nil {
fmt.Println(err)
}
ctxProvider := sdk.Context()
ctx, err := ctxProvider()
if err != nil {
fmt.Println(err)
}
// Start Http Server if it's not running
if !caServer.Running() {
caServer.Start(lis, ctx.CryptoSuite())
}
return sdk
}
答案 0 :(得分:0)
导入mspclient
mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
mspClient, err := mspclient.New(
*fabsdk.FabricSDK.Context(),
mspclient.WithOrg(OrgName),
)
if err != nil {
return mspClient, errors.WithMessage(err, "failed to create MSP client")
}
之后,您可以使用以下服务
err = mspClient.Enroll("Admin@org1",
msp.WithSecret("Admin@org1"),
msp.WithProfile("tls"),
)
if err != nil {
return errors.WithMessage(err, "failed to register identity")
}