从文件文本转换比特币私钥-逐行 [英] Convert Bitcoin private key from file text - line by line
问题描述
我开始学习Python以使下面的项目成功,但是我需要帮助.
i started learning Python to succeed my project below, but i need help.
我有一些旧钱包的比特币/莱特币私钥.我很确定这些地址是空的,但是在删除此旧文件之前,我想将这些不同的私钥转换为公用地址,以检查是否全部为空.我会使用在线服务一次检查所有这些地址(有些可以控制多达50个地址).
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)
我有两个问题:
- 计算私钥->地址工作正常,但仅处理输入文本文件的第一行.
- 第一行处理了65次,因此我的输出文件包含65次从我的第一个私钥生成的相同地址.在Python解释器中,也会出现65次.
我了解 f.readline()
逐行读取文件,并且我认为 for数据行:
会逐行读取此文件以处理每个文件线.
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.
我试图移动变量 data
的位置,但这一次,仅第二行正在处理.
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 ??
预先感谢您的帮助.
推荐答案
您在滥用readline(),这只会返回一行.
You are misusing readline() this will only return one line.
不过,您可以仅使用for循环遍历文件中的行,
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)
这篇关于从文件文本转换比特币私钥-逐行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!