Skip to content

s-damian/rust-solana-wallet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Solana Wallet in Rust - Example

Solana Logo Rust Logo

#Solana #Wallet #Rust #BIP39 #Web3 #Blockchain

This is an example of a Solana Wallet in Rust πŸ¦€

Tests Static Analysis License

This example of a Solana Wallet is developed by Stephen Damian

Summary

Feature Summary

This wallet manages:

  • Generate Mnemonic: Creates a new random BIP39 mnemonic phrase.
  • Recover Keypair : Recover keypair and seed from a mnemonic phrase.
  • Seed: Derives a seed from the mnemonic phrase.
  • Passphrase: You can optionally use a passphrase.
  • Keypair Generation: Generates a Solana keypair (public and private key) from the derived seed.
  • Keypair Storage: Saves the generated keypair to a local JSON file for future use.
  • Key Derivation: Supports generating multiple keypairs from a single seed by applying BIP44 derivation paths.
  • Send SOL (lamports): Send SOL to a recipient address (sign outgoing transaction).
  • Public Key Display: Retrieves and displays the public key from the locally stored keypair.
  • Get Balance: Get balance (in SOL and in lamports) by public key.

Wallet Commands Summary

Command Description
generate_seed Generates a 12-word BIP39 mnemonic phrase, derives the corresponding seed, saves the keypair, and displays the public key.
recover_seed Accepts a user-provided BIP39 mnemonic phrase, derives the corresponding seed, restores the keypair, and displays the public key.
send Send SOL to a recipient address.
pubkey Displays the public key from a keypair stored in a JSON file.
balance_by_pubkey Get balance by public key.

To see a summary of all available commands and options:

cargo run -- --help

Project Overview

Rust Solana Wallet - A lightweight Solana wallet developed in Rust.

Status: Under development 🚧

Img

Roadmap

  • βœ… Command-line interface (CLI) implementation.
  • βœ… Testing: Functional tests & Unit tests.
  • ⬜ Graphical user interface (GUI) implementation.
  • ⬜ Add support for SPL tokens.

Prerequisites

  • Rust >= 1.75.0 (last tested: 1.80.0) - Ensure Rust is installed on your system. You can install Rust using Rustup.
  • Cargo - Rust's package manager, installed automatically with Rust. Learn more about Cargo here.

Setup

Clone the repository:

git clone https://github.com/s-damian/rust-solana-wallet.git

Navigate to the project directory:

cd /<your-path>/rust-solana-wallet

Create your .env file:

cp .env.example .env

How to use?

Keypair storage:

generate_seed and recover_seed commands write the generated keypair to the <your-path>/storage/keypair/id.json file (KEYPAIR_PATH env var), allowing you to store or utilize the keypair in your Solana applications.

Multiple keypairs (derivations):

If you want to generate several keypairs and several public keys with a single mnemonic phrase, you must set the NB_DERIVATIONS environment variable to a value greater than 0.

Your non-derived keypair will be created in your <your-path>/storage/keypair/id.json file (KEYPAIR_PATH env var) JSON file.

The other keypairs (which will be derived from your seed) will be created in JSON files in your <your-path>/storage/keypair/derived directory (KEYPAIR_DERIVATIONS_PATH env var).

🌐 generate_seed command

Generate and display a random mnemonic phrase.

This command generates a new mnemonic phrase randomly, calculates the corresponding seed, displays the seed, displays the Solana public key, and generates and writes the keypair to the JSON file.

  • Command:
cargo run -- generate_seed

Optional passphrase: You will be prompted to enter a passphrase (leave blank to not use one).

  • Example of result (with NB_DERIVATIONS=3):
BIP39 Mnemonic (random phrase): shed scorpion manual wheat monster phone winter toe dream kitchen salad column
Seed: 34A0EACFFDF41445C0B7E43C2D730C54F4CD1D8334528F73E3D5F2C2977FAABA7CAD88EBDA6A1F02CE6BB596F04036305A32B96303F93FF864D268539739AFF8
Solana Public Key: FTGJPL5hia749v3jhNWJA7uE2VoVGyofB7BBL2cLwoPc
Solana Public Key (derivation 1): EMLY3VvNZ41yMWyPQy2AiEfJTPpZdzeGNG5zaaq3Lihb
Solana Public Key (derivation 2): 8PnmS7rp3hRTSqGJkYedm8tavYuDfLjBJJL9ssBuyAis
Solana Public Key (derivation 3): 7rdf3btc5zNA7TXvA3Jc31VKnYRdc9goLmFYy6mEjbTv

Here, since we wanted 3 derivations, 4 accounts (4 public keys) have been generated.

🌐 recover_seed command

