How to build a Blockchain PoC Application using Hyperledger Fabric
Hyperledger is quickly becoming one of the most popular blockchain frameworks for large and medium-sized businesses. Its flexibility and strength, though, make it difficult to comprehend.
Before we dive into the blockchain frameworks, let’s understand what blockchain technology is.
What is Blockchain?
The blockchain is a distributed ledger technology that keeps track of all network updates and transactions. This ledger is distributed and stored not on a single isolated server, but on all of the systems that make up the system, and is stored not on a single isolated server, but all of the systems make up the system, which is known as nodes.
Smart contracts (often referred to as chain codes in Fabric) are software modules that manage access to the distributed ledger, automate procedures, control parts of transactions, and perform specified actions when predetermined circumstances are satisfied in the blockchain network.
Any changes or transactions that occur in the network will be accepted or rejected. The transaction is entered into the ledger once it has been confirmed. Data or transactions that are written to the registry over the network are encrypted, stored in blocks, and then immutable.
What is Hyperledger Fabric?
Hyperledger Fabric is a private, permissioned, open-source blockchain technology hosted by the Linux Foundation. The blockchain network is private, which means it is not open to the public and may only be joined by those who have been invited. Each party has been recognized, and every transaction has been validated, approved, verified, and tracked.
Fabric Networks can be run on-premises, or you can use blockchain as a service platform for your registry architecture.
Once numerous parties have established a network, sensitive data can be exchanged between them. If a blockchain network does not interact with numerous parties, Hyperledger Fabric blockchain solutions are ineffective.
The Fabric blockchain network’s goal is to establish trust between two or more parties, companies, or organizational units. As part of the Fabric architecture, it prioritizes a few essential qualities.
- Privacy
Every computer on the network must be identified by Fabric. A Membership Service Provider is used to sign up and identify potential members of a fabric-enabled network (MSP).
Permission membership is the term used to describe this form of membership. Many industries value data privacy, and Fabric’s appeal stems solely from this feature. For any component of the blockchain, Fabric does not require authorization. The network designer decides whether or not permission is required.
- Channels
The framework allows network participants to generate different sets of operations that are not accessible to bigger networks by partitioning the ledger into “channels.” This separates more sensitive information from non-accessible locations.
- Scalability
Fabric’s extremely scalable networking is another aspect that makes it appealing to huge companies. As with any other implementation, you can quickly scale the number of nodes in the network.
On the other hand, the system can handle vast amounts of data with fewer resources. As a result, you get the best of both worlds. A blockchain can be constructed with a limited number of nodes and grown to meet the needs of the network.
- Modularity
Individual modules can be added and implemented at various times thanks to the fabric architecture. Many components are optional, and they can be left out or added later without affecting operation.
This feature is intended to assist businesses in determining what they require and what they do not. How consensus is built, membership services for identities, proper ledger storage, specialized access APIs, and integration of connecting code are all examples of modular or “plug and play” components.
How does Hyperledger Fabric work?
Traditional blockchain networks are unable to accommodate the enterprise’s most sensitive private transactions and confidentiality agreements. Hyperledger Fabric was created in response as a flexible, scalable, and secure platform for delivering industrial blockchain solutions.
A blockchain network primarily consists of a set of peers and is considered to be a fundamental element of the network as they host ledgers and smart contracts. The ledger acts as a database and it is immutable which keeps track of all the transactions in the network. Both smart contracts and ledgers are used in a concise form to combine the shared processes and shared information in a network.
Before going ahead with transactions between the businesses, they must define a set of contracts covering common terms, rules, data, concept definitions, and processes. Then, these contracts lay out the business models that control all the interactions between the businesses. So, Smart Contract defines the set of rules between the businesses and common terms and conditions in executable code. Using blockchain network, these rules are been converted to executable programs known as Smart Contracts to open a wide variety of new applications to the blockchain.
Hyperledger Fabric is an open-source blockchain engine that covers the most crucial characteristics for analyzing and deploying blockchain in commercial applications.
A participant’s verifiable ID is a prerequisite within a private industrial network. Qualification-based membership is supported by Hyperledger Fabric. Every network participant must have a unique identifier.
Many industries, such as healthcare and banking, are governed by data-protection laws that require data to be maintained for various participants and access to various data points. Fabric is in favor of membership that is based on qualifications.
How Can You Create Applications Using Hyperledger Fabric?
If you haven’t already started, make sure you have all of the prerequisites listed below installed on the platform(s) on which you’ll be developing blockchain applications and/or running Hyperledger Fabric.
- Prerequisites:
- Install Curl:
Go ahead and download the latest version of the Curl tool if it is not already installed or if you get errors running the curl commands.
- Docker and Docker Compose:
Install the following on the platform on which you will be operating or developing the Hyperledger Fabric:
- MacOSX, *nix, or Windows 10: Docker Docker version 17.06.2-ce or greater is required.
- Older versions of Windows: Docker Toolbox – again, Docker version Docker 17.06.2-ce or greater is required.
- Go Programming Language:
Hyperledger Fabric uses the Go programming language for its components.
- Go version 1.12.x is required.
- Node.js Runtime and NPM:
Download and install Node.js, version 10 is supported from 10.15.3 and higher.
- Node.js download
Installing Node.js will also install NPM, however, it is recommended that you confirm the version of NPM installed.
Once the prerequisites are been downloaded, we are now ready to download and install the Hyperledger Fabric.
- Hyperledger Fabric smart contract (chaincode) SDKs:
Hyperledger Fabric provides several SDKs to help programmers create smart contracts (chaincode) in a variety of languages. For Go, Node.js, and Java, there are three smart contract SDKs:
- SDK for Go and documentation for Go.
- Node.js SDK docs and Node.js SDK.
- Java SDK documentation and Java SDK.
Node.js and Java currently support the new Hyperledger Fabric v1.4 smart contract development style. Support for Go will be included in a future release.
Our first Application using Hyperledger Fabric:
Let us start with our first application using Hyperledger Fabric, and learn how to write an application, smart contract to query and update a ledger and use Certificate Authority to generate the X.509 certificates which interact with permission blockchain.
- Set a Blockchain Network:
Firstly, this part requires you to be in your subdirectory within the local clone of the repository. If you have any existing networks, you do require them to stop.
./byfn.sh down
Now, you are good to launch your network.
- Launch the network:
Use the startFabric.sh shell script to get your network up and running. This command creates a blockchain network including certificate authorities, peers, orderers, and other components.
./startFabric.sh javascript
- Install Application:
Run the below command to install all the dependencies of the Application.
npm install
All the key application dependencies are included which are defined in package.json. Also, the fabric-network class is of utmost importance; it enables an application to use wallets, identities, and gateway to connect channels, submit transactions and wait for notifications.
- Enrolling the admin user:
Following that, we’ll create and enroll an Admin user, who will be used by our app to interact with the blockchain.
node enrollAdmin.js
This command has stored the CA administrator’s credentials in the wallet directory.
- Register and Enroll:
We may enroll a new user who will be used to query and update the ledger now that we have the administrator’s credentials in a wallet:
node registerUser.js
- Querying the ledger:
A blockchain network’s peer hosts a copy of the ledger, and application software can query it by running a smart contract that requests the ledger’s most current value and provides it to the application.
Let’s start by running our query.js which returns a list of all the cars on the ledger using an application. To access the ledger, this application utilizes our second identity:
The query.js returns all the data stored in the ledger, in our example, we have car data stored in our ledger.
node query.js
The output should look like this:
Wallet path: …fabric–samples/fabcar/javascript/wallet
Transaction has been evaluated, result is:
[{“Key”:”CAR0″, “Record”:{“colour”:”blue”,”make”:”Toyota”,”model”:”Prius”,”owner”:”Tomoko”}},
{“Key”:”CAR1″, “Record”:{“colour”:”red”,”make”:”Ford”,”model”:”Mustang”,”owner”:”Brad”}},
{“Key”:”CAR2″, “Record”:{“colour”:”green”,”make”:”Hyundai”,”model”:”Tucson”,”owner”:”Jin Soo”}},
{“Key”:”CAR3″, “Record”:{“colour”:”yellow”,”make”:”Volkswagen”,”model”:”Passat”,”owner”:”Max”}},
{“Key”:”CAR4″, “Record”:{“colour”:”black”,”make”:”Tesla”,”model”:”S”,”owner”:”Adriana”}},
{“Key”:”CAR5″, “Record”:{“colour”:”purple”,”make”:”Peugeot”,”model”:”205″,”owner”:”Michel”}},
{“Key”:”CAR6″, “Record”:{“colour”:”white”,”make”:”Chery”,”model”:”S22L”,”owner”:”Aarav”}},
{“Key”:”CAR7″, “Record”:{“colour”:”violet”,”make”:”Fiat”,”model”:”Punto”,”owner”:”Pari”}},
{“Key”:”CAR8″, “Record”:{“colour”:”indigo”,”make”:”Tata”,”model”:”Nano”,”owner”:”Valeria”}},
{“Key”:”CAR9″, “Record”:{“colour”:”brown”,”make”:”Holden”,”model”:”Barina”,”owner”:”Shotaro”}}]
The application begins by bringing two key fabric-network module classes, FileSystemWallet and Gateway, into scope. These classes will be used to find the users identity in the wallet and connect to the network using it:
const { FileSystemWallet, Gateway } = require(‘fabric-network’);
Next, we connect the application to the network using a gateway:
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: ‘user1’ });
This code helps us to create a new gateway and then uses it to connect the application to the network.
const ccpPath = path.resolve(__dirname, ‘..’, ‘..’, ‘first-network’, ‘connection-org1.json’);
const ccpJSON = fs.readFileSync(ccpPath, ‘utf8’);
const ccp = JSON.parse(ccpJSON);
Then the network is been divided into multiple channels, using the below commands:
Here, we access the smart contract that is fabcar, to interact with the ledger.
const contract = network.getContract(‘fabcar’);
There are many distinct transactions in fabcar, and our application uses the queryAllCars transaction to get the ledger world state data at first:
const result = await contract.evaluateTransaction(‘queryAllCars’);
- The FabCar smart contract:
Once the channel is been created, we now need to link with the smart contract.
async queryAllCars(ctx) {
const startKey = ‘CAR0’;
const endKey = ‘CAR999’;
const iterator = await ctx.stub.getStateByRange(startKey, endKey);
You should see the following output:
Wallet path: …fabric–samples/fabcar/javascript/wallet
Transaction has been evaluated, result is:
{“colour”:”black”,”make”:”Tesla”,”model”:”S”,”owner”:”Adriana”}
Now, it’s time to update the ledger.
- Updating the ledger:
Now navigate to the code block where we construct our transaction and run the below command and submit it to the network:
await contract.submitTransaction(‘createCar’, ‘CAR12’, ‘Honda’, ‘Accord’, ‘Black’, ‘Tom’);
Save it and run the program:
node invoke.js
If the invoke is successful, you will see output like this:
Wallet path: …fabric–samples/fabcar/javascript/wallet
2018–12–11T14:11:40.935Z – info: [TransactionEventHandler]: _strategySuccess: strategy success for transaction “9076cd4279a71ecf99665aed0ed3590a25bba040fa6b4dd6d010f42bb26ff5d1”
The transaction has been submitted
Congratulations, you have successfully created your first application.
What to look for when looking for a developer company:
After you have known the detailed requirements that you want to develop, try searching for a team that has experience in the type of work that you need to develop. And with the help of this team, you can create and test the quality, functionality, and utility of the developed software products.
And this will give you full control of the development of the project; however, you’ll need to find coding resources and a programmer workspace. Another best option could be to hire experts in building blockchain applications who are readily available for your remote job which will save your time as well as money.
Zeeve for building blockchain applications using Hyperledger Fabric:
Zeeve can be used to build, install, and manage nodes for businesses and startups. Zeeve is a Blockchain as a Service (BaaS) platform that helps companies and entrepreneurs build, launch, and manage secure decentralized apps. It was designed to be cloud-agnostic, letting its interface with other networks that employ different technologies, such as Ethereum. Zeeve is a Blockchain as a Service (BaaS) platform that assists enterprises and startups in developing, launching, and managing secure decentralized applications. Furthermore, Zeeves provides comprehensive APIs for constructing a range of use cases across sectors, ranging from banking and financial services to retail shops! —allowing anybody to participate! Zeeve allows businesses to benefit from the security, transparency, and efficiency of blockchain technology without having to manage the infrastructure themselves.
Responses