Bitcoin Forum
May 27, 2024, 02:07:41 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: Convert priv key to compressed public key  (Read 322 times)
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 16, 2019, 07: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 ?
ABCbits
Legendary
*
Offline Offline

Activity: 2884
Merit: 7522


Crypto Swap Exchange


View Profile
January 16, 2019, 07:48:08 PM
Merited by dbshck (4)
 #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://bitcointalk.org/index.php?topic=5032312.0, but i've no idea if it works or not.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 16, 2019, 07: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.
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 16, 2019, 08: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)
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 16, 2019, 11:07:42 PM
Last edit: January 16, 2019, 11:34:34 PM by jackg
Merited by dbshck (4), ABCbits (1)
 #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.
pooya87
Legendary
*
Offline Offline

Activity: 3458
Merit: 10594



View Profile
January 17, 2019, 04:56:05 AM
Merited by dbshck (4), ABCbits (1)
 #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)

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 17, 2019, 01:37:00 PM
 #7

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.
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 17, 2019, 08:32:10 PM
 #8

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...
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 17, 2019, 08:54:36 PM
 #9

OK yeah I have hex priv key file and for now only uncompressed PubKey file.
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 17, 2019, 08:55:54 PM
 #10

OK yeah I have hex priv key file and for now only uncompressed PubKey file.

Ok. How are they deliminatad?
A line per key?
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 17, 2019, 09:31:35 PM
 #11


Ok. How are they deliminatad?
A line per key?

Yes all keys are in separate line.
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 18, 2019, 12:42:19 PM
 #12

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.
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 18, 2019, 02:03:41 PM
 #13

ok thanks a lot Smiley
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 18, 2019, 05:39:42 PM
Merited by ABCbits (2)
 #14

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”)
even = ["0","2","4","6","8","A","C","E","a","c","e"]
for line in f:
    public_key_u=line
    public_key_x,public_key_y = public_key_u[1:66],public_key_u[66:131]
    if len(public_key_x) != len(public_key_y):
        print("Error, jackg messed up the maths!")
        break
    if public_key_y[len(public_key_y)-1] in even:
        hex_compressed_public_key = "02"+public_key_x
    else:
        hex_compressed_public_key = "03"+public_key_x
    x = open(“compressedkeys.txt”,”a”)
    x.write(hex_compressed_public_key +”\n”)
    x.close()

I'm expecting the public keys to be in hex

I'm hoping my maths isn't off too.
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 18, 2019, 05:44:37 PM
 #15

ok and i have to do

Code:
import bitcoin


or not ?
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 18, 2019, 05:46:14 PM
 #16

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).
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 18, 2019, 05:48:30 PM
 #17

now i have it but this is new error

Code:
 File "compressed.py", line 5
    f = open(“keys.txt”,”r”)
             ^
SyntaxError: invalid syntax
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 18, 2019, 05:56:40 PM
 #18

What did you do? What text editor did you use to write it to execute?
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 18, 2019, 06:04:38 PM
 #19

now i got it but theoutput is wrong

the script write the 4 too

Code:
03489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f
0343b45bca0293982b4d153883d263faae87e84b0c32bf8c575e5ac178ee280cd0d
034970707052f31de53e71ee5d2d447875480f407ab21c3ff0669ca928867ed5a26
0346c58a6f5bf387410c5e4cdb1bb7ae5a7f72eb596177263fa8cb427e96ad4ea4f
034c7d25d6f26b53e203b9fa9c7fed9f4c19dbefc5d358a07e2df04f4cf2820df4d
034450b1a2b2bec54481f9d2530eda02bc09b35c7a0a1d6919bf7334076b2d3dc96
0343bd6058a838c0286871c85ed6a19a9b5426889b677add211311a462e658a92f9
03432bbbdb5af831a04132dcc7c51d225c63545df945b81c55b8d7dd34140c572a3
03436b46a4630b0f61fdebb54128d94a495832632d28888159056279485e5fbdf35
0341d248cb03604517597b5d777ece3460463987f8261122ada571707e85b9dfec3
0340fdb2de47e0d9ce1bffbddcf5c0ecac36b0df4162329c144ff458dc918d506c5
034c94755cf4e40f3ed21102b47eb33aa3e00c752484f49624a444806ff09a61123
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


https://bit.ly/387FXHi lightning theory


View Profile
January 18, 2019, 06:08:30 PM
 #20

Can you post the first public key or make a random one?
Pages: [1] 2 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!