19

I have made paper wallets. And I continually send what bitcoins I get to them. I'm paranoid that I've been sending bitcoins to the paper bitcoin addresses where somehow at sometime my saved private keys got corrupted. So from time to time I want to run a script, python or whatever, to check if my private keys do correspond to the public address that I've been sending bitcoins to. I want an offline solution so things like the satoshi client would not do. Is there a small script written by someone that can solve this?

  • Satoshi doesn't need to be online to generate keys or to import them to your wallet. – Nick ODell Mar 1 '13 at 21:13
  • 2
    I'm sorry for you. This level of paranoia results more often in errors your side, than the risk of getting hacked if you do everything in a normal way... – o0'. Mar 1 '13 at 23:21
  • For those who want to implement this in their programming language, there is a interactive online tool here gobittest.appspot.com/PrivateKey (obviously don't use your real private keys here but get one from Directory.io) – mixdev Jun 8 '17 at 7:38
5

Bitcoin-bash-tools has what you need.

For Python, numtowif() below, from a post by forum user flatfly, does that as well:

import hashlib, binascii

t='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

def numtowif(numpriv):
 step1 = '80'+hex(numpriv)[2:].strip('L').zfill(64)
 step2 = hashlib.sha256(binascii.unhexlify(step1)).hexdigest()
 step3 = hashlib.sha256(binascii.unhexlify(step2)).hexdigest()
 step4 = int(step1 + step3[:8] , 16)
 return ''.join([t[step4/(58**l)%58] for l in range(100)])[::-1].lstrip('1')

def wiftonum(wifpriv):
 return sum([t.index(wifpriv[::-1][l])*(58**l) for l in range(len(wifpriv))])/(2**32)%(2**256)

def validwif(wifpriv):
 return numtowif(wiftonum(wifpriv))==wifpriv

print numtowif(0x0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D)
print hex(wiftonum('5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'))
print validwif('5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ')
print validwif('5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTK')

Also, Armory client has some C++ that can be compiled and easily accessed from Python which provides this.

  • 2
    Well, that 'numtowif()' gave me the key in Wallet Import Format, not the address... – lvella Oct 17 '15 at 17:58
  • +1 but this code will not work with Python3. I've managed to work with it. Thanks. – Chiheb Nexus Dec 29 '16 at 21:25
8

I'd check out the python library PyBitcoin (https://github.com/blockstack/pybitcoin or "pip install pybitcoin").

It's pretty simple to get the address from the wif private key:

>>> from pybitcoin import BitcoinPrivateKey
>>> private_key = BitcoinPrivateKey()
>>> private_key.public_key().address()
'1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T'

You can also derive a private key object from it's raw hex representation:

>>> private_key_2 = BitcoinPrivateKey('91149ee24f1ee9a6f42c3dd64c2287781c8c57a6e8e929c80976e586d5322a3d')

P.S. This library also works with Litecoin, Namecoin and a bunch of other coins.

Disclosure: I'm one of the creators.

  • Thanks! If only you had check balance and send functionality. Would be awesome. – HaltingState Feb 25 '14 at 1:54
  • Glad to hear you like it! We'll add that functionality soon enough and of course we welcome all contributions. – Ryan Feb 25 '14 at 5:17
  • 1
    I couldn't get the code above to work. However, poking through the source code I found the following that worked: >>> from pycoin.key import Key >>> k = Key.from_text("<yourWIFhere>") >>> k.address() – Keir Finlow-Bates Sep 1 '17 at 18:50
2

If I understand your problem correctly and you just want to calculate the address of a given private key, you could use brainwallet.org for that. The website is Javascript-based so you can just load the website and disconnect from the internet. If you want it even safer, you could download the source code and open the website locally on an offline machine.

Brainwallet.org has quite a lot little useful address tools.

2

Offhand it looks like pycoin is more active thanpybitcoin. So I'll put Keir Finlow-Bates' code from a comment above into an answer by itself:

from pycoin.key import Key
k = Key.from_text("<yourWIFhere>")
k.address()
0

its very simple.

just download ofline bitaddress.org page (for security, other wise online will also work, but there could be security risk )and click on wallet details tab.

enter your private key and you will get your corresponding public key.

  • 1
    They want to run a script, not do a manual process. – morsecoder Jun 22 '15 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.