Recover keypair and seed from a specific mnemonic phrase.

To generate and display the seed and Solana public key from a specific mnemonic phrase, pass the phrase (12 or 24 words, for example) as an argument.

This will also generate and write the keypair to the JSON file.

Example with this 12 words: shed scorpion manual wheat monster phone winter toe dream kitchen salad column.

  • Command:
cargo run -- recover_seed "<RECOVERY_PHRASE>"
  • Example:
cargo run -- recover_seed "shed scorpion manual wheat monster phone winter toe dream kitchen salad column"

Optional passphrase: You will be prompted to enter a passphrase (leave blank to not use one).

  • Example of result (with NB_DERIVATIONS=3):
BIP39 Mnemonic (given phrase): shed scorpion manual wheat monster phone winter toe dream kitchen salad column
Seed: 34A0EACFFDF41445C0B7E43C2D730C54F4CD1D8334528F73E3D5F2C2977FAABA7CAD88EBDA6A1F02CE6BB596F04036305A32B96303F93FF864D268539739AFF8
Solana Public Key: FTGJPL5hia749v3jhNWJA7uE2VoVGyofB7BBL2cLwoPc
Solana Public Key (derivation 1): EMLY3VvNZ41yMWyPQy2AiEfJTPpZdzeGNG5zaaq3Lihb
Solana Public Key (derivation 2): 8PnmS7rp3hRTSqGJkYedm8tavYuDfLjBJJL9ssBuyAis
Solana Public Key (derivation 3): 7rdf3btc5zNA7TXvA3Jc31VKnYRdc9goLmFYy6mEjbTv

Here, since we wanted 3 derivations, 4 accounts (4 public keys) have been generated.

🌐 send command

Send SOL (lamports) to a recipient address (sign outgoing transaction).

This command allows you to sign an outgoing transaction from your wallet to a destination address.

Example to send 0.002 SOL (2000000 lamports) to recipient address EMLY3VvNZ41yMWyPQy2AiEfJTPpZdzeGNG5zaaq3Lihb.

  • Command:
cargo run -- send <RECIPIENT_PUBKEY> <AMOUNT_IN_LAMPORTS>
  • Example:
cargo run -- send EMLY3VvNZ41yMWyPQy2AiEfJTPpZdzeGNG5zaaq3Lihb 2000000

This command will sign the transaction with the keypair which is stored in the file <your-path>/storage/keypair/id.json file (KEYPAIR_PATH env var).

  • Example of result (when successfully):
Transaction sent successfully!
  • Example of result (when it fails):
Failed to send transaction: ...

🌐 pubkey command

Retrieve public key from stored keypair.

This is useful for retrieving your Solana public key if you have already generated and stored your keypair locally.

This command reads your JSON keypair file stored, extracts the public key, and displays it.

  • Command:
cargo run -- pubkey

This command reads the keypair stored in <your-path>/storage/keypair/id.json file (KEYPAIR_PATH env var).

  • Example of result:
Solana Public Key: FTGJPL5hia749v3jhNWJA7uE2VoVGyofB7BBL2cLwoPc

🌐 balance_by_pubkey command

Get balance by public key.

This command allows you to see the balance (in SOL and in lamports) of a public address.

Example to see the balance of the public address EMLY3VvNZ41yMWyPQy2AiEfJTPpZdzeGNG5zaaq3Lihb.

  • Command:
cargo run -- balance_by_pubkey <PUBKEY>
  • Example:
cargo run -- balance_by_pubkey EMLY3VvNZ41yMWyPQy2AiEfJTPpZdzeGNG5zaaq3Lihb
  • Example of result:
Balance: 0.005910000 SOL (5910000 lamports)

Environment Variables

Environment variables are configured in the .env file.

You can configure these environment variables:

  • NB_DERIVATIONS (default value: 0).
  • KEYPAIR_PATH (default value: ./storage/keypair/id.json).
  • KEYPAIR_DERIVATIONS_PATH (default value: ./storage/keypair/derived).
    • Note: KEYPAIR_DERIVATIONS_PATH is only useful if NB_DERIVATIONS is > 0.
  • RPC_URL (default value: https://api.devnet.solana.com).

Some Interesting Links

  • BIP39: Learn more about the BIP39 standard here.
  • SLIP44: Learn more about the SLIP44 here.

Security

This wallet is an example and should not be used to store large amounts of SOL without a thorough security review. Always make sure to back up your mnemonic phrases and private keys in a secure location.

FAQ

Q: Can this wallet be used on the Solana mainnet?

A: While technically possible, this wallet is designed as an educational example and is not recommended for use on the mainnet without a thorough security review.

Various Documentations

License

This project is licensed under the MIT License. See the LICENSE file for more details.