简体   繁体   中英

Convert Bitcoin private key from file text - line by line

i started learning Python to succeed my project below, but i need help.

I have some Bitcoin/Litecoin private key of some old wallets. I'm pretty sure those addresses are empty, but before I erase this old file, I would like to convert these different private key to public address to check if all is empty. I would use an online service to check these addresses all at once (some allow control of up to 50 addresses).

I know, I could import each private key one by one in wallet but I don't have any more Bitcoin-core on my computer, and I don't want to install a new one just to check my addresses.

After a lot of research, i have this functional code:

import ecdsa
import hashlib
import base58


with open("my_private_key.txt", "r") as f:    #Input file path
      data = f.readline()
      for line in data:

                  #Convert hex private key to bytes
         private_key = bytes.fromhex(data)      

                  #Derivation of the private key
         signing_key = ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1)
         verifying_key = signing_key.get_verifying_key()

         public_key = bytes.fromhex("04") + verifying_key.to_string()

                 #Hashes of public key
         sha256_1 = hashlib.sha256(public_key)
         ripemd160 = hashlib.new("ripemd160")
         ripemd160.update(sha256_1.digest())

                 #Adding prefix to identify Network
         hashed_public_key = bytes.fromhex("00") + ripemd160.digest()

                 #Checksum calculation
         checksum_full = hashlib.sha256(hashlib.sha256(hashed_public_key).digest()).digest()
         checksum = checksum_full[:4]

                 #Adding checksum to hashpubkey         
         bin_addr = hashed_public_key + checksum

                 #Encoding to address
         address = str(base58.b58encode(bin_addr))
         final_address = address[2:-1]

         print(final_address)

         with open("my_addresses.txt", "a") as i:
            i.write(final_address)

I have two problems:

  1. Calculation private key --> address works well but only the first line of my input text file is processed.
  2. First line is processed 65 times, so my output files contains 65 times the same address generate from my first private key. In Python Interpreter, this 65 times also appear.

I understood f.readline() read a file line by line, and I thought the for line in data: would read this file line by line to process each line.

I tried to move the location of my variable data but this time, only my second text line is processing.

with open("my_private_key.txt", "r") as f:    #Input file path
      for line in data:
          data = f.readline()
                         .....

I've done a lot of tests, but I can't figure out what's wrong. Where do I go wrong??

Thank you in advance for your help.

You are misusing readline() this will only return one line.

You can however iterate over lines in a file just with a for loop,

import ecdsa
import hashlib
import base58


with open("my_private_key.txt", "r") as f:    #Input file path
      for line in f:

                  #Convert hex private key to bytes
         private_key = bytes.fromhex(line)      

                  #Derivation of the private key
         signing_key = ecdsa.SigningKey.from_string(private_key, curve=ecdsa.SECP256k1)
         verifying_key = signing_key.get_verifying_key()

         public_key = bytes.fromhex("04") + verifying_key.to_string()

                 #Hashes of public key
         sha256_1 = hashlib.sha256(public_key)
         ripemd160 = hashlib.new("ripemd160")
         ripemd160.update(sha256_1.digest())

                 #Adding prefix to identify Network
         hashed_public_key = bytes.fromhex("00") + ripemd160.digest()

                 #Checksum calculation
         checksum_full = hashlib.sha256(hashlib.sha256(hashed_public_key).digest()).digest()
         checksum = checksum_full[:4]

                 #Adding checksum to hashpubkey         
         bin_addr = hashed_public_key + checksum

                 #Encoding to address
         address = str(base58.b58encode(bin_addr))
         final_address = address[2:-1]

         print(final_address)

         with open("my_addresses.txt", "a") as i:
            i.write(final_address)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM