Last active
September 3, 2017 17:59
-
-
Save chm0815/8c5b521496a2c6402b38f0bd568ed3f7 to your computer and use it in GitHub Desktop.
generate a bitcoin address in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# generate a bitcoin address with python | |
# author: Christoph Maurer 2017 | |
# | |
# install: | |
# easy_install ecdsa | |
# pip install base58 | |
# | |
from ecdsa import SigningKey, SECP256k1 | |
import hashlib | |
import base58 | |
import binascii | |
def generate_bitcoin_address(): | |
sk = SigningKey.generate(curve=SECP256k1) | |
vk = sk.get_verifying_key() | |
private_key = sk.to_string() | |
public_key = '\x04' + vk.to_string() #0x04 = uncompressed key | |
h = hashlib.new('ripemd160') | |
h.update(hashlib.sha256(public_key).digest()) | |
fingerprint = '\x00' + h.digest() # 0x00 = version byte | |
checksum = hashlib.sha256(hashlib.sha256(fingerprint).digest()).digest() | |
address = base58.b58encode(fingerprint + checksum[:4]) | |
return (address, private_key, public_key) | |
(address, private_key, public_key) = generate_bitcoin_address() | |
print "address = %s" % (address) | |
print "public key = %s" % (binascii.hexlify(public_key)) | |
print "private key = %s" % (binascii.hexlify(private_key)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment