UNPKG

@ethersphere/swarm-cli

Version:
169 lines (168 loc) 7.22 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Import = void 0; const ethereumjs_wallet_1 = __importDefault(require("ethereumjs-wallet")); const fs_1 = require("fs"); const furious_commander_1 = require("furious-commander"); const types_1 = require("../../service/identity/types"); const utils_1 = require("../../utils"); const error_1 = require("../../utils/error"); const message_1 = require("../../utils/message"); const spinner_1 = require("../../utils/spinner"); const root_command_1 = require("../root-command"); const command_log_1 = require("../root-command/command-log"); class Import extends root_command_1.RootCommand { constructor() { super(...arguments); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: 'import' }); Object.defineProperty(this, "description", { enumerable: true, configurable: true, writable: true, value: 'Import private key or V3 wallet as a new identity' }); Object.defineProperty(this, "resource", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "identityName", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "password", { enumerable: true, configurable: true, writable: true, value: void 0 }); } async run() { super.init(); if (this.commandConfig.config.identities[this.identityName]) { throw new error_1.CommandLineError(message_1.Message.identityNameConflict(this.identityName)); } if ((0, utils_1.isPrivateKey)(this.resource)) { await this.runImportOnPrivateKey(); } else { (0, utils_1.expectFile)(this.resource); this.resource = (0, fs_1.readFileSync)(this.resource, 'utf-8'); if ((0, utils_1.isPrivateKey)(this.resource)) { await this.runImportOnPrivateKey(); } else { if (!this.password) { this.console.log(message_1.Message.optionNotDefined('password')); this.password = await this.console.askForPassword(message_1.Message.existingV3Password()); } const spinner = (0, spinner_1.createAndRunSpinner)('Decrypting V3 wallet...', this.verbosity); try { const wallet = await this.decryptV3Wallet(this.resource); spinner.text = 'Importing V3 wallet...'; await this.saveWallet(wallet); } finally { spinner.stop(); } this.console.log(`V3 Wallet imported as identity '${this.identityName}' successfully`); } } } async runImportOnPrivateKey() { if (await this.shouldConvertToV3Wallet()) { await this.convertPrivateKeyToV3Wallet(); } else { const data = { wallet: { privateKey: this.resource, }, identityType: types_1.IdentityType.simple, }; if (!this.commandConfig.saveIdentity(this.identityName, data)) { throw new error_1.CommandLineError(message_1.Message.identityNameConflictOption(this.identityName)); } this.console.log(`Private key imported as identity '${this.identityName}' successfully`); } } async decryptV3Wallet(data) { try { const wallet = await ethereumjs_wallet_1.default.fromV3(data, this.password); return wallet; } catch (error) { const message = (0, utils_1.getFieldOrNull)(error, 'message') || 'unknown error'; throw new error_1.CommandLineError(`Failed to decrypt wallet: ${message}`); } } async convertPrivateKeyToV3Wallet() { if (!this.password) { this.console.log(message_1.Message.optionNotDefined('password')); this.password = await this.console.askForPasswordWithConfirmation(message_1.Message.newV3Password(), message_1.Message.newV3PasswordConfirmation()); } const wallet = ethereumjs_wallet_1.default.fromPrivateKey(Buffer.from((0, utils_1.normalizePrivateKey)(this.resource), 'hex')); await this.saveWallet(wallet); this.console.log(`V3 Wallet imported as identity '${this.identityName}' successfully`); } async saveWallet(wallet) { const data = { wallet: await wallet.toV3(this.password), identityType: types_1.IdentityType.v3, }; if (!this.commandConfig.saveIdentity(this.identityName, data)) { throw new error_1.CommandLineError(message_1.Message.identityNameConflict(this.identityName)); } } async shouldConvertToV3Wallet() { if (this.yes) { return false; } if (this.password) { return true; } if (this.verbosity !== command_log_1.VerbosityLevel.Quiet) { const answer = await this.console.confirmAndDelete('Convert private key to a secure V3 wallet?'); return answer; } return false; } } __decorate([ (0, furious_commander_1.Argument)({ key: 'resource', required: true, description: 'Private key string or path to file with V3 Wallet or private key', autocompletePath: true, }), __metadata("design:type", String) ], Import.prototype, "resource", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'name', alias: 'i', description: 'Name of the identity to be saved as', required: true }), __metadata("design:type", String) ], Import.prototype, "identityName", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'password', alias: 'P', description: 'Password for the V3 wallet' }), __metadata("design:type", String) ], Import.prototype, "password", void 0); exports.Import = Import;