chroid 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/env.js +32 -0
- package/index.js +8 -6
- package/package.json +1 -1
- package/utils/env.js +61 -0
package/cli/env.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const askForPassword = require("../utils/askForPassword");
|
|
2
|
+
const { setEnvKey, envFileExists, getEnvPath } = require("../utils/env");
|
|
3
|
+
const { generateWalletFromPassword } = require("../utils/evm");
|
|
4
|
+
|
|
5
|
+
const envCLI = async (yargs) => {
|
|
6
|
+
const argv = await yargs
|
|
7
|
+
.command(
|
|
8
|
+
"create_env",
|
|
9
|
+
"Create or update .env",
|
|
10
|
+
{
|
|
11
|
+
n: {
|
|
12
|
+
alias: "name",
|
|
13
|
+
describe: "Env variable name for the pvkey",
|
|
14
|
+
demandOption: false,
|
|
15
|
+
type: "string",
|
|
16
|
+
default: "PRIVATE_KEY"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
async (args) => {
|
|
20
|
+
const password = await askForPassword();
|
|
21
|
+
const { privateKeyHex } = await generateWalletFromPassword(password);
|
|
22
|
+
const keyName = args.name;
|
|
23
|
+
setEnvKey(keyName, privateKeyHex);
|
|
24
|
+
if (envFileExists()) {
|
|
25
|
+
console.log(`\n${keyName} written to ${getEnvPath()}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
return yargs;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
module.exports = envCLI;
|
package/index.js
CHANGED
|
@@ -5,15 +5,17 @@ const yargs = require("yargs");
|
|
|
5
5
|
const bitcoinCLI = require("./cli/bitcoin");
|
|
6
6
|
const tronCLI = require("./cli/tron");
|
|
7
7
|
const evmCLI = require("./cli/evm");
|
|
8
|
-
const solanaCLI = require("./cli/solana")
|
|
8
|
+
const solanaCLI = require("./cli/solana");
|
|
9
|
+
const envCLI = require("./cli/env");
|
|
9
10
|
|
|
10
11
|
const mainFunction = async () => {
|
|
11
12
|
let cli = yargs;
|
|
12
|
-
cli = await bitcoinCLI(yargs)
|
|
13
|
-
cli = await tronCLI(yargs)
|
|
14
|
-
cli = await evmCLI(yargs)
|
|
15
|
-
cli = await solanaCLI(yargs)
|
|
16
|
-
cli
|
|
13
|
+
cli = await bitcoinCLI(yargs);
|
|
14
|
+
cli = await tronCLI(yargs);
|
|
15
|
+
cli = await evmCLI(yargs);
|
|
16
|
+
cli = await solanaCLI(yargs);
|
|
17
|
+
cli = await envCLI(yargs);
|
|
18
|
+
cli.help().argv;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
mainFunction()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chroid",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "CLI wallet for multi-chain support. \nBitcoin (Supporting 3 of 3 MultiSig) \nNative coins and USDT/USDC on Ethereum, Solana, Tron, BSC, Arbitrum, Optimism, Avalanche, Polygon and Base",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
package/utils/env.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
function getEnvPath() {
|
|
5
|
+
return path.join(process.cwd(), ".env");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function envFileExists() {
|
|
9
|
+
return fs.existsSync(getEnvPath());
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Read .env content. Returns empty string if file does not exist.
|
|
14
|
+
*/
|
|
15
|
+
function readEnvContent() {
|
|
16
|
+
const envPath = getEnvPath();
|
|
17
|
+
if (!fs.existsSync(envPath)) {
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
return fs.readFileSync(envPath, "utf8");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Set or update a key in .env. If key exists, replace its line; otherwise append.
|
|
25
|
+
* Writes .env file (creates if missing).
|
|
26
|
+
*/
|
|
27
|
+
function setEnvKey(keyName, value) {
|
|
28
|
+
const envPath = getEnvPath();
|
|
29
|
+
let content = readEnvContent();
|
|
30
|
+
const line = `${keyName}=${value}`;
|
|
31
|
+
const prefix = keyName + "=";
|
|
32
|
+
|
|
33
|
+
if (content.trim() === "") {
|
|
34
|
+
content = line + "\n";
|
|
35
|
+
} else {
|
|
36
|
+
const lines = content.split("\n");
|
|
37
|
+
let found = false;
|
|
38
|
+
for (let i = 0; i < lines.length; i++) {
|
|
39
|
+
if (lines[i].startsWith(prefix)) {
|
|
40
|
+
lines[i] = line;
|
|
41
|
+
found = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
content = lines.join("\n");
|
|
46
|
+
if (!found) {
|
|
47
|
+
const trimmed = content.trimEnd();
|
|
48
|
+
const needsNewline = trimmed.length > 0 && !trimmed.endsWith("\n");
|
|
49
|
+
content = trimmed + (needsNewline ? "\n" : "") + line + "\n";
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
fs.writeFileSync(envPath, content);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = {
|
|
57
|
+
getEnvPath,
|
|
58
|
+
envFileExists,
|
|
59
|
+
readEnvContent,
|
|
60
|
+
setEnvKey
|
|
61
|
+
};
|