Updated on 2023-03-07 GMT+08:00

Node.js SDK Demo

This demo provides a Node.js chaincode and a program that uses the Hyperledger Fabric SDK for Node.js (fabric-nodejs-sdk) to invoke the chaincode to describe how to use a Node.js SDK to access BCS. For details about the APIs of Hyperledger Fabric SDK for Node.js, visit https://hyperledger.github.io/fabric-sdk-node/release-1.4/index.html.

This is a demo only and is not for actual use.

Preparations

Step

Action

Description

1

Install a development tool.

Download and install the Node.js source code: https://nodejs.org/en/download/

2

Download the demo project code.

Project code: nodejs-demo.zip

Creating a BCS Instance

  1. Log in to the BCS console.
  2. Click Create BCS Instance in the upper right corner.
  3. Configure basic information about the BCS instance by referring to Table 1.

    To ensure that the demo runs properly, set the parameters as described in the following table.

    Table 1 Basic settings

    Parameter

    Setting

    Region

    Retain the default value.

    Enterprise project

    Select default.

    Instance Name

    Enter node-sdk-demo.

    Edition

    Professional

    Blockchain Type

    Select Private.

    Enhanced Hyperledger Fabric Version

    v2.2

    Consensus Mechanism

    Select SOLO.

    Resource Access Initial Password

    Enter a password.

    Confirm Password

    Confirm the password.

  4. Click Next: Configure Resources. Table 2 describes the resource parameters.

    Table 2 Resource configurations

    Parameter

    Example

    Environment Resources

    Select Custom.

    Cluster

    Select Create a new CCE cluster.

    AZ

    Select an AZ.

    ECS Specifications

    Select the flavor for 4 vCPUs | 8 GB.

    ECS Quantity

    Enter 1.

    High Availability

    Select No.

    VPC

    Select Automatically create VPC.

    Subnet

    Select Automatically create subnet.

    ECS Login Method

    Select Password.

    Password of Root User

    If you do not enter a password here, the previously specified resource access initial password will be used.

    Confirm Password

    -

    Use EIP of a CCE Node

    Select Yes.

    EIP Billed By

    Retain the default value.

    EIP Bandwidth

    Set it to 5 Mbit/s.

  5. Click Next: Configure Blockchain. Table 3 describes the blockchain parameters.

    Table 3 Blockchain configurations

    Parameter

    Example

    Blockchain Configuration

    Select Custom.

    Blockchain Mgmt. Initial Password

    If you do not enter a password here, the previously specified resource access initial password will be used.

    Confirm Password

    -

    Volume Type

    Select SFS Turbo.

    Storage Capacity of Peer Organization (GB)

    Retain the default value.

    Ledger Storage

    Select File database (GoLevelDB).

    Peer Organization

    A peer organization named organization has been automatically created. Change the peer quantity to 1.

    Channel Configuration

    The organization organization has been added to the channel automatically. Retain this default setting.

    Orderer Quantity

    Retain the default value.

    Security Mechanism

    Select ECDSA.

    NOTICE:

    Only ECDSA can be selected.

    Configure Block Generation

    Select No.

    Enable Support for RESTful API

    Select No.

  6. Click Next: Confirm.
  7. Confirm the configurations and finish the creation process.

    Wait for several minutes. After a message is displayed indicating successful installation, check the status of the instance. If it is Normal, the deployment is completed.

Installing and Instantiating a Chaincode

  1. Log in to the BCS console.
  2. In the navigation pane on the left, click Instance Management.
  3. Find the instance you just created and click Manage Blockchain to go to the Blockchain Management console.
  4. On the login page, enter the username and password, and click Log In.

    The username is admin, and the password is the Blockchain Mgmt. Initial Password set when you created the BCS instance. If you have not set this password, use the resource access initial password.

  5. Click in the upper left corner of the page.

    The parameters for chaincode installation are as follows.

    Parameter

    Setting

    Chaincode Name

    Enter bcsysq.

    Chaincode Version

    Enter 1.0

    Ledger Storage

    File database (goleveldb)

    Select All Peers

    Check the box.

    Organization & Peer

    Select peer-0.

    Language

    Select Node.js.

    Chaincode File

    Add the downloaded chaincode file nodejs-chaincode.zip.

    Chaincode Description

    Enter a description of the chaincode.

  6. Click Install.
  7. After installing the chaincode, click Instantiate in the Operation column of the chaincode list.

    The parameters for chaincode instantiation are as follows.

    Parameter

    Setting

    Chaincode Name

    Enter bcsysq.

    Channel

    Select channel.

    Chaincode Version

    Enter 1.0

    Initialization Function

    Enter init.

    Chaincode Parameters

    Enter a,200,b,250.

    Endorsement Policy

    Select Endorsement from any of the following organizations.

    Endorsing Organizations

    Select organization.

    Privacy Protection Configuration

    Select No.

Deploying the Application

  1. Download and decompress the demo project code package nodejs-demo.zip to the local PC, and use an IDE to open it.

    This demo is compiled using Node.js. It contains the fabric-client library to enable money transfer from user A to user B by running the chaincode. Use the IDE that you prefer to open the script. The following table lists the files included in the project.

    Table 4 Project content

    Fie

    Description

    invoke.js

    Invoking the chaincode to transfer money from user A to user B.

    query.js

    Querying the chaincode to determine the account balance of user A.

    sdk-config.js

    Parsing the blockchain network configuration file sdk-config.json downloaded from the BCS console.

    sdk-config.json

    Containing the blockchain network configuration data.

  2. Download SDK configurations and certificates.

    1. On the Instance Management page, click Download Client Configuration on an instance card.
    2. Select SDK Configuration File and set the parameters as described in the following table.

      Parameter

      Description

      Chaincode Name

      Enter bcsysq.

      NOTICE:

      The chaincode name must be the same as the name specified during chaincode installation and instantiation.

      Certificate Root Path

      Specify a path for storing certificates. In this example, the path is E:\code\temp.

      Channel

      Select channel.

      Organization & Peer

      Retain the default value.

      Select Orderer Certificate.

      Select Peer Certificates, select organization for Peer Organization, and select Administrator certificate.

    3. Click Download to download the SDK configuration file and the administrator certificates for the node-sdk-demo-orderer and organization organizations.

  3. Copy and decompress the package.

    1. Download the project source code javasdkdemo_src.zip and decompress it.
    2. Decompress the .zip package obtained in 2 and copy the content in the orderer and peer folders to the certificate storage path. Copy the sdk-config.json file to the directory where the certificate is stored and name the file node-sdk-demo-sdk-config.json.

Debugging the Application

  1. Open the sdk-config.js file, and change the path of the SDK configuration file to the path of node-sdk-demo-sdk-config.json.

  2. Run the node query.js command in the path where the project is located to query the account balance of user A. (As shown in the following figure, user A's balance is 10,000 in this example.)

  3. Run the node invoke.js command in the path where the project is located to execute the chaincode to transfer money from user A to user B. As shown in the following figure, the chaincode is successfully executed.

  4. Query the account balance of user A again. The result shows that the money has been successfully transferred. (As shown in the following figure, user A's balance is 9990 in this example.)

Description

  • This demo uses demos in the Fabric community as references. For more demos, visit https://github.com/hyperledger/fabric-samples.
  • Method of customizing an npm repository in the chaincode
    • Create a personal configuration file named .npmrc in the path of the chaincode package. The content of the file is as follows: Registry=https://registry.npm.taobao.org/
    • In this way, when the chaincode container is instantiated, the class library is extracted from the specified repository.
    • Run the npm config get registry command to check whether the address of the customized npm repository has been correctly configured.