Hyperledger Fabric Gateway client API for Java

The Fabric Gateway SDK allows applications to interact with a Fabric blockchain network. It provides a simple API to submit transactions to a ledger or query the contents of a ledger with minimal code. The Gateway SDK implements the Fabric programming model as described in the Running a Fabric Application tutorial of the Fabric documentation.

Client applications interact with the blockchain network using a Fabric Gateway. A session for a given client identity is established by building and connecting to a Fabric Gateway using a gRPC connection to the Gateway endpoint, client identity, and client signing implementation. The returned Gateway enables interaction with any of the blockchain Networks (channels) accessible through the Fabric Gateway. This in turn provides access to Smart Contracts within chaincode deployed to that blockchain network, and to which transactions can be submitted or queries can be evaluated.

gRPC connections to a Fabric Gateway may be shared by all Gateway instances interacting with that Fabric Gateway.

The following shows a complete code sample of how to connect to a fabric network, submit a transaction and query the ledger state using an instantiated smart contract.


import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.TlsChannelCredentials;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import org.hyperledger.fabric.client.identity.Identities;
import org.hyperledger.fabric.client.identity.Identity;
import org.hyperledger.fabric.client.identity.Signer;
import org.hyperledger.fabric.client.identity.Signers;
import org.hyperledger.fabric.client.identity.X509Identity;

public final class Example {
    public static void main(final String[] args)
            throws IOException, CertificateException, InvalidKeyException, GatewayException, CommitException,
                    InterruptedException {
        // Create client identity based on X.509 certificate.
        Reader certReader = Files.newBufferedReader(Paths.get("path/to/certificate.pem"));
        X509Certificate certificate = Identities.readX509Certificate(certReader);
        Identity identity = new X509Identity("mspId", certificate);

        // Create signing implementation based on private key.
        Reader keyReader = Files.newBufferedReader(Paths.get("path/to/private-key.pem"));
        PrivateKey privateKey = Identities.readPrivateKey(keyReader);
        Signer signer = Signers.newPrivateKeySigner(privateKey);

        // Create gRPC client connection, which should be shared by all gateway connections to this endpoint.
        ChannelCredentials tlsCredentials = TlsChannelCredentials.newBuilder()
                .trustManager(Paths.get("path/to/tls-CA-certificate.pem").toFile())
                .build();
        ManagedChannel grpcChannel = Grpc.newChannelBuilder("gateway.example.org:1337", tlsCredentials)
                .build();

        // Create a Gateway connection for a specific client identity.
        Gateway.Builder builder = Gateway.newInstance()
                .identity(identity)
                .signer(signer)
                .hash(Hash.SHA256)
                .connection(grpcChannel);

        try (Gateway gateway = builder.connect()) {
            // Obtain smart contract deployed on the network.
            Network network = gateway.getNetwork("channelName");
            Contract contract = network.getContract("chaincodeName");

            // Submit transactions that store state to the ledger.
            byte[] putResult = contract.submitTransaction(
                    "put", "time", LocalDateTime.now().toString());
            System.out.println(new String(putResult, StandardCharsets.UTF_8));

            // Evaluate transactions that query state from the ledger.
            byte[] getResult = contract.evaluateTransaction("get", "time");
            System.out.println(new String(getResult, StandardCharsets.UTF_8));
        } finally {
            grpcChannel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
        }
    }
}
    
Packages
Package
Description
This package provides the set of interfaces that enable a Java application to interact with a Fabric blockchain network.
Provides classes and interfaces for describing client identity and for creating digital signatures on behalf of client identities.