Skip to content

Latest commit

 

History

History
273 lines (169 loc) · 11 KB

run-local.md

File metadata and controls

273 lines (169 loc) · 11 KB

Create a network locally using IBM Blockchain Platform extension for VS Code

Prerequisites

You will need to install the IBM Blockchain Platform Extension for VSCode version 1.38.0 or greater

You will also need to satisfy the following requirements for the IBM Blockchain Platform Extension for VS Code:

Steps

  1. Clone the repo
  2. Use the VS Code extension to set up a smart contract on a basic Fabric network
  3. Run the application

1. Clone the repo

Clone this repository in a folder your choice:

git clone https://github.com/IBM/fabcar-blockchain-sample.git
cd fabcar-blockchain-sample

2. Use the VS Code extension to set up a smart contract on a basic Fabric network

We will use the IBM Blockchain Platform extension to package the Fabcar smart contract.

Package the smart contract

  • Open Visual Studio code and open the contract folder from this repository that was cloned earlier.

Press the F1 key to see the different VS code options. Choose IBM Blockchain Platform: Package Open Project.

Click the IBM Blockchain Platform extension button on the left. This will show the packaged contracts on top and the blockchain connections on the bottom.

  • Next, if you want to, you can export the package to a location on your machine. However, exporting the packaged smart contract is not required for installing the smart contract on the local network.

  • Right click on the packaged contract (in this case, select fabcar@1.0.0) to export it and choose Export Package. Choose a location on your machine and save the .cds file.

Setup fabric locally

You should see FABRIC ENVIRONMENTS on the left side of the editor. Under this section, you should see 1 Org Local Fabric. Click it to start the Local Fabric.

The extension will now provision the Docker containers that will act as nodes in your network. Once the provisioning is finished and the network is up and running, you will see the options to install and instantiate the smart contract, the Channels information, the Nodes and the organization msps under Organizations. You are now ready to install the smart contract.

Install and instantiate the smart contract

Install

  • In the FABRIC ENVIRONMENTS section near the bottom, click on Smart Contracts > Installed > + Install. You will see a pop-up similar to the graphic below.

  • Then select the packaged contract: fabcar@1.0.0 Packaged Note The 1.0.0 comes from your package.json line: "version": "1.0.0"

After the install is complete, you should get a message Successfully installed on peer peer0.org1.example.com. You should also see that the contract is listed under Installed under FABRIC ENVIRONMENTS.

Instantiate

  • Under Smart Contracts you will see a section that says Instantiated. Click on + Instantiate under it.

  • The extension will then ask you which contract and version to instantiate — choose fabcar@1.0.0 Installed.

  • The extension will then ask you which function to call on instantiate — type in initLedger

  • Next, it will ask you for the arguments to the function. There are none, so just hit enter.

  • Next, the extension will then ask you do you want to use a provide a private data collection configuration file? - Click on No.

  • Lastly, the extension will then ask you do you want to choose a smart contract endorsement policy. Choose Default (single endorser, any org).

Once instantiation of the contract completes, you should get the message Successfully instantiated smart contract and you should see fabcar@1.0.0 under Instantiated under FABRIC ENVIRONMENTS.

Add app-admin identity on CA Node

We will now create the app-admin identity using the CA (Certificate Authority) node. The identity information and key files are needed in order to authenticate and run the application.

Under FABRIC ENVIRONMENTS section in the left hand pane, expand Nodes and right click on Org1 CA. Choose Create Identity (register and enroll).

Type app-admin and press the enter key.

The extension will then ask if you want to add any attributes to this identity. Click on Yes.

The extension will then ask you to provide the attributes for this identity. Enter [{"name":"hf.Registrar.Roles","value":"*","ecert":true}].

Once the identity is successfully created, you should get the message Successfully created identity 'app-admin' with the attributes: [{"name":"hf.Registrar.Roles","value":"*","ecert":true}]. You can now see app-admin in the FABRIC WALLETS section under 1 Org Local Fabric > Org1.

Export Wallet

Under FABRIC WALLETS in the left hand pane, expand 1 Org Local Fabric, and under it right click on Org1 and select Export Wallet.

You can save the exported files anywhere.

From the exported directory, copy the contents of the app-admin folder to the following location in the directory where you have cloned this repo:

/fabcar-blockchain-sample/web-app/server/wallet/app-admin

Save the connection profile and update the config file

The next step is to obtain the connection profile for Org1. Under FABRIC GATEWAYS in the left-hand pane, expand 1 Org Local Fabric and right click on Org1. Select Export Connection Profile. Save the file to the following location in the directory where you have cloned this repo:

/fabcar-blockchain-sample/web-app/server

Next, open the config.json file. We need to update this file to indicate that we want to run the application locally.

  • Specify the "connection_file" as the name of the connection profile file which was downloaded earlier. Unless you specified a different name when saving the file, it should probably be named 1 Org Local Fabric - Org1_connection.json.
  • The value for "caName" can be obtained from the connection profile file. Open the connection profile and look for the certificateAuthorities section. Obtain the caName from the line just below it. It should probably be Org1CA.

  • Update gateway discovery to { enabled: true, asLocalhost: true } to connect to the local fabric network.

After these updates, the config.json file should look similar to the file shown below:

{
    "connection_file": "1 Org Local Fabric - Org1_connection.json",
    "appAdmin": "app-admin",
    "appAdminSecret": "app-adminpw",
    "orgMSPID": "Org1MSP",
    "caName": "Org1CA",
    "userName": "user1",
    "gatewayDiscovery": { "enabled": true, "asLocalhost": true }
}

Almost done, you can run the application.

3. Run the application

Register User

  • Run the registerUser.js script.

    node registerUser.js
  • You should see the following in the terminal:

    Successfully registered and enrolled the user user1 and imported it into the wallet

Run the application server

  • From the server directory, start the server.

    npm start

Start the web client

  • In a new terminal, open the web client folder and install the dependencies.

    cd web-app/client
    npm install
  • Start the client:

    npm start

You can find the app running at http://localhost:4200/