Pages:
Author

Topic: Convert priv key to compressed public key - page 2. (Read 362 times)

legendary
Activity: 3472
Merit: 10611
January 16, 2019, 11:56:05 PM
#6
if you explain what it is exactly you are trying to do with these private keys then it would be so much simpler to help you out!

With Python Bitcoin tool i only can generate the uncompressed pub key.

converting uncompressed to compressed public key is so much easier than you think! you just have to know what they mean. here are the easy fast steps:
1. take the uncompressed public key and drop its first byte (which is 0x04)
2. split the remaining 64 bytes into 2x 32 bytes
3. take the first 32 byte, this is your (x coordinate) compressed public key after next step
4. take the second 32 bytes (y coordinate) if it was even add 0x02 otherwise 0x03 to the beginning of step 3

the function you posted seems to be first generating a "random private key" which is not what you asked for here.
additionally you can make it faster by skipping the % operation and using a bitwise AND operator with 1
also zfill(64) may need to change to zfill(33)
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 16, 2019, 06:07:42 PM
#5
ok thanks for the help

but how can i implement to read from file and output to new file at this code:

Code:
from __future__ import print_function
import bitcoin

# Generate a random private key
valid_private_key = False
while not valid_private_key:
    private_key = bitcoin.random_key()
    decoded_private_key = bitcoin.decode_privkey(private_key, 'hex')
    valid_private_key = 0 < decoded_private_key < bitcoin.N


# Compress public key, adjust prefix depending on whether y is even or odd
(public_key_x, public_key_y) = public_key
compressed_prefix = '02' if (public_key_y % 2) == 0 else '03'
hex_compressed_public_key = compressed_prefix + (bitcoin.encode(public_key_x, 16).zfill(64))
print("Compressed Public Key (hex) is:", hex_compressed_public_key)


You’d add something to import your public keys,

If they keys are on individual lines, you can do something like:
Code:
f = open(“keys.txt”,”r”)
for line in f:
    public_key_y=line
    #run conversion
    x = open(“compressedkeys.txt”,”a”)
    x.write(hex_compressed_public_key +”\n”)
    x.close()

You may want to convert the key to base 58 before exporting and add a 1 to it at the front.
jr. member
Activity: 91
Merit: 3
January 16, 2019, 03:01:44 PM
#4
ok thanks for the help

but how can i implement to read from file and output to new file at this code:

Code:
from __future__ import print_function
import bitcoin

# Generate a random private key
valid_private_key = False
while not valid_private_key:
    private_key = bitcoin.random_key()
    decoded_private_key = bitcoin.decode_privkey(private_key, 'hex')
    valid_private_key = 0 < decoded_private_key < bitcoin.N


# Compress public key, adjust prefix depending on whether y is even or odd
(public_key_x, public_key_y) = public_key
compressed_prefix = '02' if (public_key_y % 2) == 0 else '03'
hex_compressed_public_key = compressed_prefix + (bitcoin.encode(public_key_x, 16).zfill(64))
print("Compressed Public Key (hex) is:", hex_compressed_public_key)
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 16, 2019, 02:49:16 PM
#3
If ETFs idea doesn't work, this one might be of use to you: https://bitcoin.stackexchange.com/questions/3059/what-is-a-compressed-bitcoin-key

2nd answer down.
legendary
Activity: 2870
Merit: 7490
Crypto Swap Exchange
January 16, 2019, 02:48:08 PM
#2
How about script on Mastering Bitcoin book at https://github.com/bitcoinbook/bitcoinbook/blob/develop/code/key-to-address-ecc-example.py?
You might want to read 4th chapter of the book at https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch04.asciidoc for more information

Also, a member posted such script few months ago at https://bitcointalksearch.org/topic/generating-public-compresseduncompressed-key-from-given-private-key-in-c-5032312, but i've no idea if it works or not.
jr. member
Activity: 91
Merit: 3
January 16, 2019, 02:33:44 PM
#1
Hello

I have a list of free generated priv keys and i want to convert these to compressed pub keys.

With Python Bitcoin tool i only can generate the uncompressed pub key.


Does anywhere exist a script which read from file and ouput in new file and convert the priv key to compressed pub key or mabey Uncompressed Pub key to compressed pub key ?
Pages:
Jump to: