@ethersphere/swarm-cli
Version:
CLI tool for Bee
67 lines (66 loc) • 2.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pickIdentity = exports.getWalletFromIdentity = exports.getV3Wallet = exports.getSimpleWallet = exports.isV3Wallet = exports.isSimpleWallet = exports.getPrintableIdentityType = void 0;
const ethereumjs_wallet_1 = __importDefault(require("ethereumjs-wallet"));
const error_1 = require("../../utils/error");
const hex_1 = require("../../utils/hex");
const message_1 = require("../../utils/message");
const types_1 = require("./types");
function getPrintableIdentityType(type) {
switch (type) {
case types_1.IdentityType.simple:
return 'Private Key';
case types_1.IdentityType.v3:
return 'V3 Wallet';
default:
return 'Unknown';
}
}
exports.getPrintableIdentityType = getPrintableIdentityType;
function isSimpleWallet(wallet, identityType) {
return identityType === types_1.IdentityType.simple;
}
exports.isSimpleWallet = isSimpleWallet;
function isV3Wallet(wallet, identityType) {
return identityType === types_1.IdentityType.v3;
}
exports.isV3Wallet = isV3Wallet;
function getSimpleWallet(wallet) {
const privateKeyBytes = (0, hex_1.hexToBytes)(wallet.privateKey);
return new ethereumjs_wallet_1.default(Buffer.from(privateKeyBytes));
}
exports.getSimpleWallet = getSimpleWallet;
function getV3Wallet(wallet, password) {
return ethereumjs_wallet_1.default.fromV3(wallet, password);
}
exports.getV3Wallet = getV3Wallet;
/** Used when identity's wallet is not sure which type */
async function getWalletFromIdentity(console, quiet, identity, password) {
const { wallet, identityType } = identity;
if (isSimpleWallet(wallet, identityType)) {
return getSimpleWallet(wallet);
}
else if (isV3Wallet(wallet, identityType)) {
if (!password) {
if (quiet) {
throw new error_1.CommandLineError('Password must be passed with the --password option in quiet mode');
}
password = await console.askForPassword('Please provide the password for this V3 Wallet');
}
return getV3Wallet(wallet, password);
}
throw new error_1.CommandLineError(`Wrong identity 'typeOfWallet' value: ${identity.identityType}`);
}
exports.getWalletFromIdentity = getWalletFromIdentity;
async function pickIdentity(commandConfig, console) {
const names = Object.keys(commandConfig.config.identities);
if (!names.length) {
throw new error_1.CommandLineError(message_1.Message.noIdentity());
}
const name = await console.promptList(names, 'Please select an identity for this action');
return name;
}
exports.pickIdentity = pickIdentity;