NBitcoin - Introduction To NBitcoin

I wrote an introduction to Stratis - A C# and .NET Blockchain solution. Stratis Platform was not built from scratch. It was built on top of NBitcoin - the most complete Bitcoin library for the .NET platform. In order to understand and use Stratis better, we need to familiar with NBitcoin. In this article, I am going to give an introduction to NBitcoin.

Introduction

I wrote an introduction to Stratis - a C# and .NET Blockchain solution. The Stratis Platform was not built from scratch. It was built on top of NBitcoin - the most complete Bitcoin library for the .NET platform. In order to understand and use Stratis better, we need to be familiar with NBitcoin. In this article, I am going to give an introduction to NBitcoin.

What is NBitcoin?

Bitcoin Core is the first software created for Bitcoin and Blockchain. It was started by Satoshi Nakamoto. Bitcoin source code in GitHub is constantly reviewed by developers to learn Bitcoin and Blockchain. It is written in C++. To most .NET developers, to read and write C++ code is time-consuming and error-prone. NBitcoin is a port of Bitcoin from C++ to C# to help C# developers learn Bitcoin and Blockchain. And, it provides Nuget packages to let developers easily build their own Blockchain solution on top of it, just like Stratis. NBitcoin source code can be accessed at GitHub.

Introduction to NBitcoin

 

The NBitcoin solution has four projects,

  • NBitcoin
    Implementation of a Bitcoin client similar to Bitcoin Core. The client can connect to Bitcoin mainnet and testnet.

  • Altcoins
    A lot of alternative cryptocurrencies are forked from Bitcoin, e.g. Litecoin. They share the same fundamental design with Bitcoin. This project is to have an implementation difference for supporting those alternative cryptocurrencies and their networks.

  • TestFramework
    A test framework for easily creating Bitcoin instances in integration tests.

  • Tests
    Unit tests for verifying NBitcoin implementation.

NBitcoin is the identical implementation to Bitcoin. Using Blockheader as an example, Blockheader is a data structure that is used for storing basic block information.

Introduction to NBitcoin

 

