/* Copyright IBM Corp 2016 All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * This example shows how to do the following in a web app. * 1) At initialization time, enroll the web app with the blockchain. * The identity must have already been registered. * 2) At run time, after a user has authenticated with the web app: * a) register and enroll an identity for the user; * b) use this identity to deploy, query, and invoke a chaincode. */ var hfc = require('hfc'); //get the addresses from the docker-compose environment var PEER_ADDRESS = process.env.PEER_ADDRESS; var MEMBERSRVC_ADDRESS = process.env.MEMBERSRVC_ADDRESS; // Create a client chain. // The name can be anything as it is only used internally. var chain = hfc.newChain("targetChain"); // Configure the KeyValStore which is used to store sensitive keys // as so it is important to secure this storage. // The FileKeyValStore is a simple file-based KeyValStore, but you // can easily implement your own to store whereever you want. // To work correctly in a cluster, the file-based KeyValStore must // either be on a shared file system shared by all members of the cluster // or you must implement you own KeyValStore which all members of the // cluster can share. chain.setKeyValStore( hfc.newFileKeyValStore('/tmp/keyValStore') ); // Set the URL for membership services chain.setMemberServicesUrl("grpc://MEMBERSRVC_ADDRESS"); // Add at least one peer's URL. If you add multiple peers, it will failover // to the 2nd if the 1st fails, to the 3rd if both the 1st and 2nd fails, etc. chain.addPeer("grpc://PEER_ADDRESS"); // Enroll "WebAppAdmin" which is already registered because it is // listed in fabric/membersrvc/membersrvc.yaml with its one time password. // If "WebAppAdmin" has already been registered, this will still succeed // because it stores the state in the KeyValStore // (i.e. in '/tmp/keyValStore' in this sample). chain.enroll("WebAppAdmin", "DJY27pEnl16d", function(err, webAppAdmin) { if (err) return console.log("ERROR: failed to register %s: %s",err); // Successfully enrolled WebAppAdmin during initialization. // Set this user as the chain's registrar which is authorized to register other users. chain.setRegistrar(webAppAdmin); // Now begin listening for web app requests listenForUserRequests(); }); // Main web app function to listen for and handle requests. This is specific to // your application but is provided here to demonstrate the pattern. function listenForUserRequests() { for (;;) { // WebApp-specific logic goes here to await the next request. // ... // Assume that we received a request from an authenticated user // and have 'userName' and 'userAccount'. // Then determined that we need to invoke the chaincode // with 'chaincodeID' and function named 'fcn' with arguments 'args'. handleUserRequest(userName,userAccount,chaincodeID,fcn,args); } } // Handle a user request function handleUserRequest(userName, userAccount, chaincodeID, fcn, args) { // Register and enroll this user. // If this user has already been registered and/or enrolled, this will // still succeed because the state is kept in the KeyValStore // (i.e. in '/tmp/keyValStore' in this sample). var registrationRequest = { roles: [ 'client' ], enrollmentID: userName, affiliation: "bank_a", attributes: [{name:'role',value:'client'},{name:'account',value:userAccount}] }; chain.registerAndEnroll( registrationRequest, function(err, user) { if (err) return console.log("ERROR: %s",err); // Issue an invoke request var invokeRequest = { // Name (hash) required for invoke chaincodeID: chaincodeID, // Function to trigger fcn: fcn, // Parameters for the invoke function args: args }; // Invoke the request from the user object and wait for events to occur. var tx = user.invoke(invokeRequest); // Listen for the 'submitted' event tx.on('submitted', function(results) { console.log("submitted invoke: %j",results); }); // Listen for the 'complete' event. tx.on('complete', function(results) { console.log("completed invoke: %j",results); }); // Listen for the 'error' event. tx.on('error', function(err) { console.log("error on invoke: %j",err); }); }); }