UNPKG

@rsksmart/rif-storage-pinning

Version:

Application for providing your storage space to other to use in exchange of RIF Tokens

150 lines (149 loc) 6.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const command_1 = require("@oclif/command"); const web3_utils_1 = require("web3-utils"); const config_1 = __importDefault(require("config")); const sequelize_store_1 = require("sequelize-store"); const utils_1 = __importStar(require("../utils")); const peer_id_1 = __importDefault(require("peer-id")); const PEER_ID_PLACEHOLDER = '<<peerId>>'; class InitCommand extends utils_1.default { constructor(argv, config) { super(argv, config, { serviceRequired: false, db: undefined }); } baseConfig(flags) { super.baseConfig(flags); const { userConfig, configObject } = this.configuration; config_1.default.util.extendDeep(config_1.default, userConfig); config_1.default.util.extendDeep(config_1.default, configObject); if (flags.network) { const networkConfigPath = path_1.default.join(__dirname, '..', '..', 'config', `${flags.network}.json5`); config_1.default.util.extendDeep(config_1.default, config_1.default.util.parseFile(networkConfigPath)); } } run() { return __awaiter(this, void 0, void 0, function* () { const offerId = this.parsedArgs.flags.offerId; if (!web3_utils_1.isAddress(offerId.toLowerCase())) throw new Error('Invalid Offer Address'); if (fs_1.default.existsSync(this.dbPath)) { const overrideDb = this.parsedArgs.flags['override-db']; if (overrideDb === false) { this.exit(); } if (overrideDb === true || this.parsedArgs.flags.skipPrompt === true || (yield this.confirm('Are you sure you want to overwrite your current DB? All data will be erased! (y/n)'))) { fs_1.default.unlinkSync(this.dbPath); } else { this.exit(); } } try { // Init DB this.spinner.start('Init DB'); yield this.initDB(this.dbPath, { migrate: true, skipPrompt: true }); this.spinner.stop(); // Store offerId this.spinner.start('Set Offer ID'); this.offerId = offerId.toLowerCase(); this.spinner.stop(); // Peer identity this.spinner.start('Generating Peer Identity'); const store = sequelize_store_1.getObject(); const peerId = (yield peer_id_1.default.create({ keyType: this.parsedArgs.flags.keyType, bits: this.parsedArgs.flags.keySize })); const peerIdJson = peerId.toJSON(); store.peerId = peerIdJson.id; store.peerPubKey = peerIdJson.pubKey; store.peerPrivKey = peerIdJson.privKey; this.spinner.stop(); if (config_1.default.has('uiUrl')) { const uiUrl = config_1.default.get('uiUrl'); this.log(`Create Offer here: ${uiUrl.replace(PEER_ID_PLACEHOLDER, peerId.toB58String())}`); this.log(`Or input your PeerId into the form: ${peerId.toB58String()}`); } else { this.log(`Your PeerId: ${peerId.toB58String()}`); } yield sequelize_store_1.getEndPromise(); } catch (e) { try { fs_1.default.unlinkSync(this.dbPath); } catch (e) { if (e.code !== 'ENOENT') { throw e; } } throw e; } this.exit(); }); } } exports.default = InitCommand; InitCommand.flags = Object.assign(Object.assign({}, utils_1.default.flags), { network: command_1.flags.string({ char: 'n', description: 'specifies to which network is the provider connected', options: ['testnet', 'mainnet'], env: 'RIFS_NETWORK' }), offerId: utils_1.promptForFlag(command_1.flags.string({ char: 'o', description: 'ID of Offer to which should the service listen to', env: 'RIFS_OFFER' })), keyType: command_1.flags.string({ char: 't', options: ['rsa', 'ed25519', 'secp256k1'], default: 'rsa', description: 'Type of private key that will be used for Peer Identity' }), keySize: command_1.flags.integer({ char: 's', default: 2048, description: 'Size of private key that will be used for Peer Identity' }), 'override-db': command_1.flags.boolean({ allowNo: true, description: 'Skip the prompt when database exists with used value --override-db/--no-override-db' }) }); InitCommand.description = 'Initialize Pinner service dependencies'; InitCommand.examples = [ '$ rif-pinning init', '$ rif-pinning init --offerId 0x123 --db ./relativeOrAbsolutePath/db.sqlite', '$ rif-pinning init --db fileName.sqlite', '$ rif-pinning init --db ./folder' ];