-
-
Save dazzaji/5d1f4482be1bfe107c46380f62bff2c7 to your computer and use it in GitHub Desktop.
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
import ecdsa | |
import ecdsa.der | |
import ecdsa.util | |
import hashlib | |
import os | |
import re | |
import struct | |
b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | |
def base58encode(n): | |
result = '' | |
while n > 0: | |
result = b58[n%58] + result | |
n /= 58 | |
return result | |
def base256decode(s): | |
result = 0 | |
for c in s: | |
result = result * 256 + ord(c) | |
return result | |
def countLeadingChars(s, ch): | |
count = 0 | |
for c in s: | |
if c == ch: | |
count += 1 | |
else: | |
break | |
return count | |
# https://en.bitcoin.it/wiki/Base58Check_encoding | |
def base58CheckEncode(version, payload): | |
s = chr(version) + payload | |
checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4] | |
result = s + checksum | |
leadingZeros = countLeadingChars(result, '\0') | |
return '1' * leadingZeros + base58encode(base256decode(result)) | |
def privateKeyToWif(key_hex): | |
return base58CheckEncode(0x80, key_hex.decode('hex')) | |
def privateKeyToPublicKey(s): | |
sk = ecdsa.SigningKey.from_string(s.decode('hex'), curve=ecdsa.SECP256k1) | |
vk = sk.verifying_key | |
return ('\04' + sk.verifying_key.to_string()).encode('hex') | |
def pubKeyToAddr(s): | |
ripemd160 = hashlib.new('ripemd160') | |
ripemd160.update(hashlib.sha256(s.decode('hex')).digest()) | |
return base58CheckEncode(0, ripemd160.digest()) | |
def keyToAddr(s): | |
return pubKeyToAddr(privateKeyToPublicKey(s)) | |
# Generate a random private key | |
private_key = os.urandom(32).encode('hex') | |
# You can verify the values on http://brainwallet.org/ | |
print "Secret Exponent (Uncompressed) : %s " % private_key | |
print "Private Key : %s " % privateKeyToWif(private_key) | |
print "Address : %s " % keyToAddr(private_key) | |
# Forked from: https://gist.github.com/dims/5000f40c539b6eac914f which was forked from https://gist.github.com/shirriff/8e6b5650361bbe78a055 | |
# Written about: here https://davanum.wordpress.com/2014/03/17/generating-a-bitcoin-private-key-and-address and here: http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment