2015-12-30 18:36:33 -08:00
from sys import stdout
2015-06-26 05:29:26 -07:00
2013-08-22 03:39:41 -07:00
from PyQt4 . QtGui import *
from PyQt4 . QtCore import *
import PyQt4 . QtCore as QtCore
2014-08-31 06:33:20 -07:00
import electrum
2013-09-11 04:55:49 -07:00
from electrum . i18n import _
2013-08-29 07:07:55 -07:00
2016-01-13 02:20:58 -08:00
from seed_dialog import SeedDisplayLayout , SeedWarningLayout , SeedInputLayout
2013-08-22 03:39:41 -07:00
from network_dialog import NetworkDialog
2013-09-24 07:57:12 -07:00
from util import *
2016-01-12 06:32:13 -08:00
from password_dialog import PasswordLayout , PW_NEW , PW_PASSPHRASE
2013-08-29 07:07:55 -07:00
2015-12-30 18:36:33 -08:00
from electrum . wallet import Wallet
2014-09-13 10:31:05 -07:00
from electrum . mnemonic import prepare_seed
2015-12-30 18:36:33 -08:00
from electrum . wizard import ( WizardBase , UserCancelled ,
MSG_ENTER_PASSWORD , MSG_RESTORE_PASSPHRASE ,
MSG_COSIGNER , MSG_ENTER_SEED_OR_MPK ,
2016-01-05 04:51:03 -08:00
MSG_SHOW_MPK , MSG_VERIFY_SEED ,
MSG_GENERATING_WAIT )
2014-04-29 12:19:42 -07:00
2016-01-12 05:31:53 -08:00
def clean_text ( seed_e ) :
text = unicode ( seed_e . toPlainText ( ) ) . strip ( )
text = ' ' . join ( text . split ( ) )
return text
2015-06-26 05:29:26 -07:00
class CosignWidget ( QWidget ) :
size = 120
def __init__ ( self , m , n ) :
QWidget . __init__ ( self )
self . R = QRect ( 0 , 0 , self . size , self . size )
self . setGeometry ( self . R )
2016-01-13 02:20:58 -08:00
self . setMinimumHeight ( self . size )
self . setMaximumHeight ( self . size )
2015-06-26 05:29:26 -07:00
self . m = m
self . n = n
def set_n ( self , n ) :
self . n = n
self . update ( )
def set_m ( self , m ) :
self . m = m
self . update ( )
def paintEvent ( self , event ) :
import math
bgcolor = self . palette ( ) . color ( QPalette . Background )
pen = QPen ( bgcolor , 7 , QtCore . Qt . SolidLine )
qp = QPainter ( )
qp . begin ( self )
qp . setPen ( pen )
qp . setRenderHint ( QPainter . Antialiasing )
qp . setBrush ( Qt . gray )
for i in range ( self . n ) :
alpha = int ( 16 * 360 * i / self . n )
alpha2 = int ( 16 * 360 * 1 / self . n )
qp . setBrush ( Qt . green if i < self . m else Qt . gray )
qp . drawPie ( self . R , alpha , alpha2 )
qp . end ( )
2016-01-02 18:18:20 -08:00
# WindowModalDialog must come first as it overrides show_error
class InstallWizard ( WindowModalDialog , WizardBase ) :
2013-08-22 03:39:41 -07:00
2015-12-30 18:36:33 -08:00
def __init__ ( self , config , app , plugins ) :
title = ' Electrum - ' + _ ( ' Install Wizard ' )
2015-12-22 05:18:59 -08:00
WindowModalDialog . __init__ ( self , None , title = title )
2015-12-30 18:36:33 -08:00
self . app = app
self . config = config
# Set for base base class
self . plugins = plugins
self . language_for_seed = config . get ( ' language ' )
2016-01-12 06:32:13 -08:00
self . setMinimumSize ( 518 , 360 )
self . setMaximumSize ( 518 , 360 )
2013-09-12 10:42:00 -07:00
self . connect ( self , QtCore . SIGNAL ( ' accept ' ) , self . accept )
2016-01-13 02:20:58 -08:00
self . title = WWLabel ( )
2016-01-12 05:31:53 -08:00
self . main_widget = QWidget ( )
self . cancel_button = QPushButton ( _ ( " Cancel " ) , self )
self . next_button = QPushButton ( _ ( " Next " ) , self )
self . next_button . setDefault ( True )
self . logo = QLabel ( )
self . please_wait = QLabel ( _ ( " Please wait... " ) )
self . please_wait . setAlignment ( Qt . AlignCenter )
self . icon_filename = None
self . loop = QEventLoop ( )
self . cancel_button . clicked . connect ( lambda : self . loop . exit ( False ) )
self . next_button . clicked . connect ( lambda : self . loop . exit ( True ) )
outer_vbox = QVBoxLayout ( self )
inner_vbox = QVBoxLayout ( )
inner_vbox = QVBoxLayout ( )
inner_vbox . addWidget ( self . title )
inner_vbox . addWidget ( self . main_widget )
inner_vbox . addStretch ( 1 )
inner_vbox . addWidget ( self . please_wait )
inner_vbox . addStretch ( 1 )
icon_vbox = QVBoxLayout ( )
icon_vbox . addWidget ( self . logo )
icon_vbox . addStretch ( 1 )
hbox = QHBoxLayout ( )
hbox . addLayout ( icon_vbox )
2016-01-13 02:20:58 -08:00
hbox . addSpacing ( 5 )
2016-01-12 05:31:53 -08:00
hbox . addLayout ( inner_vbox )
hbox . setStretchFactor ( inner_vbox , 1 )
outer_vbox . addLayout ( hbox )
outer_vbox . addLayout ( Buttons ( self . cancel_button , self . next_button ) )
self . set_icon ( ' :icons/electrum.png ' )
def set_icon ( self , filename ) :
prior_filename , self . icon_filename = self . icon_filename , filename
self . logo . setPixmap ( QPixmap ( filename ) . scaledToWidth ( 70 ) )
return prior_filename
2016-01-12 06:32:13 -08:00
def set_main_layout ( self , layout , title = None ) :
self . title . setText ( title or " " )
self . title . setVisible ( bool ( title ) )
# Get rid of any prior layout
2016-01-12 05:31:53 -08:00
prior_layout = self . main_widget . layout ( )
if prior_layout :
QWidget ( ) . setLayout ( prior_layout )
2016-01-13 02:20:58 -08:00
else :
self . show ( )
self . raise_ ( )
2016-01-12 05:31:53 -08:00
self . main_widget . setLayout ( layout )
self . cancel_button . setEnabled ( True )
self . next_button . setEnabled ( True )
self . main_widget . setVisible ( True )
self . please_wait . setVisible ( False )
if not self . loop . exec_ ( ) :
raise UserCancelled
2016-01-12 06:32:13 -08:00
self . title . setVisible ( False )
2016-01-12 05:31:53 -08:00
self . cancel_button . setEnabled ( False )
self . next_button . setEnabled ( False )
self . main_widget . setVisible ( False )
self . please_wait . setVisible ( True )
2016-01-12 06:36:25 -08:00
self . refresh_gui ( )
def refresh_gui ( self ) :
2016-01-12 05:31:53 -08:00
# For some reason, to refresh the GUI this needs to be called twice
self . app . processEvents ( )
self . app . processEvents ( )
2013-11-03 02:03:45 -08:00
2015-12-30 18:36:33 -08:00
def open_wallet ( self , * args ) :
''' Wrap the base wizard implementation with try/except blocks
to give a sensible error message to the user . '''
wallet = None
try :
wallet = super ( InstallWizard , self ) . open_wallet ( * args )
except UserCancelled :
self . print_error ( " wallet creation cancelled by user " )
2016-01-12 05:31:53 -08:00
self . accept ( )
2015-12-30 18:36:33 -08:00
return wallet
def remove_from_recently_open ( self , filename ) :
self . config . remove_from_recently_open ( filename )
2016-01-12 05:31:53 -08:00
def request_seed ( self , title , is_valid = None ) :
is_valid = is_valid or Wallet . is_any
2016-01-13 02:20:58 -08:00
slayout = SeedInputLayout ( )
2016-01-12 05:31:53 -08:00
self . next_button . setEnabled ( False )
def sanitized_seed ( ) :
return clean_text ( slayout . seed_edit ( ) )
def set_enabled ( ) :
self . next_button . setEnabled ( is_valid ( sanitized_seed ( ) ) )
slayout . seed_edit ( ) . textChanged . connect ( set_enabled )
self . set_main_layout ( slayout . layout ( ) , title )
return sanitized_seed ( )
def show_seed ( self , seed ) :
title = _ ( " Your wallet generation seed is: " )
2016-01-13 02:20:58 -08:00
slayout = SeedWarningLayout ( seed )
2016-01-12 05:31:53 -08:00
self . set_main_layout ( slayout . layout ( ) , title )
def verify_seed ( self , seed , is_valid = None ) :
while True :
r = self . request_seed ( MSG_VERIFY_SEED , is_valid )
if prepare_seed ( r ) == prepare_seed ( seed ) :
return
self . show_error ( _ ( ' Incorrect seed ' ) )
2015-12-30 18:36:33 -08:00
def show_and_verify_seed ( self , seed , is_valid = None ) :
""" Show the user their seed. Ask them to re-enter it. Return
True on success . """
self . show_seed ( seed )
self . app . clipboard ( ) . clear ( )
self . verify_seed ( seed , is_valid )
2016-01-12 06:32:13 -08:00
def pw_layout ( self , msg , kind ) :
playout = PasswordLayout ( None , msg , kind , self . next_button )
2016-01-13 02:20:58 -08:00
self . set_main_layout ( playout . layout ( ) )
2016-01-12 06:32:13 -08:00
return playout . new_password ( )
2015-12-30 18:36:33 -08:00
def request_passphrase ( self , device_text , restore = True ) :
""" Request a passphrase for a wallet from the given device and
confirm it . restore is True if restoring a wallet . Should return
a unicode string . """
if restore :
msg = MSG_RESTORE_PASSPHRASE % device_text
2016-01-12 06:32:13 -08:00
return unicode ( self . pw_layout ( msg , PW_PASSPHRASE ) or ' ' )
2015-12-30 18:36:33 -08:00
def request_password ( self , msg = None ) :
""" Request the user enter a new password and confirm it. Return
the password or None for no password . """
2016-01-12 06:32:13 -08:00
return self . pw_layout ( msg or MSG_ENTER_PASSWORD , PW_NEW )
2015-12-30 18:36:33 -08:00
def choose_server ( self , network ) :
2016-01-09 01:35:10 -08:00
self . network_dialog ( network )
def show_restore ( self , wallet , network ) :
if network :
def task ( ) :
wallet . wait_until_synchronized ( )
if wallet . is_found ( ) :
msg = _ ( " Recovery successful " )
2016-01-05 04:51:03 -08:00
else :
2016-01-09 01:35:10 -08:00
msg = _ ( " No transactions found for this seed " )
self . emit ( QtCore . SIGNAL ( ' synchronized ' ) , msg )
self . connect ( self , QtCore . SIGNAL ( ' synchronized ' ) , self . show_message )
t = threading . Thread ( target = task )
t . start ( )
else :
msg = _ ( " This wallet was restored offline. It may "
" contain more addresses than displayed. " )
self . show_message ( msg )
2016-01-06 01:31:55 -08:00
def create_addresses ( self , wallet ) :
def task ( ) :
wallet . synchronize ( )
self . emit ( QtCore . SIGNAL ( ' accept ' ) )
t = threading . Thread ( target = task )
t . start ( )
2016-01-12 06:36:25 -08:00
self . please_wait . setText ( MSG_GENERATING_WAIT )
self . refresh_gui ( )
2016-01-05 04:51:03 -08:00
2016-01-01 07:35:09 -08:00
def query_create_or_restore ( self , wallet_kinds ) :
2016-01-10 05:57:31 -08:00
""" Ask the user what they want to do, and which wallet kind.
wallet_kinds is an array of translated wallet descriptions .
Return a a tuple ( action , kind_index ) . Action is ' create ' or
' restore ' , and kind the index of the wallet kind chosen . """
2015-08-26 09:35:21 -07:00
2016-01-10 05:57:31 -08:00
actions = [ _ ( " Create a new wallet " ) ,
_ ( " Restore a wallet or import keys " ) ]
2016-01-12 05:31:53 -08:00
title = _ ( " Electrum could not find an existing wallet. " )
2016-01-10 05:57:31 -08:00
actions_clayout = ChoicesLayout ( _ ( " What do you want to do? " ) , actions )
wallet_clayout = ChoicesLayout ( _ ( " Wallet kind: " ) , wallet_kinds )
2014-05-09 04:12:07 -07:00
2016-01-10 05:57:31 -08:00
vbox = QVBoxLayout ( )
vbox . addLayout ( actions_clayout . layout ( ) )
vbox . addLayout ( wallet_clayout . layout ( ) )
2016-01-12 05:31:53 -08:00
self . set_main_layout ( vbox , title )
2016-01-12 06:32:13 -08:00
2016-01-10 05:57:31 -08:00
action = [ ' create ' , ' restore ' ] [ actions_clayout . selected_index ( ) ]
return action , wallet_clayout . selected_index ( )
2013-08-22 03:39:41 -07:00
2015-12-30 18:36:33 -08:00
def request_many ( self , n , xpub_hot = None ) :
2014-05-09 07:27:12 -07:00
vbox = QVBoxLayout ( )
2015-06-27 15:47:25 -07:00
scroll = QScrollArea ( )
scroll . setWidgetResizable ( True )
2016-01-13 02:20:58 -08:00
scroll . setFrameShape ( QFrame . NoFrame )
2015-06-27 15:47:25 -07:00
vbox . addWidget ( scroll )
w = QWidget ( )
2016-01-13 02:20:58 -08:00
innerVbox = QVBoxLayout ( w )
2015-06-27 15:47:25 -07:00
scroll . setWidget ( w )
2014-05-12 01:53:04 -07:00
entries = [ ]
2016-01-13 02:20:58 -08:00
2015-12-30 18:36:33 -08:00
if xpub_hot :
2016-01-13 02:20:58 -08:00
layout = SeedDisplayLayout ( xpub_hot , title = MSG_SHOW_MPK , sid = ' hot ' )
2015-12-30 18:36:33 -08:00
else :
2016-01-13 02:20:58 -08:00
layout = SeedInputLayout ( title = MSG_ENTER_SEED_OR_MPK , sid = ' hot ' )
entries . append ( slayout . seed_edit ( ) )
innerVbox . addLayout ( layout . layout ( ) )
2015-12-30 18:36:33 -08:00
2014-05-12 01:53:04 -07:00
for i in range ( n ) :
2016-01-13 02:20:58 -08:00
msg = MSG_COSIGNER % ( i + 1 ) if xpub_hot else MSG_ENTER_SEED_OR_MPK
layout = SeedInputLayout ( title = msg , sid = ' cold ' )
innerVbox . addLayout ( layout . layout ( ) )
entries . append ( layout . seed_edit ( ) )
2015-06-27 15:47:25 -07:00
2016-01-13 02:20:58 -08:00
self . next_button . setEnabled ( False )
2015-12-30 18:36:33 -08:00
def get_texts ( ) :
2016-01-12 05:31:53 -08:00
return [ clean_text ( entry ) for entry in entries ]
2015-12-30 18:36:33 -08:00
def set_enabled ( ) :
texts = get_texts ( )
is_valid = Wallet . is_xpub if xpub_hot else Wallet . is_any
all_valid = all ( is_valid ( text ) for text in texts )
if xpub_hot :
texts . append ( xpub_hot )
has_dups = len ( set ( texts ) ) < len ( texts )
2016-01-13 02:20:58 -08:00
self . next_button . setEnabled ( all_valid and not has_dups )
2014-05-12 01:53:04 -07:00
for e in entries :
2015-12-30 18:36:33 -08:00
e . textChanged . connect ( set_enabled )
2016-01-13 02:20:58 -08:00
self . set_main_layout ( vbox )
2015-12-30 18:36:33 -08:00
return get_texts ( )
2013-09-12 10:42:00 -07:00
2015-12-30 18:36:33 -08:00
def network_dialog ( self , network ) :
2013-09-03 05:32:56 -07:00
grid = QGridLayout ( )
grid . setSpacing ( 5 )
2013-10-03 10:09:08 -07:00
label = QLabel ( _ ( " Electrum communicates with remote servers to get information about your transactions and addresses. The servers all fulfil the same purpose only differing in hardware. In most cases you simply want to let Electrum pick one at random if you have a preference though feel free to select a server manually. " ) + " \n \n " \
2013-10-09 18:54:52 -07:00
+ _ ( " How do you want to connect to a server: " ) + " " )
2013-09-30 07:53:51 -07:00
label . setWordWrap ( True )
2013-09-03 05:32:56 -07:00
grid . addWidget ( label , 0 , 0 )
gb = QGroupBox ( )
b1 = QRadioButton ( gb )
b1 . setText ( _ ( " Auto connect " ) )
b1 . setChecked ( True )
b2 = QRadioButton ( gb )
b2 . setText ( _ ( " Select server manually " ) )
grid . addWidget ( b1 , 1 , 0 )
grid . addWidget ( b2 , 2 , 0 )
2013-11-03 02:03:45 -08:00
vbox = QVBoxLayout ( )
2013-09-03 05:32:56 -07:00
vbox . addLayout ( grid )
2013-09-12 10:42:00 -07:00
vbox . addStretch ( 1 )
2015-03-14 04:28:19 -07:00
vbox . addLayout ( Buttons ( CancelButton ( self ) , OkButton ( self , _ ( ' Next ' ) ) ) )
2013-09-12 10:42:00 -07:00
2013-11-03 02:03:45 -08:00
self . set_layout ( vbox )
2013-09-12 10:42:00 -07:00
if not self . exec_ ( ) :
2013-09-03 05:32:56 -07:00
return
2014-10-21 10:05:51 -07:00
2013-09-03 05:32:56 -07:00
if b2 . isChecked ( ) :
2015-12-30 18:36:33 -08:00
NetworkDialog ( network , self . config , None ) . do_exec ( )
2014-08-27 05:01:05 -07:00
else :
2015-05-25 00:18:52 -07:00
self . config . set_key ( ' auto_connect ' , True , True )
2015-12-30 18:36:33 -08:00
network . auto_connect = True
2013-09-03 05:32:56 -07:00
2016-01-01 16:43:56 -08:00
def query_choice ( self , msg , choices ) :
2016-01-08 22:38:27 -08:00
clayout = ChoicesLayout ( msg , choices )
2015-12-30 18:36:33 -08:00
next_button = OkButton ( self , _ ( ' Next ' ) )
next_button . setEnabled ( bool ( choices ) )
2016-01-08 22:38:27 -08:00
layout = clayout . layout ( )
layout . addStretch ( 1 )
layout . addLayout ( Buttons ( CancelButton ( self ) , next_button ) )
self . set_layout ( layout )
2014-09-05 07:14:40 -07:00
if not self . exec_ ( ) :
2015-12-30 18:36:33 -08:00
raise UserCancelled
2016-01-08 22:38:27 -08:00
return clayout . selected_index ( )
2014-09-05 07:14:40 -07:00
2015-12-30 18:36:33 -08:00
def query_multisig ( self , action ) :
2015-06-26 05:29:26 -07:00
cw = CosignWidget ( 2 , 2 )
m_edit = QSpinBox ( )
n_edit = QSpinBox ( )
m_edit . setValue ( 2 )
n_edit . setValue ( 2 )
n_edit . setMinimum ( 2 )
n_edit . setMaximum ( 15 )
m_edit . setMinimum ( 1 )
m_edit . setMaximum ( 2 )
n_edit . valueChanged . connect ( m_edit . setMaximum )
n_edit . valueChanged . connect ( cw . set_n )
m_edit . valueChanged . connect ( cw . set_m )
hbox = QHBoxLayout ( )
hbox . addWidget ( QLabel ( _ ( ' Require ' ) ) )
hbox . addWidget ( m_edit )
hbox . addWidget ( QLabel ( _ ( ' of ' ) ) )
hbox . addWidget ( n_edit )
hbox . addWidget ( QLabel ( _ ( ' signatures ' ) ) )
hbox . addStretch ( 1 )
2016-01-13 02:20:58 -08:00
vbox = QVBoxLayout ( )
vbox . addWidget ( cw )
vbox . addWidget ( WWLabel ( _ ( " Choose the number of signatures needed "
" to unlock funds in your wallet: " ) ) )
2015-06-26 05:29:26 -07:00
vbox . addLayout ( hbox )
2016-01-13 02:20:58 -08:00
self . set_main_layout ( vbox , _ ( " Multi-Signature Wallet " ) )
2015-06-26 05:29:26 -07:00
m = int ( m_edit . value ( ) )
n = int ( n_edit . value ( ) )
wallet_type = ' %d of %d ' % ( m , n )
return wallet_type