@ethersphere/swarm-cli
Version:
CLI tool for Bee
124 lines (123 loc) • 5.56 kB
JavaScript
;
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.Create = void 0;
const crypto_1 = require("crypto");
const ethereumjs_wallet_1 = __importDefault(require("ethereumjs-wallet"));
const furious_commander_1 = require("furious-commander");
const identity_1 = require("../../service/identity");
const types_1 = require("../../service/identity/types");
const error_1 = require("../../utils/error");
const message_1 = require("../../utils/message");
const spinner_1 = require("../../utils/spinner");
const text_1 = require("../../utils/text");
const identity_command_1 = require("./identity-command");
class Create extends identity_command_1.IdentityCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'create'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Create Ethereum compatible keypair to sign chunks'
});
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
});
Object.defineProperty(this, "onlyKeypair", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
if (furious_commander_1.Utils.getSourcemap().name === 'default') {
this.console.info(`No identity name specified, defaulting to '${this.identityName}'`);
}
if (this.commandConfig.config.identities[this.identityName]) {
throw new error_1.CommandLineError(message_1.Message.identityNameConflictArgument(this.identityName));
}
const wallet = this.generateWallet();
const identity = this.onlyKeypair ? this.createPrivateKeyIdentity(wallet) : await this.createV3Identity(wallet);
const saved = this.commandConfig.saveIdentity(this.identityName, identity);
if (!saved) {
throw new error_1.CommandLineError(message_1.Message.identityNameConflictArgument(this.identityName));
}
this.console.log((0, text_1.createKeyValue)('Name', this.identityName));
this.console.log((0, text_1.createKeyValue)('Type', (0, identity_1.getPrintableIdentityType)(identity.identityType)));
this.printWallet(wallet);
this.printWalletQuietly(wallet);
}
async createV3Identity(wallet) {
if (!this.password) {
this.console.log(message_1.Message.optionNotDefined('password'));
this.console.info('If you want to create passwordless keypair, use the --only-keypair option');
this.password = await this.console.askForPasswordWithConfirmation(message_1.Message.newV3Password(), message_1.Message.newV3PasswordConfirmation());
}
const spinner = (0, spinner_1.createAndRunSpinner)('Creating V3 wallet...', this.verbosity);
const v3 = await wallet.toV3(this.password);
spinner.stop();
return {
wallet: v3,
identityType: types_1.IdentityType.v3,
};
}
createPrivateKeyIdentity(wallet) {
return {
wallet: {
privateKey: wallet.getPrivateKeyString(),
},
identityType: types_1.IdentityType.simple,
};
}
generateWallet() {
const privateKey = (0, crypto_1.randomBytes)(32);
return new ethereumjs_wallet_1.default(privateKey);
}
}
__decorate([
(0, furious_commander_1.Argument)({ key: 'name', default: 'main', description: 'Reference name of the generated identity' }),
__metadata("design:type", String)
], Create.prototype, "identityName", void 0);
__decorate([
(0, furious_commander_1.Option)({ key: 'password', alias: 'P', description: 'Password for the wallet' }),
__metadata("design:type", String)
], Create.prototype, "password", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'only-keypair',
alias: 'k',
type: 'boolean',
description: 'Generate only the keypair for the identity. The private key will be stored cleartext. Fast to generate',
}),
__metadata("design:type", Boolean)
], Create.prototype, "onlyKeypair", void 0);
exports.Create = Create;