-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getKeypairFromFile() and getKeypairFromEnvironment()
Plus tests and docs etc.
- Loading branch information
1 parent
130310e
commit cda92f3
Showing
8 changed files
with
1,508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# From https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs#using-the-nodejs-starter-workflow | ||
name: Node.js and Solana CI | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [20.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
# Install Solana | ||
# From https://docs.solana.com/cli/install-solana-cli-tools | ||
- run: sh -c "$(curl -sSfL https://release.solana.com/stable/install)" | ||
|
||
- name: Add to PATH | ||
shell: bash | ||
run: | | ||
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH | ||
# Install everything | ||
- run: npm ci | ||
|
||
- name: Check Solana keygen is installed | ||
run: which solana-keygen | ||
|
||
# Run tests | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Solana node helpers | ||
|
||
This package contains node-js specific Solana helper functions. Unlike `@solana/web3.js` - which works in both the browser and node.js - the `node-helpers` package is just for node.js functions, for example functions that load files. | ||
|
||
## getKeypairFromFile | ||
|
||
Gets a keypair from a file - the format must be the same as [Solana CLI](https://docs.solana.com/wallet-guide/file-system-wallet) uses, ie, a JSON array of numbers: | ||
|
||
To load the default keypair `~/.config/solana/id.json`, just run: | ||
|
||
``` | ||
const keyPair = await Keypair.fromFile(); | ||
``` | ||
|
||
or to load a specific file: | ||
|
||
``` | ||
const keyPair = await Keypair.fromFile('somefile.json'); | ||
``` | ||
|
||
or using home dir expansion: | ||
|
||
``` | ||
const keyPair = await Keypair.fromFile('~/code/solana/demos/steve.json'); | ||
``` | ||
|
||
## getKeypairFromEnvironment | ||
|
||
Gets a keypair from a secret key stored in an environment variable. This is typically used to load secret keys from [env files](https://stackoverflow.com/questions/68267862/what-is-an-env-or-dotenv-file-exactly). | ||
|
||
### Secret key format | ||
|
||
Secret keys can be in either the more compact base58 format (`base58.encode(randomKeypair.secretKey);`), like: | ||
|
||
``` | ||
# A random secret key for demo purposes | ||
SECRET_KEY=QqKYBnj5mcgUsS4vrCeyMczbTyV1SMrr7SjSAPj7JGFtxfrgD8AWU8NciwHNCbmkscbvj4HdeEen42GDBSHCj1N | ||
``` | ||
|
||
Or the longer, array of numbers format `JSON.stringify(Object.values(randomKeypair.secretKey));`: | ||
|
||
``` | ||
# A random secret key for demo purposes | ||
SECRET_KEY=[112,222,91,246,55,109,221,4,23,148,251,127,212,180,44,249,182,139,18,13,209,208,6,7,193,210,186,249,148,237,237,1,70,118,1,153,238,134,239,75,187,96,101,138,147,130,181,71,22,82,44,217,194,122,59,208,134,119,98,53,136,108,44,105] | ||
``` | ||
|
||
## Development | ||
|
||
To run tests | ||
|
||
``` | ||
npm run test | ||
``` | ||
|
||
The tests use the [node native test runner](https://blog.logrocket.com/exploring-node-js-native-test-runner/). |
Oops, something went wrong.