Skip to content

Commit 4adf2f6

Browse files
kakao-jun-emajecty
authored andcommitted
Prompt an address dialog when --address option is omitted
1 parent 176d6ae commit 4adf2f6

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"enquirer": "^1.0.3",
4141
"lodash": "^4.17.10",
4242
"prompt-confirm": "^2.0.4",
43-
"prompt-password": "^1.2.0"
43+
"prompt-password": "^1.2.0",
44+
"prompt-list": "^3.2.0"
4445
}
4546
}

src/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
ImportOption,
2323
ListOption
2424
} from "./types";
25+
import { getAddressFromKey } from "./util";
2526

2627
const VERSION = "0.3.0";
2728

@@ -134,8 +135,11 @@ async function createCommand(args: any[], option: CreateOption) {
134135
async function deleteCommand(args: any[], option: DeleteOption) {
135136
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
136137
const accountType = parseAccountType(option.parent.accountType);
137-
const address = parseAddress(accountType, option.address);
138138
const networkId = option.parent.networkId;
139+
if (_.isUndefined(option.address) && process.stdout.isTTY) {
140+
option.address = await selectAddress(cckey, networkId, accountType);
141+
}
142+
const address = parseAddress(accountType, option.address);
139143
await deleteKey(
140144
{
141145
cckey,
@@ -182,9 +186,12 @@ async function importRawCommand([privateKey]: any[], option: ImportOption) {
182186
async function exportCommand(args: any[], option: ExportOption) {
183187
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
184188
const accountType = parseAccountType(option.parent.accountType);
189+
const networkId = option.parent.networkId;
190+
if (_.isUndefined(option.address) && process.stdout.isTTY) {
191+
option.address = await selectAddress(cckey, networkId, accountType);
192+
}
185193
const address = parseAddress(accountType, option.address);
186194
const passphrase = await parsePassphrase(option.passphrase);
187-
const networkId = option.parent.networkId;
188195
const secret = await exportKey(
189196
{
190197
cckey,
@@ -274,3 +281,24 @@ async function parsePassphrase(passphrase: string): Promise<string> {
274281
}
275282
return answers.passphrase;
276283
}
284+
285+
async function selectAddress(
286+
cckey: CCKey,
287+
networkId: string,
288+
accountType: AccountType
289+
): Promise<string> {
290+
const Enquirer = require("enquirer");
291+
const enquirer = new Enquirer();
292+
enquirer.register("list", require("prompt-list"));
293+
294+
let keys = await cckey[accountType].getKeys();
295+
keys = _.map(keys, key => getAddressFromKey(accountType, key, networkId));
296+
const questions = {
297+
type: "list",
298+
name: "address",
299+
message: "Select your address please",
300+
choices: keys
301+
};
302+
const answers = await enquirer.ask(questions);
303+
return answers.address;
304+
}

yarn.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,14 @@ prompt-base@^4.0.1, prompt-base@^4.0.2:
15791579
readline-utils "^2.2.3"
15801580
static-extend "^0.1.2"
15811581

1582+
prompt-checkbox@^2.2.0:
1583+
version "2.2.0"
1584+
resolved "https://registry.yarnpkg.com/prompt-checkbox/-/prompt-checkbox-2.2.0.tgz#a1b086bac7e3422a3f1c34f66d6da52486472690"
1585+
dependencies:
1586+
ansi-cyan "^0.1.1"
1587+
debug "^2.6.8"
1588+
prompt-base "^4.0.2"
1589+
15821590
prompt-choices@^3.0.3:
15831591
version "3.0.6"
15841592
resolved "https://registry.yarnpkg.com/prompt-choices/-/prompt-choices-3.0.6.tgz#5c35c3d99ee17edfd9563c1fb81046b0849ef8ff"
@@ -1636,6 +1644,14 @@ prompt-input@^3.0.0:
16361644
debug "^2.6.8"
16371645
prompt-base "^4.0.2"
16381646

1647+
prompt-list@^3.2.0:
1648+
version "3.2.0"
1649+
resolved "https://registry.yarnpkg.com/prompt-list/-/prompt-list-3.2.0.tgz#b33472e01677a5751f59094fa7fe20b09da9db94"
1650+
dependencies:
1651+
ansi-cyan "^0.1.1"
1652+
ansi-dim "^0.1.1"
1653+
prompt-radio "^1.2.1"
1654+
16391655
prompt-password@^1.2.0:
16401656
version "1.2.0"
16411657
resolved "https://registry.yarnpkg.com/prompt-password/-/prompt-password-1.2.0.tgz#87dba4afd5c34954e511c2c069fb922dab60b718"
@@ -1667,6 +1683,13 @@ prompt-question@^5.0.1:
16671683
koalas "^1.0.2"
16681684
prompt-choices "^4.0.5"
16691685

1686+
prompt-radio@^1.2.1:
1687+
version "1.2.1"
1688+
resolved "https://registry.yarnpkg.com/prompt-radio/-/prompt-radio-1.2.1.tgz#66a4cb2b189d3373e7f9ddbbc10700186c8959ff"
1689+
dependencies:
1690+
debug "^2.6.8"
1691+
prompt-checkbox "^2.2.0"
1692+
16701693
punycode@^1.4.1:
16711694
version "1.4.1"
16721695
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"

0 commit comments

Comments
 (0)