Title: Convert priv key to compressed public key Post by: PietCoin97 on January 16, 2019, 07:33:44 PM 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 ? Title: Re: Convert priv key to compressed public key Post by: ABCbits on January 16, 2019, 07:48:08 PM How about script on Mastering Bitcoin book at https://github.com/bitcoinbook/bitcoinbook/blob/develop/code/key-to-address-ecc-example.py (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 (https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch04.asciidoc) for more information Also, a member posted such script few months ago at https://bitcointalk.org/index.php?topic=5032312.0 (https://bitcointalk.org/index.php?topic=5032312.0), but i've no idea if it works or not. Title: Re: Convert priv key to compressed public key Post by: jackg on January 16, 2019, 07:49:16 PM 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. Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 16, 2019, 08:01:44 PM 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 Title: Re: Convert priv key to compressed public key Post by: jackg on January 16, 2019, 11:07:42 PM 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 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”) You may want to convert the key to base 58 before exporting and add a 1 to it at the front. Title: Re: Convert priv key to compressed public key Post by: pooya87 on January 17, 2019, 04:56:05 AM 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) Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 17, 2019, 01:37:00 PM Quote 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. Can I run this or what is the complete code? Sry I am newbie in python programming. Title: Re: Convert priv key to compressed public key Post by: jackg on January 17, 2019, 08:32:10 PM Bump this tomorrow and I'll give you the code, don't run what etf suggested (sorry etf it's just a bit wrong)... You've imported a public key from a file and then generated the private key for another public key and saved that.
Although, maybe I'm wrong. Op do you have the public keys already in the file or not? I'm as lost as pooya at this point... Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 17, 2019, 08:54:36 PM OK yeah I have hex priv key file and for now only uncompressed PubKey file.
Title: Re: Convert priv key to compressed public key Post by: jackg on January 17, 2019, 08:55:54 PM OK yeah I have hex priv key file and for now only uncompressed PubKey file. Ok. How are they deliminatad? A line per key? Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 17, 2019, 09:31:35 PM Ok. How are they deliminatad? A line per key? Yes all keys are in separate line. Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 12:42:19 PM I'm on a train so can't exactly program at the moment, give me 5 hours and I'll get something for you to run.
Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 18, 2019, 02:03:41 PM ok thanks a lot :)
Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 05:39:42 PM As per what pooya said:
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 Code: f = open(“keys.txt”,”r”) I'm expecting the public keys to be in hex I'm hoping my maths isn't off too. Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 18, 2019, 05:44:37 PM ok and i have to do
Code: import bitcoin or not ? Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 05:46:14 PM Nope. The script should work.
You might want to tell me where the file is though if possible? If not you'll have to edit it on both lines where a file is needed (editing the first string in the open function). Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 18, 2019, 05:48:30 PM now i have it but this is new error
Code: File "compressed.py", line 5 Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 05:56:40 PM What did you do? What text editor did you use to write it to execute?
Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 18, 2019, 06:04:38 PM now i got it but theoutput is wrong
the script write the 4 too Code: 03489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 06:08:30 PM Can you post the first public key or make a random one?
Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 18, 2019, 06:11:06 PM this is my first pub key
Code: 0489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3faab396827dea71071568548e9ddc0ffdefe649db9b8601e3db79a8d1e91c0797 and output is 03 4 89158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f and the 4 is too much Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 06:22:45 PM I have it!
As per what pooya said: 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 Code: f = open(“keys.txt”,”r”) I'm expecting the public keys to be in hex I'm hoping my maths isn't off too. Run the new code. I changed line 5. Title: Re: Convert priv key to compressed public key Post by: Coding Enthusiast on January 18, 2019, 06:23:17 PM change:
public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:130] to public_key_x,public_key_y = public_key_u[2:66],public_key_u[66:130] Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 18, 2019, 06:31:37 PM yeah it is
Quote public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:130] to public_key_x,public_key_y = public_key_u[2:66],public_key_u[66:130] Title: Re: Convert priv key to compressed public key Post by: jackg on January 18, 2019, 06:38:18 PM change: public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:130] to public_key_x,public_key_y = public_key_u[2:66],public_key_u[66:130] Well that went badly :). Probably better than expected actually, I've been doing too much with signatures clearly ;D. Simple maths definitely isn't the strong point. Title: Re: Convert priv key to compressed public key Post by: PietCoin97 on January 19, 2019, 08:59:21 AM No problem thank you very much for your help guys.
|