如何使用节点SDK调用启用TLS的链码?

时间:2019-01-22 17:12:26

标签: hyperledger-fabric

我按照此答案中所述的环境变量设置了网络:How to setup a blockchain network with TLS in peers and orderers?

并使用以下代码来加载CLI中使用的证书:

let serverCert = fs.readFileSync('./tlsca.com-cert.pem');
let clientCert = fs.readFileSync('./server.crt');
let clientKey = fs.readFileSync('./server.key');

fabricClient.setTlsClientCertAndKey(Buffer.from(clientCert).toString(), Buffer.from(clientKey).toString());

let peer = fabricClient.newPeer(peerEndpoint, {
    'pem': Buffer.from(serverCert).toString()
});
peerList.push(peer);
channel.addPeer(peer);

channel.addOrderer(fabricClient.newOrderer(ordererEndpoint, {
    'pem': Buffer.from(serverCert).toString()
}));

由于以下错误,链码可与命令peer invoke一起使用,但不适用于节点sdk:

routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED

1 个答案:

答案 0 :(得分:0)

尝试使用此方法:

var Client = require('fabric-client');
var Channel = require('fabric-client').Channel;
var client = Client.loadFromConfig("config/configfile.yaml");

async function invokeTransaction(channelName, mspId, chaincodeId, fcn, args){
    var channel = new Channel(channelName, client);
    let tx_id = client.newTransactionID(true);
    let chaincodeInvokeRequest = {
        targets: client.getPeersForOrg(mspId),
        chaincodeId: chaincodeId,
        txId: tx_id,
        fcn: fcn,
        args: args
    }
    var result = await channel.sendTransactionProposal(chaincodeInvokeRequest, 60000);
    if(result[0][0].response.status != 200){
        return result[0][0].response;
    }
    let transactionRequest = {
        txId: tx_id,
        proposal: result[1],
        proposalResponses: result[0]
    }
    var response = await channel.sendTransaction(transactionRequest, 60000);
    return response;
}

您可以查看一些教程来创建配置文件here。不要忘记在configfile.yaml中设置客户端标头