Skip to content

Commit 2a2e3fe

Browse files
majectykseo
authored andcommitted
Make network id switchable
1 parent a7d4fbd commit 2a2e3fe

File tree

10 files changed

+61
-25
lines changed

10 files changed

+61
-25
lines changed

src/command/create.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { getAddressFromKey } from "../util";
77
export async function createKey(
88
cckey: CCKey,
99
accountType: AccountType,
10-
passphrase: string
10+
passphrase: string,
11+
networkId: string
1112
): Promise<void> {
1213
const key = await cckey[accountType].createKey({
1314
passphrase
1415
});
1516

16-
console.log(getAddressFromKey(accountType, key));
17+
console.log(getAddressFromKey(accountType, key, networkId));
1718
}

src/command/delete.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { findMatchingKey } from "../util";
88
export async function deleteKey(
99
cckey: CCKey,
1010
accountType: AccountType,
11-
address: string
11+
address: string,
12+
networkId: string
1213
): Promise<void> {
1314
const keys = await cckey[accountType].getKeys();
14-
const key = findMatchingKey(accountType, keys, address);
15+
const key = findMatchingKey(accountType, keys, address, networkId);
1516
const Enquirer = require("enquirer");
1617
const enquirer = new Enquirer();
1718
enquirer.register("confirm", require("prompt-confirm"));

src/command/export.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ export async function exportKey(
77
cckey: CCKey,
88
accountType: AccountType,
99
address: string,
10-
passphrase: string
10+
passphrase: string,
11+
networkId: string
1112
): Promise<SecretStorage> {
1213
const keys = await cckey[accountType].getKeys();
13-
const key = findMatchingKey(accountType, keys, address);
14+
const key = findMatchingKey(accountType, keys, address, networkId);
1415
return cckey[accountType].exportKey({
1516
key,
1617
passphrase

src/command/import.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export async function importKey(
88
cckey: CCKey,
99
accountType: AccountType,
1010
secret: SecretStorage,
11-
passphrase: string
11+
passphrase: string,
12+
networkId: string
1213
): Promise<void> {
1314
const key = await cckey[accountType].importKey({
1415
secret,
1516
passphrase
1617
});
1718

18-
console.log(getAddressFromKey(accountType, key));
19+
console.log(getAddressFromKey(accountType, key, networkId));
1920
}

src/command/importRaw.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ export async function importRawKey(
99
cckey: CCKey,
1010
accountType: AccountType,
1111
privateKey: PrivateKey,
12-
passphrase: string
12+
passphrase: string,
13+
networkId: string
1314
): Promise<void> {
1415
const key = await cckey[accountType].importRaw({
1516
privateKey,
1617
passphrase
1718
});
1819

19-
console.log(getAddressFromKey(accountType, key));
20+
console.log(getAddressFromKey(accountType, key, networkId));
2021
}

src/command/list.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import { getAddressFromKey } from "../util";
66

77
export async function listKeys(
88
cckey: CCKey,
9-
accountType: AccountType
9+
accountType: AccountType,
10+
networkId: string
1011
): Promise<void> {
1112
let keys = await cckey[accountType].getKeys();
12-
keys = _.map(keys, key => getAddressFromKey(accountType, key));
13+
keys = _.map(keys, key => getAddressFromKey(accountType, key, networkId));
1314
if (keys.length === 0) {
1415
console.log("");
1516
} else {

src/const.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/index.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ program
3737
"--keys-path <keysPath>",
3838
"the path to store the keys",
3939
DEFAULT_KEYS_PATH
40+
)
41+
.option(
42+
"--network-id <networkId>",
43+
"the id of the network (use 'tc' for husky, use 'sc' for saluki)",
44+
"tc"
4045
);
4146

4247
program
@@ -102,44 +107,62 @@ function handleError(
102107
async function listCommand(args: any[], option: ListOption) {
103108
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
104109
const accountType = parseAccountType(option.parent.accountType);
105-
await listKeys(cckey, accountType);
110+
const networkId = option.parent.networkId;
111+
await listKeys(cckey, accountType, networkId);
106112
}
107113

108114
async function createCommand(args: any[], option: CreateOption) {
109115
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
110116
const accountType = parseAccountType(option.parent.accountType);
111117
const passphrase = await parsePassphrase(option.passphrase);
112-
await createKey(cckey, accountType, passphrase);
118+
const networkId = option.parent.networkId;
119+
await createKey(cckey, accountType, passphrase, networkId);
113120
}
114121

115122
async function deleteCommand(args: any[], option: DeleteOption) {
116123
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
117124
const accountType = parseAccountType(option.parent.accountType);
118125
const address = parseAddress(option.address);
119-
await deleteKey(cckey, accountType, address);
126+
const networkId = option.parent.networkId;
127+
await deleteKey(cckey, accountType, address, networkId);
120128
}
121129

122130
async function importCommand([path]: any[], option: ImportOption) {
123131
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
124132
const accountType = parseAccountType(option.parent.accountType);
125133
const passphrase = await parsePassphrase(option.passphrase);
126134
const contents = fs.readFileSync(path, { encoding: "utf8" });
127-
await importKey(cckey, accountType, JSON.parse(contents), passphrase);
135+
const networkId = option.parent.networkId;
136+
await importKey(
137+
cckey,
138+
accountType,
139+
JSON.parse(contents),
140+
passphrase,
141+
networkId
142+
);
128143
}
129144

130145
async function importRawCommand([privateKey]: any[], option: ImportOption) {
131146
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
132147
const accountType = parseAccountType(option.parent.accountType);
133148
const passphrase = await parsePassphrase(option.passphrase);
134-
await importRawKey(cckey, accountType, privateKey, passphrase);
149+
const networkId = option.parent.networkId;
150+
await importRawKey(cckey, accountType, privateKey, passphrase, networkId);
135151
}
136152

137153
async function exportCommand(args: any[], option: ExportOption) {
138154
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
139155
const accountType = parseAccountType(option.parent.accountType);
140156
const address = parseAddress(option.address);
141157
const passphrase = await parsePassphrase(option.passphrase);
142-
const secret = await exportKey(cckey, accountType, address, passphrase);
158+
const networkId = option.parent.networkId;
159+
const secret = await exportKey(
160+
cckey,
161+
accountType,
162+
address,
163+
passphrase,
164+
networkId
165+
);
143166
const res = option.pretty
144167
? JSON.stringify(secret, null, 2)
145168
: JSON.stringify(secret);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type Action = "list" | "create" | "delete";
44
export interface CommonOption {
55
accountType: string;
66
keysPath: string;
7+
networkId: string;
78
}
89

910
export interface ListOption {

src/util.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import {
55
} from "codechain-sdk/lib/key/classes";
66
import _ = require("lodash");
77

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

12-
export function getAddressFromKey(accountType: AccountType, key: Key): string {
11+
export function getAddressFromKey(
12+
accountType: AccountType,
13+
key: Key,
14+
networkId: string
15+
): string {
1316
if (accountType === "platform") {
14-
const platformAddress = PlatformAddress.fromAccountId(key);
17+
const platformAddress = PlatformAddress.fromAccountId(key, {
18+
networkId
19+
});
1520
return platformAddress.toString();
1621
} else if (accountType === "asset") {
1722
const assetAddress = AssetTransferAddress.fromTypeAndPayload(1, key, {
@@ -26,9 +31,12 @@ export function getAddressFromKey(accountType: AccountType, key: Key): string {
2631
export function findMatchingKey(
2732
accountType: AccountType,
2833
keys: Key[],
29-
address: string
34+
address: string,
35+
networkId: string
3036
): string {
31-
const addresses = _.map(keys, key => getAddressFromKey(accountType, key));
37+
const addresses = _.map(keys, key =>
38+
getAddressFromKey(accountType, key, networkId)
39+
);
3240
const index = _.indexOf(addresses, address);
3341
if (index === -1) {
3442
throw new CLIError(CLIErrorType.NoSuchAddress, { address });

0 commit comments

Comments
 (0)