从文件文本中转换比特币私钥--逐行转换。

问题描述 投票:0回答:1

我开始学习Python,以成功我的项目下面,但我需要帮助。

我有一些旧钱包的BitcoinLitecoin私钥。我很确定这些地址是空的,但是在我删除这个旧文件之前,我想把这些不同的私钥转换为公共地址来检查是否都是空的。我会使用一个在线服务来一次性检查这些地址(有些允许控制多达50个地址)。

我知道,我可以在钱包中逐一导入每个私钥,但我的电脑上没有任何更多的Bitcoin-core,我不想为了检查地址而安装一个新的。

经过大量的研究,我有了这个功能代码。

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)

我有两个问题。

  1. 计算私钥--> 地址工作良好,但只有我输入的文本文件的第一行被处理。
  2. 第一行被处理了65次,所以我的输出文件中包含了65次由我的第一个私钥生成的相同地址。 在Python解释器中,也出现了这65次。

我明白了 f.readline() 逐行读取一个文件,我以为是 for line in data: 会逐行读取这个文件来处理每一行。

我试着把我的变量 data 但这次,只有我的第二行文字在处理。

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

我做了很多测试,但我想不出是哪里出了问题。我到底哪里出了问题?

提前感谢您的帮助。

python bitcoin private-key
1个回答
2
投票

你误用了readline(),它只能返回一行。

不过你可以在文件中用for循环来迭代一行。

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)
© www.soinside.com 2019 - 2024. All rights reserved.