Question

I am trying to extract data from local bitcoin database. As I know, bitcoin-qt is using BerkeleyDB. I have installed berkley db from Oracle web site, and found here dll for .NET: libdb_dotnet60.dll. I am trying to open any file, but I get DatabaseException. Here is my code:

using BerkeleyDB;
class Program
{
    static void Main(string[] args)
    {
        var btreeConfig = new BTreeDatabaseConfig();
        var btreeDb = BTreeDatabase.Open(@"c:\Users\<user>\AppData\Roaming\Bitcoin\blocks\blk00000.dat", btreeConfig);
    }
}

Does anyone has examples how to work with bitcoin database (on any other language)?

Was it helpful?

Solution

What are you trying to extract? Only the wallet.dat file is Berkeley database.

Blocks are stored one after the other in the blkxxxxx.dat files with four bytes representing a network identifier and four bytes giving the block size, before each block.

An index for unspent outputs in stored as a leveldb database.

Knowing what type of information you are looking for would help.

OTHER TIPS

In .NET you could use something like BitcoinBlockchain that is available as a NuGet package at https://www.nuget.org/packages/BitcoinBlockchain/. Its usage is trivial. If you want o see how it is implemented the sources are available on GitHub.

If you want to store the blockchain in a SQL database that you could query faster and in more ways that the raw blockchain you could use something like the BitcoinDatabaseGenerator tool available at https://github.com/ladimolnar/BitcoinDatabaseGenerator.

There is library NBitcoin: https://github.com/MetacoSA/NBitcoin

How to enumerate blocks:

var store = new BlockStore(@"C:\Bitcoin\blocks\", Network.Main);
// this loop will enumerate all blocks ordered by height starting with genesis block
foreach (var block in store.EnumerateFolder())
{
    var item = block.Item;
    string blockID = item.Header.ToString();
    foreach (var tx in item.Transactions)
    {
        string txID = tx.GetHash().ToString();
        string raw = tx.ToHex();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top