Skip to content

Commit

Permalink
feat: add getKeypairFromFile() and getKeypairFromEnvironment()
Browse files Browse the repository at this point in the history
Plus tests and docs etc.
  • Loading branch information
mikemaccana committed Sep 6, 2023
1 parent 130310e commit cda92f3
Show file tree
Hide file tree
Showing 8 changed files with 1,508 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/tests.yaml
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
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
55 changes: 55 additions & 0 deletions README.md
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/).
Loading

0 comments on commit cda92f3

Please sign in to comment.