Skip to content

Commit 2ce7b0c

Browse files
committed
Bump codechain-keystore and fix the build
1 parent b42e8be commit 2ce7b0c

File tree

9 files changed

+33
-90
lines changed

9 files changed

+33
-90
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"typescript": "^3.0.1"
3434
},
3535
"dependencies": {
36-
"codechain-keystore": "git://github.com/CodeChain-io/codechain-keystore.git#5a106d3b10717457f02a6fba5e9846d28671b128",
36+
"codechain-keystore": "git://github.com/CodeChain-io/codechain-keystore.git#1483361b94c79e468e055188df0583046d4c9540",
3737
"codechain-sdk": "^0.1.0-alpha.21.rc1",
3838
"commander": "^2.17.1",
3939
"enquirer": "^1.0.3",

src/command/create.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
import { CCKey } from "codechain-keystore";
2-
import { H256 } from "codechain-sdk/lib/core/classes";
3-
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
42
import * as _ from "lodash";
53

6-
import { CLIError, CLIErrorType } from "../error";
74
import { AccountType } from "../types";
8-
import { getAddressFromPublic } from "../util";
5+
import { getAddressFromKey } from "../util";
96

107
export async function createKey(
118
cckey: CCKey,
129
accountType: AccountType,
1310
passphrase: string
1411
): Promise<void> {
15-
const publicKey = await cckey[accountType].createKey({
12+
const key = await cckey[accountType].createKey({
1613
passphrase
1714
});
18-
switch (accountType) {
19-
case "platform":
20-
const accountId = getAccountIdFromPublic(publicKey);
21-
cckey.mapping.add({ key: accountId, value: publicKey });
22-
break;
23-
case "asset":
24-
const hash = H256.ensure(blake256(publicKey)).value;
25-
cckey.mapping.add({ key: hash, value: publicKey });
26-
break;
27-
default:
28-
throw new CLIError(CLIErrorType.Unreachable);
29-
}
3015

31-
console.log(getAddressFromPublic(accountType, publicKey));
16+
console.log(getAddressFromKey(accountType, key));
3217
}

src/command/delete.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { CCKey } from "codechain-keystore";
2+
import * as _ from "lodash";
23

34
import { CLIError, CLIErrorType } from "../error";
45
import { AccountType } from "../types";
5-
import { findPublicKey } from "../util";
6+
import { findMatchingKey } from "../util";
67

78
export async function deleteKey(
89
cckey: CCKey,
910
accountType: AccountType,
1011
address: string
1112
): Promise<void> {
12-
const publicKeys = await cckey[accountType].getKeys();
13-
const publicKey = findPublicKey(accountType, publicKeys, address);
13+
const keys = await cckey[accountType].getKeys();
14+
const key = findMatchingKey(accountType, keys, address);
1415
const Enquirer = require("enquirer");
1516
const enquirer = new Enquirer();
1617
enquirer.register("confirm", require("prompt-confirm"));
@@ -21,9 +22,7 @@ export async function deleteKey(
2122
.prompt(["delete"])
2223
.then(async (answers: { delete: boolean }) => {
2324
if (answers.delete) {
24-
const result = await cckey[accountType].deleteKey({
25-
publicKey
26-
});
25+
const result = await cckey[accountType].deleteKey({ key });
2726
if (!result) {
2827
throw new CLIError(CLIErrorType.Unknown, {
2928
message: "Delete failed"

src/command/export.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { CCKey, SecretStorage } from "codechain-keystore";
22

33
import { AccountType } from "../types";
4-
import { findPublicKey } from "../util";
4+
import { findMatchingKey } from "../util";
55

66
export async function exportKey(
77
cckey: CCKey,
88
accountType: AccountType,
99
address: string,
1010
passphrase: string
1111
): Promise<SecretStorage> {
12-
const publicKeys = await cckey[accountType].getKeys();
13-
const publicKey = findPublicKey(accountType, publicKeys, address);
12+
const keys = await cckey[accountType].getKeys();
13+
const key = findMatchingKey(accountType, keys, address);
1414
return cckey[accountType].exportKey({
15-
publicKey,
15+
key,
1616
passphrase
1717
});
1818
}

src/command/import.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
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";
52
import * as _ from "lodash";
63

7-
import { CLIError, CLIErrorType } from "../error";
84
import { AccountType } from "../types";
9-
import { getAddressFromPublic } from "../util";
5+
import { getAddressFromKey } from "../util";
106

117
export async function importKey(
128
cckey: CCKey,
139
accountType: AccountType,
1410
secret: SecretStorage,
1511
passphrase: string
1612
): Promise<void> {
17-
const publicKey = await cckey[accountType].importKey({
13+
const key = await cckey[accountType].importKey({
1814
secret,
1915
passphrase
2016
});
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-
}
3317

34-
console.log(getAddressFromPublic(accountType, publicKey));
18+
console.log(getAddressFromKey(accountType, key));
3519
}

src/command/importRaw.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
import { CCKey } from "codechain-keystore";
2-
3-
import { H256 } from "codechain-sdk/lib/core/classes";
4-
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
2+
import { PrivateKey } from "codechain-keystore/lib/types";
53
import * as _ from "lodash";
64

7-
import { CLIError, CLIErrorType } from "../error";
85
import { AccountType } from "../types";
9-
import { getAddressFromPublic } from "../util";
6+
import { getAddressFromKey } from "../util";
107

118
export async function importRawKey(
129
cckey: CCKey,
1310
accountType: AccountType,
14-
privateKey: string,
11+
privateKey: PrivateKey,
1512
passphrase: string
1613
): Promise<void> {
17-
const publicKey = await cckey[accountType].importRaw({
14+
const key = await cckey[accountType].importRaw({
1815
privateKey,
1916
passphrase
2017
});
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-
}
3318

34-
console.log(getAddressFromPublic(accountType, publicKey));
19+
console.log(getAddressFromKey(accountType, key));
3520
}

src/command/list.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import { CCKey } from "codechain-keystore";
22
import * as _ from "lodash";
33

44
import { AccountType } from "../types";
5-
import { getAddressFromPublic } from "../util";
5+
import { getAddressFromKey } from "../util";
66

77
export async function listKeys(
88
cckey: CCKey,
99
accountType: AccountType
1010
): Promise<void> {
1111
let keys = await cckey[accountType].getKeys();
12-
keys = _.map(keys, publicKey =>
13-
getAddressFromPublic(accountType, publicKey)
14-
);
12+
keys = _.map(keys, key => getAddressFromKey(accountType, key));
1513
if (keys.length === 0) {
1614
console.log("");
1715
} else {

src/util.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
import { H256 } from "codechain-sdk/lib/core/classes";
1+
import { Key } from "codechain-keystore/lib/types";
22
import {
33
AssetTransferAddress,
44
PlatformAddress
55
} from "codechain-sdk/lib/key/classes";
6-
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
76
import _ = require("lodash");
87

98
import { networkId } from "./const";
109
import { CLIError, CLIErrorType } from "./error";
1110
import { AccountType } from "./types";
1211

13-
export function getAddressFromPublic(
14-
accountType: AccountType,
15-
publicKey: string
16-
): string {
12+
export function getAddressFromKey(accountType: AccountType, key: Key): string {
1713
if (accountType === "platform") {
18-
const accountId = getAccountIdFromPublic(publicKey);
19-
const platformAddress = PlatformAddress.fromAccountId(accountId);
14+
const platformAddress = PlatformAddress.fromAccountId(key);
2015
return platformAddress.toString();
2116
} else if (accountType === "asset") {
22-
const hash = H256.ensure(blake256(publicKey));
23-
const assetAddress = AssetTransferAddress.fromTypeAndPayload(1, hash, {
17+
const assetAddress = AssetTransferAddress.fromTypeAndPayload(1, key, {
2418
networkId
2519
});
2620
return assetAddress.toString();
@@ -29,18 +23,16 @@ export function getAddressFromPublic(
2923
}
3024
}
3125

32-
export function findPublicKey(
26+
export function findMatchingKey(
3327
accountType: AccountType,
34-
publicKeys: string[],
28+
keys: Key[],
3529
address: string
3630
): string {
37-
const addresses = _.map(publicKeys, key =>
38-
getAddressFromPublic(accountType, key)
39-
);
31+
const addresses = _.map(keys, key => getAddressFromKey(accountType, key));
4032
const index = _.indexOf(addresses, address);
4133
if (index === -1) {
4234
throw new CLIError(CLIErrorType.NoSuchAddress, { address });
4335
}
4436

45-
return publicKeys[index];
37+
return keys[index];
4638
}

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ code-point-at@^1.0.0:
556556
version "1.1.0"
557557
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
558558

559-
"codechain-keystore@git://github.com/CodeChain-io/codechain-keystore.git#5a106d3b10717457f02a6fba5e9846d28671b128":
559+
"codechain-keystore@git://github.com/CodeChain-io/codechain-keystore.git#1483361b94c79e468e055188df0583046d4c9540":
560560
version "0.1.0"
561-
resolved "git://github.com/CodeChain-io/codechain-keystore.git#5a106d3b10717457f02a6fba5e9846d28671b128"
561+
resolved "git://github.com/CodeChain-io/codechain-keystore.git#1483361b94c79e468e055188df0583046d4c9540"
562562
dependencies:
563563
codechain-sdk "^0.1.0-alpha.18"
564564
config "^2.0.1"

0 commit comments

Comments
 (0)