<s> import errno <EOL> import socket <EOL> import urllib <EOL> from ssl import SSLError <EOL> from httplib import BadStatusLine <EOL> from bitcoinrpc . exceptions import WalletUnlockNeeded <EOL> from bitcoinrpc . exceptions import WalletPassphraseIncorrect <EOL> from bitcoinrpc . exceptions import WalletAlreadyUnlocked <EOL> from bitcoinrpc . exceptions import TransportException <EOL> from flask import abort <EOL> from flask import flash <EOL> from flask import render_template <EOL> from flask import redirect <EOL> from flask import request <EOL> from flask import url_for <EOL> from flask . views import View <EOL> from flaskwallet import cache <EOL> from flaskwallet import session <EOL> from settingsapp . helpers import get_setting <EOL> from settingsapp . models import Setting <EOL> from walletapp . lib import connection <EOL> from walletapp . forms . account import AccountMoveForm <EOL> from walletapp . forms . account import CreateAccountForm <EOL> from walletapp . forms . wallet import WalletForm <EOL> from walletapp . forms . wallet import SendForm <EOL> from walletapp . forms . wallet import EncryptForm <EOL> from walletapp . forms . wallet import UnlockForm <EOL> from walletapp . forms . wallet import OTPUnlockForm <EOL> from walletapp . forms . wallet import SetTXFeeForm <EOL> from walletapp . helpers import get_accounts <EOL> from walletapp . helpers import human_format <EOL> from walletapp . helpers import real_format <EOL> from walletapp . helpers import strtofloat <EOL> from walletapp . helpers import get_cachetime <EOL> from walletapp . models import Wallet <EOL> import onetimepass as otp <EOL> def get_wallet_info ( wallet ) : <EOL> """<STR_LIT>""" <EOL> info = cache . get ( '<STR_LIT>' % wallet . id ) <EOL> if info == None : <EOL> try : <EOL> conn = connection . get_connection ( wallet ) <EOL> info = conn . getinfo ( ) <EOL> except socket . error , v : <EOL> if v [ <NUM_LIT:0> ] == errno . ECONNREFUSED : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> elif v [ <NUM_LIT:0> ] == errno . ECONNRESET : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> elif v [ <NUM_LIT:0> ] == errno . EHOSTUNREACH : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> elif v [ <NUM_LIT:0> ] == - <NUM_LIT:2> : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> else : <EOL> print v [ <NUM_LIT:0> ] <EOL> print dir ( v ) <EOL> print type ( v ) <EOL> flash ( u"<STR_LIT>" , '<STR_LIT:error>' ) <EOL> except ValueError : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> except BadStatusLine : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> except SSLError : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % wallet . label , '<STR_LIT:error>' ) <EOL> except TransportException , e : <EOL> if e . code == <NUM_LIT> : <EOL> info = False <EOL> flash ( u"<STR_LIT>" % e . msg , '<STR_LIT:error>' ) <EOL> else : <EOL> flash ( u"<STR_LIT>" , '<STR_LIT:error>' ) <EOL> except Exception , e : <EOL> print e <EOL> print dir ( e ) <EOL> print type ( e ) <EOL> flash ( u"<STR_LIT>" , '<STR_LIT:error>' ) <EOL> cache . set ( '<STR_LIT>' % wallet . id , info , timeout = get_cachetime ( ) ) <EOL> return info <EOL> class WalletListView ( View ) : <EOL> def dispatch_request ( self ) : <EOL> wallets = Wallet . query . order_by ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) . all ( ) <EOL> index_connect = int ( get_setting ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if index_connect : <EOL> for wallet in wallets : <EOL> wallet . info = get_wallet_info ( wallet ) <EOL> otp_secret = get_setting ( '<STR_LIT>' , False ) <EOL> if otp_secret : <EOL> show_otp = False <EOL> else : <EOL> show_otp = True <EOL> return render_template ( '<STR_LIT>' , wallets = wallets , <EOL> show_otp = show_otp ) <EOL> class WalletAddView ( View ) : <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self ) : <EOL> form = WalletForm ( request . form ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> newwallet = Wallet ( ** form . data ) <EOL> session . add ( newwallet ) <EOL> session . commit ( ) <EOL> flash ( "<STR_LIT>" % newwallet . label ) <EOL> return redirect ( url_for ( "<STR_LIT>" ) ) <EOL> return render_template ( "<STR_LIT>" , form = form ) <EOL> class WalletDetailBase ( View ) : <EOL> def dispatch_request ( self , id ) : <EOL> self . wallet = session . query ( Wallet ) . get ( id ) <EOL> if not self . wallet : <EOL> abort ( <NUM_LIT> ) <EOL> class WalletEditView ( WalletDetailBase ) : <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletEditView , self ) . dispatch_request ( id ) <EOL> form = WalletForm ( request . form , self . wallet ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> form . populate_obj ( self . wallet ) <EOL> session . commit ( ) <EOL> flash ( u"<STR_LIT>" % self . wallet . label , '<STR_LIT:info>' ) <EOL> return redirect ( url_for ( "<STR_LIT>" ) ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> form = form ) <EOL> class WalletConnectedBase ( WalletDetailBase ) : <EOL> """<STR_LIT>""" <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletConnectedBase , self ) . dispatch_request ( id ) <EOL> self . conn = connection . get_connection ( self . wallet ) <EOL> class WalletDetailView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletDetailView , self ) . dispatch_request ( id ) <EOL> data = { } <EOL> data [ '<STR_LIT:info>' ] = self . conn . getinfo ( ) <EOL> data [ '<STR_LIT>' ] = get_accounts ( self . conn , getbalance = True ) <EOL> data [ '<STR_LIT>' ] = int ( get_setting ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> data [ '<STR_LIT>' ] = { } <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = CreateAccountForm ( ) <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = SetTXFeeForm ( ) <EOL> form = CreateAccountForm ( request . form ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> data = data , form = form ) <EOL> class WalletTransactionsView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletTransactionsView , self ) . dispatch_request ( id ) <EOL> transactions = self . conn . listtransactions ( ) <EOL> return render_template ( "<STR_LIT>" , <EOL> wallet = self . wallet , transactions = transactions ) <EOL> class WalletReceivedView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletReceivedView , self ) . dispatch_request ( id ) <EOL> data = { } <EOL> data [ '<STR_LIT>' ] = self . conn . listreceivedbyaddress ( ) <EOL> data [ '<STR_LIT>' ] = self . conn . listreceivedbyaccount ( ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> data = data ) <EOL> class WalletGroupingsView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletGroupingsView , self ) . dispatch_request ( id ) <EOL> try : <EOL> groupings = self . conn . listaddressgroupings ( ) <EOL> except AttributeError : <EOL> flash ( u"<STR_LIT>" , <EOL> '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = self . wallet . id ) ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> groupings = groupings ) <EOL> class WalletPeersView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletPeersView , self ) . dispatch_request ( id ) <EOL> peers = self . conn . getpeerinfo ( ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> peers = peers ) <EOL> class WalletMoveView ( WalletConnectedBase ) : <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletMoveView , self ) . dispatch_request ( id ) <EOL> form = AccountMoveForm ( request . form ) <EOL> choices = get_accounts ( self . conn , getchoice = True ) <EOL> form . fromaccount . choices = choices <EOL> form . toaccount . choices = choices <EOL> accounts = get_accounts ( self . conn , getbalance = True ) <EOL> fromaccount = urllib . unquote_plus ( form . data [ '<STR_LIT>' ] ) <EOL> toaccount = urllib . unquote_plus ( form . data [ '<STR_LIT>' ] ) <EOL> fromaccount = real_format ( fromaccount ) <EOL> toaccount = real_format ( toaccount ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> amount = strtofloat ( form . data [ '<STR_LIT>' ] ) <EOL> self . conn . move ( <EOL> fromaccount , <EOL> toaccount , <EOL> amount , <EOL> int ( get_setting ( '<STR_LIT>' , <NUM_LIT:6> ) ) , <EOL> form . data [ '<STR_LIT>' ] , <EOL> ) <EOL> flash ( u"<STR_LIT>" % ( amount , <EOL> human_format ( fromaccount ) , human_format ( toaccount ) ) , <EOL> '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = id ) ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> form = form , accounts = accounts ) <EOL> class WalletUnlockedBase ( WalletConnectedBase ) : <EOL> """<STR_LIT>""" <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletUnlockedBase , self ) . dispatch_request ( id ) <EOL> try : <EOL> unlocked = self . conn . getinfo ( ) . unlocked_until <EOL> if not unlocked : <EOL> url = url_for ( '<STR_LIT>' , id = self . wallet . id ) <EOL> flash ( '<STR_LIT>' % url , <EOL> "<STR_LIT>" ) <EOL> except AttributeError : <EOL> pass <EOL> class WalletSendView ( WalletUnlockedBase ) : <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletSendView , self ) . dispatch_request ( id ) <EOL> form = SendForm ( request . form ) <EOL> balance = self . conn . getbalance ( ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> amount = strtofloat ( form . data [ '<STR_LIT>' ] ) <EOL> txid = self . conn . sendtoaddress ( <EOL> form . toaddress . data , <EOL> amount , <EOL> form . comment . data , <EOL> form . to . data <EOL> ) <EOL> flash ( u"<STR_LIT>" % ( amount , form . toaddress . data ) , <EOL> '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( "<STR_LIT>" , id = id , txid = txid ) ) <EOL> return render_template ( '<STR_LIT>' , wallet = self . wallet , <EOL> form = form , balance = balance ) <EOL> class WalletDeleteView ( WalletDetailBase ) : <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletDeleteView , self ) . dispatch_request ( id ) <EOL> session . delete ( self . wallet ) <EOL> session . commit ( ) <EOL> flash ( u"<STR_LIT>" % self . wallet . label , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( "<STR_LIT>" ) ) <EOL> class WalletStopView ( WalletConnectedBase ) : <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletStopView , self ) . dispatch_request ( id ) <EOL> self . conn . stop ( ) <EOL> flash ( u"<STR_LIT>" % self . wallet . label , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( "<STR_LIT>" ) ) <EOL> class WalletHelpView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletHelpView , self ) . dispatch_request ( id ) <EOL> implemented = ( <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> '<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> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> interested = ( <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> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> ignored = ( <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> '<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> ) <EOL> resp = self . conn . help ( ) <EOL> data = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : implemented , <EOL> '<STR_LIT>' : ignored , <EOL> '<STR_LIT>' : interested , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> lines = sorted ( resp . split ( "<STR_LIT:\n>" ) ) <EOL> for line in lines : <EOL> command = line . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> ] <EOL> if command in implemented : <EOL> data [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> data [ '<STR_LIT>' ] . append ( ( command , line ) ) <EOL> for command in implemented : <EOL> unknown = True <EOL> for cmd , val in data [ '<STR_LIT>' ] : <EOL> if command == cmd : <EOL> unknown = False <EOL> break <EOL> if unknown : <EOL> data [ '<STR_LIT>' ] . append ( command ) <EOL> data [ '<STR_LIT>' ] = len ( data [ '<STR_LIT>' ] ) <EOL> data [ '<STR_LIT>' ] = len ( implemented ) - data [ '<STR_LIT>' ] <EOL> return render_template ( '<STR_LIT>' , wallet = self . wallet , <EOL> data = data ) <EOL> class WalletMiningInfoView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletMiningInfoView , self ) . dispatch_request ( id ) <EOL> data = self . conn . getmininginfo ( ) <EOL> return render_template ( '<STR_LIT>' , <EOL> wallet = self . wallet , data = data ) <EOL> class WalletEncryptView ( WalletConnectedBase ) : <EOL> """<STR_LIT>""" <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletEncryptView , self ) . dispatch_request ( id ) <EOL> form = EncryptForm ( request . form ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> if form . passphrase . data == form . confirm . data : <EOL> ret = self . conn . encryptwallet ( form . passphrase . data ) <EOL> flash ( ret , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( "<STR_LIT>" ) ) <EOL> else : <EOL> flash ( "<STR_LIT>" , "<STR_LIT:error>" ) <EOL> return render_template ( '<STR_LIT>' , wallet = self . wallet , <EOL> form = form ) <EOL> class WalletUnlockView ( WalletConnectedBase ) : <EOL> """<STR_LIT>""" <EOL> methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletUnlockView , self ) . dispatch_request ( id ) <EOL> otpsetting = session . query ( Setting ) . get ( '<STR_LIT>' ) <EOL> if otpsetting : <EOL> form = OTPUnlockForm ( request . form ) <EOL> secret = otpsetting . value <EOL> otp_valid = otp . valid_totp ( token = form . otp . data , secret = secret ) <EOL> else : <EOL> form = UnlockForm ( request . form ) <EOL> otp_valid = True <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) and otp_valid : <EOL> flash ( "<STR_LIT>" , "<STR_LIT:success>" ) <EOL> try : <EOL> ret = self . conn . walletpassphrase ( form . passphrase . data , <EOL> form . timeout . data ) <EOL> flash ( '<STR_LIT>' , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , <EOL> id = self . wallet . id ) ) <EOL> except WalletPassphraseIncorrect : <EOL> flash ( '<STR_LIT>' , '<STR_LIT:error>' ) <EOL> except WalletAlreadyUnlocked : <EOL> flash ( '<STR_LIT>' , '<STR_LIT:error>' ) <EOL> elif request . method == '<STR_LIT:POST>' and form . validate ( ) and not otp_valid : <EOL> flash ( "<STR_LIT>" , "<STR_LIT:error>" ) <EOL> return render_template ( '<STR_LIT>' , wallet = self . wallet , <EOL> form = form ) <EOL> class WalletLockView ( WalletConnectedBase ) : <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletLockView , self ) . dispatch_request ( id ) <EOL> self . conn . walletlock ( ) <EOL> flash ( '<STR_LIT>' , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = self . wallet . id ) ) <EOL> class TXDetailView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id , txid ) : <EOL> super ( TXDetailView , self ) . dispatch_request ( id ) <EOL> tx = self . conn . gettransaction ( txid ) <EOL> return render_template ( "<STR_LIT>" , <EOL> wallet = self . wallet , tx = tx ) <EOL> class BlockDetailView ( WalletConnectedBase ) : <EOL> def dispatch_request ( self , id , block ) : <EOL> super ( BlockDetailView , self ) . dispatch_request ( id ) <EOL> block = self . conn . getblock ( block ) <EOL> return render_template ( "<STR_LIT>" , wallet = self . wallet , <EOL> block = block ) <EOL> class WalletSetTXFeeView ( WalletConnectedBase ) : <EOL> methods = [ '<STR_LIT:POST>' ] <EOL> def dispatch_request ( self , id ) : <EOL> super ( WalletSetTXFeeView , self ) . dispatch_request ( id ) <EOL> form = SetTXFeeForm ( request . form ) <EOL> if form . validate ( ) : <EOL> self . conn . settxfee ( float ( form . data [ '<STR_LIT>' ] ) ) <EOL> flash ( u"<STR_LIT>" % form . data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:success>' ) <EOL> else : <EOL> self . conn . settxfee ( <NUM_LIT:0> ) <EOL> flash ( u"<STR_LIT>" , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( "<STR_LIT>" , id = id ) ) </s>
