Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions BitcoinKernel.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{F8E63A4F-7D3E-4B2A-9C1D-8A5F6E9B3C2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel.Tests", "tests\BitcoinKernel.Tests\BitcoinKernel.Tests.csproj", "{BC90EFB4-1692-CBCC-EF52-778255F591E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicUsage", "examples\BasicUsage\BasicUsage.csproj", "{0E2DDF4A-A1FE-5424-03EA-7A8E76751354}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel.Interop", "src\BitcoinKernel.Interop\BitcoinKernel.Interop.csproj", "{9C4AFB5E-ED75-909E-FC61-1BD17BB6BC23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel.Core", "src\BitcoinKernel.Core\BitcoinKernel.Core.csproj", "{2BDCF8E8-DD35-3BA0-0148-5A2588335678}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel", "src\BitcoinKernel\BitcoinKernel.csproj", "{D6F509B1-C990-0533-2DD1-CFFBA7506249}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel", "src\BitcoinKernel\BitcoinKernel.csproj", "{2BDCF8E8-DD35-3BA0-0148-5A2588335678}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockProcessing", "examples\BlockProcessing\BlockProcessing.csproj", "{23E19BC3-8829-42BE-BCB4-A2050BE04975}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel.Core.Tests", "tests\BitcoinKernel.Core.Tests\BitcoinKernel.Core.Tests.csproj", "{267842B2-D915-4B9E-8448-F9B5816D4A0A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitcoinKernel.Tests", "tests\BitcoinKernel.Tests\BitcoinKernel.Tests.csproj", "{267842B2-D915-4B9E-8448-F9B5816D4A0A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "kernel-bindings-test-handler", "tools\kernel-bindings-test-handler\kernel-bindings-test-handler.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}"
EndProject
Expand All @@ -37,18 +33,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Debug|x64.ActiveCfg = Debug|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Debug|x64.Build.0 = Debug|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Debug|x86.ActiveCfg = Debug|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Debug|x86.Build.0 = Debug|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Release|Any CPU.Build.0 = Release|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Release|x64.ActiveCfg = Release|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Release|x64.Build.0 = Release|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Release|x86.ActiveCfg = Release|Any CPU
{BC90EFB4-1692-CBCC-EF52-778255F591E2}.Release|x86.Build.0 = Release|Any CPU
{0E2DDF4A-A1FE-5424-03EA-7A8E76751354}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E2DDF4A-A1FE-5424-03EA-7A8E76751354}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E2DDF4A-A1FE-5424-03EA-7A8E76751354}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -85,18 +69,6 @@ Global
{2BDCF8E8-DD35-3BA0-0148-5A2588335678}.Release|x64.Build.0 = Release|Any CPU
{2BDCF8E8-DD35-3BA0-0148-5A2588335678}.Release|x86.ActiveCfg = Release|Any CPU
{2BDCF8E8-DD35-3BA0-0148-5A2588335678}.Release|x86.Build.0 = Release|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Debug|x64.ActiveCfg = Debug|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Debug|x64.Build.0 = Debug|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Debug|x86.ActiveCfg = Debug|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Debug|x86.Build.0 = Debug|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Release|Any CPU.Build.0 = Release|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Release|x64.ActiveCfg = Release|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Release|x64.Build.0 = Release|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Release|x86.ActiveCfg = Release|Any CPU
{D6F509B1-C990-0533-2DD1-CFFBA7506249}.Release|x86.Build.0 = Release|Any CPU
{23E19BC3-8829-42BE-BCB4-A2050BE04975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23E19BC3-8829-42BE-BCB4-A2050BE04975}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23E19BC3-8829-42BE-BCB4-A2050BE04975}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -138,11 +110,9 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BC90EFB4-1692-CBCC-EF52-778255F591E2} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{0E2DDF4A-A1FE-5424-03EA-7A8E76751354} = {B36A84DF-456D-A817-6EDD-3EC3E7F6E11F}
{9C4AFB5E-ED75-909E-FC61-1BD17BB6BC23} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{2BDCF8E8-DD35-3BA0-0148-5A2588335678} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{D6F509B1-C990-0533-2DD1-CFFBA7506249} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{23E19BC3-8829-42BE-BCB4-A2050BE04975} = {B36A84DF-456D-A817-6EDD-3EC3E7F6E11F}
{267842B2-D915-4B9E-8448-F9B5816D4A0A} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890} = {F8E63A4F-7D3E-4B2A-9C1D-8A5F6E9B3C2D}
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2026-03-03

### Breaking Changes
- Removed `BitcoinKernel` facade package and `KernelLibrary` fluent builder; consumers now use the managed API directly
- Renamed package/namespaces from `BitcoinKernel.Core` to `BitcoinKernel`
- `Chain` class moved from `BitcoinKernel.Abstractions` to `BitcoinKernel.Chain`; import `using BitcoinKernel.Chain;` to access it
- `BitcoinKernel.Abstractions` namespace renamed to `BitcoinKernel.Primatives`; update any `using BitcoinKernel.Abstractions;` imports accordingly
- `LoggingConnection` moved from the global namespace into `BitcoinKernel`; add `using BitcoinKernel;` if not already present

