File tree Expand file tree Collapse file tree 3 files changed +27
-14
lines changed
Expand file tree Collapse file tree 3 files changed +27
-14
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { H256 } from "codechain-sdk/lib/core/classes";
33import { blake256 , getAccountIdFromPublic } from "codechain-sdk/lib/utils" ;
44import * as _ from "lodash" ;
55
6+ import { CLIError , CLIErrorType } from "../error" ;
67import { AccountType } from "../types" ;
78import { getAddressFromPublic } from "../util" ;
89
@@ -14,13 +15,17 @@ export async function createKey(
1415 const publicKey = await cckey [ accountType ] . createKey ( {
1516 passphrase
1617 } ) ;
17- if ( accountType === "platform" ) {
18- const accountId = getAccountIdFromPublic ( publicKey ) ;
19- cckey . mapping . add ( { key : accountId , value : publicKey } ) ;
20- }
21- if ( accountType === "asset" ) {
22- const hash = H256 . ensure ( blake256 ( publicKey ) ) . value ;
23- cckey . mapping . add ( { key : hash , value : publicKey } ) ;
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 ) ;
2429 }
2530
2631 console . log ( getAddressFromPublic ( accountType , publicKey ) ) ;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { H256 } from "codechain-sdk/lib/core/classes";
44import { blake256 , getAccountIdFromPublic } from "codechain-sdk/lib/utils" ;
55import * as _ from "lodash" ;
66
7+ import { CLIError , CLIErrorType } from "../error" ;
78import { AccountType } from "../types" ;
89import { getAddressFromPublic } from "../util" ;
910
@@ -17,13 +18,17 @@ export async function importKey(
1718 secret,
1819 passphrase
1920 } ) ;
20- if ( accountType === "platform" ) {
21- const accountId = getAccountIdFromPublic ( publicKey ) ;
22- cckey . mapping . add ( { key : accountId , value : publicKey } ) ;
23- }
24- if ( accountType === "asset" ) {
25- const hash = H256 . ensure ( blake256 ( publicKey ) ) . value ;
26- cckey . mapping . add ( { key : hash , value : publicKey } ) ;
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 ) ;
2732 }
2833
2934 console . log ( getAddressFromPublic ( accountType , publicKey ) ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export enum CLIErrorType {
55 InvalidAction ,
66 OptionRequired ,
77 NoSuchAddress ,
8+ Unreachable ,
89 Unknown
910}
1011
@@ -26,6 +27,8 @@ function getErrorMessage(type: CLIErrorType, args: any = {}) {
2627 return `Option --${ args . optionName } is required` ;
2728 case CLIErrorType . NoSuchAddress :
2829 return `${ args . address } is not found` ;
30+ case CLIErrorType . Unreachable :
31+ return "Unreachable code reached" ;
2932 case CLIErrorType . Unknown :
3033 return `Internal error ${ args . message } ` ;
3134 default :
You can’t perform that action at this time.
0 commit comments