<s> """<STR_LIT>""" <EOL> import os <EOL> from config import DASHVEND_DIR , BIP32_MAINNET_SEED , BIP32_TESTNET_SEED <EOL> from logger import info <EOL> from pycoin . key import Key <EOL> class Bip32Chain ( object ) : <EOL> def __init__ ( self , mainnet , dashrpc ) : <EOL> self . dashrpc = dashrpc <EOL> self . mainnet = mainnet <EOL> self . _init_state_dir ( os . path . join ( DASHVEND_DIR , '<STR_LIT:state>' ) ) <EOL> self . key = Key . from_text ( <EOL> mainnet and BIP32_MAINNET_SEED or BIP32_TESTNET_SEED ) <EOL> self . _init_next_address ( ) <EOL> def _init_state_dir ( self , statedir ) : <EOL> """<STR_LIT>""" <EOL> self . statedir = statedir <EOL> if not os . path . isdir ( statedir ) : <EOL> os . makedirs ( statedir ) <EOL> index_filename = ( <EOL> '<STR_LIT>' + ( self . mainnet and '<STR_LIT>' or '<STR_LIT>' ) ) <EOL> self . bip32_index_file = os . path . join ( statedir , index_filename ) <EOL> if not os . path . isfile ( self . bip32_index_file ) : <EOL> with open ( self . bip32_index_file , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT:0>' ) <EOL> def _index_state ( self , index = None ) : <EOL> """<STR_LIT>""" <EOL> with open ( self . bip32_index_file , '<STR_LIT>' ) as f : <EOL> if index is not None : <EOL> f . write ( str ( index ) ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> return f . read ( ) <EOL> def get_bip32_address_info ( self , index ) : <EOL> """<STR_LIT>""" <EOL> addr = self . key . subkey ( index ) . address ( use_uncompressed = False ) <EOL> return { <EOL> "<STR_LIT:index>" : index , <EOL> "<STR_LIT>" : addr , <EOL> "<STR_LIT>" : float ( <EOL> self . dashrpc . _proxy . getreceivedbyaddress ( addr ) ) <EOL> } <EOL> def _init_next_address ( self , increment = False ) : <EOL> """<STR_LIT>""" <EOL> r = self . dashrpc . _proxy <EOL> index = int ( self . _index_state ( ) ) <EOL> if increment : <EOL> index += <NUM_LIT:1> <EOL> unused_found = False <EOL> while ( not unused_found ) : <EOL> addr = self . get_bip32_address_info ( index ) <EOL> if addr [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> index += <NUM_LIT:1> <EOL> continue <EOL> unused_found = True <EOL> r . importaddress ( addr [ '<STR_LIT>' ] , '<STR_LIT>' + str ( index ) , False ) <EOL> self . _index_state ( index ) <EOL> self . next_address = addr <EOL> def get_next_address ( self , increment = False ) : <EOL> self . _init_next_address ( increment ) <EOL> info ( "<STR_LIT>" % self . next_address [ '<STR_LIT>' ] ) <EOL> return self . next_address [ '<STR_LIT>' ] </s>
