Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js fabric sdk节点如何处理TLS证书和InvokeChaincode API_Node.js_Hyperledger Fabric_Hyperledger Fabric Sdk Js - Fatal编程技术网

Node.js fabric sdk节点如何处理TLS证书和InvokeChaincode API

Node.js fabric sdk节点如何处理TLS证书和InvokeChaincode API,node.js,hyperledger-fabric,hyperledger-fabric-sdk-js,Node.js,Hyperledger Fabric,Hyperledger Fabric Sdk Js,我正在尝试为我以前制作的fabric网络构建一个web前端。我找到了一些fabric sdk节点示例,但我太傻了,无法理解它们是如何工作的。所以,我想寻求帮助。 我正在学习一个教程,它要求我使用“npm安装”、“registerAdmin.js”和“registerUser.js”,它们都很有效。但是当我试图运行我的代码时,它遇到了一些错误 root@oyu-virtual-machine:~/hyperledger-fabric/test/webapp# node get.js Load pr

我正在尝试为我以前制作的fabric网络构建一个web前端。我找到了一些fabric sdk节点示例,但我太傻了,无法理解它们是如何工作的。所以,我想寻求帮助。 我正在学习一个教程,它要求我使用“npm安装”、“registerAdmin.js”和“registerUser.js”,它们都很有效。但是当我试图运行我的代码时,它遇到了一些错误

root@oyu-virtual-machine:~/hyperledger-fabric/test/webapp# node get.js
Load privateKey and signedCert
Get History
Assigning transaction_id:  35e9ed932366df66448d789fbf5989e6ba31be555f96eaca3197475a1602749c
E0429 15:09:28.130483373    4413 ssl_transport_security.cc:1245] Handshake failed with fatal error SSL_ERROR_SSL: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed.
returned from gethistory
Gethistory result count =  1
error from gethistory =  Error: 14 UNAVAILABLE: failed to connect to all addresses
    at Object.exports.createStatusError (/root/hyperledger-fabric/test/webapp/node_modules/fabric-client/node_modules/grpc/src/common.js:91:15)
    at Object.onReceiveStatus (/root/hyperledger-fabric/test/webapp/node_modules/fabric-client/node_modules/grpc/src/client_interceptors.js:1209:28)
    at InterceptingListener._callNext (/root/hyperledger-fabric/test/webapp/node_modules/fabric-client/node_modules/grpc/src/client_interceptors.js:568:42)
    at InterceptingListener.onReceiveStatus (/root/hyperledger-fabric/test/webapp/node_modules/fabric-client/node_modules/grpc/src/client_interceptors.js:618:8)
    at callback (/root/hyperledger-fabric/test/webapp/node_modules/fabric-client/node_modules/grpc/src/client_interceptors.js:847:24) {
  code: 14,
  metadata: Metadata { _internal_repr: {}, flags: 0 },
  details: 'failed to connect to all addresses'
}
Response is  Error: 14 UNAVAILABLE: failed to connect to all addresses
在我看来,最重要的信息是

E0429 15:09:28.130483373 4413 ssl_传输_安全。cc:1245]握手失败,出现致命错误ssl_错误_ssl:错误:1416F086:ssl例程:tls_进程_服务器_证书:证书验证失败

