【问题标题】:Generating bitcoin key pair in python 3.6 from public key to public address在 python 3.6 中从公钥到公共地址生成比特币密钥对
【发布时间】:2017-11-16 00:46:39
【问题描述】:

我有一个关于我正在尝试编写的为比特币地址生成密钥对的脚本的问题。我已经生成了一个随机私钥并生成了一个公钥。我知道(或想得太多?)我的第一部分代码是正确的。当我去bitaddress.org 并检查我生成的私钥以获取详细信息时,我总是得到正确的生成公钥。

这就是我现在拥有的

import os
import ecdsa
import binascii

private_key = binascii.hexlify(os.urandom(32)).decode()
print("private key = " + private_key)

Private_key = bytes.fromhex(private_key)

signing_key = ecdsa.SigningKey.from_string(Private_key, curve = ecdsa.SECP256k1)
verifying_key = signing_key.get_verifying_key()

public_key = bytes.fromhex("04") + verifying_key.to_string()
print ("public key = " + public_key.hex())

问题是现在我得到了 130 个字符的公钥,我想将其转换为比特币地址。我不明白该怎么做。我需要做一些编码/解码,但无法绕开它。 这是我从网上找到但看不懂的解释:

Bitcoin address explanation png

有人可以帮我解决这个问题

【问题讨论】:

  • 不好意思问一下;我忘了将到目前为止的代码添加到问题中,但更新了它。我是一个新手“开发人员”,并且正在尽我所能去了解它。我的印象是我的问题是具体的。如果不够具体,请注明。
  • 您使用的是哪个 ecdsa 模块?
  • 感谢您的回复!我用的是ecdsa 0.13(全称:ecdsa-0.13+26.gc877639-py3.6.egg)。
  • 好的,所以在概述的过程中有 5-10 个步骤。你被哪一个困住了,为什么?
  • 抱歉我的愚蠢问题。我只想告诉你,我真的很感激你花时间自愿帮助我。我会尽量具体一点,以免浪费您的时间。正如您已经注意到的那样,我两周前开始编码,每天花费 10 多个小时。到目前为止只是喜欢它并想学习所有内容!相信我,我确实搜索过:) 对于我的问题:我现在有 pubkey。我需要执行 sha256 散列,但在 python 3.6 中找不到任何示例或明确的文档。我不只是想使用分机。库。因为我想学习。

标签: python python-3.x hash cryptography bitcoin


【解决方案1】:

此代码可在 Python 2 和 Python 3 中运行。 它不仅打印比特币地址,还打印一些中间值。 公钥是 pubkey 变量中的 130 十六进制字符字符串。

请注意there are two possible and valid bitcoin addresses for each public key:未压缩和压缩的形式。更改 compress_key 布尔变量以提取每个变量。

#!/usr/bin/env python
# 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(bytearray.fromhex(pubkey[-2:])) % 2 == 0):
        pubkey_compressed = '02'
    else:
        pubkey_compressed = '03'
    pubkey_compressed += pubkey[2:66]
    hex_str = bytearray.fromhex(pubkey_compressed)
else:
    hex_str = bytearray.fromhex(pubkey)

# Obtain key:

key_hash = '00' + hash160(hex_str)

# Obtain signature:

sha = hashlib.sha256()
sha.update( bytearray.fromhex(key_hash) )
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( bytes(bytearray.fromhex(key_hash + checksum)) ) )

【讨论】:

    猜你喜欢
    • 2013-07-14
    • 1970-01-01
    • 2019-05-16
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多