Query LOCAL Bitcoin blockchain with C# .NET

Query LOCAL Bitcoin blockchain with C# .NET

I am trying to check the balance of a given Bitcoin address by using ONLY the locally stored blockchain (downloaded via Bitcoin Core). Something similar to this (by using NBitCoin and/or QBitNinja), but without needing access to the network:

private static readonly QBitNinjaClient client = new QBitNinjaClient(Network.Main);

public decimal CheckBalance(BitcoinPubKeyAddress address) { var balanceModel = client.GetBalance(address, true).Result; decimal balance = 0;

if (balanceModel.Operations.Count > 0)
{
    var unspentCoins = new List<Coin>();
    foreach (var operation in balanceModel.Operations)
        unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin));
    balance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
}
return balance;

}

The example above needs access to the network. I need to do the same thing offline. I came up with something like this, but obviously it doesn't work:

public decimal CheckBalanceLocal(BitcoinPubKeyAddress address)
{
    var node = Node.ConnectToLocal(Network.Main);
    node.VersionHandshake();
    var chain = node.GetChain();

var store = new BlockStore(@"F:\Program Files\Bitcoin\Cache\blocks", Network.Main);

var index = new IndexedBlockStore(new InMemoryNoSqlRepository(), store);
index.ReIndex();

var headers = chain.ToEnumerable(false).ToArray();

var balance = (
    from header in headers
    select index.Get(header.HashBlock) 
    into block
    from tx in block.Transactions
    from txout in tx.Outputs
    where txout.ScriptPubKey.GetDestinationAddress(Network.Main) == address
    select txout.Value.ToDecimal(MoneyUnit.BTC)).Sum();

return balance;

}

  1. It hangs during the query
  2. I want something instead of InMemoryNoSqlRepository to be stored in a file in order to prevent using ReIndex() which slows everything down

My requirement is to Check Balance the same way as in the first method but by querying blocks stored on my disk.

bitcoin c

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.