In Bitcoin, Blockheader is defined in bitcoin/src/primitives/block.h and bitcoin/src/primitives/block.cpp. It has 5 properties: version, prev block hash, Merkle root hash, time, bits, and nonce.

  1. …  
  2.     template <typename Stream, typename Operation>  
  3.     inline void SerializationOp(Stream& s, Operation ser_action) {  
  4.         READWRITE(this->nVersion);  
  5.         READWRITE(hashPrevBlock);  
  6.         READWRITE(hashMerkleRoot);  
  7.         READWRITE(nTime);  
  8.         READWRITE(nBits);  
  9.         READWRITE(nNonce);  
  10. ...  

All properties are initialized as 0 or Null in SetNull()

  1. …  
  2.     void SetNull()  
  3.     {  
  4.         nVersion = 0;  
  5.         hashPrevBlock.SetNull();  
  6.         hashMerkleRoot.SetNull();  
  7.         nTime = 0;  
  8.         nBits = 0;  
  9.         nNonce = 0;  
  10.     }  
  11. ...  

Correspondingly, in NBitcoin, Blockheader is defined in NBitcoin/NBitcoin/Block.cs

  1. …  
  2.   
  3.         // header  
  4.         const int CURRENT_VERSION = 3;  
  5.   
  6.         protected uint256 hashPrevBlock;  
  7.   
  8.         public uint256 HashPrevBlock  
  9.         {  
  10.             get  
  11.             {  
  12.                 return hashPrevBlock;  
  13.             }  
  14.             set  
  15.             {  
  16.                 hashPrevBlock = value;  
  17.             }  
  18.         }  
  19.         protected uint256 hashMerkleRoot;  
  20.   
  21.         protected uint nTime;  
  22.         protected uint nBits;  
  23.   
  24.         public Target Bits  
  25.         {  
  26.             get  
  27.             {  
  28.                 return nBits;  
  29.             }  
  30.             set  
  31.             {  
  32.                 nBits = value;  
  33.             }  
  34.         }  
  35.   
  36.         protected int nVersion;  
  37.   
  38.         public int Version  
  39.         {  
  40.             get  
  41.             {  
  42.                 return nVersion;  
  43.             }  
  44.             set  
  45.             {  
  46.                 nVersion = value;  
  47.             }  
  48.         }  
  49.   
  50.         protected uint nNonce;  
  51.   
  52.         public uint Nonce  
  53.         {  
  54.             get  
  55.             {  
  56.                 return nNonce;  
  57.             }  
  58.             set  
  59.             {  
  60.                 nNonce = value;  
  61.             }  
  62.         }  
  63.         public uint256 HashMerkleRoot  
  64.         {  
  65.             get  
  66.             {  
  67.                 return hashMerkleRoot;  
  68.             }  
  69.             set  
  70.             {  
  71.                 hashMerkleRoot = value;  
  72.             }  
  73.         }  
  74. ...  

All properties are initialized to 0 or default value in SetNull()

  1. …  
  2.         internal void SetNull()  
  3.         {  
  4.             nVersion = CURRENT_VERSION;  
  5.             hashPrevBlock = 0;  
  6.             hashMerkleRoot = 0;  
  7.             nTime = 0;  
  8.             nBits = 0;  
  9.             nNonce = 0;  
  10.         }  
  11. ...  

For .NET developers, the implementation in NBitcoin is obviously more intuitive and easier to understand.

Going Beyond Bitcoin

On top of  Bitcoin implementation to .NET, NBitcoin also added some features to improve efficiency and privacy. One example is the Stealth Address. It was designed for solving the problem that Bitcoin transaction could be potentially traced by associating a Bitcoin address with a real-world identity. Stealth Address gives users a way to generate a random one-time address for each transaction. Therefore, tracing transactions becomes very difficult. Stealth address is implemented in NBitcoin/NBicoin/Stealth,

Introduction to NBitcoin

 

Developing on top of NBitcoin

NBitcoin is a .NET port of Bitcoin, an alternative Bitcoin Core client application, and also a Blockchain development framework. It provides a suite of APIs for developing a Blockchain platform, systems, or applications.

Hello World in NBitcoin

To exploit the potential of NBitcoin, we can start with a simple Hello World program that will just generate a new key from the Bitcoin main network. The sample requires .NET Core 2.1. The following are the steps:

  • Setup the program
    1. mkdir nbitcoin-hello-world  
    2. cd nbitcoin-hello-world  
    3. dotnet new console  
    4. dotnet add package NBitcoin  
    5. dotnet restore  
  • Update Program.cs
    1. using System;  
    2. using NBitcoin;  
    3.   
    4. namespace nbitcoin_hello_world  
    5. {  
    6.     class Program  
    7.     {  
    8.         static void Main(string[] args)  
    9.         {  
    10.             Console.WriteLine("Hello World! " + new Key().GetWif(Network.Main));  
    11.         }  
    12.     }  
    13. }  
  • The new Key().GetWif(Network.Main)) will create a key pair and generate a Bitcoin secret from the key for the Network.Main network.

  • Run the console application

    dotnet run

    Introduction to NBitcoin

Nuget Packages

NBitcoin can be easily consumed in a .NET core application by adding its NuGet packages:

  • NBitcoin - The primary NBitcoin package that has all Bitcoin features.
  • Indexer - Library for querying data indexed by NBitcoin.Indexer on Azure Storage.
  • Altcoins - Library for using alternative coin features.

Stratis Platform

Stratis Platform uses NBitcoin as the Network Layer and a port of the Consensus Layer.

Introduction to NBitcoin

Summary

There were about 8 million .NET developers worldwide according to Scott Hanselman’s estimate several years ago. Helping them learn and contribute to Blockchain will dramatically impact the Blockchain industry. NBitcoin, as a .NET port of Bitcoin, is easy to understand and simple to code. It can be combined with other Blockchain solutions/frameworks, such as Stratis, as the base for creating enterprise Blockchain applications and systems.