Recovery wallet.dat after delete from hdd (python script)

operator17
Posts: 6
Joined: Thu Jul 06, 2017 3:00 pm
os: linux

Recovery wallet.dat after delete from hdd (python script)

Post by operator17 » Thu Jul 06, 2017 3:14 pm

I am fighting with recovery my NMC wallet > (private key)
I was found this python script:

Code: Select all

#!/usr/bin/python

import binascii
import os
import hashlib
import sys

# bytes to read at a time from file (10meg)
readlength=10*1024*1024

if len(sys.argv)!=2:
  print "./keyhunter.py <filename>"
  exit()

filename = sys.argv[1]

f = open(filename)
magic = '\x01\x30\x82\x01\x13\x02\x01\x01\x04\x20'
magiclen = len(magic)



##### start code from pywallet.py #############
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

def b58encode(v):
  """ encode v, which is a string of bytes, to base58.
  """

  long_value = 0L
  for (i, c) in enumerate(v[::-1]):
    long_value += (256**i) * ord(c)

  result = ''
  while long_value >= __b58base:
    div, mod = divmod(long_value, __b58base)
    result = __b58chars[mod] + result
    long_value = div
  result = __b58chars[long_value] + result

  # Bitcoin does a little leading-zero-compression:
  # leading 0-bytes in the input become leading-1s
  nPad = 0
  for c in v:
    if c == '\0': nPad += 1
    else: break

  return (__b58chars[0]*nPad) + result

def Hash(data):
  return hashlib.sha256(hashlib.sha256(data).digest()).digest()

def EncodeBase58Check(secret):
  hash = Hash(secret)
  return b58encode(secret + hash[0:4])

########## end code from pywallet.py ############



# read through target file
# one block at a time
while True:
  data = f.read(readlength)
  if not data:
    break

  # look in this block for keys
  x=0
  while True:
    # find the magic number
    pos=data.find(magic,x)
    #pos=data.find('\13\02\01\01\04\20',0)
    if pos==-1:
      break
    print EncodeBase58Check('\x80'+data[pos+magiclen:pos+magiclen+32])
    x+=(pos+1)
  
  # are we at the end of the file?
  if len(data) < readlength:
    break

  # make sure we didn't miss any keys at the end of the block
  f.seek(f.tell()-(32+magiclen))

# code grabbed from pywallet.py


It's working, but is for BTC, so the format of output privat key is incorrect.
Anyone can help witch adaptation for NMC format?

biolizard89
Posts: 1971
Joined: Tue Jun 05, 2012 6:25 am
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by biolizard89 » Fri Jul 07, 2017 8:24 am

I suspect that this line:

Code: Select all

print EncodeBase58Check('\x80'+data[pos+magiclen:pos+magiclen+32])
Is the one that needs to be changed. 0x80 == 128. Note the presence of 128 here: https://github.com/namecoin/namecoin-co ... s.cpp#L136 , and note that it's changed to 180 here: https://github.com/namecoin/namecoin-co ... s.cpp#L169 . 180 == 0xb4. So it might be as easy as replacing '\x80' with '\xb4'.

Let us know if that helps.
Jeremy Rand, Lead Namecoin Application Engineer
NameID: id/jeremy
DyName: Dynamic DNS update client for .bit domains.

Donations: BTC 1EcUWRa9H6ZuWPkF3BDj6k4k1vCgv41ab8 ; NMC NFqbaS7ReiQ9MBmsowwcDSmp4iDznjmEh5

operator17
Posts: 6
Joined: Thu Jul 06, 2017 3:00 pm
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by operator17 » Fri Jul 07, 2017 2:47 pm

