From f0dca204466135ce0e8e44eca307c122b96f6e9e Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Mon, 10 Jun 2019 19:14:36 +0100 Subject: [PATCH] [FAB-14532] Remove LL FabCar sample Remove the Fabric v2.0 version of the low-level FabCar sample. We should not be advertising how to use the low-level APIs at this point. The low-level samples are still available for Fabric v1.4 to show the difference between the low-level and high-level APIs. Signed-off-by: Simon Stone Change-Id: I0faef40d4cfddc689e6d7184491fd3759a3558a1 --- fabcar/javascript-low-level/enrollAdmin.js | 75 ------- fabcar/javascript-low-level/invoke.js | 217 -------------------- fabcar/javascript-low-level/package.json | 22 -- fabcar/javascript-low-level/query.js | 77 ------- fabcar/javascript-low-level/registerUser.js | 82 -------- 5 files changed, 473 deletions(-) delete mode 100644 fabcar/javascript-low-level/enrollAdmin.js delete mode 100644 fabcar/javascript-low-level/invoke.js delete mode 100644 fabcar/javascript-low-level/package.json delete mode 100644 fabcar/javascript-low-level/query.js delete mode 100644 fabcar/javascript-low-level/registerUser.js diff --git a/fabcar/javascript-low-level/enrollAdmin.js b/fabcar/javascript-low-level/enrollAdmin.js deleted file mode 100644 index 9798479e49..0000000000 --- a/fabcar/javascript-low-level/enrollAdmin.js +++ /dev/null @@ -1,75 +0,0 @@ -'use strict'; -/* -* Copyright IBM Corp All Rights Reserved -* -* SPDX-License-Identifier: Apache-2.0 -*/ -/* - * Enroll the admin user - */ - -var Fabric_Client = require('fabric-client'); -var Fabric_CA_Client = require('fabric-ca-client'); - -var path = require('path'); -var util = require('util'); -var os = require('os'); - -// -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); - -// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting -Fabric_Client.newDefaultKeyValueStore({ path: store_path -}).then((state_store) => { - // assign the store to the fabric client - fabric_client.setStateStore(state_store); - var crypto_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) - var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path}); - crypto_suite.setCryptoKeyStore(crypto_store); - fabric_client.setCryptoSuite(crypto_suite); - var tlsOptions = { - trustedRoots: [], - 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); - - // first check to see if the admin is already enrolled - return fabric_client.getUserContext('admin', true); -}).then((user_from_store) => { - if (user_from_store && user_from_store.isEnrolled()) { - console.log('Successfully loaded admin from persistence'); - admin_user = user_from_store; - return null; - } else { - // need to enroll it with CA server - return fabric_ca_client.enroll({ - enrollmentID: 'admin', - enrollmentSecret: 'adminpw' - }).then((enrollment) => { - console.log('Successfully enrolled admin user "admin"'); - return fabric_client.createUser( - {username: 'admin', - mspid: 'Org1MSP', - cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate } - }); - }).then((user) => { - admin_user = user; - return fabric_client.setUserContext(admin_user); - }).catch((err) => { - console.error('Failed to enroll and persist admin. Error: ' + err.stack ? err.stack : err); - throw new Error('Failed to enroll admin'); - }); - } -}).then(() => { - console.log('Assigned the admin user to the fabric client ::' + admin_user.toString()); -}).catch((err) => { - console.error('Failed to enroll admin: ' + err); -}); diff --git a/fabcar/javascript-low-level/invoke.js b/fabcar/javascript-low-level/invoke.js deleted file mode 100644 index e30b7e7c36..0000000000 --- a/fabcar/javascript-low-level/invoke.js +++ /dev/null @@ -1,217 +0,0 @@ -'use strict'; -/* -* Copyright IBM Corp All Rights Reserved -* -* SPDX-License-Identifier: Apache-2.0 -*/ -/* - * Chaincode Invoke - */ - -const Fabric_Client = require('fabric-client'); -const path = require('path'); -const util = require('util'); -const os = require('os'); - -invoke(); - -async function invoke() { - console.log('\n\n --- invoke.js - start'); - try { - console.log('Setting up client side network objects'); - // fabric client instance - // starting point for all interactions with the fabric network - const fabric_client = new Fabric_Client(); - - // setup the fabric network - // -- channel instance to represent the ledger named "mychannel" - 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'); - 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 - const state_store = await Fabric_Client.newDefaultKeyValueStore({ path: store_path}); - // assign the store to the fabric client - fabric_client.setStateStore(state_store); - const crypto_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) - const crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path}); - crypto_suite.setCryptoKeyStore(crypto_store); - fabric_client.setCryptoSuite(crypto_suite); - - // get the enrolled user from persistence and assign to the client instance - // this user will sign all requests for the fabric network - const user = await fabric_client.getUserContext('user1', true); - if (user && user.isEnrolled()) { - console.log('Successfully loaded "user1" from user store'); - } else { - throw new Error('\n\nFailed to get user1.... run registerUser.js'); - } - - console.log('Successfully setup client side'); - console.log('\n\nStart invoke processing'); - - // 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. - const tx_id = fabric_client.newTransactionID(); - console.log(util.format("\nCreated a transaction ID: %s", tx_id.getTransactionID())); - - // The fabcar chaincode is able to perform a few functions - // 'createCar' - requires 5 args, ex: args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'] - // 'changeCarOwner' - requires 2 args , ex: args: ['CAR10', 'Dave'] - const proposal_request = { - targets: [peer], - chaincodeId: 'fabcar', - fcn: 'createCar', - args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'], - txId: tx_id - }; - - // notice the proposal_request has the peer defined in the 'targets' attribute - - // Send the transaction proposal to the endorsing peers. - // The peers will run the function requested with the arguments supplied - // based on the current state of the ledger. If the chaincode successfully - // runs this simulation it will return a postive result in the endorsement. - const endorsement_results = await channel.sendTransactionProposal(proposal_request); - - // The results will contain a few different items - // first is the actual endorsements by the peers, these will be the responses - // from the peers. In our sammple there will only be one results since - // only sent the proposal to one peer. - // second is the proposal that was sent to the peers to be endorsed. This will - // be needed later when the endorsements are sent to the orderer. - const proposalResponses = endorsement_results[0]; - const proposal = endorsement_results[1]; - - // check the results to decide if we should send the endorsment to be orderered - if (proposalResponses[0] instanceof Error) { - console.error('Failed to send Proposal. Received an error :: ' + proposalResponses[0].toString()); - throw proposalResponses[0]; - } else if (proposalResponses[0].response && proposalResponses[0].response.status === 200) { - console.log(util.format( - 'Successfully sent Proposal and received response: Status - %s', - proposalResponses[0].response.status)); - } else { - const error_message = util.format('Invoke chaincode proposal:: %j', proposalResponses[i]); - console.error(error_message); - throw new Error(error_message); - } - - // The proposal was good, now send to the orderer to have the transaction - // committed. - - const commit_request = { - orderer: orderer, - proposalResponses: proposalResponses, - proposal: proposal - }; - - //Get the transaction ID string to be used by the event processing - const transaction_id_string = tx_id.getTransactionID(); - - // create an array to hold on the asynchronous calls to be executed at the - // same time - const promises = []; - - // this will send the proposal to the orderer during the execuction of - // the promise 'all' call. - const sendPromise = channel.sendTransaction(commit_request); - //we want the send transaction first, so that we know where to check status - promises.push(sendPromise); - - // get an event hub that is associated with our peer - let event_hub = channel.newChannelEventHub(peer); - - // create the asynchronous work item - let txPromise = new Promise((resolve, reject) => { - // setup a timeout of 30 seconds - // if the transaction does not get committed within the timeout period, - // report TIMEOUT as the status. This is an application timeout and is a - // good idea to not let the listener run forever. - let handle = setTimeout(() => { - event_hub.unregisterTxEvent(transaction_id_string); - event_hub.disconnect(); - resolve({event_status : 'TIMEOUT'}); - }, 30000); - - // this will register a listener with the event hub. THe included callbacks - // will be called once transaction status is received by the event hub or - // an error connection arises on the connection. - event_hub.registerTxEvent(transaction_id_string, (tx, code) => { - // this first callback is for transaction event status - - // callback has been called, so we can stop the timer defined above - clearTimeout(handle); - - // now let the application know what happened - const return_status = {event_status : code, tx_id : transaction_id_string}; - if (code !== 'VALID') { - console.error('The transaction was invalid, code = ' + code); - resolve(return_status); // we could use reject(new Error('Problem with the tranaction, event status ::'+code)); - } else { - console.log('The transaction has been committed on peer ' + event_hub.getPeerAddr()); - resolve(return_status); - } - }, (err) => { - //this is the callback if something goes wrong with the event registration or processing - reject(new Error('There was a problem with the eventhub ::'+err)); - }, - {disconnect: true} //disconnect when complete - ); - - // now that we have a protective timer running and the listener registered, - // have the event hub instance connect with the peer's event service - event_hub.connect(); - console.log('Registered transaction listener with the peer event service for transaction ID:'+ transaction_id_string); - }); - - // set the event work with the orderer work so they may be run at the same time - promises.push(txPromise); - - // now execute both pieces of work and wait for both to complete - console.log('Sending endorsed transaction to the orderer'); - const results = await Promise.all(promises); - - // since we added the orderer work first, that will be the first result on - // the list of results - // success from the orderer only means that it has accepted the transaction - // you must check the event status or the ledger to if the transaction was - // committed - if (results[0].status === 'SUCCESS') { - console.log('Successfully sent transaction to the orderer'); - } else { - const message = util.format('Failed to order the transaction. Error code: %s', results[0].status); - console.error(message); - throw new Error(message); - } - - if (results[1] instanceof Error) { - console.error(message); - throw new Error(message); - } else if (results[1].event_status === 'VALID') { - console.log('Successfully committed the change to the ledger by the peer'); - console.log('\n\n - try running "node query.js" to see the results'); - } else { - const message = util.format('Transaction failed to be committed to the ledger due to : %s', results[1].event_status) - console.error(message); - throw new Error(message); - } - } catch(error) { - console.log('Unable to invoke ::'+ error.toString()); - } - console.log('\n\n --- invoke.js - end'); -}; diff --git a/fabcar/javascript-low-level/package.json b/fabcar/javascript-low-level/package.json deleted file mode 100644 index 481773ab0f..0000000000 --- a/fabcar/javascript-low-level/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "fabcar", - "version": "1.0.0", - "description": "Hyperledger Fabric Car Sample Application", - "main": "fabcar.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "dependencies": { - "fabric-ca-client": "unstable", - "fabric-client": "unstable" - }, - "author": "Anthony O'Dowd", - "license": "Apache-2.0", - "keywords": [ - "Hyperledger", - "Fabric", - "Car", - "Sample", - "Application" - ] -} diff --git a/fabcar/javascript-low-level/query.js b/fabcar/javascript-low-level/query.js deleted file mode 100644 index ba57ac3fb9..0000000000 --- a/fabcar/javascript-low-level/query.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict'; -/* -* Copyright IBM Corp All Rights Reserved -* -* SPDX-License-Identifier: Apache-2.0 -*/ -/* - * Chaincode query - */ - -var Fabric_Client = require('fabric-client'); -var path = require('path'); -var util = require('util'); -var os = require('os'); - -// -var fabric_client = new Fabric_Client(); - -// setup the fabric network -var channel = fabric_client.newChannel('mychannel'); -var peer = fabric_client.newPeer('grpc://localhost:7051'); -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 -}).then((state_store) => { - // assign the store to the fabric client - fabric_client.setStateStore(state_store); - var crypto_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) - var crypto_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 requests - return fabric_client.getUserContext('user1', true); -}).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'); - } - - // queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'], - // queryAllCars chaincode function - requires no arguments , ex: args: [''], - const request = { - //targets : --- letting this default to the peers assigned to the channel - chaincodeId: 'fabcar', - fcn: 'queryAllCars', - args: [''] - }; - - // send the query proposal to the peer - return channel.queryByChaincode(request); -}).then((query_responses) => { - console.log("Query has completed, checking results"); - // query_responses could have more than one results if there multiple peers were used as targets - if (query_responses && query_responses.length == 1) { - if (query_responses[0] instanceof Error) { - 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); -}); diff --git a/fabcar/javascript-low-level/registerUser.js b/fabcar/javascript-low-level/registerUser.js deleted file mode 100644 index 9aa93a9c21..0000000000 --- a/fabcar/javascript-low-level/registerUser.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict'; -/* -* Copyright IBM Corp All Rights Reserved -* -* SPDX-License-Identifier: Apache-2.0 -*/ -/* - * Register and Enroll a user - */ - -var Fabric_Client = require('fabric-client'); -var Fabric_CA_Client = require('fabric-ca-client'); - -var path = require('path'); -var util = require('util'); -var os = require('os'); - -// -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); - -// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting -Fabric_Client.newDefaultKeyValueStore({ path: store_path -}).then((state_store) => { - // assign the store to the fabric client - fabric_client.setStateStore(state_store); - var crypto_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) - var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path}); - crypto_suite.setCryptoKeyStore(crypto_store); - fabric_client.setCryptoSuite(crypto_suite); - var tlsOptions = { - trustedRoots: [], - 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); - - // first check to see if the admin is already enrolled - return fabric_client.getUserContext('admin', true); -}).then((user_from_store) => { - if (user_from_store && user_from_store.isEnrolled()) { - console.log('Successfully loaded admin from persistence'); - admin_user = user_from_store; - } else { - throw new Error('Failed to get admin.... run enrollAdmin.js'); - } - - // at this point we should have the admin user - // first need to register the user with the CA server - return fabric_ca_client.register({enrollmentID: 'user1', affiliation: 'org1.department1',role: 'client'}, admin_user); -}).then((secret) => { - // next we need to enroll the user with CA server - console.log('Successfully registered user1 - secret:'+ secret); - - return fabric_ca_client.enroll({enrollmentID: 'user1', enrollmentSecret: secret}); -}).then((enrollment) => { - console.log('Successfully enrolled member user "user1" '); - return fabric_client.createUser( - {username: 'user1', - mspid: 'Org1MSP', - cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate } - }); -}).then((user) => { - member_user = user; - - return fabric_client.setUserContext(member_user); -}).then(()=>{ - console.log('User1 was successfully registered and enrolled and is ready to interact with the fabric network'); - -}).catch((err) => { - console.error('Failed to register: ' + err); - if(err.toString().indexOf('Authorization') > -1) { - console.error('Authorization failures may be caused by having admin credentials from a previous CA instance.\n' + - 'Try again after deleting the contents of the store directory '+store_path); - } -});