Wallet

這是如何用python生成比特幣地址嗎?

  • September 12, 2020

所以我已經有了公鑰。我的問題是我是否正確應用了 b58 編碼,因為比特幣使用 b58check,並且還添加了 x00 前綴。有人可以解釋一下嗎?謝謝。

import hashlib
import base58

# ECDSA Public Key
base64_str = 'MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE7P0EE5amecnYKMlq96RUL3Q+mZJCQrta6iHyjQWtsbbgcBMayhR/CTzDi5j4Fb/wD9EclHt3dpYRyJcl9Rtmug=='

hex_str = base64_str.decode('base64').encode('hex')

sha = hashlib.sha256()
rip = hashlib.new('ripemd160')

sha.update(base64_str.decode('base64'))

rip.update(sha.hexdigest())

# Get address
print base58.b58encode(rip.hexdigest())

如果公鑰是正確的(請參閱 Mark 的答案),則程式碼不完整(缺少簽名)並且包含不正確的 ASCII 十六進制摘要的散列。

我想為這個來自公鑰的比特幣地址的實現做出貢獻。

它涵蓋了未壓縮和壓縮比特幣地址的情況(僅更改 compress_pubkey 布爾值)。

#!/usr/bin/env python2
# https://en.bitcoin.it/wiki/Protocol_documentation#Addresses

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') )
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]

print ( "checksum = \t" + sha.hexdigest() )
print ( "key_hash + checksum = \t" + key_hash + ' ' + checksum )
print ( "bitcoin address = \t" + base58.b58encode( (key_hash + checksum).decode('hex') ) )

不。

公鑰(您要散列的數據)如下所示:

04 xx xx xx xx xx ... (uncompressed, 65 hex bytes)

要麼

(02 or 03) xx xx xx xx xx ... (compressed, 33 hex bytes)

它不是 DER 格式的公鑰。

引用自:https://bitcoin.stackexchange.com/questions/56923