-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-credentials.js
More file actions
76 lines (63 loc) · 2.37 KB
/
Copy pathsetup-credentials.js
File metadata and controls
76 lines (63 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const KEY_PATH = path.join(process.env.HOME || process.env.USERPROFILE, ".mmm-fintech-key");
const SOURCE_PATH = path.join(__dirname, "cdp_api_key.json");
const ENCRYPTED_PATH = path.join(__dirname, "cdp-credentials.enc");
function generateKey() {
return crypto.randomBytes(32);
}
function encrypt(data, key) {
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
const encrypted = Buffer.concat([cipher.update(data, "utf8"), cipher.final()]);
const authTag = cipher.getAuthTag();
return Buffer.concat([iv, authTag, encrypted]);
}
function prompt(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim().toLowerCase());
});
});
}
async function main() {
console.log("=== MMM-Fintech Credential Encryption Setup ===\n");
if (!fs.existsSync(SOURCE_PATH)) {
console.error("Error: cdp_api_key.json not found in module directory.");
console.error("Place your CDP API key JSON file here first.");
process.exit(1);
}
let key;
if (fs.existsSync(KEY_PATH)) {
console.log("Using existing encryption key at " + KEY_PATH);
const existingKeyHex = fs.readFileSync(KEY_PATH, "utf8").trim();
key = Buffer.from(existingKeyHex, "hex");
} else {
console.log("Generating new encryption key...");
key = generateKey();
fs.writeFileSync(KEY_PATH, key.toString("hex"), { mode: 0o600 });
console.log("Key saved to " + KEY_PATH);
}
console.log("Reading credentials from cdp_api_key.json...");
const credentials = fs.readFileSync(SOURCE_PATH, "utf8");
console.log("Encrypting...");
const encrypted = encrypt(credentials, key);
fs.writeFileSync(ENCRYPTED_PATH, encrypted);
console.log("Encrypted credentials saved to cdp-credentials.enc\n");
const answer = await prompt("Delete the original cdp_api_key.json? (yes/no): ");
if (answer === "yes" || answer === "y") {
fs.unlinkSync(SOURCE_PATH);
console.log("Original file deleted.");
} else {
console.log("Original file kept. Remember to delete it manually for security.");
}
console.log("\nSetup complete.");
}
main().catch(console.error);