kivy.py (4377B)
1 #!/usr/bin/env python 2 # 3 # Electrum - Lightweight Bitcoin Client 4 # Copyright (C) 2015 Thomas Voegtlin 5 # 6 # Permission is hereby granted, free of charge, to any person 7 # obtaining a copy of this software and associated documentation files 8 # (the "Software"), to deal in the Software without restriction, 9 # including without limitation the rights to use, copy, modify, merge, 10 # publish, distribute, sublicense, and/or sell copies of the Software, 11 # and to permit persons to whom the Software is furnished to do so, 12 # subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be 15 # included in all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 21 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 # SOFTWARE. 25 26 from functools import partial 27 from threading import Thread 28 import re 29 from decimal import Decimal 30 31 from kivy.clock import Clock 32 33 from electrum.i18n import _ 34 from electrum.plugin import hook 35 from .trustedcoin import TrustedCoinPlugin, server, KIVY_DISCLAIMER, TrustedCoinException, ErrorConnectingServer 36 37 38 39 class Plugin(TrustedCoinPlugin): 40 41 disclaimer_msg = KIVY_DISCLAIMER 42 43 def __init__(self, parent, config, name): 44 super().__init__(parent, config, name) 45 46 @hook 47 def load_wallet(self, wallet, window): 48 if not isinstance(wallet, self.wallet_class): 49 return 50 self.start_request_thread(wallet) 51 52 def go_online_dialog(self, wizard): 53 # we skip this step on android 54 wizard.run('accept_terms_of_use') 55 56 def prompt_user_for_otp(self, wallet, tx, on_success, on_failure): 57 from ...gui.kivy.uix.dialogs.label_dialog import LabelDialog 58 msg = _('Please enter your Google Authenticator code') 59 d = LabelDialog(msg, '', lambda otp: self.on_otp(wallet, tx, otp, on_success, on_failure)) 60 d.open() 61 62 def on_otp(self, wallet, tx, otp, on_success, on_failure): 63 try: 64 wallet.on_otp(tx, otp) 65 except TrustedCoinException as e: 66 if e.status_code == 400: # invalid OTP 67 Clock.schedule_once(lambda dt: on_failure(_('Invalid one-time password.'))) 68 else: 69 Clock.schedule_once(lambda dt, bound_e=e: on_failure(_('Error') + ':\n' + str(bound_e))) 70 except Exception as e: 71 Clock.schedule_once(lambda dt, bound_e=e: on_failure(_('Error') + ':\n' + str(bound_e))) 72 else: 73 on_success(tx) 74 75 def accept_terms_of_use(self, wizard): 76 def handle_error(msg, e): 77 wizard.show_error(msg + ':\n' + repr(e)) 78 wizard.terminate(aborted=True) 79 try: 80 tos = server.get_terms_of_service() 81 except ErrorConnectingServer as e: 82 Clock.schedule_once(lambda dt, bound_e=e: handle_error(_('Error connecting to server'), bound_e)) 83 except Exception as e: 84 Clock.schedule_once(lambda dt, bound_e=e: handle_error(_('Error'), bound_e)) 85 else: 86 f = lambda x: self.read_email(wizard) 87 wizard.tos_dialog(tos=tos, run_next=f) 88 89 def read_email(self, wizard): 90 f = lambda x: self.create_remote_key(x, wizard) 91 wizard.email_dialog(run_next=f) 92 93 def request_otp_dialog(self, wizard, short_id, otp_secret, xpub3): 94 f = lambda otp, reset: self.check_otp(wizard, short_id, otp_secret, xpub3, otp, reset) 95 wizard.otp_dialog(otp_secret=otp_secret, run_next=f) 96 97 @hook 98 def abort_send(self, window): 99 wallet = window.wallet 100 if not isinstance(wallet, self.wallet_class): 101 return 102 if wallet.can_sign_without_server(): 103 return 104 if wallet.billing_info is None: 105 self.start_request_thread(wallet) 106 Clock.schedule_once( 107 lambda dt: window.show_error(_('Requesting account info from TrustedCoin server...') + '\n' + 108 _('Please try again.'))) 109 return True 110 return False