hyperledger(教程笔记第四天)

'use strict';varFabric_Client =require('fabric-client');varpath =require('path');varutil =require('util');varos =require('os');varfabric_client =newFabric_Client();// 设置fabric网络varchannel = fabric_client.newChannel('mychannel');varpeer = fabric_client.newPeer('grpc://localhost:7051');channel.addPeer(peer);//varmember_user =null;varstore_path = path.join(__dirname,'hfc-key-store');console.log('Store path:'+store_path);vartx_id =null;varquery =async(fcn,args)=>{try{// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' settingvarstate_store =awaitFabric_Client.newDefaultKeyValueStore({path: store_path});// assign the store to the fabric clientfabric_client.setStateStore(state_store);varcrypto_suite = Fabric_Client.newCryptoSuite();// use the same location for the state store (where the users' certificate are kept)// and the crypto store (where the users' keys are kept)varcrypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});        crypto_suite.setCryptoKeyStore(crypto_store);        fabric_client.setCryptoSuite(crypto_suite);// get the enrolled user from persistence, this user will sign all requestsvaruser_from_store =awaitfabric_client.getUserContext('user1',true);if(user_from_store && user_from_store.isEnrolled()) {console.log('Successfully loaded user1 from persistence');            member_user = user_from_store;        }else{thrownewError('Failed to get user1.... run registerUser.js');        }// queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],// queryAllCars chaincode function - requires no arguments , ex: args: [''],constrequest = {//targets : --- letting this default to the peers assigned to the channelchaincodeId:'fabcar',fcn: fcn,args: args        };// send the query proposal to the peervarquery_responses =awaitchannel.queryByChaincode(request);console.log("Query has completed, checking results");// query_responses could have more than one  results if there multiple peers were used as targetsif(query_responses && query_responses.length ==1) {if(query_responses[0]instanceofError) {console.error("error from query = ", query_responses[0]);            }else{console.log("Response is ", query_responses[0].toString());            }        }else{console.log("No payloads were returned from query");        }    }catch(err){console.error('Failed to query successfully :: '+ err);    }};console.log(process.argv[2]);console.log(process.argv[3]);varargs =newArray(process.argv[3]);query(process.argv[2],args);

constexpress =require('express')constapp = express()varFabric_Client =require('fabric-client');varpath =require('path');varutil =require('util');varos =require('os');varfabric_client =newFabric_Client();// 设置fabric网络varchannel = fabric_client.newChannel('mychannel');varpeer = fabric_client.newPeer('grpc://localhost:7051');channel.addPeer(peer);//varmember_user =null;varstore_path = path.join(__dirname,'hfc-key-store');console.log('Store path:'+store_path);vartx_id =null;varquery =async(fcn,args)=>{try{// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' settingvarstate_store =awaitFabric_Client.newDefaultKeyValueStore({path: store_path});// assign the store to the fabric clientfabric_client.setStateStore(state_store);varcrypto_suite = Fabric_Client.newCryptoSuite();// use the same location for the state store (where the users' certificate are kept)// and the crypto store (where the users' keys are kept)varcrypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});        crypto_suite.setCryptoKeyStore(crypto_store);        fabric_client.setCryptoSuite(crypto_suite);// get the enrolled user from persistence, this user will sign all requestsvaruser_from_store =awaitfabric_client.getUserContext('user1',true);if(user_from_store && user_from_store.isEnrolled()) {console.log('Successfully loaded user1 from persistence');            member_user = user_from_store;        }else{thrownewError('Failed to get user1.... run registerUser.js');        }// queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],// queryAllCars chaincode function - requires no arguments , ex: args: [''],constrequest = {//targets : --- letting this default to the peers assigned to the channelchaincodeId:'fabcar',fcn: fcn,args: args        };// send the query proposal to the peervarquery_responses =awaitchannel.queryByChaincode(request);console.log("Query has completed, checking results");// query_responses could have more than one  results if there multiple peers were used as targetsif(query_responses && query_responses.length ==1) {if(query_responses[0]instanceofError) {return("error from query = ", query_responses[0]);            }else{return("Response is ", query_responses[0].toString());            }        }else{return("No payloads were returned from query");        }    }catch(err){return('Failed to query successfully :: '+ err);    }};app.get('/:fcn/:fcn1',async(req, res) =>{console.log(req.params.fcn);console.log(req.params.fcn1);varresult =awaitquery(req.params.fcn,newArray(req.params.fcn1));    res.send('Hello World!'+ result);});app.listen(80, () =>console.log('Example app listening on port 80!'))

你可能感兴趣的:(hyperledger(教程笔记第四天))