How do I read the contents of wallet.dat in C#

7

4

I'd like to be able to add, remove, export etc. keypairs from my wallet.dat file. Looking over the source code of pywallet it doesn't look like such a tough task, but of course it's not as simple as from bsddb.db import * in C# and Google is failing me. Does anyone have some sample code or resources for how to handle wallet.dat in C#?

David Perry

Posted 2011-09-10T05:00:28.867

Reputation: 13 848

Answers

4

Wallet.dat is a Berkeley DB file. You could check out Berkeley DB for .NET to see if it is able to read the file.

Erv Walter

Posted 2011-09-10T05:00:28.867

Reputation: 636

There are precompiled binaries in the Files section of the sourceforge project you could try.Erv Walter 2011-09-11T14:19:47.967

The precompiled binaries worked well enough. If I figure it out 100% I'll start a github repo and link to it here.David Perry 2011-09-16T04:29:36.230

For some reason I can't build this. I get a ton of these type of error: Error 1 Inconsistent accessibility: parameter type 'out BerkeleyDb.DB_SEQUENCE_STAT*' is less accessible than delegate 'BerkeleyDb.DB_SEQUENCE.StatFcn' C:\Users\enmaku\Documents\Visual Studio 2010\Projects\libdb-dotnet\src\DB_SEQUENCE.cs 146 30 libdb_dotNET45David Perry 2011-09-10T19:17:20.197

VS2010 Win7 x64 if it matters.David Perry 2011-09-10T19:19:08.033

1

In addition to what Erv mentioned, the next version of the wallet may be using LevelDB for the block chain and some custom format for the private key store.

Here is more information on the LevelDB access from C# but I hope someone updates this post with a link to the best LevelDB for C# for our purposes

goodguys_activate

Posted 2011-09-10T05:00:28.867

Reputation: 11 898

0

Take a look at BitcoinSharp, a port of bitcoinj.

ripper234

Posted 2011-09-10T05:00:28.867

Reputation: 25 192

BitcoinSharp and bitcoinj use a different wallet format than using by bitcoin-core.DeathAndTaxes 2014-08-04T15:58:59.997

I hadn't considered that BitcoinSharp, being a direct port of Bitcoin to C# would, by necessity, contain the necessary code to handle wallet.dat (duh). I'd much rather have a small snippet of sample code than dig through the whole BitcoinSharp project, but given that Erv's link goes to unbuildable code this may be my only choice...David Perry 2011-09-10T19:18:46.473

And now I get The input stream is not a valid binary format opening a perfectly valid wallet.dat with Wallet.LoadFromFile - has anyone actually USED this before?David Perry 2011-09-10T20:09:13.947

Glancing over the code it looks like BitcoinSharp just serializes and deserializes its wallet object with a BinaryFormatter, it doesn't appear to use the Berkeley DB format that the regular Bitcoin client does, so this is unfortunately a dead end :(David Perry 2011-09-10T20:25:44.680