Bitcoin Forum
May 28, 2024, 08:08:30 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Python 3.8 private to public convertor script  (Read 90 times)
Desyationer (OP)
Newbie
*
Offline Offline

Activity: 34
Merit: 0


View Profile
May 13, 2021, 12:56:55 PM
 #1

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.
WanderingPhilospher
Full Member
***
Offline Offline

Activity: 1078
Merit: 219

Shooters Shoot...


View Profile
May 16, 2021, 03:40:49 PM
 #2

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.
Pages: [1]
  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!