personal key – Generate BTC Compressed Handle

0
23


The beneath code generate uncompressed handle with and the Personal key for this uncompressed handle. The best way to generate compressed handle with it is personal key ?
I do know there’s packages like bitcoin and different generate each, however they generate it from random numbers and the beneath code generate it from phrase:

Thanks upfront

=============================

import codecs
import hashlib
import ecdsa

class BrainWallet:

@staticmethod
def generate_address_from_passphrase(passphrase):
    private_key = str(hashlib.sha256(
        passphrase.encode('utf-8')).hexdigest())
    handle =  BrainWallet.generate_address_from_private_key(private_key)
    return private_key, handle

@staticmethod
def generate_address_from_private_key(private_key):
    public_key = BrainWallet.__private_to_public(private_key)
    handle = BrainWallet.__public_to_address(public_key)
    return handle

@staticmethod
def __private_to_public(private_key):
    private_key_bytes = codecs.decode(private_key, 'hex')
    # Get ECDSA public key
    key = ecdsa.SigningKey.from_string(
        private_key_bytes, curve=ecdsa.SECP256k1).verifying_key
    key_bytes = key.to_string()
    key_hex = codecs.encode(key_bytes, 'hex')
    # Add bitcoin byte
    bitcoin_byte = b'04'
    public_key = bitcoin_byte + key_hex
    return public_key

@staticmethod
def __public_to_address(public_key):
    public_key_bytes = codecs.decode(public_key, 'hex')
    # Run SHA256 for the general public key
    sha256_bpk = hashlib.sha256(public_key_bytes)
    sha256_bpk_digest = sha256_bpk.digest()
    # Run ripemd160 for the SHA256
    ripemd160_bpk = hashlib.new('ripemd160')
    ripemd160_bpk.replace(sha256_bpk_digest)
    ripemd160_bpk_digest = ripemd160_bpk.digest()
    ripemd160_bpk_hex = codecs.encode(ripemd160_bpk_digest, 'hex')
    # Add community byte
    network_byte = b'00'
    network_bitcoin_public_key = network_byte + ripemd160_bpk_hex
    network_bitcoin_public_key_bytes = codecs.decode(
        network_bitcoin_public_key, 'hex')
    # Double SHA256 to get checksum
    sha256_nbpk = hashlib.sha256(network_bitcoin_public_key_bytes)
    sha256_nbpk_digest = sha256_nbpk.digest()
    sha256_2_nbpk = hashlib.sha256(sha256_nbpk_digest)
    sha256_2_nbpk_digest = sha256_2_nbpk.digest()
    sha256_2_hex = codecs.encode(sha256_2_nbpk_digest, 'hex')
    checksum = sha256_2_hex[:8]
    # Concatenate public key and checksum to get the handle
    address_hex = (network_bitcoin_public_key + checksum).decode('utf-8')
    pockets = BrainWallet.base58(address_hex)
    return pockets

@staticmethod
def base58(address_hex):
    alphabet="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    b58_string = ''
    # Get the variety of main zeros and convert hex to decimal
    leading_zeros = len(address_hex) - len(address_hex.lstrip('0'))
    # Convert hex to decimal
    address_int = int(address_hex, 16)
    # Append digits to the beginning of string
    whereas address_int > 0:
        digit = address_int % 58
        digit_char = alphabet[digit]
        b58_string = digit_char + b58_string
        address_int //= 58
    # Add '1' for every 2 main zeros
    ones = leading_zeros // 2
    for one in vary(ones):
        b58_string = '1' + b58_string
    return b58_string
    

passphrase=”password”
pockets = BrainWallet()
private_key, handle = pockets.generate_address_from_passphrase(passphrase)
print(f’passphrase: {passphrase}’)
print(f’personal key: {private_key}’)
print(f’handle: {handle}’)



Supply hyperlink

Leave a reply