NBitcoin Documentation

repository·master·Indexed 24 days ago

https://github.com/metacosa/nbitcoin

A comprehensive Bitcoin library for .NET implementing most relevant Bitcoin Improvement Proposals (BIPs). It provides high-level object-oriented APIs and low-level access to Bitcoin primitives for building wallets, nodes, and blockchain applications. The ecosystem includes NBitcoin.Altcoins for various altcoin networks, NBitcoin.Secp256k1 for a managed C# implementation of the secp256k1 elliptic curve, and NBitcoin.TestFramework for orchestrating bitcoind instances in integration tests.

Tokens
3.6K
Snippets
13
Records
22
Agent score
84%

What's inside NBitcoin

  1. Overview of NBitcoin features

    master

    NBitcoin is a comprehensive Bitcoin library for .NET that implements most relevant Bitcoin Improvement Proposals (BIPs). It provides both high-level object-oriented APIs and low-level access to Bitcoin primitives.

    Core Capabilities:

    • Transaction Management: TransactionBuilder supporting Stealth, Open Asset, and standard transactions.
    • Scripting: Full script evaluation, parsing, serialization, and creation of custom scripts.
    • Wallets & Keys: Hierarchical Deterministic Wallets (BIP 32), Mnemonic code (BIP 39), and SPV Wallet implementation.
    • Address Support: Bech32 SegWit (BIP 173) with error detection.
    • Security: Signing and verification with private keys (including compact signatures), Two-Factor keys (BIP 38), and Stealth Addresses.
    • Network Clients: RPC Client and Rest Client.
    • Protocol Support: Payment Protocol (BIP 70), Payment URLs (BIP 21, BIP 72), and Segregated Witness (BIP 141, 143, 144).
  2. Understand when to use NBitcoin.Secp256k1 vs NBitcoin

    master

    The NBitcoin.Secp256k1 library is a managed C# implementation of the secp256k1 project. It provides low-level mathematical abstractions.

    Use NBitcoin.Secp256k1 if:

    • You need a fully managed implementation of secp256k1 (e.g., for environments like WASM or Unity where PInvoke might not be supported).
    • You are implementing low-level cryptographic primitives that require a faithful port of the secp256k1 C codebase.

    DO NOT use this project if:

    • You only need standard Bitcoin features (use the main NBitcoin library instead).
    • You do not have a specific requirement for the low-level secp256k1 mathematical abstractions.
  3. Quickstart: Generate a Bitcoin Key and WIF in .NET Core

    master

    To get started with a .NET Core console application, follow these steps:

    1. Create and enter a new directory:
    mkdir MyProject
    cd MyProject
    1. Initialize a new console project and add NBitcoin:
    dotnet new console
    dotnet add package NBitcoin
    dotnet restore
    1. Use the following code in Program.cs to generate a new private key and its Wallet Import Format (WIF) for the Main network:
    using System;
    using NBitcoin;
    
    namespace _125350929
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World! " + new Key().GetWif(Network.Main));
            }
        }
    }
    1. Run the application:
    dotnet run
    using System;
    using NBitcoin;
    
    namespace _125350929
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World! " + new Key().GetWif(Network.Main));
            }
        }
    }
  4. Access NBitcoin documentation and resources

    master

    For in-depth learning and implementation details, refer to the following resources:

  5. Compile NBitcoin for Unity

    master

    To use NBitcoin in Unity, you must use at least Unity 2018.2 with Script Runtime Version set to .NET 4.x Equivalent and Api Compatibility Level set to .NET Standard 2.0.

    Follow these steps to compile the necessary libraries:

    1. Clone the repository and navigate to the NBitcoin directory:
    git clone https://github.com/MetacoSA/NBitcoin/
    cd NBitcoin/NBitcoin
    1. Publish the library for netstandard2.0:
    dotnet publish -c Release -f netstandard2.0
    1. Clean up the output by removing the runtimes folder:
    Remove-Item -Force -Recurse .\bin\Release\netstandard2.0\publish\runtimes\
    1. Copy the contents of .\bin\Release\netstandard2.0 into your Unity Assets folder.

    If you require altcoin support, repeat the process but navigate to NBitcoin/NBitcoin.Altcoins before publishing.

    git clone https://github.com/MetacoSA/NBitcoin/
    cd NBitcoin/NBitcoin
    dotnet publish -c Release -f netstandard2.0
    Remove-Item -Force -Recurse .\bin\Release\netstandard2.0\publish\runtimes\
  6. Install NBitcoin.Altcoins for Altcoin support

    master

    If you need to work with altcoins or your own blockchain, install the NBitcoin.Altcoins package instead of the standard NBitcoin package.

    Install-Package NBitcoin.Altcoins
    Install-Package NBitcoin.Altcoins
  7. Setup NBitcoin in .NET Core

    master

    To create a new .NET Core console application using NBitcoin, follow these steps:

    1. Create a new directory and initialize a console project.
    2. Add the NBitcoin package.
    3. Restore dependencies.
    4. Implement your logic in Program.cs.
    5. Run the application using dotnet run.
    mkdir MyProject
    cd MyProject
    dotnet new console
    dotnet add package NBitcoin
    dotnet restore

    Example Program.cs:

    using System;
    using NBitcoin;
    
    namespace _125350929
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World! " + new Key().GetWif(Network.Main));
            }
        }
    }

    Run with:

    dotnet run
  8. Debug NBitcoin source code in Visual Studio

    master

    To step into NBitcoin code using Visual Studio (version 15.9 or later), configure your debugging settings as follows:

    1. Disable 'Just My Code': Go to Tools / Options / Debugging / General and uncheck Enable Just My Code.

    2. Configure Symbol Locations: Go to Tools / Options / Debugging / Symbols. Add https://symbols.nuget.org/download/symbols to the Symbol file (.pdb) locations list and ensure it is checked.

    Note: It is recommended to also check Microsoft Symbol Server to improve the debugging experience.

  9. How to support a new altcoin

    master

    To add support for a new altcoin to the NBitcoin ecosystem, you should follow the implementation pattern used by existing altcoins like Litecoin and submit a pull request.

    Note: NBitcoin developers do not test these pull requests; you are responsible for ensuring the implementation remains functional and correct.

  10. Install NBitcoin via NuGet

    master

    To use NBitcoin in your .NET projects, install it using the NuGet package manager.

    For .NET Core projects, use the dotnet CLI:

    dotnet add package NBitcoin

    For legacy .NET Framework projects in Visual Studio, use the Package Manager Console:

    Install-Package NBitcoin

    Alternatively, you can use the Manage NuGet Packages window directly in Visual Studio.

    dotnet add package NBitcoin
  11. Filter unit tests by Trait in Test Explorer

    master

    When running tests in a development environment, you can group and filter NBitcoin unit tests using specific Trait identifiers in your Test Explorer. This allows you to run specific subsets of the test suite based on their purpose and dependencies:

    • Core: Tests ported directly from the bitcoind source. These have no external dependencies.
    • UnitTest: Self-contained tests that are not ported from Bitcoin Core.
    • Protocol: Tests that verify NBitcoin's correct interpretation of P2P messaging in compatibility with Bitcoin Core.
    • Altcoins: A test battery intended for verifying altcoin integrations. (For more details, see the NBitcoin.Altcoins documentation).