|
2 | 2 |
|
3 | 3 | import { CCKey } from "codechain-keystore"; |
4 | 4 | import * as program from "commander"; |
| 5 | +import * as fs from "fs"; |
5 | 6 | import * as _ from "lodash"; |
6 | 7 | import * as process from "process"; |
7 | 8 |
|
8 | 9 | import { createKey } from "./command/create"; |
9 | 10 | import { deleteKey } from "./command/delete"; |
| 11 | +import { importKey } from "./command/import"; |
10 | 12 | import { listKeys } from "./command/list"; |
11 | 13 | import { CLIError, CLIErrorType } from "./error"; |
12 | | -import { AccountType, CreateOption, DeleteOption, ListOption } from "./types"; |
| 14 | +import { |
| 15 | + AccountType, |
| 16 | + CreateOption, |
| 17 | + DeleteOption, |
| 18 | + ImportOption, |
| 19 | + ListOption |
| 20 | +} from "./types"; |
13 | 21 |
|
14 | 22 | const VERSION = "0.1.1"; |
15 | 23 |
|
@@ -47,12 +55,23 @@ program |
47 | 55 | .option("-a, --address <address>", "address") |
48 | 56 | .action(handleError(deleteCommand)); |
49 | 57 |
|
| 58 | +program |
| 59 | + .command("import <path>") |
| 60 | + .description("import a key") |
| 61 | + .option( |
| 62 | + "-t, --account-type <accountType>", |
| 63 | + "'platform' or 'asset'. The type of the key", |
| 64 | + "platform" |
| 65 | + ) |
| 66 | + .option("-p, --passphrase <passphrase>", "passphrase") |
| 67 | + .action(handleError(importCommand)); |
| 68 | + |
50 | 69 | function handleError( |
51 | | - f: (option: any) => Promise<void> |
52 | | -): (option: any) => Promise<void> { |
53 | | - return async (option: any) => { |
| 70 | + f: (...args: any[]) => Promise<void> |
| 71 | +): (...args: any[]) => Promise<void> { |
| 72 | + return async (...args: any[]) => { |
54 | 73 | try { |
55 | | - await f(option); |
| 74 | + await f(...args); |
56 | 75 | } catch (err) { |
57 | 76 | console.error(err.toString()); |
58 | 77 | process.exit(1); |
@@ -80,6 +99,14 @@ async function deleteCommand(option: DeleteOption) { |
80 | 99 | await deleteKey(cckey, accountType, address); |
81 | 100 | } |
82 | 101 |
|
| 102 | +async function importCommand(path: string, option: ImportOption) { |
| 103 | + const cckey = await CCKey.create(); |
| 104 | + const accountType = parseAccountType(option.accountType); |
| 105 | + const passphrase = parsePassphrase(option.passphrase); |
| 106 | + const contents = fs.readFileSync(path, { encoding: "utf8" }); |
| 107 | + await importKey(cckey, accountType, JSON.parse(contents), passphrase); |
| 108 | +} |
| 109 | + |
83 | 110 | program.on("--help", () => { |
84 | 111 | console.log(` Examples: |
85 | 112 |
|
|
0 commit comments