Instantiate and Connect to a Gateway

As you may have noticed, we have already created the Gateway object in the submitTransaction.js main function. The Gateway class is used to connect to the blockchain network, access channels, and run transactions.

A gateway connects to the blockchain network via the connect method:

async connect(connectionProfile, connectionOptions)

The first argument is a connection profile, which we have discussed previously. The second argument is connection options. Connection options are used in conjunction with a connection profile to control precisely how a gateway interacts with a network.

  • identity

    The identity option sets a user identity that will be used by a gateway for the network interaction. This option is required and can be set to either a label matching an identity within a supplied wallet or an Identity object itself.

  • wallet

    The wallet option identifies a wallet that will be used by a gateway on behalf of the application. The wallet should be specified if a gateway needs to retrieve identity or clientTlsIdentity from it.

  • identityProvider

    identityProvider is a required option in case identity represents an object of a custom type unknown to the SDK.

  • clientTlsIdentity

    clientTlsIdentity defines an identity that is retrieved from a wallet and used for secure communications between a gateway and different channel components, such as peers and orderers. This option is advised to set in production environments.

    🚩clientTlsIdentity is different from identity. The scope of clientTlsIdentity does not extend beyond secure network communications. clientTlsIdentity is not used to sign any transactions that go into the ledger.

  • tlsInfo

    tlsInfo is an object containing credentials to use as clientTlsIdentity.

  • eventHandlerOptions

    The eventHandlerOptions object specifies event handling options during the transaction submission, i.e., the maximum amount of time a gateway should wait for a transaction to be committed or endorsed and an event handling strategy to identify successful transaction commits.

  • queryHandlerOptions

    The queryHandlerOptions object specifies query handling options during the transaction evaluation, i.e., the maximum amount of time a gateway should wait for a query to complete and a query handling strategy.

  • discovery.enabled

    discovery.enabled boolean value determines whether a gateway uses service discovery to augment the network topology specified in the connection profile. The default value is true.

  • discovery.asLocalhost

    discovery.asLocalhost determines whether IP addresses found during service discovery are translated from the Docker network to a local host. The default value is true.

To learn more about connection options, visit the Hyperledger Fabric documentation or the Node.js SDK documentation.

Now, let’s set the connection options in submitTransaction.js and connect the gateway to the blockchain network.

try {
    let args = process.argv.slice(2);

    const identityLabel = args[0];
    const orgName = identityLabel.split('@')[1];
    const orgNameWithoutDomain = orgName.split('.')[0];

    let connectionProfile = JSON.parse(fs.readFileSync(
        path.join(testNetworkRoot,
            'organizations/peerOrganizations',
            orgName,
            `/connection-${orgNameWithoutDomain}.json`), 'utf8')
    ); 

    let connectionOptions = {
        identity: identityLabel,
        wallet: wallet,
        discovery: {enabled: true, asLocalhost: true }
    }; 

    await gateway.connect(connectionProfile, connectionOptions);
}

To make the application more flexible, we will use CLI arguments. In the code snippet above, we used the first argument as an identity label to have a possibility to submit transactions on behalf of different users from different organizations.

Last updated

Was this helpful?