Hyperledger fabric java sdk tutorial

In a blockchain solution, the network works as a back end with an application front end to communicate with the network using an SDK.

To set up the communication between the front and back ends, the Hyperledger Fabric community provides a number of SDKs for a variety of programming languages, including Node. Have you ever tried to build a Hyperledger Fabric Java blockchain app? If that sounds familiar, this developer code pattern will help you get the process started.

Blockchain is a shared, immutable ledger for recording the history of transactions. Hyperledger is a collaborative open source effort created to advance cross-industry blockchain technologies. Hyperledger Fabric is a blockchain framework implementation and one of the Hyperledger projects hosted by The Linux Foundation.

The Hyperledger Fabric platform provides solutions for a distributed ledger underpinned by a modular architecture delivering high degrees of confidentiality, resiliency, flexibility, and scalability.

In a blockchain solution, the blockchain network works as a back end with an application front end to communicate with the network using an SDK. It provides a means to execute user chaincode, query blocks, and transactions on the channel, and monitor events on that channel.

In this pattern, the blockchain network is built using Docker Compose and will provision a Hyperledger Fabric 1. This pattern demonstrates creating and initializing the channel, installing and instantiating chain code, and performing invoke and query on your blockchain network.

To put this code pattern to use, see the details on how to get started running and using this application in the README. Close Close. Code Pattern Create and deploy a blockchain network using Hyperledger Fabric SDK for Java Set up and initialize the channel, install and instantiate chaincode, and perform invoke and query on your blockchain network. Get the code. Logo github. Blockchain Java. Docker Hyperledger Fabric. Summary Description Flow Instructions.This tutorial illustrates the creation of a Hyperledger Fabric channel using the Node.

6 - What is chaincode?

It shows how to use an initial default channel definition and how to start with that definition to build a custom definition. The process to create a network and channels also involves creating and working with cryptographic material, this will not be discussed here. The following assumes an understanding of the Hyperledger Fabric network orderers and peersprotobufand of Node application development, including the use of the Javascript Promise.

The examples shown below are based on the balance-transfer sample application. The initial binary channel configuration definition generated by the configtxgen tool is a binary file containing the Hyperledger Fabric configuration protobuf common. Envelope element. Inside this element is the common. ConfigUpdate protobuf element.

hyperledger fabric java sdk tutorial

This configuration element is the one that must be signed. A profile element in the configtx. Have the fabric-client SDK extract the config update element from the mychannel. You may ask why is a common.

ConfigUpdate used for a create. This makes the process of create and update the same. The create of a new channel is a delta on what is defined in the system channel and an update is a delta on what is defined in the channel.

The common. ConfigUpdate object submitted will only contain the changes for both a create and an update. The easiest way to get started with creating a custom channel configuration is to have the configtxlator convert an existing binary that has been or could be used to create a new channel to human readable JSON.

There are many elements of the configuration and it would be very difficult to start with nothing. Using the same configtx.

Then by sending that binary to the configtxlator to convert it to JSON, you will be able to see the layout and have a starting point. That JSON could also be used as a template for creating other new channels on your network.

A new channel will inherit settings from the system channel for settings not defined in the new channel configuration.

Organizations that will be on the new channel must be defined in a consortium on the system channel. Therefore having the readable definition of the system channel of your network would be helpful when creating a new channel.

hyperledger fabric java sdk tutorial

Send the genesis. Use the configtxgen tool to produce the binary config files. Send the two binary files to the configtxlator service. Since this step is done only once and does not require a Node. Notice that configtxlator service path has decode convert from binary to JSON. The path must also include the type of object of the binary, in the first case, it is a common.

The results of decoding the file mychannel. Envelope produced by the configtxgen tool contains a common. ConfigUpdate object. This is the object that is needed as the source of the template to be used for creating new channels.

ConfigUpdate is the object that will be signed by all organizations and submitted to the orderer to create a new channel. ConfigUpdate object from the decode of the "TwoOrgsChannel" channel create binary generated above.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Is there a working example starting from connecting to the Fabric node, doing queries, etc? All I found so far through extensive googling are "let's-write-some-chaincode" examples. Here is an example, implementing some functionality from fabcar query. I used no implementation for json to avoid additional libraries it is required to deal with certs a bit - see below.

I find this Java example to be more helpful than the links provided. Out of the box it provides you with an end to end test without bloat.

Shows you how to do everything without CLI, in plain Java. Learn more. Asked 2 years, 10 months ago. Active 1 year, 7 months ago.

Viewed 12k times. Xenonite Xenonite 1, 2 2 gold badges 13 13 silver badges 31 31 bronze badges. Active Oldest Votes. This can help. Ashishkel Ashishkel 8 8 silver badges 15 15 bronze badges. If you use another OS please update paths accordingly. You will need fabcar example up and running. BufferedReader; import java. FileInputStream; import java. IOException; import java. InputStream; import java. InputStreamReader; import java. Files; import java.

Path; import java.

Developer tutorial for creating a Hyperledger Composer solution

Paths; import java. GeneralSecurityException; import java. KeyFactory; import java. PrivateKey; import java.It is also worth noting that this tutorial serves as an introduction to Fabric applications and uses simple smart contracts and applications.

For a more in-depth look at Fabric applications and smart contracts, check out our Developing Applications section or the Commercial paper tutorial. This tutorial provides an introduction to how Fabric applications interact with deployed blockchain networks.

The tutorial uses sample programs built using the Fabric SDKs — described in detail in the Application topic — to invoke a smart contract which queries and updates the ledger with the smart contract API — described in detail in Smart Contract Processing.

We will also use our sample programs and a deployed Certificate Authority to generate the X. The sample applications and the smart contract they invoke are collectively known as FabCar. Setting up a development environment. Explore a sample smart contract.

Interact with the smart contract with a sample application. Our application will use the FabCar smart contract to query and update car assets on the ledger. After completing this tutorial you should have a basic understanding of how Fabric applications and smart contracts work together to manage data on the distributed ledger of a blockchain network.

This tutorial demonstrates the JavaScript versions of the FabCar smart contract and application, but the fabric-samples repo also contains Go, Java and TypeScript versions of this sample. To try the Go, Java or TypeScript versions, change the javascript argument for.

Navigate to the fabcar subdirectory within your local clone of the fabric-samples repo. Launch your network using the startFabric. This command will deploy the Fabric test network with two peers and an ordering service.

Instead of using the cryptogen tool, we will bring up the test network using Certificate Authorities. We will use one of these CAs to create the certificates and keys that will be used by our applications in a future step.

The startFabric. From the fabcar directory inside fabric-samplesnavigate to the javascript folder. Run the following command to install the application dependencies. It will take about a minute to complete:. This process is installing the key application dependencies defined in package.

Project Management

The most important of which is the fabric-network class; it enables an application to use identities, wallets, and gateways to connect to channels, submit transactions, and wait for notifications. This tutorial also uses the fabric-ca-client class to enroll users with their respective certificate authorities, generating a valid identity which is then used by fabric-network class methods.

Once npm install completes, everything is in place to run the application. The following two sections involve communication with the Certificate Authority.

When we created the network, an admin user — literally called admin — was created as the registrar for the certificate authority CA. Our first step is to generate the private key, public key, and X.

This process uses a Certificate Signing Request CSR — the private and public key are first generated locally and the public key is then sent to the CA which returns an encoded certificate for use by the application. These credentials are then stored in the wallet, allowing us to act as an administrator for the CA. Our admin is used to work with the CA. Run the following command to register and enroll a new user named appUser :.

Similar to the admin enrollment, this program uses a CSR to enroll appUser and store its credentials alongside those of admin in the wallet. We now have identities for two separate users — admin and appUser — that can be used by our application.

hyperledger fabric java sdk tutorial

Each peer in a blockchain network hosts a copy of the ledger.It provides basic instructions for getting started interacting with a TLS-enabled network. In blockchain solutions, the blockchain network works as a back end while an application front-end communicates with the network using an SDK.

