【问题标题】:Address generation for bitcoin with Python error带有 Python 错误的比特币地址生成
【发布时间】:2019-12-02 13:13:05
【问题描述】:

我正在尝试用 python 理解比特币并尝试创建自己的虚地址生成器。

下面是while循环的sn-p。循环运行大约 10 次后,我不断收到错误消息。任何帮助将不胜感激。我搜索了论坛并找到了答案。

但它们不起作用。 IE。我改变了

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

修改了几次代码,结果还是一样。

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

【问题讨论】:

    标签: python-3.x bitcoin


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2013-07-14
      • 1970-01-01
      • 2014-10-09
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多