Skip to content

Commit f0da602

Browse files
majectykseo
authored andcommitted
Extract common variables as Context
1 parent 2a2e3fe commit f0da602

File tree

8 files changed

+73
-49
lines changed

8 files changed

+73
-49
lines changed

src/command/create.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { CCKey } from "codechain-keystore";
21
import * as _ from "lodash";
32

4-
import { AccountType } from "../types";
3+
import { Context } from "../types";
54
import { getAddressFromKey } from "../util";
65

76
export async function createKey(
8-
cckey: CCKey,
9-
accountType: AccountType,
10-
passphrase: string,
11-
networkId: string
7+
{ cckey, accountType, networkId }: Context,
8+
passphrase: string
129
): Promise<void> {
1310
const key = await cckey[accountType].createKey({
1411
passphrase

src/command/delete.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { CCKey } from "codechain-keystore";
21
import * as _ from "lodash";
32

43
import { CLIError, CLIErrorType } from "../error";
5-
import { AccountType } from "../types";
4+
import { Context } from "../types";
65
import { findMatchingKey } from "../util";
76

87
export async function deleteKey(
9-
cckey: CCKey,
10-
accountType: AccountType,
11-
address: string,
12-
networkId: string
8+
{ cckey, accountType, networkId }: Context,
9+
address: string
1310
): Promise<void> {
1411
const keys = await cckey[accountType].getKeys();
1512
const key = findMatchingKey(accountType, keys, address, networkId);

src/command/export.ts

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

3-
import { AccountType } from "../types";
3+
import { Context } from "../types";
44
import { findMatchingKey } from "../util";
55

66
export async function exportKey(
7-
cckey: CCKey,
8-
accountType: AccountType,
7+
{ cckey, accountType, networkId }: Context,
98
address: string,
10-
passphrase: string,
11-
networkId: string
9+
passphrase: string
1210
): Promise<SecretStorage> {
1311
const keys = await cckey[accountType].getKeys();
1412
const key = findMatchingKey(accountType, keys, address, networkId);

src/command/import.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { CCKey, SecretStorage } from "codechain-keystore";
1+
import { SecretStorage } from "codechain-keystore";
22
import * as _ from "lodash";
33

4-
import { AccountType } from "../types";
4+
import { Context } from "../types";
55
import { getAddressFromKey } from "../util";
66

77
export async function importKey(
8-
cckey: CCKey,
9-
accountType: AccountType,
8+
{ cckey, accountType, networkId }: Context,
109
secret: SecretStorage,
11-
passphrase: string,
12-
networkId: string
10+
passphrase: string
1311
): Promise<void> {
1412
const key = await cckey[accountType].importKey({
1513
secret,

src/command/importRaw.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { CCKey } from "codechain-keystore";
21
import { PrivateKey } from "codechain-keystore/lib/types";
32
import * as _ from "lodash";
43

5-
import { AccountType } from "../types";
4+
import { Context } from "../types";
65
import { getAddressFromKey } from "../util";
76

87
export async function importRawKey(
9-
cckey: CCKey,
10-
accountType: AccountType,
8+
{ cckey, accountType, networkId }: Context,
119
privateKey: PrivateKey,
12-
passphrase: string,
13-
networkId: string
10+
passphrase: string
1411
): Promise<void> {
1512
const key = await cckey[accountType].importRaw({
1613
privateKey,

src/command/list.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { CCKey } from "codechain-keystore";
21
import * as _ from "lodash";
32

4-
import { AccountType } from "../types";
3+
import { Context } from "../types";
54
import { getAddressFromKey } from "../util";
65

7-
export async function listKeys(
8-
cckey: CCKey,
9-
accountType: AccountType,
10-
networkId: string
11-
): Promise<void> {
6+
export async function listKeys({
7+
cckey,
8+
accountType,
9+
networkId
10+
}: Context): Promise<void> {
1211
let keys = await cckey[accountType].getKeys();
1312
keys = _.map(keys, key => getAddressFromKey(accountType, key, networkId));
1413
if (keys.length === 0) {

src/index.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,41 @@ async function listCommand(args: any[], option: ListOption) {
108108
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
109109
const accountType = parseAccountType(option.parent.accountType);
110110
const networkId = option.parent.networkId;
111-
await listKeys(cckey, accountType, networkId);
111+
await listKeys({
112+
cckey,
113+
accountType,
114+
networkId
115+
});
112116
}
113117

114118
async function createCommand(args: any[], option: CreateOption) {
115119
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
116120
const accountType = parseAccountType(option.parent.accountType);
117121
const passphrase = await parsePassphrase(option.passphrase);
118122
const networkId = option.parent.networkId;
119-
await createKey(cckey, accountType, passphrase, networkId);
123+
await createKey(
124+
{
125+
cckey,
126+
accountType,
127+
networkId
128+
},
129+
passphrase
130+
);
120131
}
121132

122133
async function deleteCommand(args: any[], option: DeleteOption) {
123134
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
124135
const accountType = parseAccountType(option.parent.accountType);
125136
const address = parseAddress(option.address);
126137
const networkId = option.parent.networkId;
127-
await deleteKey(cckey, accountType, address, networkId);
138+
await deleteKey(
139+
{
140+
cckey,
141+
accountType,
142+
networkId
143+
},
144+
address
145+
);
128146
}
129147

130148
async function importCommand([path]: any[], option: ImportOption) {
@@ -134,11 +152,13 @@ async function importCommand([path]: any[], option: ImportOption) {
134152
const contents = fs.readFileSync(path, { encoding: "utf8" });
135153
const networkId = option.parent.networkId;
136154
await importKey(
137-
cckey,
138-
accountType,
155+
{
156+
cckey,
157+
accountType,
158+
networkId
159+
},
139160
JSON.parse(contents),
140-
passphrase,
141-
networkId
161+
passphrase
142162
);
143163
}
144164

@@ -147,7 +167,15 @@ async function importRawCommand([privateKey]: any[], option: ImportOption) {
147167
const accountType = parseAccountType(option.parent.accountType);
148168
const passphrase = await parsePassphrase(option.passphrase);
149169
const networkId = option.parent.networkId;
150-
await importRawKey(cckey, accountType, privateKey, passphrase, networkId);
170+
await importRawKey(
171+
{
172+
cckey,
173+
accountType,
174+
networkId
175+
},
176+
privateKey,
177+
passphrase
178+
);
151179
}
152180

153181
async function exportCommand(args: any[], option: ExportOption) {
@@ -157,11 +185,13 @@ async function exportCommand(args: any[], option: ExportOption) {
157185
const passphrase = await parsePassphrase(option.passphrase);
158186
const networkId = option.parent.networkId;
159187
const secret = await exportKey(
160-
cckey,
161-
accountType,
188+
{
189+
cckey,
190+
accountType,
191+
networkId
192+
},
162193
address,
163-
passphrase,
164-
networkId
194+
passphrase
165195
);
166196
const res = option.pretty
167197
? JSON.stringify(secret, null, 2)

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { CCKey } from "codechain-keystore";
2+
13
export type AccountType = "platform" | "asset";
24
export type Action = "list" | "create" | "delete";
35

@@ -32,3 +34,9 @@ export interface ExportOption {
3234
passphrase: string;
3335
pretty: boolean;
3436
}
37+
38+
export interface Context {
39+
cckey: CCKey;
40+
accountType: AccountType;
41+
networkId: string;
42+
}

0 commit comments

Comments
 (0)