带有Python错误的比特币地址生成 [英] Address generation for bitcoin with Python error

查看:62
本文介绍了带有Python错误的比特币地址生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用python理解比特币,并试图创建自己的虚荣地址生成器.

I am trying to understand bitcoin with python and trying to create my own vanity address generator.

下面是while循环的一小段.循环运行大约10次后,我一直收到错误消息.任何帮助将不胜感激.我已经搜索了论坛并找到了答案.

Below is a snippet of the while loop. I keep getting an error after the loop runs about 10 times. Any help would be highly appreciated. i have searched the forum and have found answers.

但是它们不起作用.IE.我更改了

But they don't work. IE. i changed the

intermed = hashlib.sha256(string).digest() 

几次修改代码,但结果仍然相同.

a few times modifying the code but still the same result.

Traceback (most recent call last):
  File "main.py", line 38, in <module>
    compressed_address_base58check = bitcoin.pubkey_to_address(hex_compressed_public_key)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/bitcoin/mai
n.py", line 452, in pubkey_to_address
    return bin_to_b58check(bin_hash160(pubkey), magicbyte)  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/bitcoin/mai
n.py", line 334, in bin_hash160
    intermed = hashlib.sha256(string).digest()
TypeError: Unicode-objects must be encoded before hashing

while True:
  pk = binascii.hexlify(os.urandom(32)).decode('utf-8').upper()
  privkey = f"{pk:0>64}"
  pub = privtopub(privkey)   # Get pub addr from priv key
  addr = pubtoaddr(pub)   # Get the 1Btc... address
  decoded_private_key = bitcoin.decode_privkey(privkey, 'hex')
  wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif')
  # Add suffix '01' to indicate a compressed private Key
  compressed_private_key = privkey + '01'
  # generate a WIF format from the compressed private key (WIF-compressed)
  wif_compressed_private_key = bitcoin.encode_privkey(bitcoin.decode_privkey(compressed_private_key, 'hex'), 'wif')
  # Multiply de EC generator G with the priveate key to get a public key point
  pubkey = bitcoin.fast_multiply(bitcoin.G, decoded_private_key)
  # Encode as hex, prefix 04
  hex_encoded_public_key = bitcoin.encode_pubkey(pubkey, 'hex')
  # Compress public key, adjust prefix depending on whether y is even or odd
  (public_key_x, public_key_y) = pubkey
  if public_key_y % 2 == 0:
    compressed_prefix = '02'
  else:
    compressed_prefix = '03'
  hex_compressed_public_key = compressed_prefix + bitcoin.encode(public_key_x, 16)
  print ('Compressed Public Key: ' + hex_compressed_public_key)
  # Generate compressedd bitcoin address from compressed public key 

  compressed_address_base58check = bitcoin.pubkey_to_address(hex_compressed_public_key)
  print ("private key:           " + privkey )
  print ("uncompressed address:     "+ addr )
  print ('Compressed address:       ' + bitcoin.pubkey_to_address(hex_compressed_public_key))
  C_address = bitcoin.pubkey_to_address(hex_compressed_public_key)
  U_address = addr

推荐答案

您必须对 hex_compressed_public_key 进行编码才能生成地址.

You have to encode your hex_compressed_public_key to generate the address.

compressed_address_base58check = bitcoin.pubkey_to_address(hex_compressed_public_key.encode('utf-8'))

这篇关于带有Python错误的比特币地址生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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