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?
  • Yes, this is certainly possible to do. The blockchain contains all the information needed. – Greg Hewgill Jun 15 '15 at 22:16
  • @GregHewgill how he will query blockchain with NBitcoin? – Igor Barinov Jun 16 '15 at 1:18
  • I'm afraid I don't know anything about how to use NBitcoin specifically. However, I've done exactly this with Python (for the blockchain of a different coin, but the structure is largely the same), so it's certainly possible. – Greg Hewgill Jun 16 '15 at 1:20

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));

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.