Skip to content

Commit 2b9740a

Browse files
committed
Add import command
1 parent 1504e95 commit 2b9740a

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

src/command/import.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { CCKey, SecretStorage } from "codechain-keystore";
2+
3+
import { H256 } from "codechain-sdk/lib/core/classes";
4+
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
5+
import * as _ from "lodash";
6+
7+
import { AccountType } from "../types";
8+
import { getAddressFromPublic } from "../util";
9+
10+
export async function importKey(
11+
cckey: CCKey,
12+
accountType: AccountType,
13+
secret: SecretStorage,
14+
passphrase: string
15+
): Promise<void> {
16+
const publicKey = await cckey[accountType].importKey({
17+
secret,
18+
passphrase
19+
});
20+
if (accountType === "platform") {
21+
const accountId = getAccountIdFromPublic(publicKey);
22+
cckey.mapping.add({ key: accountId, value: publicKey });
23+
}
24+
if (accountType === "asset") {
25+
const hash = H256.ensure(blake256(publicKey)).value;
26+
cckey.mapping.add({ key: hash, value: publicKey });
27+
}
28+
29+
console.log(getAddressFromPublic(accountType, publicKey));
30+
}

src/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
import { CCKey } from "codechain-keystore";
44
import * as program from "commander";
5+
import * as fs from "fs";
56
import * as _ from "lodash";
67
import * as process from "process";
78

89
import { createKey } from "./command/create";
910
import { deleteKey } from "./command/delete";
11+
import { importKey } from "./command/import";
1012
import { listKeys } from "./command/list";
1113
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";
1321

1422
const VERSION = "0.1.1";
1523

@@ -47,12 +55,23 @@ program
4755
.option("-a, --address <address>", "address")
4856
.action(handleError(deleteCommand));
4957

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+
5069
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[]) => {
5473
try {
55-
await f(option);
74+
await f(...args);
5675
} catch (err) {
5776
console.error(err.toString());
5877
process.exit(1);
@@ -80,6 +99,14 @@ async function deleteCommand(option: DeleteOption) {
8099
await deleteKey(cckey, accountType, address);
81100
}
82101

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+
83110
program.on("--help", () => {
84111
console.log(` Examples:
85112

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ export interface DeleteOption {
1414
accountType: string;
1515
address: string;
1616
}
17+
18+
export interface ImportOption {
19+
accountType: string;
20+
passphrase: string;
21+
}

0 commit comments

Comments
 (0)