@nexex/cli
Version:
70 lines • 2.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("@nexex/api");
const chalk_1 = __importDefault(require("chalk"));
const ethers_1 = require("ethers");
const fs_1 = __importDefault(require("fs"));
const prompts_1 = __importDefault(require("prompts"));
const Base_1 = __importDefault(require("./Base"));
class WalletBase extends Base_1.default {
async readPassphrase() {
const response = await prompts_1.default({
type: 'password',
name: 'passphrase',
message: 'passphrase for the wallet',
validate: input => (!input || input === '' ? 'Passphrase is required.' : true)
}, { onCancel: () => this.exit(1) });
return response.passphrase;
}
walletPath() {
return `${this.homeDir}/wallet.json`;
}
readWalletAddr() {
this.checkWallet();
try {
const encrypted = JSON.parse(fs_1.default.readFileSync(this.walletPath(), { encoding: 'utf-8' }));
const address = encrypted.address.startsWith('0x') ? encrypted.address : `0x${encrypted.address}`;
return address;
}
catch (e) {
console.log(chalk_1.default.red('read wallet fails'));
return;
}
}
checkWallet() {
if (!fs_1.default.existsSync(this.walletPath())) {
console.log(chalk_1.default.red('wallet not exists. use nexex-cli wallet:import to create one first.'));
this.exit(1);
}
}
async readWallet() {
this.checkWallet();
const passphrase = await this.readPassphrase();
const encrypted = fs_1.default.readFileSync(this.walletPath(), { encoding: 'utf-8' });
return ethers_1.Wallet.fromEncryptedJson(encrypted, passphrase);
}
async initApi() {
const dexConfig = await this.getDexConfig();
this.dex = await api_1.Dex.create(dexConfig);
return this.dex;
}
async tokenToAddr(tokens) {
const tokenPromises = tokens.map(token => {
if (token.startsWith('0x')) {
return Promise.resolve(token);
}
else {
return this.dex.tokenRegistry.getTokenAddressBySymbol(token.toUpperCase());
}
});
return Promise.all(tokenPromises);
}
}
WalletBase.flags = {
...Base_1.default.flags
};
exports.default = WalletBase;
//# sourceMappingURL=WalletBase.js.map