@@ -2,9 +2,8 @@ import * as fs from 'fs';
2
2
import * as BitPaySDK from '../index' ;
3
3
import * as readline from 'readline' ;
4
4
5
- const privateKeyPath = __dirname + '/../secure/private_key' ;
6
- const ConfFilePath = __dirname + '/../secure/BitPay.config.json' ;
7
5
const keyUtils = new BitPaySDK . KeyUtils ( ) ;
6
+ let configFilePath = process . cwd ( ) ;
8
7
let keyPair ;
9
8
let ecKey ;
10
9
let environment ;
@@ -58,10 +57,9 @@ const setEnv = async (env) => {
58
57
} ;
59
58
const selectCreateKey = async ( ) => {
60
59
try {
61
- console . log ( 'Enter your private key or its location' ) ;
62
- rl . question ( 'Or press Enter to generate a brand new key: ' , async ( answer ) => {
60
+ rl . question ( 'Press (E) for existing or (N) for new private key: ' , async ( answer ) => {
63
61
switch ( answer . toLowerCase ( ) ) {
64
- case '' :
62
+ case 'n ' :
65
63
await createNewKey ( ) ;
66
64
break ;
67
65
default :
@@ -81,7 +79,7 @@ const createNewKey = async () => {
81
79
await sleep ( 2000 ) ;
82
80
console . log ( 'Generated Private Key: ' + ecKey . getPrivate ( 'hex' ) ) ;
83
81
console . log ( 'With Public Key: ' + ecKey . getPublic ( 'hex' ) + '\n' ) ;
84
- await storeKey ( ) ;
82
+ await prepareDirectory ( ) ;
85
83
} catch ( e ) {
86
84
console . log ( e ) ;
87
85
}
@@ -111,48 +109,81 @@ const loadKey = async (privateKey) => {
111
109
console . log ( e ) ;
112
110
}
113
111
} ;
114
- const storeKey = async ( ) => {
115
- try {
116
- if ( ! fs . existsSync ( __dirname + '/../secure' ) ) {
117
- fs . mkdirSync ( __dirname + '/../secure' ) ;
118
- }
119
- console . log ( 'Select the way you want to store your private key:' ) ;
120
- rl . question ( 'Press F for storing in a text file or T for plain text in your config file: ' , async ( answer ) => {
121
- switch ( answer . toLowerCase ( ) ) {
122
- case 'f' :
123
- storeFile = true ;
124
- keyPath = privateKeyPath + '_' + environment . toLowerCase ( ) + '.key' ;
125
112
126
- console . log ( 'Saving private key... \n' ) ;
127
- sleep ( 500 ) ;
128
- fs . writeFile (
129
- privateKeyPath + '_' + environment . toLowerCase ( ) + '.key' ,
130
- ecKey . getPrivate ( 'hex' ) ,
131
- { mode : 0o755 } ,
132
- function ( err ) {
133
- if ( err ) throw err ;
134
- console . log ( 'Private key saved in file: ' + keyPath + '\n' ) ;
135
- }
136
- ) ;
137
- await sleep ( 1000 ) ;
113
+ const formatDirecotryPath = ( dir : string ) => {
114
+ if ( dir === '' || dir === '.' || dir === '/' || ! dir ) {
115
+ return process . cwd ( ) ;
116
+ }
138
117
139
- selectTokens ( ) ;
140
- break ;
141
- case 't' :
142
- storeFile = false ;
143
- keyPlain = ecKey . getPrivate ( 'hex' ) ;
144
- console . log ( 'Saving private key... \n' ) ;
145
- await sleep ( 1000 ) ;
118
+ if ( dir && dir . charAt ( 0 ) !== '/' ) {
119
+ return process . cwd ( ) + `/${ dir } ` ;
120
+ } else {
121
+ return process . cwd ( ) + dir ;
122
+ }
123
+ } ;
146
124
147
- selectTokens ( ) ;
148
- break ;
149
- default :
150
- storeKey ( ) ;
125
+ const prepareDirectory = async ( ) => {
126
+ rl . question ( 'Enter the directory for your new private key: ' , async ( dir ) => {
127
+ configFilePath = formatDirecotryPath ( dir ) ;
128
+
129
+ try {
130
+ if ( ! fs . existsSync ( configFilePath ) ) {
131
+ console . error ( `Directory ${ configFilePath } does not exist.` ) ;
132
+ console . log ( `Do you want to create ${ configFilePath } ?` ) ;
133
+ rl . question ( `Press (Y) to create or (N) to cancel process: ` , async ( answer ) => {
134
+ switch ( answer . toLowerCase ( ) ) {
135
+ case 'n' :
136
+ rl . close ( ) ;
137
+ break ;
138
+ default :
139
+ fs . mkdirSync ( configFilePath ) ;
140
+ await storeKey ( ) ;
141
+ }
142
+ } ) ;
143
+ } else {
144
+ await storeKey ( ) ;
151
145
}
152
- } ) ;
153
- } catch ( e ) {
154
- console . log ( e ) ;
155
- }
146
+ } catch ( e ) {
147
+ console . log ( e ) ;
148
+ }
149
+ } ) ;
150
+ } ;
151
+
152
+ const storeKey = async ( ) => {
153
+ console . log ( 'Select the way you want to store your private key:' ) ;
154
+ rl . question ( 'Press F for storing in a text file or T for plain text in your config file: ' , async ( answer ) => {
155
+ switch ( answer . toLowerCase ( ) ) {
156
+ case 'f' :
157
+ storeFile = true ;
158
+
159
+ if ( configFilePath . charAt ( configFilePath . length - 1 ) !== '/' ) {
160
+ keyPath = configFilePath + '/private_key' + '_' + environment . toLowerCase ( ) + '.key' ;
161
+ } else {
162
+ keyPath = configFilePath + 'private_key' + '_' + environment . toLowerCase ( ) + '.key' ;
163
+ }
164
+
165
+ console . log ( 'Saving private key... \n' ) ;
166
+ sleep ( 500 ) ;
167
+ fs . writeFile ( keyPath , ecKey . getPrivate ( 'hex' ) , { mode : 0o755 } , function ( err ) {
168
+ if ( err ) throw err ;
169
+ console . log ( 'Private key saved in file: ' + keyPath + '\n' ) ;
170
+ } ) ;
171
+ await sleep ( 1000 ) ;
172
+
173
+ selectTokens ( ) ;
174
+ break ;
175
+ case 't' :
176
+ storeFile = false ;
177
+ keyPlain = ecKey . getPrivate ( 'hex' ) ;
178
+ console . log ( 'Saving private key... \n' ) ;
179
+ await sleep ( 1000 ) ;
180
+
181
+ selectTokens ( ) ;
182
+ break ;
183
+ default :
184
+ prepareDirectory ( ) ;
185
+ }
186
+ } ) ;
156
187
} ;
157
188
const selectTokens = async ( ) => {
158
189
try {
@@ -291,10 +322,10 @@ const updateConfigFile = async () => {
291
322
}
292
323
} ;
293
324
294
- fs . writeFile ( ConfFilePath , JSON . stringify ( configurationObject , null , 4 ) , function ( err ) {
325
+ fs . writeFile ( configFilePath + '/BitPay.config.json' , JSON . stringify ( configurationObject , null , 4 ) , function ( err ) {
295
326
if ( err ) throw err ;
296
327
console . log ( 'Generated configuration file' ) ;
297
- console . log ( 'And saved in file: ' + ConfFilePath + '\n' ) ;
328
+ console . log ( 'And saved in file: ' + configFilePath + '/BitPay.config.json' + '\n' ) ;
298
329
} ) ;
299
330
await sleep ( 5000 ) ;
300
331
0 commit comments