Skip to content

Commit b42e8be

Browse files
committed
Add importRaw command
1 parent d31d8a7 commit b42e8be

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/command/importRaw.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { CCKey } 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 { CLIError, CLIErrorType } from "../error";
8+
import { AccountType } from "../types";
9+
import { getAddressFromPublic } from "../util";
10+
11+
export async function importRawKey(
12+
cckey: CCKey,
13+
accountType: AccountType,
14+
privateKey: string,
15+
passphrase: string
16+
): Promise<void> {
17+
const publicKey = await cckey[accountType].importRaw({
18+
privateKey,
19+
passphrase
20+
});
21+
switch (accountType) {
22+
case "platform":
23+
const accountId = getAccountIdFromPublic(publicKey);
24+
cckey.mapping.add({ key: accountId, value: publicKey });
25+
break;
26+
case "asset":
27+
const hash = H256.ensure(blake256(publicKey)).value;
28+
cckey.mapping.add({ key: hash, value: publicKey });
29+
break;
30+
default:
31+
throw new CLIError(CLIErrorType.Unreachable);
32+
}
33+
34+
console.log(getAddressFromPublic(accountType, publicKey));
35+
}

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { createKey } from "./command/create";
1010
import { deleteKey } from "./command/delete";
1111
import { exportKey } from "./command/export";
1212
import { importKey } from "./command/import";
13+
import { importRawKey } from "./command/importRaw";
1314
import { listKeys } from "./command/list";
1415
import { CLIError, CLIErrorType } from "./error";
1516
import {
@@ -54,6 +55,12 @@ program
5455
.option("-p, --passphrase <passphrase>", "passphrase")
5556
.action(handleError(importCommand));
5657

58+
program
59+
.command("import-raw <privateKey>")
60+
.description("import a raw private key (32 byte hexadecimal string)")
61+
.option("-p, --passphrase <passphrase>", "passphrase")
62+
.action(handleError(importRawCommand));
63+
5764
program
5865
.command("export")
5966
.description("export the key")
@@ -103,6 +110,13 @@ async function importCommand(path: string, option: ImportOption) {
103110
await importKey(cckey, accountType, JSON.parse(contents), passphrase);
104111
}
105112

113+
async function importRawCommand(privateKey: string, option: ImportOption) {
114+
const cckey = await CCKey.create();
115+
const accountType = parseAccountType(option.parent.accountType);
116+
const passphrase = parsePassphrase(option.passphrase);
117+
await importRawKey(cckey, accountType, privateKey, passphrase);
118+
}
119+
106120
async function exportCommand(option: ExportOption) {
107121
const cckey = await CCKey.create();
108122
const accountType = parseAccountType(option.parent.accountType);

0 commit comments

Comments
 (0)