UNPKG

ethstats-cli

Version:
253 lines (216 loc) 8.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var REGEX_EMAIL_VALIDATOR = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var Register = /*#__PURE__*/ function () { function Register(diContainer) { _classCallCheck(this, Register); this.config = diContainer.config; this.cli = diContainer.cli; this.inquirer = diContainer.inquirer; this.log = diContainer.logger; this.lodash = diContainer.lodash; this.server = diContainer.server; this.configurator = diContainer.configurator; this.chalk = diContainer.chalk; } _createClass(Register, [{ key: "askInstallationType", value: function askInstallationType() { var _this = this; var askNetwork = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; this.configurator.get({ configName: 'privacyPolicyUrl' }).then(function (privacyPolicyUrl) { if (privacyPolicyUrl === null) { _this.log.error('Could not get privacy policy url', false, true); } else { _this.log.echo(_this.chalk.bgBlue('Thank you for using \'ethstats-cli\'. Your privacy is important to us.')); _this.log.echo(_this.chalk.bgBlue("For this we kindly ask you to read our privacy policy here: ".concat(_this.chalk.redBright(privacyPolicyUrl)))); _this.log.echo(_this.chalk.bgBlue('By continuing to the next step you acknowledge and agree to our terms and conditions.')); _this.inquirer.prompt([{ type: 'list', name: 'installationType', message: 'Is your node already registered ?', choices: [{ name: 'New node', value: 'new-node' }, { name: 'Existing node', value: 'existing-node' }] }]).then(function (answer) { if (askNetwork) { _this.askNetwork(answer.installationType); } else if (answer.installationType === 'new-node') { _this.askRegistration(); } else { _this.askRecoveryAccountEmail(); } }); } }); } }, { key: "askNetwork", value: function askNetwork(installationType) { var _this2 = this; var choices = []; if (this.lodash.isEmpty(this.config.serverUrls)) { this.log.error('Networks not found', false, true); } else { Object.keys(this.config.serverUrls).forEach(function (item) { choices.push({ name: item.charAt(0).toUpperCase() + item.slice(1), value: item }); }); } this.inquirer.prompt([{ type: 'list', name: 'networkName', message: 'Please select network ?', choices: choices }]).then(function (answer) { var serverConfig = _this2.config.serverUrls[answer.networkName]; if (serverConfig) { _this2.server.url = serverConfig.url; _this2.server.configToSave = { net: answer.networkName }; _this2.server.create(); _this2.server.socket.on('open', function () { if (_this2.config.configStore.get('firstRun') !== false) { if (installationType === 'new-node') { _this2.askRegistration(); } else { _this2.askRecoveryAccountEmail(); } } }); } else { _this2.log.error('Server config for selected network not found', false, true); } }); } }, { key: "askRegistration", value: function askRegistration() { var _this3 = this; this.inquirer.prompt([{ type: 'input', name: 'accountEmail', message: 'Please enter account email:', validate: function validate(input) { var result = true; if (!REGEX_EMAIL_VALIDATOR.test(input)) { result = 'Please enter a valid email address'; } return result; } }, { type: 'input', name: 'nodeName', message: 'Please enter node name:', validate: function validate(input) { return _this3.server.sendAndWait('checkIfNodeExists', { nodeName: input }).then(function (response) { var result = true; if (response.success) { var resposeData = response.data[0]; if (resposeData.exists) { result = 'Node already registered'; } } else { result = response.errors[0]; } return result; }); } }]).then(function (answer) { _this3.server.registerNode(answer.accountEmail, answer.nodeName); }); } }, { key: "askRecoveryAccountEmail", value: function askRecoveryAccountEmail() { var _this4 = this; this.inquirer.prompt([{ type: 'input', name: 'accountEmail', message: 'Please enter account email:', validate: function validate(input) { return _this4.server.sendAndWait('checkIfEmailExists', { accountEmail: input }).then(function (response) { var result = true; if (response.success) { var resposeData = response.data[0]; if (!resposeData.exists) { result = 'Email does not exist'; } } else { result = response.errors[0]; } return result; }); } }]).then(function (answer) { _this4.server.sendAndWait('sendRecoveryEmail', { accountEmail: answer.accountEmail }).then(function (response) { if (response.success) { _this4.log.echo('A message was sent to the provided address with a URL that will contain your list of nodes.'); _this4.log.echo('To use an existing node, type the corresponding recovery hash of your desired node.'); var responseData = response.data[0]; _this4.askNodeRecoveryHash(responseData.recoveryRequestId); } else { _this4.log.error(response.errors[0], false, true); } }); }); } }, { key: "askNodeRecoveryHash", value: function askNodeRecoveryHash(recoveryRequestId) { var _this5 = this; this.inquirer.prompt([{ type: 'input', name: 'nodeRecoveryHash', message: 'Please enter node recovery hash:', validate: function validate(input) { return _this5.server.sendAndWait('checkIfNodeRecoveryHashExists', { recoveryRequestId: recoveryRequestId, nodeRecoveryHash: input }).then(function (response) { var result = true; if (response.success) { var resposeData = response.data[0]; if (!resposeData.exists) { result = 'Node recovery hash is invalid/expired or does not exist'; } } else { result = response.errors[0]; } return result; }); } }]).then(function (answer) { _this5.server.send('recoverNode', { recoveryRequestId: recoveryRequestId, nodeRecoveryHash: answer.nodeRecoveryHash }); }); } }]); return Register; }(); exports.default = Register;