<s> import time , thread , sys , socket , os <EOL> import urllib2 , json <EOL> import Queue <EOL> import sqlite3 <EOL> from electrum import Wallet , WalletStorage , SimpleConfig , Network , set_verbosity <EOL> set_verbosity ( False ) <EOL> import ConfigParser <EOL> config = ConfigParser . ConfigParser ( ) <EOL> config . read ( "<STR_LIT>" ) <EOL> my_password = config . get ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> my_host = config . get ( '<STR_LIT>' , '<STR_LIT:host>' ) <EOL> my_port = config . getint ( '<STR_LIT>' , '<STR_LIT:port>' ) <EOL> database = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> received_url = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> expired_url = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> cb_password = config . get ( '<STR_LIT>' , '<STR_LIT:password>' ) <EOL> wallet_path = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> master_public_key = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> master_chain = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> pending_requests = { } <EOL> num = <NUM_LIT:0> <EOL> def check_create_table ( conn ) : <EOL> global num <EOL> c = conn . cursor ( ) <EOL> c . execute ( "<STR_LIT>" ) <EOL> data = c . fetchall ( ) <EOL> if not data : <EOL> c . execute ( """<STR_LIT>""" ) <EOL> conn . commit ( ) <EOL> c . execute ( "<STR_LIT>" ) <EOL> num = c . fetchone ( ) [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" , num <EOL> def on_wallet_update ( ) : <EOL> for addr , v in pending_requests . items ( ) : <EOL> h = wallet . history . get ( addr , [ ] ) <EOL> requested_amount = v . get ( '<STR_LIT>' ) <EOL> requested_confs = v . get ( '<STR_LIT>' ) <EOL> value = <NUM_LIT:0> <EOL> for tx_hash , tx_height in h : <EOL> tx = wallet . transactions . get ( tx_hash ) <EOL> if not tx : continue <EOL> if wallet . verifier . get_confirmations ( tx_hash ) < requested_confs : continue <EOL> for o in tx . outputs : <EOL> o_address , o_value = o <EOL> if o_address == addr : <EOL> value += o_value <EOL> s = ( value ) / <NUM_LIT> <EOL> print "<STR_LIT>" % addr , s , requested_amount <EOL> if s >= requested_amount : <EOL> print "<STR_LIT>" , addr <EOL> out_queue . put ( ( '<STR_LIT>' , addr ) ) <EOL> stopping = False <EOL> def do_stop ( password ) : <EOL> global stopping <EOL> if password != my_password : <EOL> return "<STR_LIT>" <EOL> stopping = True <EOL> return "<STR_LIT>" <EOL> def process_request ( amount , confirmations , expires_in , password ) : <EOL> global num <EOL> if password != my_password : <EOL> return "<STR_LIT>" <EOL> try : <EOL> amount = float ( amount ) <EOL> confirmations = int ( confirmations ) <EOL> expires_in = float ( expires_in ) <EOL> except Exception : <EOL> return "<STR_LIT>" <EOL> account = wallet . accounts [ "<STR_LIT>" ] <EOL> addr = account . get_address ( <NUM_LIT:0> , num ) <EOL> num += <NUM_LIT:1> <EOL> out_queue . put ( ( '<STR_LIT>' , ( addr , amount , confirmations , expires_in ) ) ) <EOL> return addr <EOL> def server_thread ( conn ) : <EOL> from jsonrpclib . SimpleJSONRPCServer import SimpleJSONRPCServer <EOL> server = SimpleJSONRPCServer ( ( my_host , my_port ) ) <EOL> server . register_function ( process_request , '<STR_LIT>' ) <EOL> server . register_function ( do_stop , '<STR_LIT>' ) <EOL> server . serve_forever ( ) <EOL> def send_command ( cmd , params ) : <EOL> import jsonrpclib <EOL> server = jsonrpclib . Server ( '<STR_LIT>' % ( my_host , my_port ) ) <EOL> try : <EOL> if cmd == '<STR_LIT>' : <EOL> out = server . request ( * params ) <EOL> elif cmd == '<STR_LIT>' : <EOL> out = server . stop ( * params ) <EOL> else : <EOL> out = "<STR_LIT>" <EOL> except socket . error : <EOL> print "<STR_LIT>" <EOL> return <NUM_LIT:1> <EOL> print out <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> cmd = sys . argv [ <NUM_LIT:1> ] <EOL> params = sys . argv [ <NUM_LIT:2> : ] + [ my_password ] <EOL> ret = send_command ( cmd , params ) <EOL> sys . exit ( ret ) <EOL> conn = sqlite3 . connect ( database ) ; <EOL> check_create_table ( conn ) <EOL> config = SimpleConfig ( { '<STR_LIT>' : wallet_path } ) <EOL> network = Network ( config ) <EOL> network . start ( wait = True ) <EOL> storage = WalletStorage ( config ) <EOL> wallet = Wallet ( storage ) <EOL> if not storage . file_exists : <EOL> wallet . seed = '<STR_LIT>' <EOL> wallet . create_watching_only_wallet ( master_chain , master_public_key ) <EOL> wallet . synchronize = lambda : None <EOL> wallet . start_threads ( network ) <EOL> network . register_callback ( '<STR_LIT>' , on_wallet_update ) <EOL> out_queue = Queue . Queue ( ) <EOL> thread . start_new_thread ( server_thread , ( conn , ) ) <EOL> while not stopping : <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" ) <EOL> data = cur . fetchall ( ) <EOL> for item in data : <EOL> addr , amount , confirmations = item <EOL> if addr in pending_requests : <EOL> continue <EOL> else : <EOL> with wallet . lock : <EOL> print "<STR_LIT>" % addr <EOL> pending_requests [ addr ] = { '<STR_LIT>' : float ( amount ) , '<STR_LIT>' : int ( confirmations ) } <EOL> wallet . synchronizer . subscribe_to_addresses ( [ addr ] ) <EOL> wallet . up_to_date = False <EOL> try : <EOL> cmd , params = out_queue . get ( True , <NUM_LIT:10> ) <EOL> except Queue . Empty : <EOL> cmd = '<STR_LIT>' <EOL> if cmd == '<STR_LIT>' : <EOL> addr = params <EOL> print "<STR_LIT>" , addr <EOL> cur . execute ( "<STR_LIT>" % addr ) <EOL> elif cmd == '<STR_LIT>' : <EOL> addr , amount , confs , minutes = params <EOL> sql = "<STR_LIT>" + "<STR_LIT>" % ( addr , amount , confs , minutes ) <EOL> print sql <EOL> cur . execute ( sql ) <EOL> cur . execute ( """<STR_LIT>""" ) <EOL> cur . execute ( """<STR_LIT>""" ) <EOL> data = cur . fetchall ( ) <EOL> for item in data : <EOL> address , paid = item <EOL> paid = bool ( paid ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> data_json = { '<STR_LIT:address>' : address , '<STR_LIT:password>' : cb_password , '<STR_LIT>' : paid } <EOL> data_json = json . dumps ( data_json ) <EOL> url = received_url if paid else expired_url <EOL> req = urllib2 . Request ( url , data_json , headers ) <EOL> try : <EOL> response_stream = urllib2 . urlopen ( req ) <EOL> cur . execute ( "<STR_LIT>" % ( id ) ) <EOL> except urllib2 . HTTPError : <EOL> print "<STR_LIT>" , data_json <EOL> except ValueError , e : <EOL> print e <EOL> print "<STR_LIT>" , data_json <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> print "<STR_LIT>" </s>
