Zergatul
Zergatul

Reputation: 2010

How to open local bitcoin database

I am trying to extract data from local bitcoin database. As I know, bitcoin-qt is using BerkeleyDB. I have installed BerkleyDB from Oracle web site, and found there a DLL for .NET: libdb_dotnet60.dll. I am trying to open a file, but I get a 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 have examples how to work with a Bitcoin database (in any other language)?

Upvotes: 7

Views: 4472

Answers (3)

Zergatul
Zergatul

Reputation: 2010

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

Upvotes: 3

Ladi
Ladi

Reputation: 1294

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.

Upvotes: 1

Matthew Mitchell
Matthew Mitchell

Reputation: 5393

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.

Upvotes: 7

Related Questions