学习
实践
活动
工具
TVP
写文章

用 Python 实现密钥和比特币地址

最全面的比特币 Python 库是 Vitalik Buterin 写的 pybitcointools。在例4-4 中,我们使用 pybitcointools 库(导入为“bitcoin”)来生成和显示不同格式的密钥和比特币地址。

例 4-4 使用 pybitcointools 库的密钥和比特币地址的生成和格式化过

import bitcoin

# Generate a random private key

valid_private_key = False while not valid_private_key: private_key = bitcoin.random_key() decoded_private_key =

bitcoin.decode_privkey(private_key, 'hex')

valid_private_key= 0>

print "Private Key (hex) is: ", private_key

print "Private Key (decimal) is: ",decoded_private_key

# Convert private key to WIF format wif_encoded_private_key = bitcoin.encode_privkey(decoded_private_key, 'wif') print "Private Key (WIF) is: ",wif_encoded_private_key

# Add suffix "01" to indicate a compressed private key compressed_private_key = private_key + '01'

print "Private Key Compressed (hex) is: ", compressed_private_key

# 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')

print "Private Key (WIF-Compressed) is: ", wif_compressed_private_key

#MultiplytheECgeneratorpointGwiththeprivatekeyto get a public keypoint

public_key = bitcoin.base10_multiply(bitcoin.G, decoded_private_key)print"PublicKey(x,y)coordinatesis:", public_key

# Encode as hex, prefix 04

hex_encoded_public_key = bitcoin.encode_pubkey(public_key,'hex') print "Public Key (hex) is:", hex_encoded_public_key

#Compresspublickey,adjustprefixdependingonwhethery is even orodd

(public_key_x,public_key_y)=public_keyif(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) is:", hex_compressed_public_key

# Generate bitcoin address from public key print "Bitcoin Address (b58check) is:", bitcoin.pubkey_to_address(public_key)

#Generatecompressedbitcoinaddressfromcompressedpublic key

print "Compressed Bitcoin Address (b58check) is:", bitcoin.pubkey_to_address(hex_compressed_public_key)

例 4-5 显示了上段代码运行结果。

例 4-5 运 行 key-to-address-ecc-example.py

$ python key-to-address-ecc-example.py Private Key (hex) is:

e41daa6

Private Key (decimal) is:

265632300484379575922325538266636964406067566859201174768

Private Key (WIF) is: 5JG9hT3beGTJuUAmCQEmNaxAuMacCTfXuw1R3FCXig23RQHMr4K Private Key Compressed (hex) is:

e41daa601

Private Key (WIF-Compressed) is: KyBsPXxTuVD82av65KZkrGrWi5qLMah5SdNq6uftawDbgKa2wv6S Public Key (x,y) coordinates is:

(41637322786646325214887832269588396900663353932545912953 362782457239403430124L,

163889351287812384055267104667247415937610851208643314490

Public Key (hex) is:

243bcefdd4347074d44bd7356d6a53c495737dd96295e2a9374bf5f02 ebfc176

Compressed Public Key (hex) is:

Bitcoin Address (b58check) is: 1thMirt546nngXqyPEz532S8fLwbozud8

Compressed Bitcoin Address (b58check) is: 14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3

例 4-6 是另外一个示例,使用的是 Python ECDSA 库来做椭圆曲线计算而非使

用 bitcoin 的库。

例 4-6 使用在比特币密钥中的椭圆曲线算法的脚本

import ecdsa import random

from ecdsa.util import string_to_number, number_to_string

# secp256k1, http://www.oid-info.com/get/1.3.132.0.10

_p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF EFFFFFC2FL

_r = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8 CD0364141L

_b = 0x0000000000000000000000000000000000000000000000000000000

_a = 0x0000000000000000000000000000000000000000000000000000000

_Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815 B16F81798L

curve_secp256k1 = ecdsa.ellipticcurve.CurveFp(_p, _a, _b)

generator_secp256k1 = ecdsa.ellipticcurve.Point(curve_secp256k1, _Gx, _Gy, _r) oid_secp256k1 = (1, 3, 132, 0, 10)

SECP256k1=ecdsa.curves.Curve("SECP256k1",curve_secp256k1, generator_secp256k1,

oid_secp256k1) ec_order = _r

curve = curve_secp256k1 generator = generator_secp256k1

def random_secret():

random_char = lambda: chr(random.randint(0, 255)) convert_to_int = lambda array:

int("".join(array).encode("hex"), 16)

byte_array = [random_char() for i in range(32)] return convert_to_int(byte_array)

def get_point_pubkey(point): if point.y() & 1:

key = '03' + '%064x' % point.x() else:

key = '02' + '%064x' % point.x() return key.decode('hex')

def get_point_pubkey_uncompressed(point): key='04'+

'%064x' % point.x() + '%064x' % point.y()

return key.decode('hex')

# Generate a new private key. secret = random_secret() print "Secret: ",secret

# Get the public key point. point = secret * generator print "EC point:",point

print "BTC public key:", get_point_pubkey(point).encode("hex")

# Given the point (x, y) we can create the object using: point1 = ecdsa.ellipticcurve.Point(curve, point.x(), point.y(), ec_order)

assert point1 == point

例 4-7 显示了运行脚本的结果。

例 4-7 安装 Python ECDSA 库,运行 ec_math.py 脚本

running the ec_math.py script

$ # Install Python PIP package manager

$ sudo apt-get install python-pip

$ # Install the Python ECDSA library

$ sudo pip install ecdsa

$ # Run the script

$ python ec-math.py Secret:

380908350159543588624811326288874439059062049959123782780

EC point: (70048853531867179489857750497606966272382583471322935454

624595540007269312627,

105262206478686743191060800263479589329920209527285803935

736021686045542353380)

BTC public key: 029ade3effb0a67d5c8609850d797366af428f4a0d5194cb221d80777 0a1522873

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180223A0JIXE00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码关注腾讯云开发者

领取腾讯云代金券