A lightweight .NET library for reading raw Bitcoin block files produced by Bitcoin Core.
- Reads Bitcoin Core
blk*.datblock files - Enumerates blocks in file order or in blockchain height order
- Resumes parsing from a block file and byte position
- Detects outputs types
- Detects addresses in outputs
- Interprets scripts
- Supports SegWit and Taproot
- Supports mainnet, testnet, and regtest
- Supports file XOR-deobfuscation (Bitcoin Core 28.0 or newer)
- Zero dependencies
- A local Bitcoin Core
blocksdirectory containingblk*.datfiles - Sufficient disk access for the block files
dotnet add package BitcoinBlockchainParserPass the Bitcoin Core blocks directory to Blockchain. Mainnet is selected by default.
using BitcoinBlockchainParser;
var blockchain = new Blockchain(@"C:\Bitcoin\blocks");
foreach (var block in blockchain.EnumerateBlocks().Take(5))
{
Console.WriteLine($"Block: {block.Id}");
Console.WriteLine($"Time: {block.Time:o}");
foreach (var tx in block.Transactions.Take(3))
{
Console.WriteLine(
$" TX: {tx.TXID}; inputs: {tx.Inputs.Length}; outputs: {tx.Outputs.Length}");
}
}EnumerateBlocks() yields blocks as they appear in blk*.dat files. This is the fastest option when blockchain order is not required.
foreach (var block in blockchain.EnumerateBlocks())
{
// block: BlockFiled
// block.FilePosition contains the source file number and byte position
}You can start at a specific file and byte position:
foreach (var block in blockchain.EnumerateBlocks(
startFile: 100,
position: 1024))
{
Console.WriteLine(block.Id);
}For a single block file:
foreach (var block in blockchain.EnumerateFileBlocks(file: 100))
{
Console.WriteLine(block.Id);
}EnumerateBlocksOrdered() yields blocks in chain order and includes the calculated block height.
foreach (var block in blockchain.EnumerateBlocksOrdered().Skip(50).Take(20))
{
Console.WriteLine($"#{block.Height} {block.Id}");
}Ordered parsing needs to connect block hashes and may buffer out-of-order blocks while traversing the available files.
foreach (var block in blockchain.EnumerateBlocks().Take(1))
{
foreach (var tx in block.Transactions)
{
foreach (var output in tx.Outputs)
{
Console.WriteLine($"Type: {output.Type}");
Console.WriteLine($"Address: {output.Address}");
Console.WriteLine($"Sats: {output.Amount}");
Console.WriteLine($"Script: {output.ScriptPubKey}");
}
}
}TxOutput.Amount is expressed in satoshis. TxOutput.Address may be null for scripts that do not map to a supported address format, such as OP_RETURN or an unknown script.
Mainnet is the default. Specify another network explicitly:
var mainnet = new Blockchain(@"C:\Bitcoin\blocks", Network.Mainnet);
var testnet = new Blockchain(@"C:\BitcoinTestnet\blocks", Network.Testnet3);
var regtest = new Blockchain(@"C:\BitcoinRegtest\blocks", Network.Regtest);The network affects address encoding and block magic validation.
A runnable console example is available at ExampleApp/Program.cs.
EnumerateBlocks()andEnumerateBlocksOrdered()are lazy; each call starts reading from the source files.- Block IDs and transaction IDs are returned as reversed-display hex strings by
HashId.Id. - Bitcoin Core 28.0+ may store obfuscated block files. The library automatically looks for
xor.datin the supplied blocks directory. - Parsing a pruned Bitcoin Core installation provides only blocks that are still present locally.
MIT - see LICENSE.