Welcome & allow me be your guide you through building a Solana wallet backend using C# and .NET, no too much talk! Let's dive in. We’ll start with wallet creation, recovery, and balance retrieval functionality. This will be the first part of our Solana wallet series.
Prerequisites
- Visual Studio or any preferred IDE.
- .NET SDK installed.
- Basic understanding of Solana and blockchain technology.
- NuGet package: Solnett.
First let us organise our folder structure, (feel free to call the project anything you want, for me? I will call mine Solark: "Sol for Solana" and "Ark for the strength of Noah's Ark")
SolarkAPI/
Controllers/
WalletController.cs
Services/
WalletService.cs
Program.cs
Step 1: Setting Up the Project
Create a new ASP.NET Core Web API project:
dotnet new webapi -n SolarkAPI
cd SolarkAPI
Install the Solana SDK:
dotnet add package Solnett
Step 2: Implementing Wallet Functionality
We will create the WalletService to handle wallet operations.
using Solnet.Wallet;
using Solnet.Rpc;
using Solnet.Rpc.Builders;
using Solnet.Wallet.Bip39;
namespace SolarkAPI.Services
{
public class WalletService
{
public Wallet CreateNewWallet()
{
var mnemonic = new Mnemonic(WordList.English, WordCount.Twelve);
return new Wallet(mnemonic);
}
public Wallet RecoverWallet(string mnemonicPhrase)
{
var mnemonic = new Mnemonic(mnemonicPhrase);
return new Wallet(mnemonic);
}
public async Task<ulong> GetWalletBalanceAsync(string publicKey)
{
var rpcClient = ClientFactory.GetClient(Cluster.DevNet);
var balanceResult = await rpcClient.GetBalanceAsync(publicKey);
if (balanceResult.WasSuccessful)
return balanceResult.Result.Value;
throw new Exception($"Failed to fetch balance: {balanceResult.Reason}");
}
}
}
Note: you can choose the amount of strings you want your mnemonic phrase to be, i like Twelve because it short
Step 3: Setting Up the Wallet Controller
We will expose wallet functionalities through RESTful API endpoints.
using Microsoft.AspNetCore.Mvc;
using Solark.Services;
[ApiController]
[Route("api/wallet")]
public class WalletController : ControllerBase
{
private readonly WalletService _walletService;
public WalletController()
{
_walletService = new WalletService();
}
[HttpGet("new")]
public IActionResult CreateWallet()
{
var wallet = _walletService.CreateNewWallet();
return Ok(new
{
Mnemonic = wallet.Mnemonic,
PublicKey = wallet.Account.PublicKey.Key
});
}
[HttpPost("recover")]
public IActionResult RecoverWallet([FromBody] string mnemonic)
{
try
{
var wallet = _walletService.RecoverWallet(mnemonic);
return Ok(new { PublicKey = wallet.Account.PublicKey.Key });
}
catch (Exception ex)
{
return BadRequest(new { Error = ex.Message });
}
}
[HttpGet("balance/{publicKey}")]
public async Task<IActionResult> GetBalance(string publicKey)
{
try
{
var balance = await _walletService.GetWalletBalanceAsync(publicKey);
return Ok(new { Balance = balance });
}
catch (Exception ex)
{
return BadRequest(new { Error = ex.Message });
}
}
}
Note this is just a basic setup, for creating a wallet, mnemonic phrase & recovery and Remember to add your service & map controller to your programs.cs file so your can view your endpoints on swagger.
Step 4: Testing your new API
Run the API and test the endpoints using Swagger or any tool you're comfortable with.
Endpoints:
- Create a new wallet: GET /api/wallet/new
- Recover a wallet: POST /api/wallet/recover
- Get wallet balance: GET /api/wallet/balance/{publicKey}
Example Output
Create Wallet:
{
"Mnemonic": "abandon abandon abandon ...",
"PublicKey": "B63V..."
}Recover Wallet
{
"PublicKey": "B63V..."
}Get Balance:
{
"Balance": 50000000
}
Congratulations, you have successfully created an API to generate a Solana wallet address along with its own mnemonic phrase, you can recover a wallet and also get the balance of that wallet with these endpoints.
For out next part, I'll be writing on making transactions, verifying transaction status & viewing transaction details.
Stay tuned for more Solana & .Net contents.
Top comments (0)