#Solana #Wallet #Rust #BIP39 #Web3 #Blockchain
This is an example of a Solana Wallet in Rust π¦
This example of a Solana Wallet is developed by Stephen Damian
- Feature Summary
- Wallet Commands Summary
- Project Overview
- Roadmap
- Prerequisites
- Setup
- How to use?
- Environment Variables
- Some Interesting Links
- Security
- FAQ
- Various Documentations
- License
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.
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
Rust Solana Wallet - A lightweight Solana wallet developed in Rust.
Status: Under development π§
- β Command-line interface (CLI) implementation.
- β Testing: Functional tests & Unit tests.
- β¬ Graphical user interface (GUI) implementation.
- β¬ Add support for SPL tokens.
- Rust
>= 1.75.0
(last tested:1.83.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.
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
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 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 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 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: ...
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
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 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 ifNB_DERIVATIONS
is >0
.
- Note:
RPC_URL
(default value:https://api.devnet.solana.com
).
- BIP39: Learn more about the BIP-0039 standard here.
- BIP44: Learn more about the BIP-0044 standard here.
- SLIP44: Learn more about the SLIP-0044 standard here.
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.
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.
- See further technical details here: Notes-tech.md
This project is licensed under the MIT License. See the LICENSE file for more details.