@@ -8,6 +8,7 @@ import { Command, Flags } from "@oclif/core";
88import { confirm } from "@inquirer/prompts" ;
99import { generateNewPrivateKey , storePrivateKey , keyExists } from "@layr-labs/ecloud-sdk" ;
1010import { showPrivateKey , displayWarning } from "../../utils/security" ;
11+ import { withTelemetry } from "../../telemetry" ;
1112
1213export default class AuthGenerate extends Command {
1314 static description = "Generate a new private key" ;
@@ -27,14 +28,15 @@ export default class AuthGenerate extends Command {
2728 } ;
2829
2930 async run ( ) : Promise < void > {
30- const { flags } = await this . parse ( AuthGenerate ) ;
31+ return withTelemetry ( this , async ( ) => {
32+ const { flags } = await this . parse ( AuthGenerate ) ;
3133
32- // Generate new key
33- this . log ( "Generating new private key...\n" ) ;
34- const { privateKey, address } = generateNewPrivateKey ( ) ;
34+ // Generate new key
35+ this . log ( "Generating new private key...\n" ) ;
36+ const { privateKey, address } = generateNewPrivateKey ( ) ;
3537
36- // Display key securely
37- const content = `
38+ // Display key securely
39+ const content = `
3840━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3941A new private key was generated for you.
4042
@@ -53,61 +55,62 @@ Private key: ${privateKey}
5355Press 'q' to exit and continue...
5456` ;
5557
56- const displayed = await showPrivateKey ( content ) ;
57-
58- if ( ! displayed ) {
59- this . log ( "Key generation cancelled." ) ;
60- return ;
61- }
62-
63- // Ask about storing
64- let shouldStore = flags . store ;
65-
66- if ( ! shouldStore && displayed ) {
67- shouldStore = await confirm ( {
68- message : "Store this key in your OS keyring?" ,
69- default : true ,
70- } ) ;
71- }
72-
73- if ( shouldStore ) {
74- // Check if key already exists
75- const exists = await keyExists ( ) ;
76-
77- if ( exists ) {
78- displayWarning ( [
79- `WARNING: A private key for ecloud already exists!` ,
80- "If you continue, the existing key will be PERMANENTLY REPLACED." ,
81- "This cannot be undone!" ,
82- "" ,
83- "The previous key will be lost forever if you haven't backed it up." ,
84- ] ) ;
85-
86- const confirmReplace = await confirm ( {
87- message : `Replace existing key for ecloud?` ,
88- default : false ,
58+ const displayed = await showPrivateKey ( content ) ;
59+
60+ if ( ! displayed ) {
61+ this . log ( "Key generation cancelled." ) ;
62+ return ;
63+ }
64+
65+ // Ask about storing
66+ let shouldStore = flags . store ;
67+
68+ if ( ! shouldStore && displayed ) {
69+ shouldStore = await confirm ( {
70+ message : "Store this key in your OS keyring?" ,
71+ default : true ,
8972 } ) ;
73+ }
9074
91- if ( ! confirmReplace ) {
92- this . log (
93- "\nKey not stored. If you did not save your new key when it was displayed, it is now lost and cannot be recovered." ,
94- ) ;
95- return ;
75+ if ( shouldStore ) {
76+ // Check if key already exists
77+ const exists = await keyExists ( ) ;
78+
79+ if ( exists ) {
80+ displayWarning ( [
81+ `WARNING: A private key for ecloud already exists!` ,
82+ "If you continue, the existing key will be PERMANENTLY REPLACED." ,
83+ "This cannot be undone!" ,
84+ "" ,
85+ "The previous key will be lost forever if you haven't backed it up." ,
86+ ] ) ;
87+
88+ const confirmReplace = await confirm ( {
89+ message : `Replace existing key for ecloud?` ,
90+ default : false ,
91+ } ) ;
92+
93+ if ( ! confirmReplace ) {
94+ this . log (
95+ "\nKey not stored. If you did not save your new key when it was displayed, it is now lost and cannot be recovered." ,
96+ ) ;
97+ return ;
98+ }
9699 }
97- }
98100
99- // Store the key
100- try {
101- await storePrivateKey ( privateKey ) ;
102- this . log ( `\n✓ Private key stored in OS keyring` ) ;
103- this . log ( `✓ Address: ${ address } ` ) ;
104- this . log ( "\nYou can now use ecloud commands without --private-key flag." ) ;
105- } catch ( err : any ) {
106- this . error ( `Failed to store key: ${ err . message } ` ) ;
101+ // Store the key
102+ try {
103+ await storePrivateKey ( privateKey ) ;
104+ this . log ( `\n✓ Private key stored in OS keyring` ) ;
105+ this . log ( `✓ Address: ${ address } ` ) ;
106+ this . log ( "\nYou can now use ecloud commands without --private-key flag." ) ;
107+ } catch ( err : any ) {
108+ this . error ( `Failed to store key: ${ err . message } ` ) ;
109+ }
110+ } else {
111+ this . log ( "\nKey not stored in keyring." ) ;
112+ this . log ( "Remember to save the key shown above in a secure location." ) ;
107113 }
108- } else {
109- this . log ( "\nKey not stored in keyring." ) ;
110- this . log ( "Remember to save the key shown above in a secure location." ) ;
111- }
114+ } ) ;
112115 }
113116}
0 commit comments