Skip to content

Commit

Permalink
Add importRaw command
Browse files Browse the repository at this point in the history
  • Loading branch information
kseo committed Sep 5, 2018
1 parent d31d8a7 commit b42e8be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/command/importRaw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CCKey } from "codechain-keystore";

import { H256 } from "codechain-sdk/lib/core/classes";
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
import * as _ from "lodash";

import { CLIError, CLIErrorType } from "../error";
import { AccountType } from "../types";
import { getAddressFromPublic } from "../util";

export async function importRawKey(
cckey: CCKey,
accountType: AccountType,
privateKey: string,
passphrase: string
): Promise<void> {
const publicKey = await cckey[accountType].importRaw({
privateKey,
passphrase
});
switch (accountType) {
case "platform":
const accountId = getAccountIdFromPublic(publicKey);
cckey.mapping.add({ key: accountId, value: publicKey });
break;
case "asset":
const hash = H256.ensure(blake256(publicKey)).value;
cckey.mapping.add({ key: hash, value: publicKey });
break;
default:
throw new CLIError(CLIErrorType.Unreachable);
}

console.log(getAddressFromPublic(accountType, publicKey));
}
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createKey } from "./command/create";
import { deleteKey } from "./command/delete";
import { exportKey } from "./command/export";
import { importKey } from "./command/import";
import { importRawKey } from "./command/importRaw";
import { listKeys } from "./command/list";
import { CLIError, CLIErrorType } from "./error";
import {
Expand Down Expand Up @@ -54,6 +55,12 @@ program
.option("-p, --passphrase <passphrase>", "passphrase")
.action(handleError(importCommand));

program
.command("import-raw <privateKey>")
.description("import a raw private key (32 byte hexadecimal string)")
.option("-p, --passphrase <passphrase>", "passphrase")
.action(handleError(importRawCommand));

program
.command("export")
.description("export the key")
Expand Down Expand Up @@ -103,6 +110,13 @@ async function importCommand(path: string, option: ImportOption) {
await importKey(cckey, accountType, JSON.parse(contents), passphrase);
}

async function importRawCommand(privateKey: string, option: ImportOption) {
const cckey = await CCKey.create();
const accountType = parseAccountType(option.parent.accountType);
const passphrase = parsePassphrase(option.passphrase);
await importRawKey(cckey, accountType, privateKey, passphrase);
}

async function exportCommand(option: ExportOption) {
const cckey = await CCKey.create();
const accountType = parseAccountType(option.parent.accountType);
Expand Down

0 comments on commit b42e8be

Please sign in to comment.