Skip to content

Commit 3feff30

Browse files
committed
Add export command
1 parent 2ac6dff commit 3feff30

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/command/export.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CCKey, SecretStorage } from "codechain-keystore";
2+
3+
import { AccountType } from "../types";
4+
import { findPublicKey } from "../util";
5+
6+
export async function exportKey(
7+
cckey: CCKey,
8+
accountType: AccountType,
9+
address: string,
10+
passphrase: string
11+
): Promise<SecretStorage> {
12+
const publicKeys = await cckey[accountType].getKeys();
13+
const publicKey = findPublicKey(accountType, publicKeys, address);
14+
return cckey[accountType].exportKey({
15+
publicKey,
16+
passphrase
17+
});
18+
}

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import * as process from "process";
88

99
import { createKey } from "./command/create";
1010
import { deleteKey } from "./command/delete";
11+
import { exportKey } from "./command/export";
1112
import { importKey } from "./command/import";
1213
import { listKeys } from "./command/list";
1314
import { CLIError, CLIErrorType } from "./error";
1415
import {
1516
AccountType,
1617
CreateOption,
1718
DeleteOption,
19+
ExportOption,
1820
ImportOption,
1921
ListOption
2022
} from "./types";
@@ -52,6 +54,13 @@ program
5254
.option("-p, --passphrase <passphrase>", "passphrase")
5355
.action(handleError(importCommand));
5456

57+
program
58+
.command("export")
59+
.description("export the key")
60+
.option("-a, --address <address>", "address")
61+
.option("-p, --passphrase <passphrase>", "passphrase")
62+
.action(handleError(exportCommand));
63+
5564
function handleError(
5665
f: (...args: any[]) => Promise<void>
5766
): (...args: any[]) => Promise<void> {
@@ -93,6 +102,15 @@ async function importCommand(path: string, option: ImportOption) {
93102
await importKey(cckey, accountType, JSON.parse(contents), passphrase);
94103
}
95104

105+
async function exportCommand(option: ExportOption) {
106+
const cckey = await CCKey.create();
107+
const accountType = parseAccountType(option.parent.accountType);
108+
const address = parseAddress(option.address);
109+
const passphrase = parsePassphrase(option.passphrase);
110+
const secret = await exportKey(cckey, accountType, address, passphrase);
111+
console.log(JSON.stringify(secret, null, 2));
112+
}
113+
96114
program.on("--help", () => {
97115
console.log(` Examples:
98116

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ export interface ImportOption {
2727
};
2828
passphrase: string;
2929
}
30+
31+
export interface ExportOption {
32+
parent: {
33+
accountType: string;
34+
};
35+
address: string;
36+
passphrase: string;
37+
}

0 commit comments

Comments
 (0)