Skip to content

Commit

Permalink
Merge pull request #5 from solana-developers/add-env-keypair
Browse files Browse the repository at this point in the history
feat: generate keypair and add to .env if one doesn't exist
  • Loading branch information
mikemaccana authored Nov 16, 2023
2 parents 7822e70 + d11ccf3 commit f2cb1dd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/temp
dist
node_modules
16 changes: 14 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@digitak/esrun": "^3.2.24",
"bs58": "^5.0.0",
"dotenv": "^16.3.1",
"prettier": "^3.0.3",
"typescript": "^5.2.2"
},
Expand Down
59 changes: 53 additions & 6 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { describe, test, before } from "node:test";
import { before, describe, test } from "node:test";
import {
getKeypairFromEnvironment,
getKeypairFromFile,
addKeypairToEnvironment,
} from "./index";

import { Keypair } from "@solana/web3.js";
import assert from "node:assert/strict";
import { promisify } from "util";
import base58 from "bs58";
import { exec as execForOldPeople } from "child_process";
import { promisify } from "util";
import { writeFile, unlink } from "node:fs/promises";
import dotenv from "dotenv";

const exec = promisify(execForOldPeople);
import { Keypair } from "@solana/web3.js";
import { getKeypairFromFile, getKeypairFromEnvironment } from "./index";
import { writeFile } from "node:fs/promises";
import base58 from "bs58";

const log = console.log;

Expand Down Expand Up @@ -83,3 +90,43 @@ describe("getKeypairFromEnvironment", () => {
);
});
});

describe("addKeypairToEnvironment", () => {
let TEST_ENV_VAR_ARRAY_OF_NUMBERS = "TEST_ENV_VAR_ARRAY_OF_NUMBERS";

before(async () => {
const randomKeypair = Keypair.generate();

process.env[TEST_ENV_VAR_ARRAY_OF_NUMBERS] = JSON.stringify(
Object.values(randomKeypair.secretKey),
);
});

test("generates new keypair and writes to env if variable doesn't exist", async () => {
// Generates new keypair and writes it to the .env file
addKeypairToEnvironment("TEMP_KEYPAIR");
// Load the .env file
dotenv.config();
// Get the secret from the .env file
const secretKeyString = process.env["TEMP_KEYPAIR"];

if (!secretKeyString) {
throw new Error("TEMP_KEYPAIR not found in environment");
}
const decodedSecretKey = Uint8Array.from(JSON.parse(secretKeyString));
const envKeypair = Keypair.fromSecretKey(decodedSecretKey);

assert.ok(envKeypair.secretKey);

unlink(".env");
});

test("throws a nice error if the env var already exists", async () => {
assert.rejects(
async () => addKeypairToEnvironment(TEST_ENV_VAR_ARRAY_OF_NUMBERS),
{
message: `'TEST_ENV_VAR_ARRAY_OF_NUMBERS' already exists in environment.`,
},
);
});
});
17 changes: 16 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Keypair } from "@solana/web3.js";
import { readFile } from "fs/promises";
import base58 from "bs58";
import path from "path";
import { readFile } from "fs/promises";
import { appendFileSync } from "node:fs";
const log = console.log;

// Default value from Solana CLI
Expand Down Expand Up @@ -72,3 +73,17 @@ export const getKeypairFromEnvironment = (variableName: string) => {
}
return Keypair.fromSecretKey(decodedSecretKey);
};

export const addKeypairToEnvironment = (variableName: string) => {
const secretKeyString = process.env[variableName];
if (!secretKeyString) {
// Generate a new keypair if one doesn't exist and add it to a `.env` file
const keypair = Keypair.generate();
appendFileSync(
".env",
`\n${variableName}=${JSON.stringify(Array.from(keypair.secretKey))}`,
);
} else {
throw new Error(`'${variableName}' already exists in environment.`);
}
};

0 comments on commit f2cb1dd

Please sign in to comment.