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

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

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

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

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

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

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

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:

Nuget Packages

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

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.