Author

Topic: Importing list of Hexadecimal (64 characters) Private Keys (Read 860 times)

HCP
legendary
Activity: 2086
Merit: 4361
Oh right... It's probably just reading a stray space or newline character or something at the end of the file and attempting to convert that into a private key Tongue

Good to know that the script is working ok other than that minor glitch. Thanks for letting me know. Smiley
jr. member
Activity: 43
Merit: 4
That code works perfect, even if the input file has 1000 keys, it converts them all in output file.

I guess there is just an error at the end of the file,
HCP
legendary
Activity: 2086
Merit: 4361
Works great!!,  (So, it really does not matter what warnings it has)  Please let me know if there is something configured wrong on my side.

....
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 427, in b58check_to_bin
    assert bin_dbl_sha256(data[:-4])[:4] == data[-4:]

huh... that's odd. That error indicates that the checksum is failing when it is checking the private key??!?... Huh

Did you run it on your keys? or just on the example ones I gave you? I don't get that error... Huh
jr. member
Activity: 43
Merit: 4
Works great!!,  (So, it really does not matter what warnings it has)  Please let me know if there is something configured wrong on my side.

Code:
~/Documents$ python hex2add.py
Traceback (most recent call last):
  File "hex2add.py", line 12, in
    outfile.write(x+":"+bitcoi[Suspicious link removed]ivtoaddr(x)+"\n")
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 304, in privkey_to_address
    return pubkey_to_address(privkey_to_pubkey(priv), magicbyte)
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 291, in privkey_to_pubkey
    f = get_privkey_format(privkey)
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 217, in get_privkey_format
    bin_p = b58check_to_bin(priv)
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 427, in b58check_to_bin
    assert bin_dbl_sha256(data[:-4])[:4] == data[-4:]
AssertionError
~/Documents$ cat hex2add.txt
1c2448d1a829c3f0c363f5e1d77a72cbd85ed1e9b4405eb5c160945db3a21ba001:15LbnZxveboebARPggJxQn1c5nDVVeUCXQ
f9d606ff0d9474483bde36a711f1c41896122056cc7e7a23b05b3e68438165da01:1FyXSZ2D7eoYEAtSzQ5BXtD7coLsU4tuDV
a5f2143180073849ec38a4936aceecde398e031b57a17aad20194b6132d0612101:1PmuScdC33quyPjj7SC85c3bnEJJGfBkw5
64c85fc1473a8ea297e3c65a68b9fc2a1ac64c966111f8c7d0f5abfe8b574ad501:1BTcYd2Hr3urk7zJtP2jdcekUJAmpyJn8a
93917a448caecfe50e0850880edea56e1e5202bfdd6a68e32e049ab535ca000f01:19bkMCgmCCEBQQcNiDfr7QScmSGk23jyqy
~/Documents$

jr. member
Activity: 43
Merit: 4
HCP
legendary
Activity: 2086
Merit: 4361
You don't need to pass anything into the privtoaddr() function except for the private key "hex"... it doesn't need a 'format' parameter like the encode_privkey() function.

So you can just do this:
Code: (hex_to_addr.py)
import bitcoin

with open("hex_keys.txt","r") as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
f.close()


outfile = open("hex2add.txt","w")
for x in content:
  outfile.write(x+":"+bitcoin.privtoaddr(x)+"\n")
 
outfile.close()


That should generate something like this:
Code: (hex2add.txt)
1c2448d1a829c3f0c363f5e1d77a72cbd85ed1e9b4405eb5c160945db3a21ba001:15LbnZxveboebARPggJxQn1c5nDVVeUCXQ
f9d606ff0d9474483bde36a711f1c41896122056cc7e7a23b05b3e68438165da01:1FyXSZ2D7eoYEAtSzQ5BXtD7coLsU4tuDV
a5f2143180073849ec38a4936aceecde398e031b57a17aad20194b6132d0612101:1PmuScdC33quyPjj7SC85c3bnEJJGfBkw5
64c85fc1473a8ea297e3c65a68b9fc2a1ac64c966111f8c7d0f5abfe8b574ad501:1BTcYd2Hr3urk7zJtP2jdcekUJAmpyJn8a
93917a448caecfe50e0850880edea56e1e5202bfdd6a68e32e049ab535ca000f01:19bkMCgmCCEBQQcNiDfr7QScmSGk23jyqy
jr. member
Activity: 43
Merit: 4
I am missing something about the Hex format, I just cant figure out the fix.

here is my python:

Code:
import bitcoin

with open("hex_keys.txt","r") as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
f.close()

outfile = open("hex2add.txt","w")
for x in content:
  outfile.write(bitcoi[Suspicious link removed]ivkey_to_address(x,'hex')+"\n")
  outfile.write((x)+"\n")
 
outfile.close()


I get the following error:

