Logo

dev-resources.site

for different kinds of informations.

Building a Solana Wallet Backend with .NET (Part 1)

Published at
1/9/2025
Categories
web3
solana
csharp
dotnet
Author
ilucky_israel
Categories
4 categories in total
web3
open
solana
open
csharp
open
dotnet
open
Author
13 person written this
ilucky_israel
open
Building a Solana Wallet Backend with .NET (Part 1)

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}");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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 });
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

  1. Create a new wallet: GET /api/wallet/new
  2. Recover a wallet: POST /api/wallet/recover
  3. 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.

dotnet Article's
30 articles in total
Favicon
Pushed Authorization Requests in .NET 9: Why and How to Use Them
Favicon
clickonce silent install
Favicon
Testing with JWT in .NET APIs
Favicon
What is Quartz.Net and its simple implementation
Favicon
Publish Private Nuget Packages on Github
Favicon
Beyond the Random Class: Cryptographic Randomness in .NET 6+
Favicon
Demystifying AIContents in Microsoft.Extensions.AI
Favicon
Fine-Tuning Large Language Models (LLMs) with .NET Core, Python, and Azure
Favicon
Simplifying Dependency Management with NuGet Central Package Management
Favicon
A tutorial on WinUI 3 (2025)
Favicon
Building Modern Desktop Applications with .NET 9: Features and Best Practices
Favicon
Orden en el Código .NET
Favicon
My Favorite Tech Stack for Startup Success in 2025
Favicon
Building a Solana Wallet Backend with .NET (Part 1)
Favicon
IL
Favicon
Experiment with a universal CRUD API using UkrGuru.Sql
Favicon
.Net tarixi
Favicon
dotnet терминал команды
Favicon
Cqrs
Favicon
Why You Should Learn C#/.NET in 2025
Favicon
Custom Middleware Extensions in .NET Core Web API
Favicon
Azure OpenAI Error Handling in Semantic Kernel
Favicon
Qt/C++ Senior Experts Provide Customized Software Development Services
Favicon
Static Lambda in C# 12 : Performance Improvement
Favicon
Dapper vs EF Core: Understanding When to Use Each
Favicon
The Unsung Hero of DevOps AWS X-Ray
Favicon
Learning some Fantomas AST
Favicon
Permission-Based Authentication and Authorization in .NET, via Cookies
Favicon
Simplifying CRUD Operations Using Primary Constructors for Dependency Injection with Generic Interfaces
Favicon
What is GCHandle in C#? (Part 1)

Featured ones: