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?
4
  • Yes, this is certainly possible to do. The blockchain contains all the information needed. Commented Jun 15, 2015 at 22:16
  • @GregHewgill how he will query blockchain with NBitcoin? Commented Jun 16, 2015 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. Commented Jun 16, 2015 at 1:20

2 Answers 2

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

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

Disclaimer: I am the developer.

3
  • Just a note: when I click on GitHub/Twitter/Facebook or any contact link nothing happens.
    – nopara73
    Commented Oct 26, 2017 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
    Commented Nov 13, 2021 at 4:17
  • @kdas # All deposits to bitmex https://blockchainsql.io/rR1gE3 # All deposits to bitmex of at least 1 btc https://blockchainsql.io/FkuyZ7 Commented Dec 7, 2021 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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