Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

HyperLeger Fabric SDK Development (3)-- fabsdk

Shulou Source: shulou.com Published: 2022-06-03 02:40:46 09月30日 Update

HyperLeger Fabric SDK development (3)-- fabsdk 1, introduction to fabsdk 1, introduction to fabsdk

Fabsdk is the main package for Fabric SDK, and fabsdk supports clients to use Hyperledger Fabric blockchain networks. Fabsdk creates a context based on the configuration, which is used in the client package.

Official documents:

Https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/fabsdk

2. The basic process of using fabsdk

The basic process for using Fabsdk is as follows:

Instantiate a fabsdk instance using configuration

B. Use fabsdk instances to create contexts based on organizations and users

C. Use the New function to create a client instance with the context as a parameter. You can create a client instance for each context you need.

D. use the functional functions provided by each client instance to write business logic and build a solution.

E, call the fsbsdk.Close () function to release resources and cache.

Second, fabsdk common interface 1, type definition

Var ErrAnonymousIdentity = errors.New ("missing credentials")

If the option to create an identity contains neither the user name nor the identity, return ErrAnonymousIdentity.

Type Option func (opts * options) error

Option is used to configure SDK.

2. Obtain FabricSDK instance type FabricSDK struct {opts options provider * context.Provider cryptoSuite core.CryptoSuite}

FabricSDK provides access (and context) to SDK-managed clients

Func New (configProvider core.ConfigProvider, opts... Option) (* FabricSDK, error)

New initializes the SDK based on the set of options provided. ConfigOptions provides application configuration.

Examples of use are as follows:

Sdk, err = fabsdk.New (config.FromFile (". / config.yaml"))

3. Get the channel context

Func (sdk * FabricSDK) ChannelContext (channelID string, options... ContextOption) contextApi.ChannelProvider

ChannelContext creates and returns the channel context.

4. Close the FabricSDK instance

Func (sdk * FabricSDK) Close ()

Release caches and connections maintained by SDK

5. Get the FabricSDK configuration backend

Func (sdk * FabricSDK) Config () (core.ConfigBackend, error)

Config returns the configuration backend used by all SDK configuration types

6. Create a context client

Func (sdk * FabricSDK) Context (options... ContextOption) contextApi.ClientProvider

Context creates and returns a contextual client containing all the required providers.

7. Option to build type identityOptions struct {signingIdentity msp.SigningIdentity orgName string username string} type ContextOption func (s * identityOptions) error

ContextOption provides parameters for creating a session

Func WithIdentity (signingIdentity msp.SigningIdentity) ContextOption

WithIdentity uses a pre-constructed identity object as the credential for the session

Func WithUser (username string) ContextOption

WithUser uses the specified user to load the identity / identity

Func WithOrg (org string) ContextOption

WithOrg uses the specified organization

Examples of use:

Var (sdk * fabsdk.FabricSDK org = "org1" user = "Admin") ctx: = sdk.Context (fabsdk.WithOrg (org), fabsdk.WithUser (user)) type Option func (opts * options) errorfunc WithCorePkg (core sdkApi.CoreProviderFactory) Option

Inject the core implementation into SDK

Func WithCryptoSuiteConfig (cryptoConfigs... interface {}) Option

WithCryptoSuiteConfig injects CryptoSuiteConfig interfaces into SDK, and SDK receives a full set of interfaces for each CryptoSuiteConfig implementation or a list of subinterfaces for each CryptoSuiteConfig implementation

Func WithEndpointConfig (endpointConfigs... interface {}) Option

WithEndpointConfig injects EndpointConfig interfaces into SDK, and SDK receives a full set of interfaces for each EndpointConfig implementation or a list of subinterfaces for each EndpointConfig implementation

Func WithIdentityConfig (identityConfigs... interface {}) Option

WithIdentityConfig injects IdentityConfig interfaces into SDK, and SDK receives a full set of interfaces for each IdentityConfig implementation or a list of subinterfaces for each IdentityConfig implementation

Func WithLoggerPkg (logger api.LoggerProvider) Option

WithLoggerPkg injects logger implementation into SDK

Func WithMSPPkg (msp sdkApi.MSPProviderFactory) Option

WithMSPPkg injects MSP implementation into SDK

Func WithServicePkg (service sdkApi.ServiceProviderFactory) Option

WithServicePkg injects service implementation into SDK

Tags: Interface up and down context instance customer client configuration environment identity full set function user parameter process example introduction type cache channel development Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Linux NVidia MySQL