|
1 | 1 | import { writeFile } from "node:fs/promises";
|
2 | 2 | import { join } from "node:path";
|
3 |
| -import { generatePrivateKey } from "viem/accounts"; |
| 3 | +import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
4 | 4 | import { generateEncryptionKeyHex } from "@/helpers";
|
5 | 5 |
|
6 | 6 | console.log("Generating keys...");
|
7 | 7 |
|
| 8 | +const person = process.argv[2]; |
8 | 9 | const walletKey = generatePrivateKey();
|
| 10 | +const account = privateKeyToAccount(walletKey); |
9 | 11 | const encryptionKeyHex = generateEncryptionKeyHex();
|
| 12 | +const publicKey = account.address; |
10 | 13 |
|
11 | 14 | const filePath = join(process.cwd(), ".env");
|
12 | 15 |
|
13 |
| -await writeFile( |
14 |
| - filePath, |
15 |
| - `WALLET_KEY=${walletKey} |
| 16 | +// Format the environment variables based on whether a person name was provided |
| 17 | +let envContent; |
| 18 | +if (person) { |
| 19 | + envContent = `# ${person.toLowerCase()} |
| 20 | +WALLET_KEY_${person.toUpperCase()}=${walletKey} |
| 21 | +ENCRYPTION_KEY_${person.toUpperCase()}=${encryptionKeyHex} |
| 22 | +# public key is ${publicKey} |
| 23 | +`; |
| 24 | +} else { |
| 25 | + envContent = `# generic keys |
| 26 | +WALLET_KEY=${walletKey} |
16 | 27 | ENCRYPTION_KEY=${encryptionKeyHex}
|
17 |
| -`, |
18 |
| - { |
19 |
| - flag: "a", |
20 |
| - }, |
21 |
| -); |
| 28 | +# public key is ${publicKey} |
| 29 | +`; |
| 30 | +} |
22 | 31 |
|
23 |
| -console.log(`Keys written to ${filePath}`); |
| 32 | +await writeFile(filePath, envContent, { flag: "a" }); |
| 33 | + |
| 34 | +// Log appropriate message based on whether a person name was provided |
| 35 | +if (person) { |
| 36 | + console.log(`Keys for ${person} written to ${filePath}`); |
| 37 | +} else { |
| 38 | + console.log(`Generic keys written to ${filePath}`); |
| 39 | +} |
0 commit comments