Code Examples

Copy-paste code snippets for integrating Solana wallet generation into your project.

rustRust — Basic Generation
use solana_sdk::signer::keypair::Keypair;
use solana_sdk::signer::Signer;

fn main() {
    let keypair = Keypair::new();
    let pubkey = keypair.pubkey();
    println!("Address: {}", pubkey);
    println!("Secret key: {:?}", keypair.to_bytes());
}
rustRust — Vanity Address
use solana_sdk::signer::keypair::Keypair;
use solana_sdk::signer::Signer;

fn find_vanity(prefix: &str) -> Keypair {
    loop {
        let keypair = Keypair::new();
        let addr = keypair.pubkey().to_string();
        if addr.starts_with(prefix) {
            return keypair;
        }
    }
}

fn main() {
    let kp = find_vanity("Sol");
    println!("Found: {}", kp.pubkey());
}
rustRust — Multi-threaded Mining
use solana_sdk::signer::keypair::Keypair;
use solana_sdk::signer::Signer;
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use std::thread;

fn main() {
    let found = Arc::new(AtomicBool::new(false));
    let threads: Vec<_> = (0..num_cpus::get()).map(|_| {
        let found = found.clone();
        thread::spawn(move || {
            while !found.load(Ordering::Relaxed) {
                let kp = Keypair::new();
                if kp.pubkey().to_string().starts_with("Sol") {
                    found.store(true, Ordering::Relaxed);
                    println!("Found: {}", kp.pubkey());
                    return Some(kp);
                }
            }
            None
        })
    }).collect();

    for t in threads { t.join().ok(); }
}
typescriptTypeScript — Basic Generation
import { Keypair } from '@solana/web3.js';

const keypair = Keypair.generate();
console.log('Address:', keypair.publicKey.toBase58());
console.log('Secret key:', JSON.stringify(Array.from(keypair.secretKey)));
typescriptTypeScript — Vanity Address
import { Keypair } from '@solana/web3.js';

function findVanity(prefix: string): Keypair {
  let attempts = 0;
  while (true) {
    const kp = Keypair.generate();
    attempts++;
    if (kp.publicKey.toBase58().startsWith(prefix)) {
      console.log(`Found in ${attempts} attempts`);
      return kp;
    }
  }
}

const wallet = findVanity('So1');
console.log('Address:', wallet.publicKey.toBase58());
typescriptTypeScript — Sign & Verify
import { Keypair } from '@solana/web3.js';
import * as nacl from 'tweetnacl';

const keypair = Keypair.generate();
const message = new TextEncoder().encode('Hello, Solana!');

// Sign
const signature = nacl.sign.detached(message, keypair.secretKey);
console.log('Signature:', Buffer.from(signature).toString('hex'));

// Verify
const isValid = nacl.sign.detached.verify(
  message, signature, keypair.publicKey.toBytes()
);
console.log('Valid:', isValid); // true
typescriptTypeScript — Restore Keypair
import { Keypair } from '@solana/web3.js';
import * as fs from 'fs';

// From JSON file (Solana CLI format)
const secretKey = JSON.parse(fs.readFileSync('wallet.json', 'utf-8'));
const keypair = Keypair.fromSecretKey(new Uint8Array(secretKey));
console.log('Restored address:', keypair.publicKey.toBase58());
bashShell — Generate & Verify
#!/bin/bash
# Generate a vanity wallet
./scripts/generate-vanity.sh Sol

# Verify the generated keypair
./scripts/verify-keypair.sh ./keys/SolXXX.json

# Batch generate
./scripts/batch-generate.sh --prefix Sol --count 5