|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @fileoverview This class is boilerplate for Example Programs. They should inherit from this class. |
| 5 | + * @author rachin@ost.com (Rachin Kapoor) |
| 6 | + */ |
| 7 | + |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | +const OpenST = require('@openstfoundation/openst.js'); |
| 11 | + |
| 12 | +class PerformerBase { |
| 13 | + constructor(program) { |
| 14 | + let configPath = program.config || './openst-setup/config.json', |
| 15 | + historyPath = program.history || './openst-setup/history.log'; |
| 16 | + |
| 17 | + this.setHistoryArgs(program.rawArgs, historyPath); |
| 18 | + |
| 19 | + this.setConfigPath(configPath); |
| 20 | + let config = this.getSetupConfig(); |
| 21 | + |
| 22 | + let provider = config.gethRpcEndPoint; |
| 23 | + //Create Object of openst.js. |
| 24 | + this.openST = new OpenST(provider); |
| 25 | + this.web3 = this.openST.web3(); |
| 26 | + |
| 27 | + //Add Geth Signer Service so that we can unlock and sign using deployerAddress. |
| 28 | + let gethSigner = new this.openST.utils.GethSignerService(this.web3); |
| 29 | + let passphrase = 'testtest'; |
| 30 | + gethSigner.addAccount(config.deployerAddress, passphrase); |
| 31 | + gethSigner.addAccount(config.organizationAddress, passphrase); |
| 32 | + gethSigner.addAccount(config.facilitator, passphrase); |
| 33 | + gethSigner.addAccount(config.chainOwnerAddress, passphrase); |
| 34 | + gethSigner.addAccount(config.opsAddress, passphrase); |
| 35 | + gethSigner.addAccount(config.wallet1, passphrase); |
| 36 | + gethSigner.addAccount(config.wallet2, passphrase); |
| 37 | + |
| 38 | + if (config.knownAddresses) { |
| 39 | + let kAddress; |
| 40 | + for (kAddress in config.knownAddresses) { |
| 41 | + gethSigner.addAccount(kAddress, config.knownAddresses[kAddress]); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + this.openST.signers.setSignerService(gethSigner); |
| 46 | + } |
| 47 | + |
| 48 | + setConfigPath(configInPath) { |
| 49 | + try { |
| 50 | + this.configPath = path.resolve(configInPath); |
| 51 | + } catch (e) { |
| 52 | + this.logError(e); |
| 53 | + let error = new Error( |
| 54 | + 'Invalid Config File Path: ' + (configPath || this.configInPath) + '\nPlease provide openst-setup/config.json path using -c or --config flag' |
| 55 | + ); |
| 56 | + this.exitWithError(error); |
| 57 | + } |
| 58 | + } |
| 59 | + /** |
| 60 | + * getSetupConfig() returns the openst-setup config object |
| 61 | + * based on passed config file path. |
| 62 | + * |
| 63 | + * @param {string} configInPath - Required. Relative or absolute Path to config.json. |
| 64 | + * @return {object} openst-setup config object. |
| 65 | + */ |
| 66 | + getSetupConfig() { |
| 67 | + let configPath = null; |
| 68 | + try { |
| 69 | + configPath = path.resolve(this.configPath); |
| 70 | + return require(configPath); |
| 71 | + } catch (e) { |
| 72 | + this.logError(e); |
| 73 | + let error = new Error( |
| 74 | + 'Invalid Config File Path: ' + (configPath || this.configPath) + '\nPlease provide openst-setup/config.json path using -c or --config flag' |
| 75 | + ); |
| 76 | + this.exitWithError(error); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + addKeyInfoToConfig(address, passphrase) { |
| 81 | + let config = this.getSetupConfig(); |
| 82 | + let configPath = path.resolve(this.configPath); |
| 83 | + |
| 84 | + config['knownAddresses'] = config['knownAddresses'] || {}; |
| 85 | + let knownAddresses = config['knownAddresses']; |
| 86 | + knownAddresses[address] = passphrase; |
| 87 | + try { |
| 88 | + let updatedConfig = JSON.stringify(config, null, 1); |
| 89 | + fs.writeFileSync(configPath, updatedConfig); |
| 90 | + } catch (e) { |
| 91 | + this.logError(e); |
| 92 | + let error = new Error( |
| 93 | + 'Invalid Config File Path: ' + (configPath || this.configPath) + '\nPlease provide openst-setup/config.json path using -c or --config flag' |
| 94 | + ); |
| 95 | + this.exitWithError(error); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + setHistoryArgs(rawArgs, historyFilePath) { |
| 100 | + try { |
| 101 | + this.historyFilePath = path.resolve(historyFilePath); |
| 102 | + } catch (e) { |
| 103 | + this.logError(e); |
| 104 | + let error = new Error( |
| 105 | + 'Invalid History File Path: ' + (this.historyFilePath || historyFilePath) + '\nPlease provide correct path using -h or --history flag' |
| 106 | + ); |
| 107 | + this.exitWithError(error); |
| 108 | + } |
| 109 | + |
| 110 | + //Copy rawArgs & clean it. |
| 111 | + let args = Array.apply(null, rawArgs); |
| 112 | + //Remove node |
| 113 | + args.shift(); |
| 114 | + //Push node |
| 115 | + args.unshift('node'); |
| 116 | + |
| 117 | + let cmd = args.join(' '); |
| 118 | + |
| 119 | + this.logData = { |
| 120 | + cmd: cmd, |
| 121 | + start: new Date(), |
| 122 | + end: null, |
| 123 | + logs: [], |
| 124 | + success: false, |
| 125 | + output: null |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + writeHistory() { |
| 130 | + if (!this.historyFilePath) { |
| 131 | + return; |
| 132 | + } |
| 133 | + this.logData['end'] = new Date(); |
| 134 | + try { |
| 135 | + let content = '\n=====\n' + JSON.stringify(this.logData, null, 2); |
| 136 | + fs.writeFileSync(this.historyFilePath, content, { |
| 137 | + encoding: 'utf8', |
| 138 | + flag: 'a' |
| 139 | + }); |
| 140 | + } catch (e) { |
| 141 | + //Ignore |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + parseArguments(args) { |
| 146 | + if (!args instanceof Array) { |
| 147 | + return args; |
| 148 | + } |
| 149 | + let len = args.length; |
| 150 | + while (len--) { |
| 151 | + let a = args[len]; |
| 152 | + //Check if JSON |
| 153 | + if ((a.indexOf('{') === 0 && a.indexOf('}') === a.length - 1) || (a.indexOf('[') === 0 && a.indexOf(']') === a.length - 1)) { |
| 154 | + try { |
| 155 | + args[len] = JSON.parse(a); |
| 156 | + } catch (e) { |
| 157 | + //Ignore. |
| 158 | + console.log('Error', e); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + return args; |
| 163 | + } |
| 164 | + |
| 165 | + log(message) { |
| 166 | + let ar = this.logData['logs']; |
| 167 | + ar.push.apply(ar, arguments); |
| 168 | + console.log.apply(console, arguments); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * logReceipt() logs etherium transaction receipt |
| 173 | + * |
| 174 | + * @param {object} receipt - Required. Etherium Transaction Receipt to be logged. |
| 175 | + */ |
| 176 | + logReceipt(receipt) { |
| 177 | + //Add to logs. |
| 178 | + this.logData['logs'].push('Transaction Receipt', JSON.stringify(receipt)); |
| 179 | + |
| 180 | + let message = JSON.stringify(receipt, null, 1); |
| 181 | + if (receipt.status) { |
| 182 | + console.log('\n\n', '=====\x1b[32m Transaction Successful \x1b[0m=====', '\n\n'); |
| 183 | + } else { |
| 184 | + console.log('\n\n', '=====\x1b[31m Transaction Failed \x1b[0m=====', '\n\n'); |
| 185 | + } |
| 186 | + console.log('\x1b[2m', message, '\x1b[0m'); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * logError() logs an error message. |
| 191 | + * |
| 192 | + * @param {string} message - Message to be logged. |
| 193 | + */ |
| 194 | + logError(message) { |
| 195 | + console.error('\x1b[0m', '\x1b[31m', message || '', '\x1b[0m'); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * logSuccess() logs success message. |
| 200 | + m |
| 201 | + * @param {string} subject - Subject of message to be logged. |
| 202 | + * @param {string} message - Success message to be logged. |
| 203 | + */ |
| 204 | + logSuccess(subject, message) { |
| 205 | + console.info('\x1b[0m', '\x1b[32m', subject, '\x1b[0m', '\x1b[1m', message || '', '\x1b[0m'); |
| 206 | + } |
| 207 | + |
| 208 | + logReceiptEvent(receipt, eventName, logType) { |
| 209 | + console.log(eventName, receipt.events[eventName]); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * exitWithError() logs error message and exits the program. |
| 214 | + * |
| 215 | + * @param {string|object} error - Error object or error message. |
| 216 | + */ |
| 217 | + exitWithError(error) { |
| 218 | + let message = typeof error === 'string' ? error : error.message; |
| 219 | + if (error) { |
| 220 | + console.log('\n\n', '==========\x1b[31m ERROR \x1b[0m==========', '\n\n'); |
| 221 | + this.logError(message); |
| 222 | + } |
| 223 | + |
| 224 | + //Update the history log. |
| 225 | + this.logData['success'] = false; |
| 226 | + this.logData['output'] = { |
| 227 | + message: message, |
| 228 | + error: error |
| 229 | + }; |
| 230 | + this.writeHistory(); |
| 231 | + |
| 232 | + console.log('\n\n', '==========\x1b[1m END-OF-PROGRAM \x1b[0m==========', '\n\n'); |
| 233 | + process.exit(1); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * exitWithError() logs success message and exits the program. |
| 238 | + * |
| 239 | + * @param {string} subject - Subject of message to be logged. |
| 240 | + * @param {string} message - Success message to be logged. |
| 241 | + */ |
| 242 | + exitWithoutError(subject, message) { |
| 243 | + message = message || ''; |
| 244 | + if (subject || message) { |
| 245 | + console.log('\n\n', '==========\x1b[32m SUCCESS \x1b[0m==========', '\n\n'); |
| 246 | + this.logSuccess(subject, message); |
| 247 | + } |
| 248 | + |
| 249 | + //Update the history log. |
| 250 | + this.logData['success'] = true; |
| 251 | + this.logData['output'] = { |
| 252 | + subject: subject, |
| 253 | + message: message |
| 254 | + }; |
| 255 | + this.writeHistory(); |
| 256 | + |
| 257 | + console.log('\n\n', '==========\x1b[1m END-OF-PROGRAM \x1b[0m==========', '\n\n'); |
| 258 | + process.exit(0); |
| 259 | + } |
| 260 | + |
| 261 | + static getProgram() { |
| 262 | + const program = require('commander'); |
| 263 | + |
| 264 | + program |
| 265 | + .option('-l, --log <file>', 'defaults to ./openst-setup/history.log. Path to history.log file. You can always lookup history for address and logs.') |
| 266 | + .option('-c, --config <file>', 'defaults to ./openst-setup/config.json. Path to openst-setup config.json file.'); |
| 267 | + |
| 268 | + return program; |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +module.exports = PerformerBase; |
0 commit comments