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
2014-04-19 11:23:27 -07:00
import seed_dialog
2013-08-22 03:39:41 -07:00
from network_dialog import NetworkDialog
2013-09-24 07:57:12 -07:00
from util import *
2015-12-30 18:36:33 -08:00
from password_dialog import PasswordDialog
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
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 )
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 ' )
2013-09-12 10:42:00 -07:00
self . setMinimumSize ( 575 , 400 )
2014-08-26 07:23:24 -07:00
self . setMaximumSize ( 575 , 400 )
2013-09-12 10:42:00 -07:00
self . connect ( self , QtCore . SIGNAL ( ' accept ' ) , self . accept )
2013-11-03 02:03:45 -08:00
self . stack = QStackedLayout ( )
self . setLayout ( self . stack )
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 " )
return wallet
def remove_from_recently_open ( self , filename ) :
self . config . remove_from_recently_open ( filename )
# Called by plugins
def confirm ( self , msg , icon = None ) :
''' Returns True or False '''
vbox = QVBoxLayout ( )
self . set_layout ( vbox )
if icon :
logo = QLabel ( )
logo . setPixmap ( icon )
vbox . addWidget ( logo )
label = QLabel ( msg )
label . setWordWrap ( True )
vbox . addWidget ( label )
vbox . addStretch ( 1 )
vbox . addLayout ( Buttons ( CancelButton ( self , _ ( " Cancel " ) ) ,
OkButton ( self , _ ( " Next " ) ) ) )
if not self . exec_ ( ) :
raise UserCancelled
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 )
def pw_dialog ( self , msg , kind ) :
dialog = PasswordDialog ( self , None , msg , kind )
accepted , p , pass_text = dialog . run ( )
if not accepted :
raise UserCancelled
return pass_text
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
return unicode ( self . pw_dialog ( msg , PasswordDialog . PW_PASSPHRASE ) or ' ' )
def request_password ( self , msg = None ) :
""" Request the user enter a new password and confirm it. Return
the password or None for no password . """
return self . pw_dialog ( msg or MSG_ENTER_PASSWORD , PasswordDialog . PW_NEW )
def choose_server ( self , network ) :
# Show network dialog if config does not exist
if self . config . get ( ' server ' ) is None :
self . network_dialog ( network )
2016-01-05 04:51:03 -08:00
def show_restore ( self , wallet , network , action ) :
def on_finished ( b ) :
if action == ' restore ' :
if network :
if wallet . is_found ( ) :
msg = _ ( " Recovery successful " )
else :
msg = _ ( " No transactions found for this seed " )
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 ( )
vbox = QVBoxLayout ( )
self . waiting_label = QLabel ( MSG_GENERATING_WAIT )
vbox . addWidget ( self . waiting_label )
self . set_layout ( vbox )
self . raise_ ( )
self . exec_ ( )
2016-01-05 04:51:03 -08:00
2013-11-03 02:03:45 -08:00
def set_layout ( self , layout ) :
w = QWidget ( )
w . setLayout ( layout )
2014-09-19 11:23:24 -07:00
self . stack . addWidget ( w )
self . stack . setCurrentWidget ( w )
2015-03-07 23:07:30 -08:00
self . show ( )
2013-08-22 03:39:41 -07:00
2016-01-01 07:35:09 -08:00
def query_create_or_restore ( self , wallet_kinds ) :
""" Ask the user what they want to do, and to what wallet kind.
wallet_kinds is an array of tuples ( kind , description ) .
Return a tuple ( action , kind ) . Action is ' create ' or ' restore ' ,
and kind is one of the wallet kinds passed . """
2014-05-09 04:12:07 -07:00
vbox = QVBoxLayout ( )
2015-08-26 09:35:21 -07:00
2014-05-09 04:12:07 -07:00
main_label = QLabel ( _ ( " Electrum could not find an existing wallet. " ) )
vbox . addWidget ( main_label )
2015-08-26 09:35:21 -07:00
2013-09-03 05:32:56 -07:00
grid = QGridLayout ( )
grid . setSpacing ( 5 )
2015-08-26 09:35:21 -07:00
2014-09-05 07:14:40 -07:00
gb1 = QGroupBox ( _ ( " What do you want to do? " ) )
vbox . addWidget ( gb1 )
2015-08-26 09:35:21 -07:00
vbox1 = QVBoxLayout ( )
gb1 . setLayout ( vbox1 )
2014-05-09 04:12:07 -07:00
b1 = QRadioButton ( gb1 )
2013-09-03 05:32:56 -07:00
b1 . setText ( _ ( " Create new wallet " ) )
b1 . setChecked ( True )
2015-08-26 09:35:21 -07:00
2014-05-09 04:12:07 -07:00
b2 = QRadioButton ( gb1 )
2015-03-08 00:14:11 -08:00
b2 . setText ( _ ( " Restore a wallet or import keys " ) )
2015-08-26 09:35:21 -07:00
2014-09-05 07:14:40 -07:00
group1 = QButtonGroup ( )
2014-05-09 04:12:07 -07:00
group1 . addButton ( b1 )
group1 . addButton ( b2 )
2015-08-26 09:35:21 -07:00
vbox1 . addWidget ( b1 )
vbox1 . addWidget ( b2 )
2013-09-03 05:32:56 -07:00
2014-09-05 07:14:40 -07:00
gb2 = QGroupBox ( _ ( " Wallet type: " ) )
vbox . addWidget ( gb2 )
2015-08-26 09:35:21 -07:00
vbox2 = QVBoxLayout ( )
gb2 . setLayout ( vbox2 )
2014-05-09 04:12:07 -07:00
group2 = QButtonGroup ( )
2016-01-01 07:35:09 -08:00
for i , ( wtype , name ) in enumerate ( wallet_kinds ) :
2014-09-05 07:28:53 -07:00
if not filter ( lambda x : x [ 0 ] == wtype , electrum . wallet . wallet_types ) :
continue
2014-07-01 09:46:11 -07:00
button = QRadioButton ( gb2 )
2014-09-05 07:28:53 -07:00
button . setText ( name )
2015-08-26 09:35:21 -07:00
vbox2 . addWidget ( button )
2014-07-01 09:46:11 -07:00
group2 . addButton ( button )
group2 . setId ( button , i )
2015-08-26 09:35:21 -07:00
2014-07-01 09:46:11 -07:00
if i == 0 :
button . setChecked ( True )
2014-05-09 04:12:07 -07:00
2013-09-12 10:42:00 -07:00
vbox . addStretch ( 1 )
2016-01-03 03:09:05 -08:00
OK = OkButton ( self , _ ( ' Next ' ) )
vbox . addLayout ( Buttons ( CancelButton ( self ) , OK ) )
2016-01-02 17:39:44 -08:00
self . set_layout ( vbox )
2016-01-03 03:09:05 -08:00
OK . setDefault ( True )
2014-05-26 09:59:05 -07:00
self . raise_ ( )
2013-09-12 10:42:00 -07:00
if not self . exec_ ( ) :
2015-12-30 18:36:33 -08:00
raise UserCancelled
2014-10-21 10:05:51 -07:00
2014-05-09 04:12:07 -07:00
action = ' create ' if b1 . isChecked ( ) else ' restore '
2016-01-01 07:35:09 -08:00
wallet_type = wallet_kinds [ group2 . checkedId ( ) ] [ 0 ]
2014-07-01 09:46:11 -07:00
return action , wallet_type
2013-08-22 03:39:41 -07:00
2015-12-30 18:36:33 -08:00
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
2015-12-22 22:10:15 -08:00
self . show_error ( _ ( ' Incorrect seed ' ) )
2013-08-22 03:39:41 -07:00
2014-04-19 11:23:27 -07:00
def get_seed_text ( self , seed_e ) :
2014-04-22 06:49:32 -07:00
text = unicode ( seed_e . toPlainText ( ) ) . strip ( )
2014-04-22 06:12:36 -07:00
text = ' ' . join ( text . split ( ) )
return text
2013-08-22 03:39:41 -07:00
2015-12-30 18:36:33 -08:00
def request_seed ( self , msg , is_valid = None ) :
is_valid = is_valid or Wallet . is_any
vbox , seed_e = seed_dialog . enter_seed_box ( msg , self )
2013-09-12 10:42:00 -07:00
vbox . addStretch ( 1 )
2015-03-14 04:28:19 -07:00
button = OkButton ( self , _ ( ' Next ' ) )
vbox . addLayout ( Buttons ( CancelButton ( self ) , button ) )
2014-04-19 11:23:27 -07:00
button . setEnabled ( False )
2015-12-30 18:36:33 -08:00
def set_enabled ( ) :
button . setEnabled ( is_valid ( self . get_seed_text ( seed_e ) ) )
seed_e . textChanged . connect ( set_enabled )
2013-11-03 02:03:45 -08:00
self . set_layout ( vbox )
2013-09-12 10:42:00 -07:00
if not self . exec_ ( ) :
2015-12-30 18:36:33 -08:00
raise UserCancelled
2014-04-19 11:23:27 -07:00
return self . get_seed_text ( seed_e )
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 . setEnabled ( True )
scroll . setWidgetResizable ( True )
vbox . addWidget ( scroll )
w = QWidget ( )
scroll . setWidget ( w )
innerVbox = QVBoxLayout ( )
w . setLayout ( innerVbox )
2014-05-12 01:53:04 -07:00
entries = [ ]
2015-12-30 18:36:33 -08:00
if xpub_hot :
vbox0 = seed_dialog . show_seed_box ( MSG_SHOW_MPK , xpub_hot , ' hot ' )
else :
vbox0 , seed_e1 = seed_dialog . enter_seed_box ( MSG_ENTER_SEED_OR_MPK , self , ' hot ' )
entries . append ( seed_e1 )
innerVbox . addLayout ( vbox0 )
2014-05-12 01:53:04 -07:00
for i in range ( n ) :
2015-12-30 18:36:33 -08:00
if xpub_hot :
msg = MSG_COSIGNER % ( i + 1 )
else :
msg = MSG_ENTER_SEED_OR_MPK
2015-06-27 23:52:16 -07:00
vbox2 , seed_e2 = seed_dialog . enter_seed_box ( msg , self , ' cold ' )
2015-06-27 15:47:25 -07:00
innerVbox . addLayout ( vbox2 )
2014-05-12 01:53:04 -07:00
entries . append ( seed_e2 )
2015-06-27 15:47:25 -07:00
2014-04-19 11:23:27 -07:00
vbox . addStretch ( 1 )
2015-03-14 04:28:19 -07:00
button = OkButton ( self , _ ( ' Next ' ) )
vbox . addLayout ( Buttons ( CancelButton ( self ) , button ) )
2014-04-19 11:23:27 -07:00
button . setEnabled ( False )
2015-12-30 18:36:33 -08:00
def get_texts ( ) :
return [ self . get_seed_text ( entry ) for entry in entries ]
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 )
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 )
2014-04-19 11:23:27 -07:00
self . set_layout ( vbox )
if not self . exec_ ( ) :
2015-12-30 18:36:33 -08:00
raise UserCancelled
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
vbox = QVBoxLayout ( )
self . set_layout ( vbox )
vbox . addWidget ( QLabel ( _ ( " Multi Signature Wallet " ) ) )
cw = CosignWidget ( 2 , 2 )
vbox . addWidget ( cw , 1 )
vbox . addWidget ( QLabel ( _ ( " Please choose the number of signatures needed to unlock funds in your wallet " ) + ' : ' ) )
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 )
vbox . addLayout ( hbox )
vbox . addStretch ( 1 )
vbox . addLayout ( Buttons ( CancelButton ( self ) , OkButton ( self , _ ( ' Next ' ) ) ) )
if not self . exec_ ( ) :
2015-12-30 18:36:33 -08:00
raise UserCancelled
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
2015-12-30 18:36:33 -08:00
def show_seed ( self , seed ) :
vbox = seed_dialog . show_seed_box_msg ( seed , None )
2015-03-14 04:28:19 -07:00
vbox . addLayout ( Buttons ( CancelButton ( self ) , OkButton ( self , _ ( " Next " ) ) ) )
2013-11-03 02:03:45 -08:00
self . set_layout ( vbox )
2015-12-30 18:36:33 -08:00
if not self . exec_ ( ) :
raise UserCancelled