Access Blockchain From Apps

KompiTech Blockchain Platform allows you to download the configuration file for Hyperledger Fabric SDKs. This makes the development of applications connecting to blockchain networks a lot easier.

General preparation

Issue a new user certificate

If you don’t have any additional user certificates issued from KompiTech Blockchain Platform issue new user certificate and make sure to save the private key to your local filesystem.

Download the Fabric SDK config

Download the SDK config for the channel you want to connect to and save this config file to your local filesystem.

Collect the variables

There will be references to the variables listed below in the following examples.

  • CHANNEL_NAME - Can be found in a detail page of the channel you want to connect to in the KompiTech Blockchain Platform as Name field in Information section. It should look like appchannel-XXXXXXXXXXXX.

  • USER_NAME - It is the name of the user in the KompiTech Blockchain Platform you’re going to use to connect to the Fabric network.

Run the Chaincode

Before a chaincode can be called it must be properly installed on peers of the blockchain network and instantiated in the chosen channel.

Make sure the chaincode is properly:

Access blockchain from Go

There is a Fabric SDK for Go language which can be used for accessing Hyperledger Fabric from Go.

This example shows how to make chaincode query and invoke calls.

Create a directory named keystore and place the private key of the user there. The directory where the keystore is present has to be the working directory when the go program is executed.

Usually when running the main.go via go run place the keystore directory next to the main.go file.

Note

Pay attention to the lines with a comment, they contain values that need to be modified.

Sample Go code:

package main

  import (
      "fmt"
      "github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
      "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
      "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
  )

  func main(){
      channelName := "CHANNEL_NAME" // set to channel name you want to connect to
      userName := "USER_NAME" // set to user name you want to use for connection
      chaincodeName := "example" // set to chaincode name you want to use
      fcn := "query" // set to function you want to call
      args := [][]byte{} // set arguments you want to pass to the call
      configPath := "" // set to path to your SDK config YAML file

      sdk, err := fabsdk.New(config.FromFile(configPath))
      if err != nil {
          return
      }

      channelContext := sdk.ChannelContext(CHANNEL_NAME, fabsdk.WithUser(USER_NAME))

      channelClient, err := channel.New(channelContext)
      if err != nil {
          sdk.Close()
          return
      }
      defer sdk.Close()

      ccReq := channel.Request{
          ChaincodeID: chaincodeName,
          Fcn: fcn,
          Args: args,
      }

      response, err := channelClient.Query(ccReq) // for invoke: channelClient.Invoke(ccReq)
      if err != nil {
          return
      }

      fmt.Printf("RESPONSE: %v \n", response)
  }