7

Is there a way to scan the Bitcoin blockchain in C# without using an external API? I am looking for the amount of spendable money of a Bitcoin address.

I am using NBitcoin.

// create new key pair based on private key
NBitcoin.Key key = new NBitcoin.Key(privateKey, privateKey.Length, false);

// parse blk*.dat
// get balance for address key.PubKey.Hash
// How to do that?
Pa Ddy
  • 71
  • 1
  • 2

2 Answers2

4

How about running a QBitNinja server locally?

var client = new QBitNinjaClient(baseAddress:"specify host here, you probably want localhost", Network.TestNet);
var balanceModel = client.GetBalance(dest: [Add any IDestination here, like new BitcoinAddress("mivD5GHroixrzgjv6Ww73pV5R55PcL8JdM", Network.TestNet)], unspentOnly: true).Result;
if (balanceModel.Operations.Count == 0) 
    throw new Exception("No coins to spend");
var unspentCoins = new List<Coin>();
foreach (var operation in balanceModel.Operations)
            unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as Coin)); 
var balance = unspentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
nopara73
  • 796
  • 5
  • 21
0

You can always use http://blockchainsql.io to SQL query the Bitcoin blockchain.

Disclaimer: I am the developer.

  • Just a note: when I click on GitHub/Twitter/Facebook or any contact link nothing happens. – nopara73 Oct 26 '17 at 14:58
  • @herman Can you provide a sample query to show how a getbalance equivalent query can be done on the sql database. For e.g in bitcoin cli console I run below set of commands to find balance of an address 1PWC7PNHL1SgvZaN7xEtygenKjWobWsCuf as below `createwallet "wallet-1" true loadwallet wallet-1 importaddress "1PWC7PNHL1SgvZaN7xEtygenKjWobWsCuf" "" false getbalance` – kdas Nov 13 '21 at 04:17
  • @kdas # All deposits to bitmex [https://blockchainsql.io/rR1gE3](https://blockchainsql.io/rR1gE3) # All deposits to bitmex of at least 1 btc [https://blockchainsql.io/FkuyZ7](https://blockchainsql.io/FkuyZ7) – Herman Schoenfeld Dec 07 '21 at 13:04