@@ -22,6 +22,7 @@ import {
2222 ImportOption ,
2323 ListOption
2424} from "./types" ;
25+ import { getAddressFromKey } from "./util" ;
2526
2627const VERSION = "0.3.0" ;
2728
@@ -134,8 +135,11 @@ async function createCommand(args: any[], option: CreateOption) {
134135async function deleteCommand ( args : any [ ] , option : DeleteOption ) {
135136 const cckey = await CCKey . create ( { dbPath : option . parent . keysPath } ) ;
136137 const accountType = parseAccountType ( option . parent . accountType ) ;
137- const address = parseAddress ( accountType , option . address ) ;
138138 const networkId = option . parent . networkId ;
139+ if ( _ . isUndefined ( option . address ) && process . stdout . isTTY ) {
140+ option . address = await selectAddress ( cckey , networkId , accountType ) ;
141+ }
142+ const address = parseAddress ( accountType , option . address ) ;
139143 await deleteKey (
140144 {
141145 cckey,
@@ -182,9 +186,12 @@ async function importRawCommand([privateKey]: any[], option: ImportOption) {
182186async function exportCommand ( args : any [ ] , option : ExportOption ) {
183187 const cckey = await CCKey . create ( { dbPath : option . parent . keysPath } ) ;
184188 const accountType = parseAccountType ( option . parent . accountType ) ;
189+ const networkId = option . parent . networkId ;
190+ if ( _ . isUndefined ( option . address ) && process . stdout . isTTY ) {
191+ option . address = await selectAddress ( cckey , networkId , accountType ) ;
192+ }
185193 const address = parseAddress ( accountType , option . address ) ;
186194 const passphrase = await parsePassphrase ( option . passphrase ) ;
187- const networkId = option . parent . networkId ;
188195 const secret = await exportKey (
189196 {
190197 cckey,
@@ -274,3 +281,24 @@ async function parsePassphrase(passphrase: string): Promise<string> {
274281 }
275282 return answers . passphrase;
276283}
284+
285+ async function selectAddress (
286+ cckey : CCKey ,
287+ networkId : string ,
288+ accountType : AccountType
289+ ) : Promise < string > {
290+ const Enquirer = require ( "enquirer" ) ;
291+ const enquirer = new Enquirer ( ) ;
292+ enquirer . register ( "list" , require ( "prompt-list" ) ) ;
293+
294+ let keys = await cckey [ accountType ] . getKeys ( ) ;
295+ keys = _ . map ( keys , key => getAddressFromKey ( accountType , key , networkId ) ) ;
296+ const questions = {
297+ type : "list" ,
298+ name : "address" ,
299+ message : "Select your address please" ,
300+ choices : keys
301+ } ;
302+ const answers = await enquirer . ask ( questions ) ;
303+ return answers . address ;
304+ }
0 commit comments