The Chain logo

Hyperledger Fabric Tutorial for Beginners: Comprehensive Guide

Many Hyperledger tools and frameworks are available

By Santiago UriasPublished 3 years ago 5 min read
Like

Many Hyperledger tools and frameworks are available. Hyperledger fabric is one of the most popular. Hyperledger Fabric, which was released in 2016, has been a very popular framework for creating enterprise applications. This Hyperledger Fabric tutorial will show you everything you need to know about the framework, including how to create your first application. Let's get started with the Hyperledger fabric tutorial.

What's Hyperledger Fabric?

Hyperledger Fabric, a blockchain-based framework for enterprises, is available. It is an open-source framework which provides permission DLT (Distributed Ledger Technology), for the development of applications and business solutions. Hyperledger Fabric includes the following core features:

Highly modular architecture

Plug-and-play agreement

Solidity and EVM Support

Supports key-based queries and JSON queries.

Support for smart contracts in multiple languages

Let's now move on to the Hyperledger fabric tutorial for beginners.

What's the Hyperledger Fabric System Architecture Architecture?

It is important to understand the architecture before you begin our core Hyperledger fabric tutorial. A solid understanding of the structure will allow you to understand the operation of the framework.

Transactions There are two types transaction: invoke and deploy. The deploy transaction is used to create chaincodes which are programs that run on the blockchain. Invoke transactions, on the other hand are used to execute transactions within the context of previously deployed chaincodes.

Data structure: Hyperledger Fabric uses key-value pairs for its data structures. The put and get operations are used to manipulate data by the chain codes.

Ledger : This is a list of all successful and unsuccessful changes made to the chain.

Nodes are communication agents, or entities, of a network.

Client - End-users and someone who acts for them.

Peer : They manage the states of the chain, and make sure everything runs smoothly.

Hyperledger Fabric Tutorial to Help Beginners Develop Their First App

We now have a basic understanding of the architecture. It's time for us to create our first Hyperledger Fabric application.

Launching and Building the First Network

This is where you install the prerequisites and samples components that will allow you to start developing your app.

Building the Network

First, you must create and launch your network. Your app won't work if there is no network. Hyperledger Fabric's prerequisite, binaries, Docker images and a sample installation guide can be found here.

After everything has been installed, you can use the following command for a move to the repository with your sample network.

cd fabric-samples/first-network

Next, run the byfn.sh command to manage the network. This network will allow for network communication and launch essentials such as containers, peers, and chaincode that will aid in the other process. Now it is time to create the artifacts.

Generating Artifacts

To generate artifacts, use the following code and then press "Y" to confirm.

.byfn.sh generate

Powering Network

Once you have generated the artifacts, it is time to power the network. This will start all components including containers and chaincode, so that new peers can join the network. The following code can be used to power the network:

./byfn.sh

Hyperledger Fabric's default language is Go. You can change to Java or Node.js by using these commands:

.byfn.shup -l node

.byfn.shup -l Java

You can also choose to use multiple programming languages using one of these codes:

./byfn.sh out -o etcdraft

./byfn.sh out -o Kafka

Launching The Network

We will now launch the network. Before that, however, we must:

Get our subdirectory for your application

Make a local clone from the fabric-sample repository

To launch your network, use the following command line:

./startFabric.sh Javascript

Once you launch the network, everything will be available to you: orderers, peers, certificate authorities and certificates to help develop your application. Smart contracts will automatically be launched because we use the subdirectory of the application.

How to Install Your First App

The following code will install the dependencies necessary to install and start the application.

Install npm

Connecting with Certificate Authorities

Let's get in touch with authorities to allow us to add users once the network is operational. We will need to complete the Certificate Signing Request (CSR) in order to do this. The below command will allow you to contact the certificate authority to create a public, private and x.509 certificates.

node enrollAdmin.js

Your wallet will now hold your information. You can also add users to your wallet by following the same procedure and using the following code.

node reisterUser.js

This code will be used to add the user and will keep the details in the wallet.

Querying an Ledger

To query a ledger via the blockchain network, you can use read queries. Because ledgers can only be read, the application will not execute write queries.

To retrieve all data from the ledger, use the code below:

node query.js

Next, establish a connection in order to transmit the data. Once the connection has been established, you will need to create a gateway that allows the application to connect with the network. This is possible using the following codes.

const FileSystemWallet, Gateway = require('fabric-network');

const gateway = new Gateway()

await gateway.connect (ccp; wallet, identity : 'user1’ );

const contract = network.getContract('subdirectory_name');

The CPP is the code used to verify and access user information. Connecting to a channel is the last line in the code. This is crucial as the entire blockchain network works on multiple channels.

Understanding the Contract

Head to the chaincode/subdirecory_name/javascript/lib subdirectory to access the application's smart contract. The subdirectory_name in the above directory will be the name for your application's subdirectory. In an editor, open the application_name.js files you have downloaded. This file contains the smart contract as well as all transactions within the Contract class. Let's say your application concerns cars. The transaction details will look something like this:

async queryCar(ctx, carNumber) ...

Updating The Ledger

This Hyperledger Fabric tutorial for beginners teaches you how to update your ledger. Let's take a look at the same car example and see how we can add a car to the log. To add a vehicle to the ledger, use the code below.

await contract.submitTransaction('createCar', 'CAR15', 'Owner', 'Brand', 'CarModel', 'Color');

The above command will create a brand new car with the attributes values. The code below will allow you to send the new transaction to your ledger.

Node invoke.js

This will update the ledger.

blockchain
Like

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.