如何解决AttributeError str对象没有属性解码
我需要修复脚本代码。该脚本从公钥生成哈希,并且可以在 Python 2 中工作,但是我需要它可以在 Python 3
中工作我从here那里获取了代码
import hashlib
import base58
# ECDSA bitcoin Public Key
pubkey = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'
# See 'compressed form' at https://en.bitcoin.it/wiki/Protocol_documentation#Signatures
compress_pubkey = False
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
print ( "key_hash = \t" + rip.hexdigest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
if (compress_pubkey):
if (ord(pubkey[-2:].decode('hex')) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = pubkey_compressed.decode('hex')
else:
hex_str = pubkey.decode('hex')
# Obtain key:
key_hash = '00' + hash160(hex_str)
# Obtain signature:
sha = hashlib.sha256()
sha.update( key_hash.decode('hex') )
从 Python 3 运行时,出现错误:
Traceback (most recent call last):
File "D: /script.py",line 27,in <module>
hex_str = pubkey.decode ('hex')
AttributeError: 'str' object has no attribute 'decode'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。