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.

solana Article's
30 articles in total
Favicon
Building a Solana Wallet Backend with .NET (Part 1)
Favicon
Solana Account Model Simplified
Favicon
Introducing Yamaswap
Favicon
I Just Launched a Meme Coin!
Favicon
solana
Favicon
Bitcoin vs Ethereum vs Solana: Trendsetters in the Web3 Revolution ✨⚛✨
Favicon
🔐 Mastering Account Initialization and PDAs on Solana 🚀
Favicon
From NFTs to DeFi: What Developers Can Do on Solana in 2025
Favicon
solana是如何算seed地址
Favicon
Ethereum vs Solana: Understanding Data Storage Differences in Their Ecosystems
Favicon
Build a Solana Wallet Balance Checker API with (C#) .NET and Solnett
Favicon
🔍 Why Are There So Many Bots on Solana, Especially in Meme Tokens?
Favicon
Solana Pro RPC with X2 Performance Launched by GetBlock
Favicon
Ironforge: A Comprehensive DevOps Platform for Web3 Applications
Favicon
GetBlock Introduces Solana PRO - Enhanced SOL RPC Powered with Rich SDK Support, Jito, Yellowstone Geyser gRPC, and More!
Favicon
Need some help for getting Web3 internship
Favicon
Shorts. Solana Versioned Transaction structure
Favicon
Shorts. Get CompactU16 array length
Favicon
A Developer’s Guide into Account Abstraction Models
Favicon
solana 获取钱包token余额,及优化
Favicon
Accounts: The Digital DNA of Solana
Favicon
Welcome to Solana: A Web2 Developer's Travel Guide to the Blockchain Nation
Favicon
Web3 explained for Web2 Developers: My Personal Journey from SQL to Solana
Favicon
How to Use DefiLlama: A Comprehensive Guide
Favicon
OKX DEX API Guide: Building a Solana Token Swap Interface
Favicon
Solana Safe Transfer (Fat-Finger-proof Transfer)
Favicon
Atom transactions using Jito bundles
Favicon
Blinks on AWS with SST
Favicon
Write and Deploy Your First Solana Program on Solana Playground
Favicon
Running Your First Solana Project with Anchor

Featured ones: