user1584887
user1584887

Reputation: 377

Fail to connect to Hyperledger Fabric using NodeJS SDK with TLS enabled

I have launched the first-network example in fabric-samples. TLS is by default enabled.

What i am trying to do is to use provided NodeJS SDK to connect to peer1/org1 and perform some smart contract query. The following is my js script, which is mostly copied from the fabcar sample. I have already installed and instantiated the chain code on peer1/org1.

'use strict';

var hfc = require('fabric-client');
var path = require('path');

var options = {
    wallet_path: path.join(__dirname, './creds'),
    user_id: 'PeerAdmin',
    channel_id: 'mychannel',
    chaincode_id: 'prov',
    network_url: 'grpc://localhost:8051',
};

var channel = {};
var client = null;

Promise.resolve().then(() => {
    console.log("Create a client and set the wallet location");
    client = new hfc();
    return hfc.newDefaultKeyValueStore({ path: options.wallet_path });
}).then((wallet) => {
    console.log("Set wallet path, and associate user ", options.user_id, " with application");
    client.setStateStore(wallet);
    return client.getUserContext(options.user_id, true);
}).then((user) => {
    console.log("Check user is enrolled, and set a query URL in the network");
    if (user === undefined || user.isEnrolled() === false) {
        console.error("User not defined, or not enrolled - error");
    }
    channel = client.newChannel(options.channel_id);
    channel.addPeer(client.newPeer(options.network_url));
    return;
}).then(() => {
    console.log("Make query");
    var transaction_id = client.newTransactionID();
    console.log("Assigning transaction_id: ", transaction_id._transaction_id);

    // queryCar - requires 1 argument, ex: args: ['CAR4'],
    // queryAllCars - requires no arguments , ex: args: [''],
    const request = {
        chaincodeId: options.chaincode_id,
        txId: transaction_id,
        fcn: 'lastWrtTxn',
        args: ['a']
    };
    return channel.queryByChaincode(request);
}).then((query_responses) => {
    console.log("returned from provenance query");
    if (!query_responses.length) {
        console.log("No payloads were returned from query");
    } else {
        console.log("Query result count = ", query_responses.length)
    }
    if (query_responses[0] instanceof Error) {
        console.error("error from query = ", query_responses[0]);
    }
    console.log("Response is ", query_responses[0].toString());
}).catch((err) => {
    console.error("Caught Error", err);
});

Unfortunately, I get the following error:

ruanpingcheng@ruanpingcheng-OptiPlex-990:~/Desktop/fabric-samples/first-network/prov_js$ node provenance_query.js  Create a client and set the wallet location Set wallet path, and associate user  PeerAdmin  with application Check user is enrolled, and set a query URL in the network Make query Assigning transaction_id:  542a40479598fa78ac9cf478b57629dc55b09c82651953146bcf6eb6eb81e800 error: [client-utils.js]: sendPeersProposal - Promise is rejected: Error: Endpoint read failed
    at /home/ruanpingcheng/Desktop/fabric-samples/first-network/prov_js/node_modules/grpc/src/node/src/client.js:554:15 returned from provenance query Query result count =  1 error from query =  { Error: Endpoint read failed
    at /home/ruanpingcheng/Desktop/fabric-samples/first-network/prov_js/node_modules/grpc/src/node/src/client.js:554:15 code: 14, metadata: Metadata { _internal_repr: {} } } Response is  Error: Endpoint read failed

I think it is related to TLS communication issue. But I haven't found an example that uses a SDK and connects a peer with TLS enabled. Fabcar in fabric-samples disables its TLS option. Any one can help to how to setup TLS identity and the connection? BTW, what is the usage of wallet path? What is user_id in option? Thanks so much!!

Upvotes: 2

Views: 1563

Answers (1)

Ratnakar Asara
Ratnakar Asara

Reputation: 71

@user1584887 When TLS is enabled you need to use grpcs so your network_url should be 'grpcs://localhost:8051'. Also you need to pass tls cert.

Your addPeer api should be something as following

let grpcOpts = { pem: Buffer.from(<<< readTLS cert here >>>).toString(), 'ssl-target-name-override': << server-hostname override here>> }; channel.addPeer(client.newPeer(options.network_url, grpcOpts));

fabcar app was written with tls disabled. Please refer the balance-transfer sample. TLS is enabled in the sample.

Upvotes: 3

Related Questions