Bitcoin Wallet Development

Keywords: Blockchain network

In this tutorial, we will use C # to develop a Bitcoin wallet, and we will use the NBitcoin library. The code in the tutorial implements the functions of storing, receiving and paying bitcoins, and can be easily transplanted to other applications.

If you want to quickly grasp the use of NBitcoin in C # program, it is recommended to visit Hui Zhi's courses:

1. Development objectives

The Bitcoin wallet we want to develop has the following functions:

  • Key recovery using BIP39 mnemonic
  • Bit addresses can be created, and bitcoins transferred from other addresses can be received.
  • You can view the Bitcoin address balance
  • Bitcoin can be paid to other addresses

2. Introducing NBitcoin development kit

First, you need to introduce the NBitcoin development kit and the QBitNinja development kit:

using NBitcoin;
using QBitNinja.Client;
using QBitNinja.Client.Models;

3. Generating BIP39 mnemonic

We need to preserve the generated auxiliary words:

public void MssGenerateMnemo(out string ssMnemo) {

Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.Twelve);

ssMnemo = mnemonic.ToString();

}

4. Generating Bitcoin Address

The following code can use NBitcoin to generate bitcoin HD addresses:

public void MssGenerateAddress(
		string ssMnemo, 
		int ssKeynumber, 
		bool ssIsTestNet, 
		out string ssAddress, 
		out string ssPrivateKey) {

  Network net;
  if (ssIsTestNet)
      net = Network.TestNet;
  else
      net = Network.Main;

  Mnemonic restoreNnemo = new Mnemonic(ssMnemo);

  ExtKey masterKey = restoreNnemo.DeriveExtKey();

  KeyPath keypth = new KeyPath("m/44'/0'/0'/0/" + ssKeynumber);
  ExtKey key = masterKey.Derive(keypth);

  ssAddress = key.PrivateKey.PubKey.GetAddress(net).ToString();
  ssPrivateKey = key.PrivateKey.GetBitcoinSecret(net).ToString();

}

5. Getting Bitcoin Address Balance

The following code can obtain the bitcoin balance at the specified address:

public void MssGetBalance(
		string ssAddress, 
		bool ssIsUnspentOnly, 
		bool ssIsTestNet, 
		out decimal ssBalance, 
		out decimal ssConfirmedBalance) {

  Network net;
  if (ssIsTestNet)
    net = Network.TestNet;
  else
    net = Network.Main;

  QBitNinjaClient client = new QBitNinjaClient(net);
  var balance = client.GetBalance(new BitcoinPubKeyAddress(ssAddress), ssIsUnspentOnly).Result;

  ssBalance = 0.0M;
  ssConfirmedBalance = 0.0M;

  if (balance.Operations.Count > 0)
  {
    var unspentCoins = new List<Coin>();
    var unspentCoinsConfirmed = new List<Coin>();    
    foreach (var operation in balance.Operations)
    {
        unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));
        if(operation.Confirmations > 0)
            unspentCoinsConfirmed.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));

    }

    ssBalance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
    ssConfirmedBalance = unspentCoinsConfirmed.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
  }

}

6. Transfer to other Bitcoin addresses

The following code can use NBitcoin to transfer bitcoins to a specified address:

public void MssGetBalance(
		string ssAddress, bool ssIsUnspentOnly, 
		bool ssIsTestNet, 
		out decimal ssBalance, out decimal ssConfirmedBalance) {

  Network net;
  if (ssIsTestNet)
      net = Network.TestNet;
  else
      net = Network.Main;

  QBitNinjaClient client = new QBitNinjaClient(net);
  var balance = client.GetBalance(new BitcoinPubKeyAddress(ssAddress), ssIsUnspentOnly).Result;

  ssBalance = 0.0M;
  ssConfirmedBalance = 0.0M;

  if (balance.Operations.Count > 0)
  {
    var unspentCoins = new List<Coin>();
    var unspentCoinsConfirmed = new List<Coin>();    
    foreach (var operation in balance.Operations)
    {
      unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));
      if(operation.Confirmations > 0)
          unspentCoinsConfirmed.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));

    }

    ssBalance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
    ssConfirmedBalance = unspentCoinsConfirmed.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
  }
}

Links to the original text: Developing your own Bitcoin Wallet with C# - Huizhi

Posted by kurtis on Wed, 24 Apr 2019 19:54:34 -0700