How do I get the public bitcoin address from a given private key in wallet import format offline?

19

6

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?

user299648

Posted 2013-03-01T20:13:21.620

Reputation: 401

Satoshi doesn't need to be online to generate keys or to import them to your wallet.Nick ODell 2013-03-01T21:13:02.743

2I'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'. 2013-03-01T23:21:30.550

For those who want to implement this in their programming language, there is a interactive online tool here http://gobittest.appspot.com/PrivateKey (obviously don't use your real private keys here but get one from Directory.io)

mixdev 2017-06-08T07:38:33.567

Answers

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.

Stephen Gornick

Posted 2013-03-01T20:13:21.620

Reputation: 26 118

2Well, that 'numtowif()' gave me the key in Wallet Import Format, not the address...lvella 2015-10-17T17:58:38.013

+1 but this code will not work with Python3. I've managed to work with it. Thanks.Chiheb Nexus 2016-12-29T21:25:37.713

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.

Ryan

Posted 2013-03-01T20:13:21.620

Reputation: 737

Thanks! If only you had check balance and send functionality. Would be awesome.HaltingState 2014-02-25T01:54:04.497

Glad to hear you like it! We'll add that functionality soon enough and of course we welcome all contributions.Ryan 2014-02-25T05:17:07.333

1I 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 2017-09-01T18:50:17.173

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.

Steven Roose

Posted 2013-03-01T20:13:21.620

Reputation: 10 855

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

nealmcb

Posted 2013-03-01T20:13:21.620

Reputation: 1 798

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.

DIn

Posted 2013-03-01T20:13:21.620

Reputation: 11

1They want to run a script, not do a manual process.morsecoder 2015-06-22T17:25:55.483