Bitcoin Forum
May 13, 2024, 12:40:00 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 18, 2019, 06:11:06 PM
 #21

this is my first pub key

Code:
0489158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3faab396827dea71071568548e9ddc0ffdefe649db9b8601e3db79a8d1e91c0797

and output is

03    4   89158b199bec930e2d10f0c16e214571c0c17287f0ee93193ca385b79135db3f

and the 4 is too much

1715604000
Hero Member
*
Offline Offline

Posts: 1715604000

View Profile Personal Message (Offline)

Ignore
1715604000
Reply with quote  #2

1715604000
Report to moderator
1715604000
Hero Member
*
Offline Offline

Posts: 1715604000

View Profile Personal Message (Offline)

Ignore
1715604000
Reply with quote  #2

1715604000
Report to moderator
"The nature of Bitcoin is such that once version 0.1 was released, the core design was set in stone for the rest of its lifetime." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


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


View Profile
January 18, 2019, 06: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.
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
January 18, 2019, 06:23:17 PM
Merited by bones261 (2), ABCbits (1)
 #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]

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 18, 2019, 06: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]
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 2856
Merit: 3071


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


View Profile
January 18, 2019, 06: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.

PietCoin97 (OP)
Jr. Member
*
Offline Offline

Activity: 91
Merit: 3


View Profile
January 19, 2019, 08:59:21 AM
 #26

No problem thank you very much for your help guys.
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!