<s> from __future__ import print_function <EOL> import argparse <EOL> import json <EOL> import re <EOL> import subprocess <EOL> import sys <EOL> from pycoin import encoding <EOL> from pycoin . ecdsa import is_public_pair_valid , generator_secp256k1 , public_pair_for_x , secp256k1 <EOL> from pycoin . serialize import b2h , h2b <EOL> from pycoin . key import Key <EOL> from pycoin . key . BIP32Node import BIP32Node <EOL> from pycoin . networks import full_network_name_for_netcode , network_name_for_netcode , NETWORK_NAMES <EOL> SEC_RE = re . compile ( r"<STR_LIT>" ) <EOL> HASH160_RE = re . compile ( r"<STR_LIT>" ) <EOL> def gpg_entropy ( ) : <EOL> try : <EOL> output = subprocess . Popen ( <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:2>" , "<STR_LIT>" ] , stdout = subprocess . PIPE ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> return output <EOL> except OSError : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> return b'<STR_LIT>' <EOL> def get_entropy ( ) : <EOL> entropy = bytearray ( ) <EOL> try : <EOL> entropy . extend ( gpg_entropy ( ) ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , file = sys . stdout ) <EOL> try : <EOL> entropy . extend ( open ( "<STR_LIT>" , "<STR_LIT:rb>" ) . read ( <NUM_LIT:64> ) ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , file = sys . stdout ) <EOL> entropy = bytes ( entropy ) <EOL> if len ( entropy ) < <NUM_LIT:64> : <EOL> raise OSError ( "<STR_LIT>" ) <EOL> return entropy <EOL> def parse_as_number ( s ) : <EOL> try : <EOL> return int ( s ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> return int ( s , <NUM_LIT:16> ) <EOL> except ValueError : <EOL> pass <EOL> def parse_as_secret_exponent ( s ) : <EOL> v = parse_as_number ( s ) <EOL> if v and v < secp256k1 . _r : <EOL> return v <EOL> def parse_as_public_pair ( s ) : <EOL> for c in "<STR_LIT>" : <EOL> if c in s : <EOL> s0 , s1 = s . split ( c , <NUM_LIT:1> ) <EOL> v0 = parse_as_number ( s0 ) <EOL> if v0 : <EOL> if s1 in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return public_pair_for_x ( generator_secp256k1 , v0 , is_even = ( s1 == '<STR_LIT>' ) ) <EOL> v1 = parse_as_number ( s1 ) <EOL> if v1 : <EOL> if not is_public_pair_valid ( generator_secp256k1 , ( v0 , v1 ) ) : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return ( v0 , v1 ) <EOL> def create_output ( item , key , subkey_path = None ) : <EOL> output_dict = { } <EOL> output_order = [ ] <EOL> def add_output ( json_key , value = None , human_readable_key = None ) : <EOL> if human_readable_key is None : <EOL> human_readable_key = json_key . replace ( "<STR_LIT:_>" , "<STR_LIT:U+0020>" ) <EOL> if value : <EOL> output_dict [ json_key . strip ( ) . lower ( ) ] = value <EOL> output_order . append ( ( json_key . lower ( ) , human_readable_key ) ) <EOL> network_name = network_name_for_netcode ( key . _netcode ) <EOL> full_network_name = full_network_name_for_netcode ( key . _netcode ) <EOL> add_output ( "<STR_LIT:input>" , item ) <EOL> add_output ( "<STR_LIT>" , full_network_name ) <EOL> add_output ( "<STR_LIT>" , key . _netcode ) <EOL> if hasattr ( key , "<STR_LIT>" ) : <EOL> if subkey_path : <EOL> add_output ( "<STR_LIT>" , subkey_path ) <EOL> add_output ( "<STR_LIT>" , key . wallet_key ( as_private = key . is_private ( ) ) ) <EOL> if key . is_private ( ) : <EOL> add_output ( "<STR_LIT>" , key . wallet_key ( as_private = False ) ) <EOL> child_number = key . child_index ( ) <EOL> if child_number >= <NUM_LIT> : <EOL> wc = child_number - <NUM_LIT> <EOL> child_index = "<STR_LIT>" % ( wc , child_number ) <EOL> else : <EOL> child_index = "<STR_LIT>" % child_number <EOL> add_output ( "<STR_LIT>" , "<STR_LIT>" % key . tree_depth ( ) ) <EOL> add_output ( "<STR_LIT>" , b2h ( key . fingerprint ( ) ) ) <EOL> add_output ( "<STR_LIT>" , b2h ( key . parent_fingerprint ( ) ) , "<STR_LIT>" ) <EOL> add_output ( "<STR_LIT>" , child_index ) <EOL> add_output ( "<STR_LIT>" , b2h ( key . chain_code ( ) ) ) <EOL> add_output ( "<STR_LIT>" , "<STR_LIT:yes>" if key . is_private ( ) else "<STR_LIT>" ) <EOL> secret_exponent = key . secret_exponent ( ) <EOL> if secret_exponent : <EOL> add_output ( "<STR_LIT>" , '<STR_LIT>' % secret_exponent ) <EOL> add_output ( "<STR_LIT>" , '<STR_LIT>' % secret_exponent , "<STR_LIT>" ) <EOL> add_output ( "<STR_LIT>" , key . wif ( use_uncompressed = False ) ) <EOL> add_output ( "<STR_LIT>" , key . wif ( use_uncompressed = True ) , "<STR_LIT>" ) <EOL> public_pair = key . public_pair ( ) <EOL> if public_pair : <EOL> add_output ( "<STR_LIT>" , '<STR_LIT>' % public_pair [ <NUM_LIT:0> ] ) <EOL> add_output ( "<STR_LIT>" , '<STR_LIT>' % public_pair [ <NUM_LIT:1> ] ) <EOL> add_output ( "<STR_LIT>" , '<STR_LIT>' % public_pair [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> add_output ( "<STR_LIT>" , '<STR_LIT>' % public_pair [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> add_output ( "<STR_LIT>" , "<STR_LIT>" if ( public_pair [ <NUM_LIT:1> ] & <NUM_LIT:1> ) else "<STR_LIT>" ) <EOL> add_output ( "<STR_LIT>" , b2h ( key . sec ( use_uncompressed = False ) ) ) <EOL> add_output ( "<STR_LIT>" , b2h ( key . sec ( use_uncompressed = True ) ) , "<STR_LIT>" ) <EOL> hash160_c = key . hash160 ( use_uncompressed = False ) <EOL> if hash160_c : <EOL> add_output ( "<STR_LIT>" , b2h ( hash160_c ) ) <EOL> hash160_u = key . hash160 ( use_uncompressed = True ) <EOL> if hash160_u : <EOL> add_output ( "<STR_LIT>" , b2h ( hash160_u ) , "<STR_LIT>" ) <EOL> if hash160_c : <EOL> address = key . address ( use_uncompressed = False ) <EOL> add_output ( "<STR_LIT:address>" , address , "<STR_LIT>" % network_name ) <EOL> output_dict [ "<STR_LIT>" % key . _netcode ] = address <EOL> if hash160_u : <EOL> address = key . address ( use_uncompressed = True ) <EOL> add_output ( "<STR_LIT>" , address , "<STR_LIT>" % network_name ) <EOL> output_dict [ "<STR_LIT>" % key . _netcode ] = address <EOL> return output_dict , output_order <EOL> def dump_output ( output_dict , output_order ) : <EOL> print ( '<STR_LIT>' ) <EOL> max_length = max ( len ( v [ <NUM_LIT:1> ] ) for v in output_order ) <EOL> for key , hr_key in output_order : <EOL> space_padding = '<STR_LIT:U+0020>' * ( <NUM_LIT:1> + max_length - len ( hr_key ) ) <EOL> val = output_dict . get ( key ) <EOL> if val is None : <EOL> print ( hr_key ) <EOL> else : <EOL> if len ( val ) > <NUM_LIT> : <EOL> val = "<STR_LIT>" % ( val [ : <NUM_LIT> ] , '<STR_LIT:U+0020>' * ( <NUM_LIT:5> + max_length ) , val [ <NUM_LIT> : ] ) <EOL> print ( "<STR_LIT>" % ( hr_key , space_padding , val ) ) <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> epilog = ( '<STR_LIT>' + <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( i , full_network_name_for_netcode ( i ) ) for i in NETWORK_NAMES ] ) ) <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , "<STR_LIT>" , help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , choices = NETWORK_NAMES ) <EOL> parser . add_argument ( "<STR_LIT>" , help = '<STR_LIT>' , <EOL> default = None , choices = NETWORK_NAMES ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , nargs = "<STR_LIT:+>" , help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> if args . override_network : <EOL> args . network = args . override_network <EOL> def _create ( _ ) : <EOL> max_retries = <NUM_LIT:64> <EOL> for _ in range ( max_retries ) : <EOL> try : <EOL> return BIP32Node . from_master_secret ( get_entropy ( ) , netcode = args . network ) <EOL> except ValueError as e : <EOL> continue <EOL> raise e <EOL> PREFIX_TRANSFORMS = ( <EOL> ( "<STR_LIT>" , lambda s : <EOL> BIP32Node . from_master_secret ( s . encode ( "<STR_LIT:utf8>" ) , netcode = args . network ) ) , <EOL> ( "<STR_LIT>" , lambda s : <EOL> BIP32Node . from_master_secret ( h2b ( s ) , netcode = args . network ) ) , <EOL> ( "<STR_LIT>" , _create ) , <EOL> ) <EOL> for item in args . item : <EOL> key = None <EOL> for k , f in PREFIX_TRANSFORMS : <EOL> if item . startswith ( k ) : <EOL> try : <EOL> key = f ( item [ len ( k ) : ] ) <EOL> break <EOL> except Exception : <EOL> pass <EOL> else : <EOL> try : <EOL> key = Key . from_text ( item ) <EOL> except encoding . EncodingError : <EOL> pass <EOL> if key is None : <EOL> secret_exponent = parse_as_secret_exponent ( item ) <EOL> if secret_exponent : <EOL> key = Key ( secret_exponent = secret_exponent , netcode = args . network ) <EOL> if SEC_RE . match ( item ) : <EOL> key = Key . from_sec ( h2b ( item ) ) <EOL> if key is None : <EOL> public_pair = parse_as_public_pair ( item ) <EOL> if public_pair : <EOL> key = Key ( public_pair = public_pair , netcode = args . network ) <EOL> if HASH160_RE . match ( item ) : <EOL> key = Key ( hash160 = h2b ( item ) , netcode = args . network ) <EOL> if key is None : <EOL> print ( "<STR_LIT>" % item , file = sys . stderr ) <EOL> continue <EOL> if args . override_network : <EOL> key . _netcode = args . override_network <EOL> for key in key . subkeys ( args . subkey or "<STR_LIT>" ) : <EOL> if args . public : <EOL> key = key . public_copy ( ) <EOL> output_dict , output_order = create_output ( item , key ) <EOL> if args . json : <EOL> print ( json . dumps ( output_dict , indent = <NUM_LIT:3> , sort_keys = True ) ) <EOL> elif args . wallet : <EOL> print ( output_dict [ "<STR_LIT>" ] ) <EOL> elif args . wif : <EOL> print ( output_dict [ "<STR_LIT>" if args . uncompressed else "<STR_LIT>" ] ) <EOL> elif args . address : <EOL> print ( output_dict [ "<STR_LIT:address>" + ( "<STR_LIT>" if args . uncompressed else "<STR_LIT>" ) ] ) <EOL> else : <EOL> dump_output ( output_dict , output_order ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
