Skip to content

Commit eb0741b

Browse files
kakao-jun-emajecty
authored andcommitted
Prompt a password dialog when --password option is omitted
1 parent 36fd60d commit eb0741b

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"commander": "^2.17.1",
3939
"enquirer": "^1.0.3",
4040
"lodash": "^4.17.10",
41-
"prompt-confirm": "^2.0.4"
41+
"prompt-confirm": "^2.0.4",
42+
"prompt-password": "^1.2.0"
4243
}
4344
}

src/index.ts

100644100755
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ async function listCommand(args: any[], option: ListOption) {
108108
async function createCommand(args: any[], option: CreateOption) {
109109
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
110110
const accountType = parseAccountType(option.parent.accountType);
111-
const passphrase = parsePassphrase(option.passphrase);
111+
const passphrase = await parsePassphrase(option.passphrase);
112112
await createKey(cckey, accountType, passphrase);
113113
}
114114

@@ -122,23 +122,23 @@ async function deleteCommand(args: any[], option: DeleteOption) {
122122
async function importCommand([path]: any[], option: ImportOption) {
123123
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
124124
const accountType = parseAccountType(option.parent.accountType);
125-
const passphrase = parsePassphrase(option.passphrase);
125+
const passphrase = await parsePassphrase(option.passphrase);
126126
const contents = fs.readFileSync(path, { encoding: "utf8" });
127127
await importKey(cckey, accountType, JSON.parse(contents), passphrase);
128128
}
129129

130130
async function importRawCommand([privateKey]: any[], option: ImportOption) {
131131
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
132132
const accountType = parseAccountType(option.parent.accountType);
133-
const passphrase = parsePassphrase(option.passphrase);
133+
const passphrase = await parsePassphrase(option.passphrase);
134134
await importRawKey(cckey, accountType, privateKey, passphrase);
135135
}
136136

137137
async function exportCommand(args: any[], option: ExportOption) {
138138
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
139139
const accountType = parseAccountType(option.parent.accountType);
140140
const address = parseAddress(option.address);
141-
const passphrase = parsePassphrase(option.passphrase);
141+
const passphrase = await parsePassphrase(option.passphrase);
142142
const secret = await exportKey(cckey, accountType, address, passphrase);
143143
const res = option.pretty
144144
? JSON.stringify(secret, null, 2)
@@ -191,11 +191,19 @@ function parseAddress(address: string): string {
191191
return address;
192192
}
193193

194-
function parsePassphrase(passphrase: string): string {
195-
if (_.isUndefined(passphrase)) {
196-
throw new CLIError(CLIErrorType.OptionRequired, {
197-
optionName: "passphrase"
198-
});
194+
async function parsePassphrase(passphrase: string): Promise<string> {
195+
if (!_.isUndefined(passphrase)) {
196+
return passphrase;
199197
}
200-
return passphrase;
198+
199+
const Enquirer = require("enquirer");
200+
const enquirer = new Enquirer();
201+
enquirer.register("password", require("prompt-password"));
202+
var questions = {
203+
type: "password",
204+
message: "Enter your passphrase please",
205+
name: "passphrase"
206+
};
207+
const answers = await enquirer.ask(questions);
208+
return answers.passphrase;
201209
}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,13 @@ prompt-input@^3.0.0:
18871887
debug "^2.6.8"
18881888
prompt-base "^4.0.2"
18891889

1890+
prompt-password@^1.2.0:
1891+
version "1.2.0"
1892+
resolved "https://registry.yarnpkg.com/prompt-password/-/prompt-password-1.2.0.tgz#87dba4afd5c34954e511c2c069fb922dab60b718"
1893+
dependencies:
1894+
debug "^2.6.8"
1895+
prompt-base "^4.0.2"
1896+
18901897
prompt-question@^3.0.3:
18911898
version "3.0.3"
18921899
resolved "https://registry.yarnpkg.com/prompt-question/-/prompt-question-3.0.3.tgz#c55858bd76b1878c1eaaecb917a928a9c13c8a37"

0 commit comments

Comments
 (0)