Python 2.7将比特币Privkey转换为WIF Privkey [英] Python 2.7 Converting Bitcoin Privkey into WIF Privkey

查看:98
本文介绍了Python 2.7将比特币Privkey转换为WIF Privkey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚以编码新手的身份学习了一个教程.本教程就是这个教程: https://www.youtube.com/watch?v= tX-XokHf_nI .而且我想用1个易于阅读(不是神秘的)Python文件生成我的比特币地址/私钥-就像现在编写代码的样式一样.

I just went through a tutorial as a coding newbie. The tutorial was this one: https://www.youtube.com/watch?v=tX-XokHf_nI. And I wanted to generate my Bitcoin addresses/privkeys with 1 single easy to read (not cryptic) Python file - just in the style the code is written right now.

本教程介绍了我以"1"开头的比特币地址,而不是以"5"开头的私钥的部分.另外,我不知道如何BIP38加密私钥(以"6"开头).如您所见,主要的比特币网络.

The tutorial got to the part where I got the Bitcoin address starting with a "1" but not the privkey starting with a "5". Plus I am missing how to BIP38 encrypt the private key (starting with a "6"). It's as you can see for the main Bitcoin network.

使用 https://en.bitcoin.it/wiki/Wallet_import_format 作为教程后的分步指南.最后,我评论了自己尝试做的事情,因为这全都是垃圾. (带有"SHA256哈希扩展私钥"的部分 这在很多层面上都是错误的"),我认为我向私钥添加了80个字节的部分可能是正确的.

Was using https://en.bitcoin.it/wiki/Wallet_import_format as a step by step guide after the tutorial. In the end I commented out my tries to do it myself because it all was rubbish. (The part with "SHA256 HASHED EXTENDED PRIVATE KEY THIS IS WRONG ON SO MANY LEVELS") I think the part where I added the 80 bytes to the Private Key could be correct.

PS:在一切正常之前,我现在正在使用静态私钥,这就是为什么我注释掉了非静态私钥部分的原因.它是通过我注释掉非静态私钥用法"的部分生成的.我还注释掉了已签名的消息代码行(在代码底部),因为它们已显示在本教程中,对于生成密钥/地址并不重要.我还尝试通过仅在文件的底部放置打印等并排序一些不同的东西等方式来美化"代码,但事实证明Python 2.7不喜欢这种方式.

PS: I'm using a static private key for now until everything works, which is why I commented out the non-static private key part. It has been generated via the part I commented out "non static private key usage". I also commented out the signed message code lines (at the bottom of the code) because they were shown in the tutorial, not important for the key/address generating. I also tried to "beautify" the code a bit by putting prints etc. only at the bottom of the file and sorting things a bit different etc. but it turned out Python 2.7 didn't like that.

我正在使用Python 2.7,已成功安装了所有程序,并且代码现在已按注释部分显示的那样正常工作.我用bitaddress.org验证了打印出来的结果,就像教程中的上传器一样.尝试搜索以找到解决方案,但我无法从搜索结果中得到任何有用的信息.

I am using Python 2.7, installed everything successfully, the code is working as it should right now with the commented out parts. I verified the results it printed with bitaddress.org, just like the uploader from the tutorial did. Tried searching to find a solution, but I could not get anything useful out of my search results.

如果您能帮助我解决一些缺少的代码行,我将很高兴!也可以在代码中解释/注释做什么.特别是对于尚未丢失的BIP38 Privkey密码加密.这样我就能明白什么是可以理解的.

If you could help me out with the few missing lines of code, I'd be happy! Also maybe explain / comment in the code what does what. Especially for the yet missing BIP38 Privkey password encryption. So I can see what is what and can understand.

运行.py脚本将返回有效结果,但我添加的80个字节除外-如果我已正确完成此操作,则不知道.添加80字节是获取最终私钥(以"5"开头)的必要步骤.

Running the .py script returns valid results, except for the 80 bytes I added - no Idea if this has been done correct by me. Adding 80 bytes is a needed step for getting the final private key starting with a "5" later.

运行它会打印:

This is my Private Key: 29a59e66fe370e901174a1b8296d31998da5588c7e0dba860f11d65a3adf2736
This is my 80 Byte Private Key: 8029a59e66fe370e901174a1b8296d31998da5588c7e0dba860f11d65a3adf2736
This is my Public Key: 04d370b77a4cf0078ab9e0ba3c9e78e8dd87cc047fa58d751b3719daa29ac7fbf2c3ba8338f9a08f60a74a5d3a2d10f26afa2f703b8c430eecad89d59a9df00ec5
This is my Bitcoin Address: 1B3wS8dQHtfMpFMSmtT5Fy4kHCYvxejtVo