Traceback (most recent call last):
  File "hex2add.py", line 13, in
    outfile.write(bitcoi[Suspicious link removed]ivkey_to_address(x,'hex')+"\n")
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 304, in privkey_to_address
    return pubkey_to_address(privkey_to_pubkey(priv), magicbyte)
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/main.py", line 451, in pubkey_to_address
    bin_hash160(binascii.unhexlify(pubkey)), magicbyte)
  File "/usr/local/lib/python2.7/dist-packages/bitcoin/py2specials.py", line 43, in bin_to_b58check
    inp_fmtd = chr(int(magicbyte)) + inp
ValueError: invalid literal for int() with base 10: 'hex'
jr. member
Activity: 43
Merit: 4
HCP, Thanks for the code

I have been messing with this and decided to give up and just ask you.

Can you modify the code so the output.txt will have both the hex key & compressed address  ?

Output.tx contents would look like:

Code:
1c2448d1a829c3f0c363f5e1d77a72cbd85ed1e9b4405eb5c160945db3a21ba001:15LbnZxveboebARPggJxQn1c5nDVVeUCXQ
f9d606ff0d9474483bde36a711f1c41896122056cc7e7a23b05b3e68438165da01:1FyXSZ2D7eoYEAtSzQ5BXtD7coLsU4tuDV
a5f2143180073849ec38a4936aceecde398e031b57a17aad20194b6132d0612101:1PmuScdC33quyPjj7SC85c3bnEJJGfBkw5
64c85fc1473a8ea297e3c65a68b9fc2a1ac64c966111f8c7d0f5abfe8b574ad501:1BTcYd2Hr3urk7zJtP2jdcekUJAmpyJn8a
93917a448caecfe50e0850880edea56e1e5202bfdd6a68e32e049ab535ca000f01:19bkMCgmCCEBQQcNiDfr7QScmSGk23jyqy
HCP
legendary
Activity: 2086
Merit: 4361
I took the liberty of hacking up a small python script that will take a text file of HEX privkeys and convert it into a text file with WIF privkeys... It will convert the hex to both "uncompressed" and "compressed" WIF keys, so you shouldn't miss any addresses. So you can simply run the script and then copy/paste the WIF keys into Electrum Wink

Code:
import bitcoin

with open("hex_keys.txt","r") as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
f.close()


outfile = open("wif_conv.txt","w")
for x in content:
  outfile.write(bitcoin.encode_privkey(x,'wif')+"\n")
  outfile.write(bitcoin.encode_privkey(x,'wif_compressed')+"\n")
 
outfile.close()


NOTES:
- You need the "bitcoin" library... ie. pip install bitcoin
- hex_keys.txt should be a text file that has one hex key per line... like this:
Code:
1c2448d1a829c3f0c363f5e1d77a72cbd85ed1e9b4405eb5c160945db3a21ba001
f9d606ff0d9474483bde36a711f1c41896122056cc7e7a23b05b3e68438165da01
a5f2143180073849ec38a4936aceecde398e031b57a17aad20194b6132d0612101
64c85fc1473a8ea297e3c65a68b9fc2a1ac64c966111f8c7d0f5abfe8b574ad501
93917a448caecfe50e0850880edea56e1e5202bfdd6a68e32e049ab535ca000f01


Script will output "wif_conv.txt" which will contain both the "WIF uncompressed" and "WIF compressed" representations of those hex keys:
Code:
5J2gRZtwPeMHgjakdLL839Xn5pLBdNTKDBeB5uHDJTcwYaL5Tvb
KxAR1SCF4wrYeNwyZNKQ4UeqzFufuXh9Nmy5WdkZJ4VdjsSM3GRv
5KiKJpBHXnRARET7KvQtYvSspTGjazcV1y4yxkbrXkSdCQL8DCR
L5bModEPxsTeZxNEvWkSXtuDJSxSy3HcfGUaR5Sqcik6kroMm4pQ
5K5NSTHgcdiizKawaczdXm7nnyWPk2seZcMpsqfGkW3peZ2rtbX
L2nHdM2drH5AL12ouPsNEmiy7vQjNwGYnw4vKPmvX1DTJKS6HVUX
5JafwWRx3dUUpX4ZFQv93DHrkKFkkyYWSWSWdRV4oZmqF3sxM6a
Kzbcsau7xbzHnjghLcZYUmFQaBa896a4wawbmm4snXesvaSMjW5e
5JwH1pa4KBjJr2zu2zCHkHQafShkQbf7kv2k3xJu2T7hTbbM9rQ
L2AZgcxudGMjmRpYfmSpv3R5agSrb9yEpq4h237zdQGZa4bvBM6t

sr. member
Activity: 267
Merit: 255
Hi, I need to import a list of hex private keys. I was hoping to just create a new wallet in Electrum and copy/paste the list of keys, but I guess Electrum only supports import of WIF private keys.

Rather than convert them all manually, is there a safe wallet that allows import of private key lists like Electrum, which supports hex format?

Thanks.
Jump to: