我正在尝试用Python创建一个比特币地址.我得到了哈希部分,但是我对Base58Check编码有些麻烦.我用这个包:
https://pypi.python.org/pypi/base58
这是一个例子:
import base58 unencoded_string = "00010966776006953D5567439E5E39F86A0D273BEED61967F6" encoded_string = base58.b58encode(unencoded_string) print(encoded_string)
输出是:
bSLesHPiFV9jKNeNbUiMyZGJm45zVSB8bSdogLWCmvs88wxHjEQituLz5daEGCrHE7R7
根据创建比特币地址的技术背景,上面的RIPEMD-160哈希值应为"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM".也就是说,我的输出是错误的,显然太长了.有谁知道我做错了什么?
编辑:
我添加了十六进制解码(.decode("hex")):
import base58 unencoded_string = "00010966776006953D5567439E5E39F86A0D273BEED61967F6" encoded_string = base58.b58encode(unencoded_string.decode("hex")) print(encoded_string)
输出现在看起来更好:
1csU3KSAQMEYLPudM8UWJVxFfptcZSDvaYY477
然而,它仍然是错误的.它必须是字节编码吗?你是如何用Python做到的?
EDIT2:
现在修复它(感谢Arpegius).将str(bytearray.fromhex(hexstring))添加到我的代码中(在Python 2.7中):
import base58 hexstring= "00010966776006953D5567439E5E39F86A0D273BEED61967F6" unencoded_string = str(bytearray.fromhex( hexstring )) encoded_string= base58.b58encode(unencoded_string) print(encoded_string)
输出:
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
Arpegius.. 7
在base58.b58encode
需要的字节(python2 STR)不是十六进制.您需要先解码它:
In [1]: import base58 In [2]: hexstring= "00010966776006953D5567439E5E39F86A0D273BEED61967F6" In [3]: unencoded_string = bytes.fromhex(hexstring) In [4]: encoded_string= base58.b58encode(unencoded_string) In [5]: print(encoded_string) 16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM
在python 2.7中你可以使用str(bytearray.fromhex( hexstring ))
.