How to Use the Hyperledger Fabric Gateway SDK for Java

Aug 31, 2024 | Blockchain

The Hyperledger Fabric Gateway SDK for Java enables seamless interaction with a Fabric blockchain network. It simplifies the process of submitting transactions and querying ledger contents with minimal coding. However, it’s essential to note that this SDK is deprecated as of Fabric v2.5, and developers should utilize the Fabric Gateway client API for applications targeting Fabric v2.4 and later.

Getting Started: A Quick Example

Imagine you’re a librarian in a vast library (the blockchain network) with countless books (transactions) scattered throughout. The Fabric Gateway SDK is like a magical book-fetching robot that allows you to quickly find and manage books on the shelves (the ledger). To use this robot effectively, follow the steps outlined in the example below:

Complete Code Sample

The following code illustrates how to connect to a Fabric network, submit a transaction, and query the ledger state using a smart contract (using the fabcar sample):

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeoutException;

import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.ContractException;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;

class Sample {
    public static void main(String[] args) throws IOException {
        // Load an existing wallet holding identities used to access the network.
        Path walletDirectory = Paths.get(wallet);
        Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);
        
        // Path to a common connection profile describing the network.
        Path networkConfigFile = Paths.get(connection.json);
        
        // Configure the gateway connection used to access the network.
        Gateway.Builder builder = Gateway.createBuilder()
                .identity(wallet, user1)
                .networkConfig(networkConfigFile);
        
        // Create a gateway connection
        try (Gateway gateway = builder.connect()) {
            // Obtain a smart contract deployed on the network.
            Network network = gateway.getNetwork(mychannel);
            Contract contract = network.getContract(fabcar);
            
            // Submit transactions that store state to the ledger.
            byte[] createCarResult = contract.createTransaction(createCar)
                    .submit(CAR10, VW, Polo, Grey, Mary);
            System.out.println(new String(createCarResult, StandardCharsets.UTF_8));
            
            // Evaluate transactions that query state from the ledger.
            byte[] queryAllCarsResult = contract.evaluateTransaction(queryAllCars);
            System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8));
        } catch (ContractException | TimeoutException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Breaking Down the Code: The Librarian’s Robot

In the above code, each step reflects a crucial task performed by our librarian’s robot:

  • Loading the Wallet: The robot needs to know who has permissions to fetch the books (load identities).
  • Configuring the Network: The robot must learn about the library layout (network configuration).
  • Connecting to the Network: Once configured, the robot connects to the library to operate (creating a gateway connection).
  • Fetching the Smart Contract: The robot identifies which particular collection of books (smart contract) it will manage.
  • Storing and Querying Books: The robot can now store new information about books (submitting transactions) and retrieve a list of all books in a particular section (querying the ledger).

Getting Setup: Dependencies

To integrate this SDK into your project, ensure you have the appropriate dependencies. Depending on your build tool, you need to add the following:

If using Maven:

<dependency>
    <groupId>org.hyperledger.fabric</groupId>
    <artifactId>fabric-gateway-java</artifactId>
    <version>2.2.0</version>
</dependency>

If using Gradle:

implementation 'org.hyperledger.fabric:fabric-gateway-java:2.2.0'

Compatibility

Note that this version of the SDK supports specific versions of Java and Fabric:

  • Fabric: 2.2
  • Java: 8, 11, 17
  • Platform: Ubuntu 22.04

Troubleshooting

When working with the Hyperledger Fabric Gateway SDK, you may encounter several issues. Here are some potential problems and their solutions:

  • Connection Issues: Ensure that the wallet and network configuration file paths are correct. Verify that the Fabric network is up and running.
  • Transaction Errors: If a transaction fails, check that the parameters passed during the transaction submission are valid.
  • Library Not Found: Ensure that the dependencies are correctly added in your project files (Maven/Gradle).

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox