Skip to content

Commit 6925c24

Browse files
authored
Merge pull request #129 from dmatuszczak/SP-1127
SP-1127 Add output destination to the Node.js SDK config generator
2 parents 5663ae5 + 3d0088e commit 6925c24

File tree

1 file changed

+77
-46
lines changed

1 file changed

+77
-46
lines changed

src/setup/BitPaySetup.ts

+77-46
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import * as fs from 'fs';
22
import * as BitPaySDK from '../index';
33
import * as readline from 'readline';
44

5-
const privateKeyPath = __dirname + '/../secure/private_key';
6-
const ConfFilePath = __dirname + '/../secure/BitPay.config.json';
75
const keyUtils = new BitPaySDK.KeyUtils();
6+
let configFilePath = process.cwd();
87
let keyPair;
98
let ecKey;
109
let environment;
@@ -58,10 +57,9 @@ const setEnv = async (env) => {
5857
};
5958
const selectCreateKey = async () => {
6059
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) => {
6361
switch (answer.toLowerCase()) {
64-
case '':
62+
case 'n':
6563
await createNewKey();
6664
break;
6765
default:
@@ -81,7 +79,7 @@ const createNewKey = async () => {
8179
await sleep(2000);
8280
console.log('Generated Private Key: ' + ecKey.getPrivate('hex'));
8381
console.log('With Public Key: ' + ecKey.getPublic('hex') + '\n');
84-
await storeKey();
82+
await prepareDirectory();
8583
} catch (e) {
8684
console.log(e);
8785
}
@@ -111,48 +109,81 @@ const loadKey = async (privateKey) => {
111109
console.log(e);
112110
}
113111
};
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';
125112

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+
}
138117

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+
};
146124

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();
151145
}
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+
});
156187
};
157188
const selectTokens = async () => {
158189
try {
@@ -291,10 +322,10 @@ const updateConfigFile = async () => {
291322
}
292323
};
293324

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) {
295326
if (err) throw err;
296327
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');
298329
});
299330
await sleep(5000);
300331

0 commit comments

Comments
 (0)