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.
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.
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.
- …
- template <typename Stream, typename Operation>
- inline void SerializationOp(Stream& s, Operation ser_action) {
- READWRITE(this->nVersion);
- READWRITE(hashPrevBlock);
- READWRITE(hashMerkleRoot);
- READWRITE(nTime);
- READWRITE(nBits);
- READWRITE(nNonce);
- ...
All properties are initialized as 0 or Null in SetNull()
- …
- void SetNull()
- {
- nVersion = 0;
- hashPrevBlock.SetNull();
- hashMerkleRoot.SetNull();
- nTime = 0;
- nBits = 0;
- nNonce = 0;
- }
- ...
Correspondingly, in NBitcoin, Blockheader is defined in NBitcoin/NBitcoin/Block.cs
- …
-
-
- const int CURRENT_VERSION = 3;
-
- protected uint256 hashPrevBlock;
-
- public uint256 HashPrevBlock
- {
- get
- {
- return hashPrevBlock;
- }
- set
- {
- hashPrevBlock = value;
- }
- }
- protected uint256 hashMerkleRoot;
-
- protected uint nTime;
- protected uint nBits;
-
- public Target Bits
- {
- get
- {
- return nBits;
- }
- set
- {
- nBits = value;
- }
- }
-
- protected int nVersion;
-
- public int Version
- {
- get
- {
- return nVersion;
- }
- set
- {
- nVersion = value;
- }
- }
-
- protected uint nNonce;
-
- public uint Nonce
- {
- get
- {
- return nNonce;
- }
- set
- {
- nNonce = value;
- }
- }
- public uint256 HashMerkleRoot
- {
- get
- {
- return hashMerkleRoot;
- }
- set
- {
- hashMerkleRoot = value;
- }
- }
- ...
All properties are initialized to 0 or default value in SetNull()
- …
- internal void SetNull()
- {
- nVersion = CURRENT_VERSION;
- hashPrevBlock = 0;
- hashMerkleRoot = 0;
- nTime = 0;
- nBits = 0;
- nNonce = 0;
- }
- ...
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,
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
- mkdir nbitcoin-hello-world
- cd nbitcoin-hello-world
- dotnet new console
- dotnet add package NBitcoin
- dotnet restore
- Update Program.cs
- using System;
- using NBitcoin;
-
- namespace nbitcoin_hello_world
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hello World! " + new Key().GetWif(Network.Main));
- }
- }
- }
- 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
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.
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.