比特币地址的Base58Check编码太长了

 萱璇妍幽 发布于 2023-01-15 20:02

我正在尝试用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 )).

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有