Skip to content

Commit 584a35f

Browse files
kakao-jun-emajecty
authored andcommitted
Check if the given address is well-formed
1 parent 35dcef2 commit 584a35f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Action } from "./types";
33
export enum CLIErrorType {
44
InvalidAccountType,
55
InvalidAction,
6+
InvalidAddress,
67
OptionRequired,
78
NoSuchAddress,
89
Unknown
@@ -22,6 +23,8 @@ function getErrorMessage(type: CLIErrorType, args: any = {}) {
2223
return "Account-type should be 'platform' or 'asset'";
2324
case CLIErrorType.InvalidAction:
2425
return `Action should one of the ${JSON.stringify(actions)}`;
26+
case CLIErrorType.InvalidAddress:
27+
return `Address error: ${args.message}`;
2528
case CLIErrorType.OptionRequired:
2629
return `Option --${args.optionName} is required`;
2730
case CLIErrorType.NoSuchAddress:

src/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env node
22

33
import { CCKey } from "codechain-keystore";
4+
import {
5+
AssetTransferAddress,
6+
PlatformAddress
7+
} from "codechain-sdk/lib/key/classes";
48
import * as program from "commander";
59
import * as fs from "fs";
610
import * as _ from "lodash";
@@ -133,7 +137,7 @@ async function createCommand(args: any[], option: CreateOption) {
133137
async function deleteCommand(args: any[], option: DeleteOption) {
134138
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
135139
const accountType = parseAccountType(option.parent.accountType);
136-
const address = parseAddress(option.address);
140+
const address = parseAddress(accountType, option.address);
137141
const networkId = option.parent.networkId;
138142
await deleteKey(
139143
{
@@ -181,7 +185,7 @@ async function importRawCommand([privateKey]: any[], option: ImportOption) {
181185
async function exportCommand(args: any[], option: ExportOption) {
182186
const cckey = await CCKey.create({ dbPath: option.parent.keysPath });
183187
const accountType = parseAccountType(option.parent.accountType);
184-
const address = parseAddress(option.address);
188+
const address = parseAddress(accountType, option.address);
185189
const passphrase = await parsePassphrase(option.passphrase);
186190
const networkId = option.parent.networkId;
187191
const secret = await exportKey(
@@ -234,13 +238,23 @@ function parseAccountType(accountType: string): AccountType {
234238
return accountType as AccountType;
235239
}
236240

237-
function parseAddress(address: string): string {
241+
function parseAddress(accountType: AccountType, address: string): string {
238242
if (_.isUndefined(address)) {
239243
throw new CLIError(CLIErrorType.OptionRequired, {
240244
optionName: "address"
241245
});
242246
}
243-
// FIXME: Validate the address.
247+
try {
248+
if (accountType === "platform") {
249+
PlatformAddress.fromString(address);
250+
} else {
251+
AssetTransferAddress.fromString(address);
252+
}
253+
} catch (err) {
254+
throw new CLIError(CLIErrorType.InvalidAddress, {
255+
message: err.message
256+
});
257+
}
244258
return address;
245259
}
246260

0 commit comments

Comments
 (0)