To set up communications between the front and back ends, the Hyperledger Fabric community offers a number of SDKs for a wide variety of programming languages. Hyperledger Fabric supports both secure and non-secure communication between nodes. The non-secure communication method is covered in the code pattern Create and deploy a blockchain network using Hyperledger Fabric SDK for Java. There is a difference between the way the SDK calls the Hyperledger Fabric network when the network is secured using TLS and when the network is not secured.

This tutorial will focus on the former situation — you will learn how to use Fabric Java SDK to enable client applications to interact with Hyperledger Fabric networks that are secured by enabling TLS.

For the SDK to connect and perform operations on the Hyperledger Fabric network, you need network details like the URLs for peer, ca, orderer, channel name, pem certificates for ca, orderer as it is TLS enabledand chaincode details. The network can be set up in different ways. When the Hyperledger Fabric network was originally launched, an admin user was registered with the certificate authority CA.

Now you need to send an enroll call to the CA server to retrieve the enrollment certificate eCert for the admin. The client application needs this certificate in order to form a user object for the admin. You will then use this admin object to register and enroll a new user. Create an instance of the org. HFCAClient class. Note: You can either use the pem cert file content as a string directly, or parse that file location and get the content.

The admin user enrollment requires certain user details. We have created a class called UserContext which holds those user details. You can refer to this class code in the code repository providedalong with the code pattern mentioned above.

It requires an admin user name and password:. This admin user context, with the enrollment set, will be used in subsequent user registration and enrollment. The admin user context should be stored to a local file system so that subsequent calls can use the admin user context.For building Hyperledger Fabric blockchain client applications, you are strongly encouraged to use the high level API.

The SDK provides a layer of abstraction on top of the wire-level protobuf based communication protocol used by client applications to interact with a Hyperledger Fabric blockchain network. It allows Java applications to manage the lifecycle of Hyperledger channels and user chaincode. The SDK also provides a means to execute user chaincode, query blocks and transactions on the channel, and monitor events on the channel.

Note, the SDK does not provide a means of persistence for the application defined channels and user artifacts on the client.

hyperledger fabric java sdk tutorial

This is left for the embedding application to best manage. Channels may be serialized via Java serialization in the context of a client. Channels deserialized are not in an initialized state. Applications need to handle migration of serialized files between versions.

The SDK also provides a client for Hyperledger's certificate authority. The SDK is however not dependent on this particular implementation of a certificate authority. This provides a summary of steps required to get you started with building and using the Java SDK. Please note that this is not the API documentation or a tutorial for the SDK, this will only help you familiarize to get started with the SDK if you are new in this domain.

Work in progress 2. You may also need to on your v2. Latest Fabric builds are seldom needed except for those working on the very latest Fabric features. Some information to help with that can been found in Developer Instructions.

To get a functioning Fabric v2. SDK depends on few third party libraries that must be included in your classpath when using the JAR file. To get a list of dependencies, refer to pom.

Create and deploy a blockchain network using Hyperledger Fabric SDK for Java

Alternatively, mvn dependency:analyze-report will produce a report in HTML format in target directory listing all the dependencies in a more readable format. To run the unit tests, please use mvn install which will run the unit tests and build the jar file. Many unit tests will test failure condition's resulting in exceptions and stack traces being displayed.Search all Tutorials Arrow right.

Hyperledger Fabric. Close Close. Hyperledger Fabric Tutorials Complete set of steps including sample code that are focused on specific tasks. Tutorials provide step-by-step instructions that a developer can follow to complete a specific task or set of tasks. February 6, December 13, October 14, Tutorial Integrate a TypeScript smart contract with a React-based dashboard app.

October 2, Tutorial Add an organization to your existing Hyperledger Fabric blockchain network using an easy tool. September 16, Tutorial Process blockchain events using serverless functions on the cloud. August 19, July 23, Tutorial Learn step-by-step how to set up a basic blockchain network.

June 6, May 30, May 16, March 13, Tutorial Upgrade a commercial paper smart contract by adding deltas history query capability. March 3, March 1, February 28, Tutorial Archived Modernizing enterprise business.

February 8, January 28, January 21, November 1, Tutorial Model and test your blockchain network. January 1, Close Modal.