Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better way to load keypairs #2021

Open
mikemaccana opened this issue Jan 9, 2024 · 0 comments
Open

A better way to load keypairs #2021

mikemaccana opened this issue Jan 9, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@mikemaccana
Copy link
Contributor

Motivation

Dev Education and DevRel also see a lot of copy-paste efforts to add secret keys to env files, and to load keys from the environment.

It would be good to provide some guidance here:

  • People frequently use node along with esrun, ts-node, tsx etc to run js/ts files on the command line when learning.
  • People need a good way to persistently save and load keys they've previously generated
  • We want to push people to use .env
  • We want to push people to .gitignore their env files.

Example use case

The DevRel helpers package has these. You can steal the code.

To load the default keypair ~/.config/solana/id.json, just run:

const keyPair = await getKeypairFromFile();

or to load a specific file:

const keyPair = await getKeypairFromFile("somefile.json");

or using home dir expansion:

const keyPair = await getKeypairFromFile("~/code/solana/demos/steve.json");

For the environment:

const keyPair = await getKeypairFromEnvironment("SECRET_KEY");

Saves a keypair to the environment file.

await addKeypairToEnvFile(testKeypair, "SECRET_KEY");

or to specify a file name:

await addKeypairToEnvFile(testKeypair, "SECRET_KEY", ".env.local");

Details

Code

Interesting, the exciting world of ES6 seems to means that node specific functions (eg, that call node:fs/promises or similar) seem to not prevent a module being used in the browser.

@mikemaccana mikemaccana added the enhancement New feature or request label Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant