<s> import hashlib <EOL> import hmac <EOL> import os <EOL> from pycoin . ecdsa . secp256k1 import generator_secp256k1 as BasePoint <EOL> from pycoin . encoding import from_bytes_32 , public_pair_to_bitcoin_address <EOL> from address import AddressRecord , LooseAddressRecord <EOL> from asset import AssetDefinition <EOL> from coloredcoinlib import ColorSet <EOL> class DeterministicAddressRecord ( AddressRecord ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( DeterministicAddressRecord , self ) . __init__ ( ** kwargs ) <EOL> if len ( self . color_set . get_data ( ) ) == <NUM_LIT:0> : <EOL> color_string = "<STR_LIT>" <EOL> else : <EOL> color_string = self . color_set . get_hash_string ( ) <EOL> self . index = kwargs . get ( '<STR_LIT:index>' ) <EOL> h = hmac . new ( str ( kwargs [ '<STR_LIT>' ] ) , <EOL> "<STR_LIT>" % ( color_string , self . index ) , hashlib . sha256 ) <EOL> string = h . digest ( ) <EOL> self . rawPrivKey = from_bytes_32 ( string ) <EOL> self . publicPoint = BasePoint * self . rawPrivKey <EOL> self . address = public_pair_to_bitcoin_address ( <EOL> self . publicPoint . pair ( ) , <EOL> compressed = False , <EOL> address_prefix = self . prefix <EOL> ) <EOL> class DWalletAddressManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , colormap , config ) : <EOL> """<STR_LIT>""" <EOL> self . config = config <EOL> self . testnet = config . get ( '<STR_LIT>' , False ) <EOL> self . colormap = colormap <EOL> self . addresses = [ ] <EOL> params = config . get ( '<STR_LIT>' , None ) <EOL> if params is None : <EOL> params = self . init_new_wallet ( ) <EOL> self . master_key = config [ '<STR_LIT>' ] <EOL> self . genesis_color_sets = params [ '<STR_LIT>' ] <EOL> self . color_set_states = params [ '<STR_LIT>' ] <EOL> for i , color_desc_list in enumerate ( self . genesis_color_sets ) : <EOL> addr = self . get_genesis_address ( i ) <EOL> addr . color_set = ColorSet ( self . colormap , <EOL> color_desc_list ) <EOL> self . addresses . append ( addr ) <EOL> for color_set_st in self . color_set_states : <EOL> color_desc_list = color_set_st [ '<STR_LIT>' ] <EOL> max_index = color_set_st [ '<STR_LIT>' ] <EOL> color_set = ColorSet ( self . colormap , color_desc_list ) <EOL> params = { <EOL> '<STR_LIT>' : self . testnet , <EOL> '<STR_LIT>' : self . master_key , <EOL> '<STR_LIT>' : color_set <EOL> } <EOL> for index in xrange ( max_index + <NUM_LIT:1> ) : <EOL> params [ '<STR_LIT:index>' ] = index <EOL> self . addresses . append ( DeterministicAddressRecord ( ** params ) ) <EOL> for addr_params in config . get ( '<STR_LIT>' , [ ] ) : <EOL> addr_params [ '<STR_LIT>' ] = self . testnet <EOL> addr_params [ '<STR_LIT>' ] = ColorSet ( self . colormap , <EOL> addr_params [ '<STR_LIT>' ] ) <EOL> address = LooseAddressRecord ( ** addr_params ) <EOL> self . addresses . append ( address ) <EOL> def init_new_wallet ( self ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT>' in self . config : <EOL> master_key = os . urandom ( <NUM_LIT:64> ) . encode ( '<STR_LIT>' ) <EOL> self . config [ '<STR_LIT>' ] = master_key <EOL> dwam_params = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> self . config [ '<STR_LIT>' ] = dwam_params <EOL> return dwam_params <EOL> def increment_max_index_for_color_set ( self , color_set ) : <EOL> """<STR_LIT>""" <EOL> for color_set_st in self . color_set_states : <EOL> color_desc_list = color_set_st [ '<STR_LIT>' ] <EOL> max_index = color_set_st [ '<STR_LIT>' ] <EOL> cur_color_set = ColorSet ( self . colormap , <EOL> color_desc_list ) <EOL> if cur_color_set . equals ( color_set ) : <EOL> max_index += <NUM_LIT:1> <EOL> color_set_st [ '<STR_LIT>' ] = max_index <EOL> return max_index <EOL> self . color_set_states . append ( { "<STR_LIT>" : color_set . get_data ( ) , <EOL> "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> return <NUM_LIT:0> <EOL> def get_new_address ( self , asset_or_color_set ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( asset_or_color_set , AssetDefinition ) : <EOL> color_set = asset_or_color_set . get_color_set ( ) <EOL> else : <EOL> color_set = asset_or_color_set <EOL> index = self . increment_max_index_for_color_set ( color_set ) <EOL> na = DeterministicAddressRecord ( master_key = self . master_key , <EOL> color_set = color_set , index = index , <EOL> testnet = self . testnet ) <EOL> self . addresses . append ( na ) <EOL> self . update_config ( ) <EOL> return na <EOL> def get_genesis_address ( self , genesis_index ) : <EOL> """<STR_LIT>""" <EOL> return DeterministicAddressRecord ( <EOL> master_key = self . master_key , <EOL> color_set = ColorSet ( self . colormap , [ ] ) , <EOL> index = genesis_index , testnet = self . testnet ) <EOL> def get_new_genesis_address ( self ) : <EOL> """<STR_LIT>""" <EOL> index = len ( self . genesis_color_sets ) <EOL> self . genesis_color_sets . append ( [ ] ) <EOL> self . update_config ( ) <EOL> address = self . get_genesis_address ( index ) <EOL> address . index = index <EOL> self . addresses . append ( address ) <EOL> return address <EOL> def update_genesis_address ( self , address , color_set ) : <EOL> """<STR_LIT>""" <EOL> assert address . color_set . color_id_set == set ( [ ] ) <EOL> address . color_set = color_set <EOL> self . genesis_color_sets [ address . index ] = color_set . get_data ( ) <EOL> self . update_config ( ) <EOL> def get_some_address ( self , color_set ) : <EOL> """<STR_LIT>""" <EOL> acs = self . get_addresses_for_color_set ( color_set ) <EOL> if acs : <EOL> return acs [ <NUM_LIT:0> ] <EOL> else : <EOL> return self . get_new_address ( color_set ) <EOL> def get_change_address ( self , color_set ) : <EOL> """<STR_LIT>""" <EOL> return self . get_some_address ( color_set ) <EOL> def get_all_addresses ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . addresses <EOL> def find_address_record ( self , address ) : <EOL> for address_rec in self . addresses : <EOL> if address_rec . get_address ( ) == address : <EOL> return address_rec <EOL> return None <EOL> def get_addresses_for_color_set ( self , color_set ) : <EOL> """<STR_LIT>""" <EOL> return [ addr for addr in self . addresses <EOL> if color_set . intersects ( addr . get_color_set ( ) ) ] <EOL> def update_config ( self ) : <EOL> """<STR_LIT>""" <EOL> dwam_params = { <EOL> '<STR_LIT>' : self . genesis_color_sets , <EOL> '<STR_LIT>' : self . color_set_states <EOL> } <EOL> self . config [ '<STR_LIT>' ] = dwam_params </s>