在这里您可以看到我的代码,根据本教程,该代码在这里和这里都得到了很好的注释: (忘记注释掉这是我的哈希的ext私钥校验和"部分,很抱歉造成混淆.这是我现在需要帮助的代码.)

Here you can see my code, commented here and there as good as I could according to the tutorial: (Forgot to comment out the "This is my hashed ext priv key checksum" part, sorry for confusion. This is the code I need help now with.)

import os
import ecdsa
import hashlib
import base58

##  STATIC KEY USAGE
private_key_static = "29a59e66fe370e901174a1b8296d31998da5588c7e0dba860f11d65a3adf2736"
##  PRINTOUT FROM STATIC PRIVATE KEY
print "This is my Private Key: " + private_key_static

## NON STATIC PRIVATE KEY USAGE
#private_key = os.urandom(32).encode("hex")
#print "this is my private key: " + private_key

##  80-BYTE EXTENDED PRIVATE KEY
private_key_plus_80byte = (('80') + private_key_static)

##  PRINTOUT 80-BYTE EXTENDED PRIVATE KEY
print "This is my 80 Byte Private Key: " + private_key_plus_80byte

## SHA256 HASHED EXTENDED PRIVATE KEY
## THIS IS WRONG ON SO MANY LEVELS
#hashed_ext_priv_key_checksum = hashlib.sha256(hashlib.sha256(private_key_plus_80byte).digest()).digest()[:4]
#hashed_ext_priv_key_checksum = hashed_ext_priv_key_checksum.decode("hex")
#print "This is my hashed ext priv key checksum: " + hashed_ext_priv_key_checksum

##  PRIVATE! SIGNING KEY ECDSA.SECP256k1
sk = ecdsa.SigningKey.from_string(private_key_static.decode("hex"),
                        curve = ecdsa.SECP256k1)

##  PUBLIC! VERIFYING KEY (64 BYTE LONG, MISSING 04 BYTE AT THE BEGINNING)
vk = sk.verifying_key

##  PUBLIC KEY
public_key = ('\04' + vk.to_string()).encode("hex")
##  PRINTOUT PUBLIC KEY
print "This is my Public Key: " + public_key

##  PUBLIC KEY ENCODING (2x RIPEMD160)
ripemd160 = hashlib.new('ripemd160')

ripemd160.update(hashlib.sha256(public_key.decode('hex')).digest())

middle_man = ('\00') + ripemd160.digest()

checksum = hashlib.sha256(hashlib.sha256(middle_man).digest()).digest()[:4]

binary_addr = middle_man + checksum

addr = base58.b58encode(binary_addr)

print "This is my Bitcoin Address: " + addr

##  MESSAGE CONTENT
#msg = "hello world"

##  SIGN MESSAGE CONTENT
#signed_msg = sk.sign(msg)

##  VERIFY MESSAGE CONTENT
#assert vk.verify(signed_msg, "hello world")

##  PRINTOUT SIGNED MESSAGE ENCODED TO HEX
#print "This is a HEX encoded signed Message: " + signed_msg.encode("hex")

推荐答案

您可能从 Bitcoin Wiki的理解中被误解了步骤是所有的哈希和填充必须在键上以 bytes (而不是字符串)完成.

What you probably misunderstood from the Bitcoin Wiki's steps is that all the hashing and stuff must be done on the keys as bytes, not as strings.

这意味着,如果您想从私钥"29a59..."派生WIF密钥,则不必对字符串 "8029a59..."进行哈希处理,而只需对二进制数据进行哈希处理>与此相对应.

This means that if you want to derive the WIF key from your Private key "29a59..." you don't have to hash the string "8029a59..." but the binary data that corresponds to it instead.

这是有效的缺少代码段

# importing binascii to be able to convert hexadecimal strings to binary data
import binascii

# Step 1: here we have the private key
private_key_static = "29a59e66fe370e901174a1b8296d31998da5588c7e0dba860f11d65a3adf2736"
# Step 2: let's add 80 in front of it
extended_key = "80"+private_key_static
# Step 3: first SHA-256
first_sha256 = hashlib.sha256(binascii.unhexlify(extended_key)).hexdigest()
# Step 4: second SHA-256
second_sha256 = hashlib.sha256(binascii.unhexlify(first_sha256)).hexdigest()
# Step 5-6: add checksum to end of extended key
final_key = extended_key+second_sha256[:8]
# Step 7: finally the Wallet Import Format is the base 58 encode of final_key
WIF = base58.b58encode(binascii.unhexlify(final_key))
print (WIF)

其中 binascii.unhexlify(...) 告诉我们所表示的二进制数据用十六进制字符串表示.

where binascii.unhexlify(...) tells us the binary data represented by the hexadecimal string.

其余的代码也可以正常工作;)

The rest of your code works just fine ;)

这篇关于Python 2.7将比特币Privkey转换为WIF Privkey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