23

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?

4
  • Satoshi doesn't need to be online to generate keys or to import them to your wallet.
    – Nick ODell
    Mar 1, 2013 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, 2013 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, 2017 at 7:38
  • general warning with self-made paper wallets, make sure when you eventually use this address, you do a clean sweep, moving all the bitcoin from this address at once. If you try a "test" transaction, you may end up losing everything to a change address you don't control. Jun 23, 2021 at 14:39

7 Answers 7

4

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
  • 3
    Well, that 'numtowif()' gave me the key in Wallet Import Format, not the address...
    – lvella
    Oct 17, 2015 at 17:58
  • +1 but this code will not work with Python3. I've managed to work with it. Thanks. Dec 29, 2016 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.

3
  • Thanks! If only you had check balance and send functionality. Would be awesome. Feb 25, 2014 at 1:54
  • Glad to hear you like it! We'll add that functionality soon enough and of course we welcome all contributions.
    – Ryan Shea
    Feb 25, 2014 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() Sep 1, 2017 at 18:50
3

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

1
  • It seems like brainwallet.org is abandoned
    – Gabe
    May 7, 2020 at 23:51
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
  • 2
    They want to run a script, not do a manual process.
    – morsecoder
    Jun 22, 2015 at 17:25
0

You can try tool btc-address-dump, installation: pip3 install btc-address-dump.

Dump btc address from hex private key:

$ btc_address_dump c7ac679b56f50bfd54dd924fe45a8dca7a1c2dced254b03dac22afc03adb9127
private key (hex) = c7ac679b56f50bfd54dd924fe45a8dca7a1c2dced254b03dac22afc03adb9127
private key (WIF) = 5KLDyKtrScLYsKMJzVCt8Mf6Nn9DEV7V3fg8njfSZnqe7ZEMqzK
private key (WIF compressed) = L3urFcPsE2yHf5zeQjVSfUB8j8FEzX5cnmhjNsJfqjKgowPP4tmg
public key (uncompressed) = 044cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1087190d91e26af594e3f8ecd3f4d3596c03c45d3b235da916903c930c6593cc4
public key (compressed) = 024cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1
address (p2pkh uncompressed) = 1Q5RqZctfcNkTPad2tuJSREByd2gB8xs63
address (p2pkh compressed) = 12W36tm2jnreFiYdrzfE6cvRaKRbicEpnA
address (p2wpkh) = bc1qzp6lz6zy9p7k68r0a7lzfpkdxvj3yapynzuatt

Dump btc address from WIF private key:

$ btc_address_dump 5KLDyKtrScLYsKMJzVCt8Mf6Nn9DEV7V3fg8njfSZnqe7ZEMqzK
private key (hex) = c7ac679b56f50bfd54dd924fe45a8dca7a1c2dced254b03dac22afc03adb9127
private key (WIF) = 5KLDyKtrScLYsKMJzVCt8Mf6Nn9DEV7V3fg8njfSZnqe7ZEMqzK
private key (WIF compressed) = L3urFcPsE2yHf5zeQjVSfUB8j8FEzX5cnmhjNsJfqjKgowPP4tmg
public key (uncompressed) = 044cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1087190d91e26af594e3f8ecd3f4d3596c03c45d3b235da916903c930c6593cc4
public key (compressed) = 024cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1
address (p2pkh uncompressed) = 1Q5RqZctfcNkTPad2tuJSREByd2gB8xs63
address (p2pkh compressed) = 12W36tm2jnreFiYdrzfE6cvRaKRbicEpnA
address (p2wpkh) = bc1qzp6lz6zy9p7k68r0a7lzfpkdxvj3yapynzuatt

Dump btc address from mnemonic words:

$ btc_address_dump "olympic wine chicken argue unaware bundle tunnel grid spider slot spell need"
mnemonic = olympic wine chicken argue unaware bundle tunnel grid spider slot spell need
private key (hex) = c7ac679b56f50bfd54dd924fe45a8dca7a1c2dced254b03dac22afc03adb9127
private key (WIF) = 5KLDyKtrScLYsKMJzVCt8Mf6Nn9DEV7V3fg8njfSZnqe7ZEMqzK
private key (WIF compressed) = L3urFcPsE2yHf5zeQjVSfUB8j8FEzX5cnmhjNsJfqjKgowPP4tmg
public key (uncompressed) = 044cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1087190d91e26af594e3f8ecd3f4d3596c03c45d3b235da916903c930c6593cc4
public key (compressed) = 024cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1
address (p2pkh uncompressed) = 1Q5RqZctfcNkTPad2tuJSREByd2gB8xs63
address (p2pkh compressed) = 12W36tm2jnreFiYdrzfE6cvRaKRbicEpnA
address (p2wpkh) = bc1qzp6lz6zy9p7k68r0a7lzfpkdxvj3yapynzuatt

Dump btc address from public key:

$ btc_address_dump 024cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1
public key (uncompressed) = 044cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1087190d91e26af594e3f8ecd3f4d3596c03c45d3b235da916903c930c6593cc4
public key (compressed) = 024cd0aaeca3b636078583408e75edd77307b5190ca7a48bb9fbc1f2576c17dff1
address (p2pkh uncompressed) = 1Q5RqZctfcNkTPad2tuJSREByd2gB8xs63
address (p2pkh compressed) = 12W36tm2jnreFiYdrzfE6cvRaKRbicEpnA
address (p2wpkh) = bc1qzp6lz6zy9p7k68r0a7lzfpkdxvj3yapynzuatt
0

An updated version of @nealmcb's answer above that I figured out from playing with the pycoin API. This solution works with the recent versions of pycoin, which has changed a lot over the years it seems.

from pycoin.symbols.btc import network
k = network.parse.private_key(" ... ")
k.address()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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