github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/tools/PTE/pte-util.js (about) 1 /** 2 * Copyright 2016 IBM All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the 'License'); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an 'AS IS' BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 var path = require('path'); 18 var fs = require('fs-extra'); 19 var os = require('os'); 20 var util = require('util'); 21 22 var jsrsa = require('jsrsasign'); 23 var KEYUTIL = jsrsa.KEYUTIL; 24 25 var hfc = require('fabric-client'); 26 var copService = require('fabric-ca-client/lib/FabricCAClientImpl.js'); 27 var User = require('fabric-client/lib/User.js'); 28 var CryptoSuite = require('fabric-client/lib/impl/CryptoSuite_ECDSA_AES.js'); 29 var KeyStore = require('fabric-client/lib/impl/CryptoKeyStore.js'); 30 var ecdsaKey = require('fabric-client/lib/impl/ecdsa/key.js'); 31 32 module.exports.CHAINCODE_PATH = 'github.com/example_cc'; 33 module.exports.CHAINCODE_UPGRADE_PATH = 'github.com/example_cc1'; 34 module.exports.CHAINCODE_MARBLES_PATH = 'github.com/marbles_cc'; 35 module.exports.END2END = { 36 channel: 'mychannel', 37 chaincodeId: 'end2end', 38 chaincodeVersion: 'v0' 39 }; 40 41 // directory for file based KeyValueStore 42 module.exports.KVS = '/tmp/hfc-test-kvs'; 43 module.exports.storePathForOrg = function(org) { 44 return module.exports.KVS + '_' + org; 45 }; 46 47 // temporarily set $GOPATH to the test fixture folder 48 module.exports.setupChaincodeDeploy = function() { 49 process.env.GOPATH = path.join(__dirname, '../fixtures'); 50 }; 51 52 // specifically set the values to defaults because they may have been overridden when 53 // running in the overall test bucket ('gulp test') 54 module.exports.resetDefaults = function() { 55 global.hfc.config = undefined; 56 require('nconf').reset(); 57 }; 58 59 module.exports.cleanupDir = function(keyValStorePath) { 60 var absPath = path.join(process.cwd(), keyValStorePath); 61 var exists = module.exports.existsSync(absPath); 62 if (exists) { 63 fs.removeSync(absPath); 64 } 65 }; 66 67 module.exports.getUniqueVersion = function(prefix) { 68 if (!prefix) prefix = 'v'; 69 return prefix + Date.now(); 70 }; 71 72 // utility function to check if directory or file exists 73 // uses entire / absolute path from root 74 module.exports.existsSync = function(absolutePath /*string*/) { 75 try { 76 var stat = fs.statSync(absolutePath); 77 if (stat.isDirectory() || stat.isFile()) { 78 return true; 79 } else 80 return false; 81 } 82 catch (e) { 83 return false; 84 } 85 }; 86 87 module.exports.readFile = readFile; 88 89 var ORGS; 90 91 var tlsOptions = { 92 trustedRoots: [], 93 verify: false 94 }; 95 96 function getMember(username, password, client, userOrg, svcFile) { 97 hfc.addConfigFile(svcFile); 98 ORGS = hfc.getConfigSetting('test-network'); 99 100 var caUrl = ORGS[userOrg].ca.url; 101 102 console.log('getMember, name: '+username+', client.getUserContext('+username+', true)'); 103 104 return client.getUserContext(username, true) 105 .then((user) => { 106 return new Promise((resolve, reject) => { 107 if (user && user.isEnrolled()) { 108 console.log('Successfully loaded member from persistence'); 109 return resolve(user); 110 } 111 112 var member = new User(username); 113 var cryptoSuite = null; 114 if (userOrg) { 115 cryptoSuite = client.newCryptoSuite({path: module.exports.storePathForOrg(ORGS[userOrg].name)}); 116 } else { 117 cryptoSuite = client.newCryptoSuite(); 118 } 119 member.setCryptoSuite(cryptoSuite); 120 121 // need to enroll it with CA server 122 var cop = new copService(caUrl, tlsOptions, ORGS[userOrg].ca.name, cryptoSuite); 123 124 return cop.enroll({ 125 enrollmentID: username, 126 enrollmentSecret: password 127 }).then((enrollment) => { 128 console.log('Successfully enrolled user \'' + username + '\''); 129 130 return member.setEnrollment(enrollment.key, enrollment.certificate, ORGS[userOrg].mspid); 131 }).then(() => { 132 return client.setUserContext(member); 133 }).then(() => { 134 return resolve(member); 135 }).catch((err) => { 136 console.log('Failed to enroll and persist user. Error: ' + err.stack ? err.stack : err); 137 }); 138 }); 139 }); 140 } 141 142 function getAdmin(client, userOrg, svcFile) { 143 hfc.addConfigFile(svcFile); 144 ORGS = hfc.getConfigSetting('test-network'); 145 var mspPath = ORGS[userOrg].mspPath; 146 var keyPath = ORGS[userOrg].adminPath + '/keystore'; 147 var keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString(); 148 var certPath = ORGS[userOrg].adminPath + '/signcerts'; 149 var certPEM = readAllFiles(certPath)[0]; 150 console.log('[getAdmin] keyPath: %s', keyPath); 151 console.log('[getAdmin] certPath: %s', certPath); 152 153 if (userOrg) { 154 client.newCryptoSuite({path: module.exports.storePathForOrg(ORGS[userOrg].name)}); 155 } 156 157 return Promise.resolve(client.createUser({ 158 username: 'peer'+userOrg+'Admin', 159 mspid: ORGS[userOrg].mspid, 160 cryptoContent: { 161 privateKeyPEM: keyPEM.toString(), 162 signedCertPEM: certPEM.toString() 163 } 164 })); 165 } 166 167 function getOrdererAdmin(client, userOrg, svcFile) { 168 hfc.addConfigFile(svcFile); 169 ORGS = hfc.getConfigSetting('test-network'); 170 var mspPath = ORGS.orderer.mspPath; 171 var keyPath = ORGS.orderer.adminPath + '/keystore'; 172 var keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString(); 173 var certPath = ORGS.orderer.adminPath + '/signcerts'; 174 var certPEM = readAllFiles(certPath)[0]; 175 console.log('[getOrdererAdmin] keyPath: %s', keyPath); 176 console.log('[getOrdererAdmin] certPath: %s', certPath); 177 178 return Promise.resolve(client.createUser({ 179 username: 'ordererAdmin', 180 mspid: ORGS.orderer.mspid, 181 cryptoContent: { 182 privateKeyPEM: keyPEM.toString(), 183 signedCertPEM: certPEM.toString() 184 } 185 })); 186 } 187 188 function readFile(path) { 189 return new Promise((resolve, reject) => { 190 fs.readFile(path, (err, data) => { 191 if (!!err) 192 reject(new Error('Failed to read file ' + path + ' due to error: ' + err)); 193 else 194 resolve(data); 195 }); 196 }); 197 } 198 199 function readAllFiles(dir) { 200 var files = fs.readdirSync(dir); 201 var certs = []; 202 files.forEach((file_name) => { 203 let file_path = path.join(dir,file_name); 204 console.log(' looking at file ::'+file_path); 205 let data = fs.readFileSync(file_path); 206 certs.push(data); 207 }); 208 return certs; 209 } 210 211 module.exports.getOrderAdminSubmitter = function(client, userOrg, svcFile) { 212 return getOrdererAdmin(client, userOrg, svcFile); 213 }; 214 215 module.exports.getSubmitter = function(username, secret, client, peerOrgAdmin, org, svcFile) { 216 if (arguments.length < 2) throw new Error('"client" and "test" are both required parameters'); 217 218 var peerAdmin, userOrg; 219 if (typeof peerOrgAdmin === 'boolean') { 220 peerAdmin = peerOrgAdmin; 221 } else { 222 peerAdmin = false; 223 } 224 225 // if the 3rd argument was skipped 226 if (typeof peerOrgAdmin === 'string') { 227 userOrg = peerOrgAdmin; 228 } else { 229 if (typeof org === 'string') { 230 userOrg = org; 231 } else { 232 userOrg = 'org1'; 233 } 234 } 235 236 if (peerAdmin) { 237 console.log(' >>>> getting the org admin'); 238 return getAdmin(client, userOrg, svcFile); 239 } else { 240 return getMember(username, secret, client, userOrg, svcFile); 241 } 242 };