Pages:
Author

Topic: Convert priv key to compressed public key (Read 324 times)

jr. member
Activity: 91
Merit: 3
January 19, 2019, 04:59:21 AM
#26
No problem thank you very much for your help guys.
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 02:38:18 PM
#25
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 Smiley. Probably better than expected actually, I've been doing too much with signatures clearly Grin. Simple maths definitely isn't the strong point.

jr. member
Activity: 91
Merit: 3
January 18, 2019, 02:31:37 PM
#24
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]
legendary
Activity: 1039
Merit: 2783
Bitcoin and C♯ Enthusiast
January 18, 2019, 02:23:17 PM
#23
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]
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 02:22:45 PM
#22
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”)
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[2:67],public_key_u[67:132]
    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.

Run the new code. I changed line 5.
jr. member
Activity: 91
Merit: 3
January 18, 2019, 02:11:06 PM
#21
this is my first pub key

Code:
0489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3faab396827dea71071568548e9ddc0ffdefe649db9b8601e3db79a8d1e91c0797

and output is

03    4   89158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f

and the 4 is too much

copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 02:08:30 PM
#20
Can you post the first public key or make a random one?
jr. member
Activity: 91
Merit: 3
January 18, 2019, 02: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
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 01:56:40 PM
#18
What did you do? What text editor did you use to write it to execute?
jr. member
Activity: 91
Merit: 3
January 18, 2019, 01: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
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 01: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).
jr. member
Activity: 91
Merit: 3
January 18, 2019, 01:44:37 PM
#15
ok and i have to do

Code:
import bitcoin


or not ?
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 01:39:42 PM
#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.
jr. member
Activity: 91
Merit: 3
January 18, 2019, 10:03:41 AM
#13
ok thanks a lot Smiley
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 18, 2019, 08:42:19 AM
#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.
jr. member
Activity: 91
Merit: 3
January 17, 2019, 05:31:35 PM
#11

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

Yes all keys are in separate line.
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 17, 2019, 04: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?
jr. member
Activity: 91
Merit: 3
January 17, 2019, 04:54:36 PM
#9
OK yeah I have hex priv key file and for now only uncompressed PubKey file.
copper member
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
January 17, 2019, 04: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...
jr. member
Activity: 91
Merit: 3
January 17, 2019, 09:37:00 AM
#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.
Pages:
Jump to: