无法使用启用了TLS的NodeJS SDK连接到Hyperledger Fabric

我已经在面料样品上推出了第一个networking的例子。 TLS默认启用。

我想要做的是使用提供的NodeJS SDK连接到peer1 / org1并执行一些智能合同查询。 以下是我的js脚本,大部分是从fabcar样本中复制的。 我已经在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); }); 

不幸的是,我得到以下错误:

 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 

我认为这与TLS沟通问题有关。 但我还没有find一个使用SDK并将TLS连接到对等体的示例。 织物样本中的 Fabcar禁用其TLS选项。 任何人都可以帮助如何设置TLS身份和连接? 顺便说一句,钱包path的用法是什么? 什么是user_id选项? 非常感谢!!

@ user1584887当启用TLS时,你需要使用grpcs,这样你的network_url应该是'grpcs:// localhost:8051' 。 你也需要通过tls证书。

你的addPeer API应该是如下的东西

让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应用程序被写入禁用。 请参考余额转移样本。 示例中启用了TLS。