Skip to content

Commit

Permalink
Merge pull request #16 from solana-developers/make-keypairs
Browse files Browse the repository at this point in the history
add: makeKeypairs()
  • Loading branch information
nickfrosty authored Feb 5, 2024
2 parents 18caf2f + 34b1637 commit cdd72fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Eventually most of these will end up in `@solana/web3.js`.

## What can I do with this module?

[Make multiple keypairs at once](#makekeypairs-amount)

[Resolve a custom error message](#getcustomerrormessageprogramerrors-errormessage)

[Get an airdrop, and wait until it's confirmed](#requestandconfirmairdropconnection-publickey-lamports)
Expand All @@ -28,6 +30,14 @@ npm i @solana-developers/helpers

## helpers for the browser and node.js

### makeKeypairs(amount)

In some situations - like making tests for your on-chain programs - you might need to make lots of keypairs at once. You can use `makeKeypairs()` combined with JS destructuring to quickly create multiple variables with distinct keypairs.

```typescript
const [sender, recipient] = makeKeypairs(2);
```

### getCustomErrorMessage(programErrors, errorMessage)

Sometimes Solana libaries return an error like:
Expand Down
8 changes: 8 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
requestAndConfirmAirdrop,
requestAndConfirmAirdropIfRequired,
getExplorerLink,
makeKeypairs,
} from "./index";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
import assert from "node:assert/strict";
Expand Down Expand Up @@ -286,3 +287,10 @@ describe("getExplorerLink", () => {
assert.equal(link, "https://explorer.solana.com/block/241889720");
});
});

describe("makeKeypairs", () => {
test("makeKeypairs() creates the correct number of keypairs", () => {
const keypairs = makeKeypairs(3);
assert.equal(keypairs.length, 3);
});
});
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,8 @@ export const requestAndConfirmAirdropIfRequired = async (
}
return balance;
};

// Shout out to deanmlittle for this technique
export const makeKeypairs = (amount: number): Array<Keypair> => {
return Array.from({ length: amount }, () => Keypair.generate());
};

0 comments on commit cdd72fb

Please sign in to comment.