PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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 ?
|
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 16, 2019, 07:49:16 PM |
|
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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: 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
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 16, 2019, 11:07:42 PM Last edit: January 16, 2019, 11:34:34 PM by jackg Merited by dbshck (4), ABCbits (1) |
|
ok thanks for the help but how can i implement to read from file and output to new file at this 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: 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
Activity: 3570
Merit: 10845
|
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)
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 17, 2019, 01:37:00 PM |
|
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
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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...
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 17, 2019, 08:54:36 PM |
|
OK yeah I have hex priv key file and for now only uncompressed PubKey file.
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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?
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 17, 2019, 09:31:35 PM |
|
Ok. How are they deliminatad? A line per key?
Yes all keys are in separate line.
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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.
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 18, 2019, 02:03:41 PM |
|
ok thanks a lot
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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 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
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 18, 2019, 05:44:37 PM |
|
ok and i have to do or not ?
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
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).
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 18, 2019, 05:48:30 PM |
|
now i have it but this is new error File "compressed.py", line 5 f = open(“keys.txt”,”r”) ^ SyntaxError: invalid syntax
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 18, 2019, 05:56:40 PM |
|
What did you do? What text editor did you use to write it to execute?
|
|
|
|
PietCoin97 (OP)
Jr. Member
Offline
Activity: 91
Merit: 3
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 18, 2019, 06:04:38 PM |
|
now i got it but theoutput is wrong the script write the 4 too 03489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f 0343b45bca0293982b4d153883d263faae87e84b0c32bf8c575e5ac178ee280cd0d 034970707052f31de53e71ee5d2d447875480f407ab21c3ff0669ca928867ed5a26 0346c58a6f5bf387410c5e4cdb1bb7ae5a7f72eb596177263fa8cb427e96ad4ea4f 034c7d25d6f26b53e203b9fa9c7fed9f4c19dbefc5d358a07e2df04f4cf2820df4d 034450b1a2b2bec54481f9d2530eda02bc09b35c7a0a1d6919bf7334076b2d3dc96 0343bd6058a838c0286871c85ed6a19a9b5426889b677add211311a462e658a92f9 03432bbbdb5af831a04132dcc7c51d225c63545df945b81c55b8d7dd34140c572a3 03436b46a4630b0f61fdebb54128d94a495832632d28888159056279485e5fbdf35 0341d248cb03604517597b5d777ece3460463987f8261122ada571707e85b9dfec3 0340fdb2de47e0d9ce1bffbddcf5c0ecac36b0df4162329c144ff458dc918d506c5 034c94755cf4e40f3ed21102b47eb33aa3e00c752484f49624a444806ff09a61123
|
|
|
|
jackg
Copper Member
Legendary
Offline
Activity: 2856
Merit: 3071
https://bit.ly/387FXHi lightning theory
|
![](https://bitcointalk.org/Themes/custom1/images/post/xx.gif) |
January 18, 2019, 06:08:30 PM |
|
Can you post the first public key or make a random one?
|
|
|
|
|