Author Topic: how to read wallet.dat  (Read 3820 times)

Mithril Man

  • Hero Member
  • *****
  • Posts: 589
    • View Profile
    • Mithril Man Web!
how to read wallet.dat
« on: February 05, 2015, 09:19:05 AM »
as i said in another post, i'm doing a C# utility to read wallet.dat to find the keys to be able then to import found keys into a new wallet
at the moment, i'm using a python script as reference to understand how to read the wallet.dat, to follow it's decoding function, etc... but i seems to fail to decode to the right address, domob can you tell me what's the main difference between bitcoin and huntercoin decrypt logic?

the reference is https://github.com/joric/pywallet/blob/master/pywallet.py
and i used even the current huntercoin code to try to link info but i think i missed something

i mean, actually i've successfull used the C# berkeley api to load the wallet DB and i can find correctly the various keys and i'm parsing the in the right way, because i can find, in the bdb Key field, the PublicKey correctly (65 byte) and from the bdb Value field, i can read correctly the (i think) private key, that's 279 bytes long

now, what to do with them?
actually this is what i do with the public key byte array:

- i do an Hash160 of the 65 bytes (this returns me a 20 byte array)
- then i append a byte depending if the wallet is from testnet or not (here i already changed from bitcoin to huntercoin define, if i'm in testnet i add 100 otherwise 40) so now i've 21 byte array here and i store it in a var (call it vh160)
- then i do a  SHA256 of vh160 byte array and store it in a variable (call it tmp). this variable is 32 byte long of course
- then i concat vh160 with the first 4 bytes of tmp, generating a 25 byte array and store it in a var (call it addr)
- now, as last step, i Encode in base 58 the addr variable, but the result is wrong


the wrong part are the lasts 6 bytes, because in a testnet wallet this is what i obtain:
he6ng2RwxfRkUR4oeUHBR4GMrDMku9ua5m

while the right address should be this one
he6ng2RwxfRkUR4oeUHBR4GMrDMkwdjT91


where do you think could be the problem?
Alternative GUI client for Huntercoin http://www.mithrilman.com
HUC donation: HMSCYGYJ5wo9FiniVU4pXWGUu8E8PSmoHE
BTC donation: 1DKLf1QKAZ5njucq37pZhMRG67qXDP3vPC

Mithril Man

  • Hero Member
  • *****
  • Posts: 589
    • View Profile
    • Mithril Man Web!
Re: how to read wallet.dat
« Reply #1 on: February 05, 2015, 10:30:14 AM »
i found it!!! i had to double SHA256 the hash to generate the 4 bytes checksum!

ok now i've to read the private key, the address alone isn't useful because i need the private key to use improtprivkey in the new wallet (i can't use dumpprivkey on the compromised wallet)
Alternative GUI client for Huntercoin http://www.mithrilman.com
HUC donation: HMSCYGYJ5wo9FiniVU4pXWGUu8E8PSmoHE
BTC donation: 1DKLf1QKAZ5njucq37pZhMRG67qXDP3vPC

domob

  • Developer
  • Sr. Member
  • *****
  • Posts: 284
    • View Profile
Re: how to read wallet.dat
« Reply #2 on: February 05, 2015, 01:04:48 PM »
Sounds like you're making good progress.  For the private key, note that the 279 bytes you see are not the secret alone (256 bit number = 32 bytes) but a serialised representation of the private key from OpenSSL.  The dumped private key (which you want to produce), on the other hand, is made from the secret alone.  I don't really know how to convert between those two, besides calling some OpenSSL function (or libsecp256k1 in the case of the bleeding-edge Bitcoin code).  Not sure if that's feasible for you.
Use your Namecoin-ID as OpenID: https://nameid.org/
Donations: 1domobKsPZ5cWk2kXssD8p8ES1qffGUCm | HBkxA5QmYSATFoPN1wFk8eBkgwPpY97Mfu

domob

  • Developer
  • Sr. Member
  • *****
  • Posts: 284
    • View Profile
Re: how to read wallet.dat
« Reply #3 on: February 05, 2015, 01:11:39 PM »
The code that does the conversion in Huntercoin (used when dumping a private key) is CKey::GetSecret in key.cpp.  The corresponding main OpenSSL function seems to be EC_KEY_get0_private_key.  Not sure if you can use that from your C#, but probably you can if there are OpenSSL bindings.
Use your Namecoin-ID as OpenID: https://nameid.org/
Donations: 1domobKsPZ5cWk2kXssD8p8ES1qffGUCm | HBkxA5QmYSATFoPN1wFk8eBkgwPpY97Mfu

Mithril Man

  • Hero Member
  • *****
  • Posts: 589
    • View Profile
    • Mithril Man Web!
Re: how to read wallet.dat
« Reply #4 on: February 05, 2015, 01:44:57 PM »
forget to update, i found priv key too! :)

wasn't easy but found it without 3rd party libs, now i can try to do what i've in mind!
Alternative GUI client for Huntercoin http://www.mithrilman.com
HUC donation: HMSCYGYJ5wo9FiniVU4pXWGUu8E8PSmoHE
BTC donation: 1DKLf1QKAZ5njucq37pZhMRG67qXDP3vPC

Mithril Man

  • Hero Member
  • *****
  • Posts: 589
    • View Profile
    • Mithril Man Web!
Re: how to read wallet.dat
« Reply #5 on: February 06, 2015, 10:59:56 AM »
finally i succeeded fixing my wallet!

I've done a C# utility that parse the wallet.dat using the barkeley provider and applying correct alghoritm to read the private key, then once i've read all the private key from the corrupted wallet, i've used them to call "importprivkey" RPC command (with false as 3rd parameter to prevent a rescan every key added) over an active huntercoind.exe instance that was using an empty wallet
at this point all my addresses were on the new wallet so i just launched again the daemon with the -rescan option and after a long time (it had to do the whole blockchain scan for every address i imported, and were a lot) it ended up with a fixed wallet!

now a strange thing, so here maybe domob can enlighten me, is that the original wallet was 195MB while the newly fixed wallet, after the rescan, is "just" 39 MB
i suppose many transaction hasn't been imported, but which? it seems working all fine, i've my full balance available and my currently alive hunters too, so i don't understand why this difference

this could make me thing that the wallet could even be pruned if a lot of info can be dropped!
maybe only unspend tx has been imported, so the difference are that on the original wallet the spent tx are still there?
Alternative GUI client for Huntercoin http://www.mithrilman.com
HUC donation: HMSCYGYJ5wo9FiniVU4pXWGUu8E8PSmoHE
BTC donation: 1DKLf1QKAZ5njucq37pZhMRG67qXDP3vPC

domob

  • Developer
  • Sr. Member
  • *****
  • Posts: 284
    • View Profile
Re: how to read wallet.dat
« Reply #6 on: February 06, 2015, 04:33:11 PM »
I have to admit that I also don't quite know where the difference in size comes from.  I don't think that only unspent tx got imported (but you can see this in the tx tab / with listtransactions).  But, of course, one could remove spent tx from the wallet if one doesn't care about the historical record.

Maybe the overhead (at least, some of it) is due to space reserved by BDB for future use.  It doesn't fully return freed space and keeps at least some to later fill in again.  I've never observed it to make such a difference, though.  You can use the BDB utils (they are called "db5.1_dump" and "db5.1_load" for me on Debian, not sure how they are available on Windows) to dump & reload the DB, then it usually gets smaller.  I do this for utxo.dat and blkindex.dat before creating a chain download, and observed up to a 50% decrease in size.
Use your Namecoin-ID as OpenID: https://nameid.org/
Donations: 1domobKsPZ5cWk2kXssD8p8ES1qffGUCm | HBkxA5QmYSATFoPN1wFk8eBkgwPpY97Mfu

Snailbrain

  • Developer
  • Hero Member
  • *****
  • Posts: 987
    • View Profile
Re: how to read walle
« Reply #7 on: October 26, 2015, 05:23:31 PM »
I had lost my wallet.dat and I don't know how can I get it back.
40 million coins in this wallet.dat,I don't know how to get those coins now.
Is anybody can help me.
Thanks very much.

This wallet had two times succeed receive coins.
The wallet never sent out coins to anybody/any address,
I can't find my wallet now. Can any one suggest me how to get those coins.

if you have lost your wallet.dat there isn't much you can do..

also there are only 10million coins in circulation atm, so maybe it was less than 40milion?