<s> """<STR_LIT>""" <EOL> import os <EOL> import json <EOL> import hashlib <EOL> import ecdsa <EOL> from binascii import hexlify , unhexlify <EOL> from ecdsa . keys import SigningKey <EOL> from utilitybelt import is_int , dev_random_entropy , dev_urandom_entropy <EOL> from bitcoin import compress , encode_privkey , get_privkey_format <EOL> from . errors import _errors <EOL> from . formatcheck import * <EOL> from . b58check import b58check_encode , b58check_decode <EOL> from . publickey import BitcoinPublicKey , PUBKEY_MAGIC_BYTE <EOL> from . passphrases import create_passphrase <EOL> def random_secret_exponent ( curve_order ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> random_hex = hexlify ( dev_random_entropy ( <NUM_LIT:32> ) ) <EOL> random_int = int ( random_hex , <NUM_LIT:16> ) <EOL> if random_int >= <NUM_LIT:1> and random_int < curve_order : <EOL> break <EOL> return random_int <EOL> class BitcoinPrivateKey ( ) : <EOL> _curve = ecdsa . curves . SECP256k1 <EOL> _hash_function = hashlib . sha256 <EOL> _pubkeyhash_version_byte = <NUM_LIT:0> <EOL> @ classmethod <EOL> def wif_version_byte ( cls ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> return cls . _wif_version_byte <EOL> return ( cls . _pubkeyhash_version_byte + <NUM_LIT> ) % <NUM_LIT> <EOL> def __init__ ( self , private_key = None , compressed = False ) : <EOL> """<STR_LIT>""" <EOL> self . _compressed = compressed <EOL> if not private_key : <EOL> secret_exponent = random_secret_exponent ( self . _curve . order ) <EOL> else : <EOL> secret_exponent = encode_privkey ( private_key , '<STR_LIT>' ) <EOL> if get_privkey_format ( private_key ) . endswith ( '<STR_LIT>' ) : <EOL> self . _compressed = True <EOL> if not is_secret_exponent ( secret_exponent , self . _curve . order ) : <EOL> raise IndexError ( _errors [ "<STR_LIT>" ] ) <EOL> self . _ecdsa_private_key = ecdsa . keys . SigningKey . from_secret_exponent ( <EOL> secret_exponent , self . _curve , self . _hash_function <EOL> ) <EOL> @ classmethod <EOL> def from_passphrase ( cls , passphrase = None ) : <EOL> """<STR_LIT>""" <EOL> if not passphrase : <EOL> while True : <EOL> passphrase = create_passphrase ( bits_of_entropy = <NUM_LIT> ) <EOL> hex_private_key = hashlib . sha256 ( passphrase ) . hexdigest ( ) <EOL> if int ( hex_private_key , <NUM_LIT:16> ) < cls . _curve . order : <EOL> break <EOL> else : <EOL> hex_private_key = hashlib . sha256 ( passphrase ) . hexdigest ( ) <EOL> if not ( int ( hex_private_key , <NUM_LIT:16> ) < cls . _curve . order ) : <EOL> raise ValueError ( _errors [ "<STR_LIT>" ] ) <EOL> keypair = cls ( hex_private_key ) <EOL> keypair . _passphrase = passphrase <EOL> return keypair <EOL> def to_bin ( self ) : <EOL> if self . _compressed : <EOL> return encode_privkey ( <EOL> self . _ecdsa_private_key . to_string ( ) , '<STR_LIT>' ) <EOL> else : <EOL> return self . _ecdsa_private_key . to_string ( ) <EOL> def to_hex ( self ) : <EOL> if self . _compressed : <EOL> return encode_privkey ( <EOL> self . _ecdsa_private_key . to_string ( ) , '<STR_LIT>' ) <EOL> else : <EOL> return hexlify ( self . to_bin ( ) ) <EOL> def to_wif ( self ) : <EOL> if self . _compressed : <EOL> return encode_privkey ( <EOL> self . _ecdsa_private_key . to_string ( ) , '<STR_LIT>' ) <EOL> else : <EOL> return b58check_encode ( <EOL> self . to_bin ( ) , version_byte = self . wif_version_byte ( ) ) <EOL> def to_pem ( self ) : <EOL> return self . _ecdsa_private_key . to_pem ( ) <EOL> def to_der ( self ) : <EOL> return hexlify ( self . _ecdsa_private_key . to_der ( ) ) <EOL> def public_key ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> ecdsa_public_key = self . _ecdsa_private_key . get_verifying_key ( ) <EOL> bin_public_key_string = PUBKEY_MAGIC_BYTE + ecdsa_public_key . to_string ( ) <EOL> if self . _compressed : <EOL> bin_public_key_string = compress ( bin_public_key_string ) <EOL> self . _public_key = BitcoinPublicKey ( <EOL> bin_public_key_string , <EOL> version_byte = self . _pubkeyhash_version_byte ) <EOL> return self . _public_key <EOL> def passphrase ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _passphrase <EOL> else : <EOL> raise Exception ( _errors [ "<STR_LIT>" ] ) <EOL> class LitecoinPrivateKey ( BitcoinPrivateKey ) : <EOL> _pubkeyhash_version_byte = <NUM_LIT> <EOL> class NamecoinPrivateKey ( BitcoinPrivateKey ) : <EOL> _pubkeyhash_version_byte = <NUM_LIT> </s>
