diff --git a/fabcar/javascript-low-level/.gitignore b/fabcar/javascript-low-level/.gitignore new file mode 100644 index 00000000..6480661f --- /dev/null +++ b/fabcar/javascript-low-level/.gitignore @@ -0,0 +1,79 @@ +# +# SPDX-License-Identifier: Apache-2.0 +# + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# next.js build output +.next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless + +hfc-key-store diff --git a/fabcar/javascript-low-level/enrollAdmin.js b/fabcar/javascript-low-level/enrollAdmin.js index 9798479e..ca1ec9e4 100644 --- a/fabcar/javascript-low-level/enrollAdmin.js +++ b/fabcar/javascript-low-level/enrollAdmin.js @@ -11,15 +11,17 @@ var Fabric_Client = require('fabric-client'); var Fabric_CA_Client = require('fabric-ca-client'); +var fs = require('fs'); var path = require('path'); -var util = require('util'); -var os = require('os'); + +var firstnetwork_path = path.resolve('..', '..', 'first-network'); +var org1tlscacert_path = path.resolve(firstnetwork_path, 'crypto-config', 'peerOrganizations', 'org1.example.com', 'tlsca', 'tlsca.org1.example.com-cert.pem'); +var org1tlscacert = fs.readFileSync(org1tlscacert_path, 'utf8'); // var fabric_client = new Fabric_Client(); var fabric_ca_client = null; var admin_user = null; -var member_user = null; var store_path = path.join(__dirname, 'hfc-key-store'); console.log(' Store path:'+store_path); @@ -35,11 +37,11 @@ Fabric_Client.newDefaultKeyValueStore({ path: store_path crypto_suite.setCryptoKeyStore(crypto_store); fabric_client.setCryptoSuite(crypto_suite); var tlsOptions = { - trustedRoots: [], + trustedRoots: [org1tlscacert], verify: false }; // be sure to change the http to https when the CA is running TLS enabled - fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', tlsOptions , 'ca.example.com', crypto_suite); + fabric_ca_client = new Fabric_CA_Client('https://localhost:7054', tlsOptions , 'ca-org1', crypto_suite); // first check to see if the admin is already enrolled return fabric_client.getUserContext('admin', true); diff --git a/fabcar/javascript-low-level/invoke.js b/fabcar/javascript-low-level/invoke.js index 65e067a7..aab136c8 100644 --- a/fabcar/javascript-low-level/invoke.js +++ b/fabcar/javascript-low-level/invoke.js @@ -9,9 +9,13 @@ */ const Fabric_Client = require('fabric-client'); +const fs = require('fs'); const path = require('path'); const util = require('util'); -const os = require('os'); + +var firstnetwork_path = path.resolve('..', '..', 'first-network'); +var org1tlscacert_path = path.resolve(firstnetwork_path, 'crypto-config', 'peerOrganizations', 'org1.example.com', 'tlsca', 'tlsca.org1.example.com-cert.pem'); +var org1tlscacert = fs.readFileSync(org1tlscacert_path, 'utf8'); invoke(); @@ -28,16 +32,15 @@ async function invoke() { const channel = fabric_client.newChannel('mychannel'); console.log('Created client side object to represent the channel'); // -- peer instance to represent a peer on the channel - const peer = fabric_client.newPeer('grpc://localhost:7051'); + const peer = fabric_client.newPeer('grpcs://localhost:7051', { + 'ssl-target-name-override': 'peer0.org1.example.com', + pem: org1tlscacert + }); console.log('Created client side object to represent the peer'); - // -- orderer instance to reprsent the channel's orderer - const orderer = fabric_client.newOrderer('grpc://localhost:7050') - console.log('Created client side object to represent the orderer'); // This sample application uses a file based key value stores to hold // the user information and credentials. These are the same stores as used // by the 'registerUser.js' sample code - const member_user = null; const store_path = path.join(__dirname, 'hfc-key-store'); console.log('Setting up the user store at path:'+store_path); // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting @@ -63,6 +66,10 @@ async function invoke() { console.log('Successfully setup client side'); console.log('\n\nStart invoke processing'); + // Use service discovery to initialize the channel + await channel.initialize({ discover: true, asLocalhost: true, target: peer }); + console.log('Used service discovery to initialize the channel'); + // get a transaction id object based on the current user assigned to fabric client // Transaction ID objects contain more then just a transaction ID, also includes // a nonce value and if built from the client's admin user. @@ -116,7 +123,6 @@ async function invoke() { // committed. const commit_request = { - orderer: orderer, proposalResponses: proposalResponses, proposal: proposal }; diff --git a/fabcar/javascript-low-level/query.js b/fabcar/javascript-low-level/query.js index ba57ac3f..85badb82 100644 --- a/fabcar/javascript-low-level/query.js +++ b/fabcar/javascript-low-level/query.js @@ -9,23 +9,27 @@ */ var Fabric_Client = require('fabric-client'); +var fs = require('fs'); var path = require('path'); -var util = require('util'); -var os = require('os'); + +var firstnetwork_path = path.resolve('..', '..', 'first-network'); +var org1tlscacert_path = path.resolve(firstnetwork_path, 'crypto-config', 'peerOrganizations', 'org1.example.com', 'tlsca', 'tlsca.org1.example.com-cert.pem'); +var org1tlscacert = fs.readFileSync(org1tlscacert_path, 'utf8'); // var fabric_client = new Fabric_Client(); // setup the fabric network var channel = fabric_client.newChannel('mychannel'); -var peer = fabric_client.newPeer('grpc://localhost:7051'); +var peer = fabric_client.newPeer('grpcs://localhost:7051', { + 'ssl-target-name-override': 'peer0.org1.example.com', + pem: org1tlscacert +}); channel.addPeer(peer); // -var member_user = null; var store_path = path.join(__dirname, 'hfc-key-store'); console.log('Store path:'+store_path); -var tx_id = null; // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting Fabric_Client.newDefaultKeyValueStore({ path: store_path @@ -44,7 +48,6 @@ Fabric_Client.newDefaultKeyValueStore({ path: store_path }).then((user_from_store) => { if (user_from_store && user_from_store.isEnrolled()) { console.log('Successfully loaded user1 from persistence'); - member_user = user_from_store; } else { throw new Error('Failed to get user1.... run registerUser.js'); } diff --git a/fabcar/javascript-low-level/registerUser.js b/fabcar/javascript-low-level/registerUser.js index 9aa93a9c..b1aacc7c 100644 --- a/fabcar/javascript-low-level/registerUser.js +++ b/fabcar/javascript-low-level/registerUser.js @@ -11,9 +11,12 @@ var Fabric_Client = require('fabric-client'); var Fabric_CA_Client = require('fabric-ca-client'); +var fs = require('fs'); var path = require('path'); -var util = require('util'); -var os = require('os'); + +var firstnetwork_path = path.resolve('..', '..', 'first-network'); +var org1tlscacert_path = path.resolve(firstnetwork_path, 'crypto-config', 'peerOrganizations', 'org1.example.com', 'tlsca', 'tlsca.org1.example.com-cert.pem'); +var org1tlscacert = fs.readFileSync(org1tlscacert_path, 'utf8'); // var fabric_client = new Fabric_Client(); @@ -35,11 +38,11 @@ Fabric_Client.newDefaultKeyValueStore({ path: store_path crypto_suite.setCryptoKeyStore(crypto_store); fabric_client.setCryptoSuite(crypto_suite); var tlsOptions = { - trustedRoots: [], + trustedRoots: [org1tlscacert], verify: false }; // be sure to change the http to https when the CA is running TLS enabled - fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', null , '', crypto_suite); + fabric_ca_client = new Fabric_CA_Client('https://localhost:7054', tlsOptions , 'ca-org1', crypto_suite); // first check to see if the admin is already enrolled return fabric_client.getUserContext('admin', true);