unfortunately no...
in oryginal script (bitcoin version) every recovery private-key is starting from 5K (5Kxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
after change to \xb4 is "74xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
IMHO should be "5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
any other idea?

here is few empty wallets:

Code: Select all

dumpprivkey NKhnrWFD2p1o9KwjpTksg3JBJCCLBt7ECd
5KYDw7RTQrBwjJg2GTqkPtWe7eXKmCSdKEc5KE9thiGPnBcME7C

dumpprivkey N866PG7iifPA9WHafUGRXkoCgMuk3konye
5HqeTraeULEMaBvXyZvPhZ1W5TuQMdoaVh8DxWKhxy7LmHUrVun

dumpprivkey N2vNkx21Ni4ELCKj7Uagx2ebXchnA822gc
5Jzn6SNAGVEvCTuHTy8wwnh6j8RuiLtQpQMz2a8Gy5HMYT2A9K5

dumpprivkey N7u8KwasRHiDNbTsRq9gVigSLuB3W7JQbj
5HtGH2TnjJEDCGSbd8aHS3mSHh2br9GR9MGR52TFeNs6SmM3aGT

dumpprivkey MyoKUADnGDda7TsKs4urJNyzervR7c7oGp
5KYmprJdF3xLDdMxwq9hukCMaQTnfdHMo38QetAcLp19n3hhUyo

biolizard89
Posts: 1971
Joined: Tue Jun 05, 2012 6:25 am
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by biolizard89 » Sat Jul 08, 2017 1:06 am

operator17 wrote:unfortunately no...
in oryginal script (bitcoin version) every recovery private-key is starting from 5K (5Kxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
after change to \xb4 is "74xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
IMHO should be "5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
any other idea?

here is few empty wallets:

Code: Select all

dumpprivkey NKhnrWFD2p1o9KwjpTksg3JBJCCLBt7ECd
5KYDw7RTQrBwjJg2GTqkPtWe7eXKmCSdKEc5KE9thiGPnBcME7C

dumpprivkey N866PG7iifPA9WHafUGRXkoCgMuk3konye
5HqeTraeULEMaBvXyZvPhZ1W5TuQMdoaVh8DxWKhxy7LmHUrVun

dumpprivkey N2vNkx21Ni4ELCKj7Uagx2ebXchnA822gc
5Jzn6SNAGVEvCTuHTy8wwnh6j8RuiLtQpQMz2a8Gy5HMYT2A9K5

dumpprivkey N7u8KwasRHiDNbTsRq9gVigSLuB3W7JQbj
5HtGH2TnjJEDCGSbd8aHS3mSHh2br9GR9MGR52TFeNs6SmM3aGT

dumpprivkey MyoKUADnGDda7TsKs4urJNyzervR7c7oGp
5KYmprJdF3xLDdMxwq9hukCMaQTnfdHMo38QetAcLp19n3hhUyo
I just used dumpprivkey on one of my Namecoin addresses, and the result started with 7. What version of Namecoin are you using? (I'm on 0.13.99-name-tab-beta1.)
Jeremy Rand, Lead Namecoin Application Engineer
NameID: id/jeremy
DyName: Dynamic DNS update client for .bit domains.

Donations: BTC 1EcUWRa9H6ZuWPkF3BDj6k4k1vCgv41ab8 ; NMC NFqbaS7ReiQ9MBmsowwcDSmp4iDznjmEh5

operator17
Posts: 6
Joined: Thu Jul 06, 2017 3:00 pm
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by operator17 » Sat Jul 08, 2017 5:25 am

biolizard89 wrote: I just used dumpprivkey on one of my Namecoin addresses, and the result started with 7. What version of Namecoin are you using? (I'm on 0.13.99-name-tab-beta1.)
old one
0.3.80

domob
Posts: 1100
Joined: Mon Jun 24, 2013 11:27 am
Contact:

Re: Recovery wallet.dat after delete from hdd (python script

Post by domob » Mon Jul 10, 2017 3:38 pm

The difference might be between compressed and uncompressed keys - the old client only has uncompressed keys, while the new client uses compressed ones.
BTC: 1domobKsPZ5cWk2kXssD8p8ES1qffGUCm | NMC: NCdomobcmcmVdxC5yxMitojQ4tvAtv99pY
BM-GtQnWM3vcdorfqpKXsmfHQ4rVYPG5pKS
Use your Namecoin identity as OpenID: https://nameid.org/

operator17
Posts: 6
Joined: Thu Jul 06, 2017 3:00 pm
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by operator17 » Tue Jul 11, 2017 12:44 pm

What I should remove from script to disable "compression" in them?

biolizard89
Posts: 1971
Joined: Tue Jun 05, 2012 6:25 am
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by biolizard89 » Tue Jul 18, 2017 2:16 am

operator17 wrote:
biolizard89 wrote: I just used dumpprivkey on one of my Namecoin addresses, and the result started with 7. What version of Namecoin are you using? (I'm on 0.13.99-name-tab-beta1.)
old one
0.3.80
May I ask why you're not using a newer release? It's entirely possible that the issue you're encountering is a bug that was fixed since 0.3.80.
Jeremy Rand, Lead Namecoin Application Engineer
NameID: id/jeremy
DyName: Dynamic DNS update client for .bit domains.

Donations: BTC 1EcUWRa9H6ZuWPkF3BDj6k4k1vCgv41ab8 ; NMC NFqbaS7ReiQ9MBmsowwcDSmp4iDznjmEh5

operator17
Posts: 6
Joined: Thu Jul 06, 2017 3:00 pm
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by operator17 » Sat Jul 22, 2017 8:18 am

biolizard89 wrote: May I ask why you're not using a newer release? It's entirely possible that the issue you're encountering is a bug that was fixed since 0.3.80.
this is old hdd, from drawer
in this time 0.3.80 was a newset version

biolizard89
Posts: 1971
Joined: Tue Jun 05, 2012 6:25 am
os: linux

Re: Recovery wallet.dat after delete from hdd (python script

Post by biolizard89 » Sat Jul 29, 2017 4:30 pm

operator17 wrote:
biolizard89 wrote: May I ask why you're not using a newer release? It's entirely possible that the issue you're encountering is a bug that was fixed since 0.3.80.
this is old hdd, from drawer
in this time 0.3.80 was a newset version
You're trying to import the recovered private keys into a new wallet, right? Why not import them into a current Namecoin Core wallet?
Jeremy Rand, Lead Namecoin Application Engineer
NameID: id/jeremy
DyName: Dynamic DNS update client for .bit domains.

Donations: BTC 1EcUWRa9H6ZuWPkF3BDj6k4k1vCgv41ab8 ; NMC NFqbaS7ReiQ9MBmsowwcDSmp4iDznjmEh5

Post Reply