<s> import hashlib <EOL> import ctypes <EOL> import ctypes . util <EOL> import sys <EOL> ssl = ctypes . cdll . LoadLibrary ( ctypes . util . find_library ( '<STR_LIT>' ) or '<STR_LIT>' ) <EOL> def check_result ( val , func , args ) : <EOL> if val == <NUM_LIT:0> : raise ValueError <EOL> else : return ctypes . c_void_p ( val ) <EOL> ssl . EC_KEY_new_by_curve_name . restype = ctypes . c_void_p <EOL> ssl . EC_KEY_new_by_curve_name . errcheck = check_result <EOL> class KEY : <EOL> def __init__ ( self ) : <EOL> NID_secp256k1 = <NUM_LIT> <EOL> self . k = ssl . EC_KEY_new_by_curve_name ( NID_secp256k1 ) <EOL> self . compressed = False <EOL> self . POINT_CONVERSION_COMPRESSED = <NUM_LIT:2> <EOL> self . POINT_CONVERSION_UNCOMPRESSED = <NUM_LIT:4> <EOL> def __del__ ( self ) : <EOL> if ssl : <EOL> ssl . EC_KEY_free ( self . k ) <EOL> self . k = None <EOL> def generate ( self , secret = None ) : <EOL> if secret : <EOL> self . prikey = secret <EOL> priv_key = ssl . BN_bin2bn ( secret , <NUM_LIT:32> , ssl . BN_new ( ) ) <EOL> group = ssl . EC_KEY_get0_group ( self . k ) <EOL> pub_key = ssl . EC_POINT_new ( group ) <EOL> ctx = ssl . BN_CTX_new ( ) <EOL> ssl . EC_POINT_mul ( group , pub_key , priv_key , None , None , ctx ) <EOL> ssl . EC_KEY_set_private_key ( self . k , priv_key ) <EOL> ssl . EC_KEY_set_public_key ( self . k , pub_key ) <EOL> ssl . EC_POINT_free ( pub_key ) <EOL> ssl . BN_CTX_free ( ctx ) <EOL> return self . k <EOL> else : <EOL> return ssl . EC_KEY_generate_key ( self . k ) <EOL> def get_pubkey ( self ) : <EOL> size = ssl . i2o_ECPublicKey ( self . k , <NUM_LIT:0> ) <EOL> mb = ctypes . create_string_buffer ( size ) <EOL> ssl . i2o_ECPublicKey ( self . k , ctypes . byref ( ctypes . pointer ( mb ) ) ) <EOL> return mb . raw <EOL> def get_secret ( self ) : <EOL> bn = ssl . EC_KEY_get0_private_key ( self . k ) ; <EOL> bytes = ( ssl . BN_num_bits ( bn ) + <NUM_LIT:7> ) / <NUM_LIT:8> <EOL> mb = ctypes . create_string_buffer ( bytes ) <EOL> n = ssl . BN_bn2bin ( bn , mb ) ; <EOL> return mb . raw . rjust ( <NUM_LIT:32> , chr ( <NUM_LIT:0> ) ) <EOL> def set_compressed ( self , compressed ) : <EOL> self . compressed = compressed <EOL> if compressed : <EOL> form = self . POINT_CONVERSION_COMPRESSED <EOL> else : <EOL> form = self . POINT_CONVERSION_UNCOMPRESSED <EOL> ssl . EC_KEY_set_conv_form ( self . k , form ) <EOL> def dhash ( s ) : <EOL> return hashlib . sha256 ( hashlib . sha256 ( s ) . digest ( ) ) . digest ( ) <EOL> def rhash ( s ) : <EOL> h1 = hashlib . new ( '<STR_LIT>' ) <EOL> h1 . update ( hashlib . sha256 ( s ) . digest ( ) ) <EOL> return h1 . digest ( ) <EOL> b58_digits = '<STR_LIT>' <EOL> def base58_encode ( n ) : <EOL> l = [ ] <EOL> while n > <NUM_LIT:0> : <EOL> n , r = divmod ( n , <NUM_LIT> ) <EOL> l . insert ( <NUM_LIT:0> , ( b58_digits [ r ] ) ) <EOL> return '<STR_LIT>' . join ( l ) <EOL> def base58_decode ( s ) : <EOL> n = <NUM_LIT:0> <EOL> for ch in s : <EOL> n *= <NUM_LIT> <EOL> digit = b58_digits . index ( ch ) <EOL> n += digit <EOL> return n <EOL> def base58_encode_padded ( s ) : <EOL> res = base58_encode ( int ( '<STR_LIT>' + s . encode ( '<STR_LIT>' ) , <NUM_LIT:16> ) ) <EOL> pad = <NUM_LIT:0> <EOL> for c in s : <EOL> if c == chr ( <NUM_LIT:0> ) : <EOL> pad += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> return b58_digits [ <NUM_LIT:0> ] * pad + res <EOL> def base58_decode_padded ( s ) : <EOL> pad = <NUM_LIT:0> <EOL> for c in s : <EOL> if c == b58_digits [ <NUM_LIT:0> ] : <EOL> pad += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> h = '<STR_LIT>' % base58_decode ( s ) <EOL> if len ( h ) % <NUM_LIT:2> : <EOL> h = '<STR_LIT:0>' + h <EOL> res = h . decode ( '<STR_LIT>' ) <EOL> return chr ( <NUM_LIT:0> ) * pad + res <EOL> def base58_check_encode ( s , version = <NUM_LIT:0> ) : <EOL> vs = chr ( version ) + s <EOL> check = dhash ( vs ) [ : <NUM_LIT:4> ] <EOL> return base58_encode_padded ( vs + check ) <EOL> def base58_check_decode ( s , version = <NUM_LIT:0> ) : <EOL> k = base58_decode_padded ( s ) <EOL> v0 , data , check0 = k [ <NUM_LIT:0> ] , k [ <NUM_LIT:1> : - <NUM_LIT:4> ] , k [ - <NUM_LIT:4> : ] <EOL> check1 = dhash ( v0 + data ) [ : <NUM_LIT:4> ] <EOL> if check0 != check1 : <EOL> raise BaseException ( '<STR_LIT>' ) <EOL> if version != ord ( v0 ) : <EOL> raise BaseException ( '<STR_LIT>' ) <EOL> return data <EOL> def gen_eckey ( passphrase = None , secret = None , pkey = None , compressed = False , rounds = <NUM_LIT:1> , version = <NUM_LIT:0> ) : <EOL> k = KEY ( ) <EOL> if passphrase : <EOL> secret = passphrase . encode ( '<STR_LIT:utf8>' ) <EOL> for i in xrange ( rounds ) : <EOL> secret = hashlib . sha256 ( secret ) . digest ( ) <EOL> if pkey : <EOL> secret = base58_check_decode ( pkey , <NUM_LIT> + version ) <EOL> compressed = len ( secret ) == <NUM_LIT> <EOL> secret = secret [ <NUM_LIT:0> : <NUM_LIT:32> ] <EOL> k . generate ( secret ) <EOL> k . set_compressed ( compressed ) <EOL> return k <EOL> def create_addr ( k , version = <NUM_LIT:0> ) : <EOL> pubkey = k . get_pubkey ( ) <EOL> secret = k . get_secret ( ) <EOL> hash160 = rhash ( pubkey ) <EOL> addr = base58_check_encode ( hash160 , version ) <EOL> payload = secret <EOL> if k . compressed : <EOL> payload = secret + chr ( <NUM_LIT:1> ) <EOL> pkey = base58_check_encode ( payload , <NUM_LIT> + version ) <EOL> return [ addr , pkey ] <EOL> def get_addr ( otherversion = <NUM_LIT:0> ) : <EOL> key_pair = create_addr ( gen_eckey ( compressed = True , version = otherversion ) , version = otherversion ) <EOL> return key_pair <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import optparse <EOL> parser = optparse . OptionParser ( usage = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' ) <EOL> '''<STR_LIT>''' <EOL> ( options , args ) = parser . parse_args ( ) <EOL> print '<STR_LIT>' % options . otherversion <EOL> get_addr ( int ( options . otherversion ) ) </s>
