Creating Your First Application

Catalyst Blockchain Platform consists of two components: smart contracts (chaincodes) deployed in the Hyperledger Fabric Service and the custom program portion, which will communicate with the Hyperledger Fabric Service.

Writing Simple “Hello World” Chaincode

Catalyst Blockchain Platform runs chaincodes as external services, meaning that this must be considered during deployment.

A detailed guide on how to write external chaincode can be found in the official Hyperledger Fabric documentation.

Here, we will be using an already created simple “Hello World” chaincode example written in Go to show specific details of how to run smart contracts in Catalyst Blockchain Platform.

The source code of this chaincode can be found following the link.

The key part of this example chaincode is the function where its server starts:

func main() {
ccid := os.Getenv("CHAINCODE_ID")
address := os.Getenv("CHAINCODE_ADDRESS")
server := &shim.ChaincodeServer{
CCID: ccid,
Address: address,
CC: &HelloWorld{},
TLSProps: shim.TLSProperties{
Disabled: true,
},
}
if err := server.Start(); err != nil {
fmt.Printf("Error starting HelloWorld chaincode: %s", err)
}
}

To run chaincode in Catalyst Blockchain Platform, you need to set CCID and Address fields of shim.ChaincodeServer as environment variables CHAINCODE_ID and CHAINCODE_ADDRESS, respectively. These two environment variables will be passed during the creation of the chaincode external service, CHAINCODE_IDmatches chaincode package id after its installation on a peer and CHAINCODE_ADDRESS is the listen address of a chaincode server that was used to build the chaincode server endpoint accessible from peer in connection.json.

Communication with Hyperledger Fabric Service

There are two ways of building your business application for communication with the Hyperledger Fabric Service: Hyperledger Fabric SDK and Catalyst Blockchain Platform API.

Hyperledger Fabric SDK

Connect to Hyperledger Fabric Service with the help of Hyperledger Fabric SDK. There are many different Hyperledger Fabric resources and guides on configuring the SDK for interaction with Fabric, and these can be utilized as well.

Catalyst Blockchain Platform needs two more steps to complete SDK integration with Hyperledger Fabric Service.

1. Create a client signing identity and TLS identity for the business application on behalf of your Catalyst Blockchain Platform user.

You can do that on Hyperledger Fabric Service UI in the CA tab. Information about how to register an identity can be found here.

2. Enroll created identities through SDK CA client from your business application or Hyperledger Fabric CA Client in CLI.

SDK method allows you to fully integrate with Hyperledger Fabric Service and interact with all network components. However, this method is complicated. Instead of using HL Fabric SDK, you can use Catalyst Blockchain Platform API.

Catalyst Blockchain Platform API

If your business application needs to communicate with chaincode, Catalyst Blockchain Platform provides you with an API that allows you to invoke and query deployed chaincodes.

Endpoints and required parameters are listed below.

Invoke endpoint

host/api/v1/channels/{channelID}/cc/{chaincodeID}/invoke [POST]
  • channelID — channel name.

  • chaincodeID— chaincode name.

Content-Type — application/json.

Request body contains fields:

  • function — name of function which is called in chaincode (type: string).

  • args— an array of function arguments (any type of argument will work).

  • is_init — indicates whether init function call is required, can be set to true only during the first operation with chaincode (type bool, default value false).

Example of the request body for invocation of chaincode “Hello World”:

{
"function":"invoke",
"args":["John"],
"is_init": true
}

Query endpoint

host/api/v1/channels/{channelID}/cc/{chaincodeID}/query [POST]

Content-Type — application/json.

The query request body contains the same fields as the invocation request.

Example of the request body for the query of chaincode “Hello World”:

{
"function":"query",
"args":["John"],
}

You can use the already created Docker image of the “Hello World” chaincode intellecteu/fabric-externalcc:latest provided by IntellectEU for testing purposes.