### Changed
- Examples rewritten to use the managed API directly without the fluent builder

## [0.1.2] - 2026-01-26

### Added
Expand Down
73 changes: 34 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
# BitcoinKernel.NET

.NET bindings and high-level library for [libbitcoinkernel](https://github.com/bitcoin/bitcoin/tree/master/src/kernel), providing access to Bitcoin Core's consensus and validation logic.
.NET bindings for [libbitcoinkernel](https://github.com/bitcoin/bitcoin/tree/master/src/kernel), providing access to Bitcoin Core's consensus and validation logic.

⚠️🚧 This library is still under construction. ⚠️🚧

⚠️🚧 This library is still under contruction. ⚠️🚧

This library uses [libbitcoinkernel](https://github.com/bitcoin/bitcoin/tree/master/src/kernel) which is in an experimental state, do not use for production purposes.

## Overview

BitcoinKernel.NET brings Bitcoin Core's robust consensus engine to .NET applications through a clean, idiomatic C# API. Built on top of libbitcoinkernel, it provides everything from low-level P/Invoke bindings to high-level abstractions for common Bitcoin operations.
This library uses [libbitcoinkernel](https://github.com/bitcoin/bitcoin/tree/master/src/kernel) which is in an experimental state, do not use for production purposes.

## Packages

| Package | Version | Description |
|---------|---------|-------------|
| **BitcoinKernel** | 0.1.2 | High-level API with fluent builder pattern |
| **BitcoinKernel.Core** | 0.1.2 | Managed wrappers and native bindings |


## Quick Start

### Installation
| **BitcoinKernel** | 0.2.0 | Managed wrappers and native bindings |

```bash
dotnet add package BitcoinKernel
```

or

```bash
dotnet add package BitcoinKernel.Core
```

## Architecture

The library is organized in three layers:
The library is organized in two layers:

1. **BitcoinKernel.Interop** - P/Invoke bindings to libbitcoinkernel (bundled, not published separately)
2. **BitcoinKernel.Core** - Managed C# wrappers with automatic memory management
3. **BitcoinKernel** - High-level facade with fluent API
2. **BitcoinKernel** - Managed C# wrappers with automatic memory management

```
┌─────────────────────────────┐
│ BitcoinKernel │ ← Fluent API, simple usage
│ (Facade Layer) │
└─────────────┬───────────────┘
┌─────────────▼───────────────┐
│ BitcoinKernel.Core │ ← Managed wrappers, IDisposable
│ BitcoinKernel │ ← Managed wrappers, IDisposable
│ (Wrapper Layer) │
└─────────────┬───────────────┘
┌─────────────▼───────────────┐
│ BitcoinKernel.Interop │ ← P/Invoke bindings
│ BitcoinKernel.Interop │ ← P/Invoke bindings (bundled)
│ (Binding Layer) │
└─────────────┬───────────────┘
Expand All @@ -63,26 +40,44 @@ The library is organized in three layers:
└─────────────────────────────┘
```

## Quick Start

```csharp
using BitcoinKernel;
using BitcoinKernel.Chain;
using BitcoinKernel.Interop.Enums;

using var logging = new LoggingConnection((category, message, level) =>
Console.WriteLine($"[{category}] {message}"));

using var chainParams = new ChainParameters(ChainType.MAINNET);
using var contextOptions = new KernelContextOptions().SetChainParams(chainParams);
using var context = new KernelContext(contextOptions);
using var options = new ChainstateManagerOptions(context, dataDir, blocksDir);
using var chainstate = new ChainstateManager(context, chainParams, options);

var chain = chainstate.GetActiveChain();
Console.WriteLine($"Height: {chain.Height}");
Console.WriteLine($"Genesis: {Convert.ToHexString(chain.GetGenesis().GetBlockHash())}");
```

## Examples

Explore the [examples](examples/) directory for complete working samples:

- **[BasicUsage](examples/BasicUsage/)** - Getting started with the high-level API
- **[BlockProcessing](examples/BlockProcessing/)** - Block validation and chain management
- **[BasicUsage](examples/BasicUsage/)** - Getting started with chain queries
- **[BlockProcessing](examples/BlockProcessing/)** - Block validation and processing

## Tools

### Kernel Bindings Test Handler

A conformance test handler for Kernel bindings Test handler framework, see [tools/kernel-bindings-test-handler](tools/kernel-bindings-test-handler/) for details.
A conformance test handler for the Kernel bindings test framework, see [tools/kernel-bindings-test-handler](tools/kernel-bindings-test-handler/) for details.

**Usage:**
```bash
dotnet run --project tools/kernel-bindings-test-handler
```

The handler communicates via stdin/stdout and is designed for automated conformance testing.

## Building from Source

### Prerequisites
Expand All @@ -109,7 +104,7 @@ This package includes pre-built `libbitcoinkernel` binaries for:
- macOS (x64, ARM64)
- others will follow

For other platforms, for now, you'll need to build libbitcoinkernel from the [Bitcoin Core repository](https://github.com/bitcoin/bitcoin).
For other platforms, you'll need to build libbitcoinkernel from the [Bitcoin Core repository](https://github.com/bitcoin/bitcoin).

## Documentation

Expand All @@ -135,4 +130,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file

- Built on [libbitcoinkernel](https://github.com/bitcoin/bitcoin/tree/master/src/kernel) from Bitcoin Core

**Note**: This library provides access to Bitcoin Core's consensus engine. The libbitcoinkernel and this package is stil experimental and not ready for production use.
**Note**: This library provides access to Bitcoin Core's consensus engine. libbitcoinkernel and this package are still experimental and not ready for production use.
135 changes: 61 additions & 74 deletions examples/BasicUsage/Program.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,63 @@
using System;
using BitcoinKernel;

namespace FacadeExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Bitcoin Kernel Basic Builder Example ===\n");

FullChainstateExample();

}

static void FullChainstateExample()
{
Console.WriteLine("2. Full Chainstate Example:");

Console.WriteLine(" Creating builder...");
var builder = KernelLibrary.Create()
.ForMainnet()
.WithWorkerThreads(2)
.WithDirectories("/tmp/regtest-data2", "/tmp/regtest-data/blocks2");

Console.WriteLine(" Configuring logging...");
builder = builder.WithLogging((category, message, level) =>
{
if (level <= (int)BitcoinKernel.Interop.Enums.LogLevel.INFO) // Only INFO and above
Console.WriteLine($" [{category}] {message}");
});

Console.WriteLine(" Building kernel...");
using var kernel = builder.Build();

Console.WriteLine(" Kernel built successfully!");
Console.WriteLine(" ✓ Chainstate initialized automatically");

// Process blocks
try
{

Console.WriteLine(" ✓ Ready to process blocks");

// Show new query methods
Console.WriteLine($" Chain height: {kernel.GetChainHeight()}");
Console.WriteLine($" Genesis hash: {Convert.ToHexString(kernel.GetGenesisBlockHash())}");

if (kernel.GetChainHeight() > 0)
{
var tipHash = kernel.GetChainTipHash();
Console.WriteLine($" Tip hash: {Convert.ToHexString(tipHash)}");

var blockInfo = kernel.GetBlockInfo(0);
if (blockInfo != null)
{
Console.WriteLine($" Block 0 hash: {Convert.ToHexString(blockInfo.Hash)}");
}
}

Console.WriteLine(" ✓ Chain queries working");
}
catch (Exception ex)
{
Console.WriteLine($" ✗ Error: {ex.Message}");
using BitcoinKernel;
using BitcoinKernel.Chain;
using BitcoinKernel.Interop.Enums;

namespace BasicUsage;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Bitcoin Kernel Basic Usage Example ===\n");
FullChainstateExample();
}

static void FullChainstateExample()
{
Console.WriteLine("Creating kernel...");

var dataDir = "/tmp/regtest-data2";
var blocksDir = "/tmp/regtest-data/blocks2";

using var logging = new LoggingConnection((category, message, level) =>
{
if (level <= (int)LogLevel.INFO)
Console.WriteLine($" [{category}] {message}");
});

using var chainParams = new ChainParameters(ChainType.MAINNET);
using var contextOptions = new KernelContextOptions().SetChainParams(chainParams);
using var context = new KernelContext(contextOptions);
using var options = new ChainstateManagerOptions(context, dataDir, blocksDir)
.SetWorkerThreads(2);
using var chainstate = new ChainstateManager(context, chainParams, options);

Console.WriteLine(" Kernel created successfully!");

try
{
var chain = chainstate.GetActiveChain();
Console.WriteLine($" Chain height: {chain.Height}");
Console.WriteLine($" Genesis hash: {Convert.ToHexString(chain.GetGenesis().GetBlockHash())}");

if (chain.Height > 0)
{
var tip = chain.GetTip();
Console.WriteLine($" Tip hash: {Convert.ToHexString(tip.GetBlockHash())}");

var genesis = chain.GetBlockByHeight(0);
if (genesis != null)
Console.WriteLine($" Block 0 hash: {Convert.ToHexString(genesis.GetBlockHash())}");
}


Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();

kernel.Dispose();
Console.WriteLine(" Kernel disposed.");
}
}
}
Console.WriteLine(" Chain queries working");
}
catch (Exception ex)
{
Console.WriteLine($" Error: {ex.Message}");
}

Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
Loading
Loading