How to open local bitcoin database
Asked Answered
O

3

7

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

Ophicleide answered 7/12, 2013 at 14:50 Comment(1)
you may use any parser for *.dat files, for example my parser github.com/ragestack/blockchain-parserResearch
E
7

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.

Endothermic answered 6/7, 2014 at 22:19 Comment(2)
I found that the easiest way to read bitcoin block database is through JSON RPC calls to bitcoin client or bitcoind. My question was asked 6 month ago :)Ophicleide
I think he could be looking for all the info / any, and just want to know the idea how to get any data from blockchain files.Galicia
O
3

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();
    }
}
Ophicleide answered 1/1, 2018 at 21:53 Comment(0)
E
1

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.

Excelsior answered 10/6, 2015 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.