代码中发生了什么

    console.log("Get History"); 
    var transaction_id = client.newTransactionID(); 
    console.log("Assigning transaction_id: ", transaction_id._transaction_id); 
    //Construct the query request parameter 
    const request = { 
        chaincodeId: options.chaincode_id, 
        txId: transaction_id, 
        fcn: 'githistory', 
        args: 's-001'
    }; 
     return channel.queryByChaincode(request); 
}).then((query_responses) => { 
    console.log("returned from gethistory"); 
我会在最后给出我的完整代码。我以前没有学过NodeJS,所以我要问一些愚蠢的问题

首先,它是一个跟踪系统,所以我要做的是通过js代码在链码中调用我的GetHistory API(为不理解的人解释,这是一系列GetValue API和InvokeChaincode API)。和我不知道我应该使用什么fabric sdk节点API,channel.queryByChaincode(),channel.sendTransactionProposal(),contract.evaluateTransaction(),contract.submitTransaction()或其他什么?

而且,我在fabric网络中启用了TLS,我找到了一个包含TLS设置的示例js代码,但是我不知道它是如何工作的,他们是否真的找到了我设置的证书。 这是我最困惑的部分,我不知道这条路指向哪里,它需要指向哪里

sdkUtils.newKeyValueStore({ 路径:“/tmp/fabric-client-stateStore/” })

无论如何,对于所有正在阅读本文的人,我想表达我诚挚的感谢

这是我的密码

//get.js
'use strict';

var hfc = require('fabric-client'); 
var path = require('path'); 
var util = require('util'); 
var sdkUtils = require('fabric-client/lib/utils') 
const fs = require('fs'); 
var options = { 
    user_id: 'Admin@sell.trace.com', 
    msp_id:'OrgSellMSP', 
    channel_id: 'mychannel', 
    chaincode_id: 'sellcc', 
    peer_url: 'grpcs://localhost:7051',//grpcs
    privateKeyFolder:'/root/hyperledger-fabric/test/basic-network/crypto-config/peerOrganizations/sell.trace.com/users/Admin@sell.trace.com/msp/keystore', 
    signedCert:'/root/hyperledger-fabric/test/basic-network/crypto-config/peerOrganizations/sell.trace.com/users/Admin@sell.trace.com/msp/signcerts/Admin@sell.trace.com-cert.pem', 
    peer_tls_cacerts:'/root/hyperledger-fabric/test/basic-network/crypto-config/peerOrganizations/sell.trace.com/peers/peer0.sell.trace.com/tls/ca.crt', 
    server_hostname: "peer0.sell.trace.com" 
};

var channel = {}; 
var client = null; 
var targets = []; 
var tx_id = null; 
const getKeyFilesInDir = (dir) => { 
        const files = fs.readdirSync(dir) 
        const keyFiles = [] 
        files.forEach((file_name) => { 
                let filePath = path.join(dir, file_name) 
                if (file_name.endsWith('_sk')) { 
                        keyFiles.push(filePath) 
                } 
        }) 
        return keyFiles 
} 
Promise.resolve().then(() => { 
    console.log("Load privateKey and signedCert"); 
    client = new hfc(); 
    var    createUserOpt = { 
                username: options.user_id, 
                mspid: options.msp_id, 
                cryptoContent: { privateKey: getKeyFilesInDir(options.privateKeyFolder)[0], 
  signedCert: options.signedCert } 
         } 
//key set
return sdkUtils.newKeyValueStore({ 
                       //What is this path
                        path: "/tmp/fabric-client-stateStore/" 
                }).then((store) => { 
                        client.setStateStore(store) 
                         return client.createUser(createUserOpt) 
                 }) 
}).then((user) => { 
    channel = client.newChannel(options.channel_id); 
    
    let data = fs.readFileSync(options.peer_tls_cacerts); 
    let peer = client.newPeer(options.peer_url, 
         { 
            pem: Buffer.from(data).toString(), 
             'ssl-target-name-override': options.server_hostname 
        } 
    ); 
    peer.setName("peer0"); 
    //set TLS cet
    channel.addPeer(peer); 
    return; 
}).then(() => { 
    console.log("Get History"); 
    var transaction_id = client.newTransactionID(); 
    console.log("Assigning transaction_id: ", transaction_id._transaction_id); 
    //Construct the query request parameter
    const request = { 
        chaincodeId: options.chaincode_id, 
        txId: transaction_id, 
        fcn: 'githistory', 
        args: 's-001'
    }; 
     return channel.queryByChaincode(request); 
}).then((query_responses) => { 
    console.log("returned from gethistory"); 
    if (!query_responses.length) { 
        console.log("No payloads were returned from gethistory"); 
    } else { 
        console.log("Gethistory result count = ", query_responses.length) 
    } 
    if (query_responses[0] instanceof Error) { 
        console.error("error from gethistory = ", query_responses[0]); 
    } 
    console.log("Response is ", query_responses[0].toString());//print result
}).catch((err) => { 
    console.error("Caught Error", err); 
});
//sell.yaml
  peer0.sell.trace.com:
    container_name: peer0.sell.trace.com
    image: hyperledger/fabric-peer:latest
    environment:
      - CORE_PEER_ID=peer0.sell.trace.com
      - CORE_PEER_ADDRESS=peer0.sell.trace.com:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.sell.trace.com:7051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer1.sell.trace.com:7051
      - CORE_PEER_LOCALMSPID=OrgSellMSP
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=test_default
      - FABRIC_LOGGING_SPEC=INFO
      #- FABRIC_LOGGING_SPEC=DEBUG
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_PROFILE_ENABLED=true
      #TLS
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
      - GODEBUG=netdns=go
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/sell.trace.com/peers/peer0.sell.trace.com/msp:/etc/hyperledger/fabric/msp
        - ./crypto-config/peerOrganizations/sell.trace.com/peers/peer0.sell.trace.com/tls:/etc/hyperledger/fabric/tls
        #- ./crypto-config/peerOrganizations/sell.trace.com/users/Admin@sell.trace.com/tls:/etc/hyperledger/client/tls
    ports:
      - 1151:7051
      - 1153:7053
    networks:
      default:
        aliases:
          - test