Help Center > > Developer Guide> Demos> RESTful APIs

RESTful APIs

Updated at: Aug 30, 2019 GMT+08:00

HUAWEI CLOUD BCS provides RESTful APIs to simplify the usage of blockchains. You only need to develop applications that support RESTful APIs to access blockchains without a need to learn Hyperledger Fabric SDKs for Golang, Java, and Node.js. This demo uses a Golang client to show how RESTful APIs are used to invoke a chaincode.

Preparation

HUAWEI CLOUD BCS services are deployed on container clusters. To ensure access from external networks and prevent data from being lost, prepare the environment before deploying a BCS service. The preparation includes cluster creation, EIP binding, and SFS file system creation for storage (in sequence).

Table 1 Preparation

Step

Action

Description

1

Create a cluster.

Create a CCE cluster of Kubernetes v1.9 or later.

NOTE:
  • For this demo application, you are advised to purchase a VM with 4 vCPUs and 8 GB memory when creating the cluster.
  • If you purchase and bind an EIP during cluster creation, skip step 2.

2

Bind an EIP to the ECS.

Apply for an EIP and bind it to an ECS so that the ECS can be accessed from the public network. The recommended bandwidth for the EIP is 5 Mbit/s.

3

Create an SFS file system.

After a container cluster is created, create a file storage system to store data. Use the default specifications when creating the system.

4

Download the demo project package.

fabric-restapi-demo.zip

Creating a BCS Service

  1. Choose Service List > Application > Blockchain Service on the HUAWEI CLOUD console.
  2. Buy a BCS service. The following table lists the service parameters.

    Table 2 Service parameters

    Parameter

    Setting

    Billing Mode

    Select Pay-per-use.

    Service Name

    Enter demo.

    Region

    Retain the default value.

    Edition

    Select Basic or Professional.

    Blockchain Type

    Select Private.

    Cluster Type

    Select CCE cluster.

    Container Cluster

    Select an existing container cluster.

    Network Storage

    Select an existing SFS file system.

    Ledger Storage

    Select File database (goleveldb).

    Peer Organization

    Create a peer organization named organization with two peers.

    NOTE:

    If multiple organizations are involved in the service, you can select Professional for the Edition.

    Consensus Mechanism

    Select Solo.

    Security Mechanism

    Select ECDSA.

    Version

    Select the latest version.

    Blockchain Mgmt. Initial Password

    Set a password.

    Confirm Password

    Enter the password again.

    Enable Support for RESTful APIs

    Select Yes. If you select No, choose More > Enable Support for RESTful APIs in the Operation column on the Service Management page to install RESTful APIs, after the service is created.

    Orderer Quantity

    Set it to 1.

    Channel Configuration

    Create a channel named c123456, and add the peer organization organization to the channel.

  3. The service is displayed on the Service Management page after successful creation, as shown in the following figure.

Installing and Instantiating the Chaincode

  1. Download the chaincode file chaincode_example02.zip.
  2. Locate a service on the Service Management page, and click Manage Blockchain to switch to the Blockchain Management console.

  3. Click Install Chaincode. Then, on the displayed dialog box, enter the chaincode name and version number, select the peers where the chaincode is to be installed, specify the programming language of the chaincode, and add the chaincode file, as shown in the following figure.

  4. After the chaincode is installed, click Instantiate in the Operation column of the chaincode list, and set the parameters as shown in the following figure.

Deploying the Application

  1. On the Service Management page, download the user certificate of the peer organization organization to the local default path.

  2. Decompress the demo project code package to the local PC, and use an IDE to open it.

    This demo project is a RESTful client compiled using Golang. It enables chaincode invocation through RESTful APIs to achieve chaincode-based money transfer. Use an IDE such as GoLand to open the package. The following figure shows the content of the project.

  3. Decompress the downloaded user certificate to the usercrypto directory of the project, as shown in the following figure.

  4. Modify parameter settings.

    1. Modify the parameters in the conf.yaml file in the config directory as shown and described the following figure and tables.

    2. Modify the main.go file in the src/restapi directory, as shown in the following figure and tables.

      NOTE:

      For each peer that needs to participate in the endorsement, construct an OrgPeer structure including the organization ID and the domain name of the peer. Add the structure to an array of the OrgPeer type, convert the structure into a byte array using the json.Marshal() method, and then convert the structure into a character string. The OrgPeer structure is as follows:

      type OrgPeer struct {

      OrgId string `json:"orgId"`

      PeerDomainName string `json:"peerDomainName"`

      }

      Table 3 Parameters

      Parameter

      Description

      Endpoint

      IP address and port number used to access RESTful APIs. To obtain an IP address, locate the service on the Service Management page, and choose More > View Service Details in the Operation column. The port number is always 32621.

      Path

      Path to the RESTful APIs service. Retain the default value.

      CryptoMethod

      Encryption algorithm. If the ECDSA algorithm is used, set this parameter to SW.

      SignCert

      Path to the signature in the downloaded certificate

      PrvKey

      Private key in the downloaded certificate

      InvokeReq

      Request body parameters. Set these parameters based on the deployed chaincode. The InvokeReq parameter descriptions in the following table are for your reference.

      QueryReq

      Similar to InvokeReq. Set this parameter based on the deployed chaincode.

      Table 4 InvokeReq parameters

      Parameter

      Description

      Example Value

      SignGzipSwitch

      Indication of whether GZIP compression is enabled. 0 indicates disabling, and 1 indicates enabling.

      1

      ChannelId

      Blockchain channel ID

      c123456

      ChaincodeId

      Chaincode ID

      testcode

      ChaincodeVersion

      Chaincode version

      1.0

      UserId

      User ID issued by the organization's CA. The default value for BCS is User1.

      User1

      OrgId

      Organization ID in a blockchain

      1232b2032faafee152b58cd82cecf52e49a22a38

      OrgPeers

      Organization ID and domain name of each peer

      "[{OrgId:" 1232b2032faafee152b58cd82cecf52e49a22a38", PeerDomainName:"peer-1232b2032faafee152b58cd82cecf52e49a22a38-0 .peer-1232b2032faafee152b58cd82cecf52e49a22a38.default.svc.cluster.local "}]"

      Opmethod

      Purpose, that is, to invoke or query chaincodes.

      invoke

      Args

      Chaincode invoking parameters

      ["invoke","a","b","100"]

  5. Build and run main().

    The code will read the QueryReq and InvokeReq parameters in conf.yaml and main.go and call /v1/chaincode/operation of the RESTful APIs to invoke the chaincode. The code running result is as follows.

    NOTE:

    This demo uses a simple REST client to invoke the chaincode through RESTful APIs. The returned invocation result is TransactionID encrypted using Base64, and the query result is data encrypted using Base64. The code is for reference only. You can use this project code to understand how to invoke RESTful APIs.

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel