Bitcoin Forum

Bitcoin => Project Development => Topic started by: Desyationer on May 13, 2021, 12:56:55 PM



Title: Python 3.8 private to public convertor script
Post by: Desyationer on May 13, 2021, 12:56:55 PM
Please not found workable script with understandable input-privatekeyslist.txt, output-publickeys.txt, errors and errors i find only. I am far from programming, I can only use someone else's script.


Title: Re: Python 3.8 private to public convertor script
Post by: WanderingPhilospher on May 16, 2021, 03:40:49 PM
Here is sample Python code to convert private keys into uncompressed and compressed public keys:

Code:
import binascii, hashlib, base58, sys, ecdsa, codecs
arq1 = open('output-uncompublickeys.txt', 'a')
arq2 = open('output-compublickeys.txt', 'a')

def uncom(prky):

    zk = ecdsa.SigningKey.from_string(prky.decode('hex'), curve=ecdsa.SECP256k1)
    zk_verify = zk.verifying_key
    z_public_key = ('\04' + zk.verifying_key.to_string()).encode('hex')
    arq1.write("%s \n" % z_public_key)
   

def com(prky):
   
    pvby = codecs.decode (prky, 'hex')
   
    #Generates the public key
    key = ecdsa.SigningKey.from_string (pvby, curve=ecdsa.SECP256k1).verifying_key
    kbs = key.to_string()
    kh = codecs.encode(kbs, 'hex')

    if(ord(bytearray.fromhex(kh[-2:])) % 2 == 0):
        #If the last byte of Y is Even, add '02'
        pkc = '02' + kh[0:64]
        arq2.write("%s \n" % pkc)

    else:
        #If the last byte of Y is Odd, add '03'
        pkc = '03' + kh[0:64]
        arq2.write("%s \n" % pkc)

with open("input-privatekeyslist.txt") as file:
    for line in file:

        prky = str.strip(line)
        uncom(prky)
        com(prky)


If you receive error, please post here and I will help.

I ran a test on 100,000 private keys and it worked.  If you only want uncom or com, you can delete or comment out the one you do not want.