Skip to main content

Load a local json file keypair

When running your local project may sometimes want to use local keypair. The default keypair path generated by the Openverse CLI is ~/.config/openverse/id.json.

You can generate a custom keypair using the Openverse CLI command openverse-keygen grind --starts-with hi:1. This will generate a keypair with a public key starting with "hi" and output the keypair to a json file. You can then load this keypair using the helper function below.



import {
Cluster,
clusterApiUrl,
Connection,
Keypair,
LAMPORTS_PER_SOL,
} from "openverse-web3";
import { readFileSync } from "fs";
import { homedir } from "os";
import path from "path";

export async function loadKeypairFromFile(filePath: string): Promise<Keypair> {
const resolvedPath = path.resolve(
filePath.startsWith("~") ? filePath.replace("~", homedir()) : filePath,
);
const loadedKeyBytes = Uint8Array.from(
JSON.parse(readFileSync(resolvedPath, "utf8")),
);
return await Keypair.fromSecretKey(loadedKeyBytes);
}

const keypair = await loadKeypairFromFile("~/.config/openverse/id.json");
console.log(keypair.publicKey.toBase58());

export async function loadDefaultKeypair(): Promise<Keypair> {
return await loadKeypairFromFile("~/.config/openverse/id.json");
}

const keypair2 = await loadDefaultKeypair();
console.log(keypair2.publicKey.toBase58());

export async function loadDefaultKeypairWithAirdrop(
cluster: Cluster,
): Promise<Keypair> {
const keypair = await loadDefaultKeypair();
const connection = new Connection(clusterApiUrl(cluster), "finalized");

try {
const balance = await connection.getBalance(keypair.publicKey);

// 1 LAMPORTS_PER_SOL === 1 SOL
console.log(`Balance: ${balance} lamports`);
if (balance < 0.05 * LAMPORTS_PER_SOL) {
console.log(`Balance low requesting airdrop`);
await connection.requestAirdrop(keypair.publicKey, 1_000_000_000);
}
} catch (err) {
console.error("Error fetching balance:", err);
}
return keypair;
}

const keypair3 = await loadDefaultKeypairWithAirdrop("devnet");
console.log(keypair3.publicKey.toBase58());