Skip to content

Export Keypair from Phantom Wallet

While the auth_token grants access to the SoyCap.io API to retrieve necessary transaction details, all blockchain-related actions — such as creating campaigns, depositing or withdrawing funds, registering conversions, and distributing rewards — require direct transaction authorization using the campaign owner’s keypair.

Each SoyCap.io related transaction must be signed directly by the campaign owner’s keypair and then sent for confirmation to SoyCap.io Solana Program. This dual authorization model ensures that while the SoyCap.io API can facilitate setup, only the campaign owner’s keypair can authorize and confirm transactions on the blockchain.

  • A keypair is a matching pair of public key and secret key.
  • The public key is used as an address that points to an account on the Solana network. A public key can be shared with anyone.
  • The secret key is used to verify authority over the account. As the name suggests, you should always keep secret keys secret.

This guide walks you through extracting your secret phrase from Phantom Wallet, generating a keypair in JSON format, and securely storing it for backend use.

Step 1. Export Secret Phrase from Phantom Wallet

  1. Open Phantom Wallet in your browser.
  2. Navigate to Settings > Security & Privacy > Secret Recovery Phrase.
  3. Follow the prompts to display your secret phrase. Copy it and store it securely, as this phrase allows you to generate your wallet’s private key.

Step 2. Generate Keypair JSON File from Secret Phrase

With the secret phrase, you can now create a keypair JSON file using the following code snippet:

// Import dependencies
import { Keypair } from '@solana/web3.js';
import base58 from "bs58";
import * as fs from 'fs';
// Replace with your actual secret phrase from Phantom Wallet
const PRIVATE_KEY = "your-secret-phrase-as-base58";
const PUBLIC_KEY = "your-public-key";
// Decode the base58 private key to a Uint8Array
const secret = base58.decode(PRIVATE_KEY);
// Create Keypair from the decoded secret
const pair = Keypair.fromSecretKey(secret);
// Verify if the public key matches
if (pair.publicKey.toString() === PUBLIC_KEY) {
// Write the keypair data to a JSON file for secure storage
fs.writeFileSync(
'./keypair.json',
JSON.stringify(Array.from(secret))
);
console.log("Keypair JSON file generated successfully.");
} else {
console.error("Public key verification failed.");
}

Step 3. Secure the Keypair File

  • Store Securely: Place keypair.json in a secure location on your backend server.
  • Environment Variables: Pass the file path through environment variables to avoid hardcoding paths in your code, as this enhances security.

Step 4. Load Keypair from Keypair JSON File

Here’s a code snippet demonstrating how to load a keypair from a JSON file:

// Import required libraries
import fs from 'fs';
import { Keypair } from '@solana/web3.js';
// Function to load the keypair from a JSON file
const loadKeypair = (filePath) => {
try {
// Read the JSON file
const keypairData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
// Create and return the Keypair from the private key data
const keypair = Keypair.fromSecretKey(new Uint8Array(keypairData));
console.log("Keypair loaded successfully.");
return keypair;
} catch (error) {
console.error("Failed to load keypair:", error);
throw error;
}
};
// Usage
const keypair = loadKeypair('./keypair.json');
console.log("Public Key:", keypair.publicKey.toString());