|
1 | 1 | import { getRandomValues } from "node:crypto";
|
2 | 2 | import * as fs from "node:fs";
|
3 | 3 | import path from "path";
|
4 |
| -import { toHex } from "viem"; |
5 |
| - |
6 |
| -export function generateKeys( |
7 |
| - walletKey?: string, |
8 |
| - encryptionKey?: string, |
9 |
| - suffix: string = "", |
10 |
| -) { |
11 |
| - encryptionKey = |
12 |
| - encryptionKey ?? |
13 |
| - process.env["ENCRYPTION_KEY" + suffix] ?? |
14 |
| - toHex(getRandomValues(new Uint8Array(32))); |
15 |
| - |
16 |
| - if (!encryptionKey.startsWith("0x")) { |
17 |
| - encryptionKey = "0x" + encryptionKey; |
18 |
| - } |
19 |
| - walletKey = |
20 |
| - walletKey ?? |
21 |
| - process.env["WALLET_KEY" + suffix] ?? |
22 |
| - toHex(getRandomValues(new Uint8Array(32))); |
| 4 | +import { toBytes, toHex } from "viem"; |
| 5 | + |
| 6 | +class KeyManager { |
| 7 | + private suffix: string; |
23 | 8 |
|
24 |
| - if (!walletKey.startsWith("0x")) { |
25 |
| - walletKey = "0x" + walletKey; |
| 9 | + constructor(suffix: string = "") { |
| 10 | + this.suffix = suffix; |
26 | 11 | }
|
27 |
| - return { walletKey, encryptionKey }; |
28 |
| -} |
29 | 12 |
|
30 |
| -export function saveKeys( |
31 |
| - walletKey: string, |
32 |
| - encryptionKey: string, |
33 |
| - suffix: string = "", |
34 |
| -) { |
35 |
| - const envFilePath = path.resolve(process.cwd(), ".env"); |
36 |
| - const envContent = `\nENCRYPTION_KEY${suffix}=${encryptionKey}\nWALLET_KEY${suffix}=${walletKey}`; |
37 |
| - |
38 |
| - // Read the existing .env file content |
39 |
| - let existingEnvContent = ""; |
40 |
| - if (fs.existsSync(envFilePath)) { |
41 |
| - existingEnvContent = fs.readFileSync(envFilePath, "utf8"); |
| 13 | + generateKeys( |
| 14 | + walletKey?: string, |
| 15 | + encryptionKey?: string, |
| 16 | + ): { |
| 17 | + walletKey: string; |
| 18 | + encryptionKey: string; |
| 19 | + encryptionKeyBytes: Uint8Array; |
| 20 | + } { |
| 21 | + encryptionKey = |
| 22 | + encryptionKey ?? |
| 23 | + process.env["ENCRYPTION_KEY" + this.suffix] ?? |
| 24 | + toHex(getRandomValues(new Uint8Array(32))); |
| 25 | + |
| 26 | + if (!encryptionKey.startsWith("0x")) { |
| 27 | + encryptionKey = "0x" + encryptionKey; |
| 28 | + } |
| 29 | + const encryptionKeyBytes = new Uint8Array( |
| 30 | + toBytes(encryptionKey as `0x${string}`), |
| 31 | + ); |
| 32 | + |
| 33 | + walletKey = |
| 34 | + walletKey ?? |
| 35 | + process.env["WALLET_KEY" + this.suffix] ?? |
| 36 | + toHex(getRandomValues(new Uint8Array(32))); |
| 37 | + |
| 38 | + if (!walletKey.startsWith("0x")) { |
| 39 | + walletKey = "0x" + walletKey; |
| 40 | + } |
| 41 | + return { walletKey, encryptionKey, encryptionKeyBytes }; |
42 | 42 | }
|
43 | 43 |
|
44 |
| - // Check if the keys already exist |
45 |
| - if ( |
46 |
| - !existingEnvContent.includes(`ENCRYPTION_KEY${suffix}=`) && |
47 |
| - !existingEnvContent.includes(`WALLET_KEY${suffix}=`) |
48 |
| - ) { |
49 |
| - fs.appendFileSync(envFilePath, envContent); |
| 44 | + saveKeys(walletKey: string, encryptionKey: string) { |
| 45 | + const envFilePath = path.resolve(process.cwd(), ".env"); |
| 46 | + const envContent = `\nENCRYPTION_KEY${this.suffix}=${encryptionKey}\nWALLET_KEY${this.suffix}=${walletKey}`; |
| 47 | + |
| 48 | + // Read the existing .env file content |
| 49 | + let existingEnvContent = ""; |
| 50 | + if (fs.existsSync(envFilePath)) { |
| 51 | + existingEnvContent = fs.readFileSync(envFilePath, "utf8"); |
| 52 | + } |
| 53 | + |
| 54 | + // Check if the keys already exist |
| 55 | + if ( |
| 56 | + !existingEnvContent.includes(`ENCRYPTION_KEY${this.suffix}=`) && |
| 57 | + !existingEnvContent.includes(`WALLET_KEY${this.suffix}=`) |
| 58 | + ) { |
| 59 | + fs.appendFileSync(envFilePath, envContent); |
| 60 | + } |
50 | 61 | }
|
51 | 62 | }
|
| 63 | + |
| 64 | +export default KeyManager; |
0 commit comments