Added migration to APPDATA from LOCALAPPDATA for windows based Electrum installations
[electrum-nvc.git] / electrum
index c571258..0d7aab4 100755 (executable)
--- a/electrum
+++ b/electrum
@@ -19,6 +19,7 @@
 import re
 import sys, os, time
 import optparse
+import platform
 
 try:
     import ecdsa  
@@ -70,8 +71,8 @@ options:\n  --fee, -f: set transaction fee\n  --fromaddr, -s: send from address
     'signtx':"Sign an unsigned transaction created by a deseeded wallet\nSyntax: signtx <filename>",
     'seed':
             "Print the generation seed of your wallet.",
-    'import': 
-            'Imports a key pair\nSyntax: import <address>:<privatekey>',
+    'importprivkey': 
+            'Import a private key\nSyntax: importprivkey <privatekey>',
     'signmessage':
             'Signs a message with a key\nSyntax: signmessage <address> <message>\nIf you want to lead or end a message with spaces, or want double spaces inside the message make sure you quote the string. I.e. " Hello  This is a weird String "',
     'verifymessage':
@@ -99,13 +100,13 @@ offline_commands = [ 'password', 'mktx', 'signtx',
                      'help', 'validateaddress',
                      'signmessage', 'verifymessage',
                      'eval', 'set', 'get', 'create', 'addresses',
-                     'import', 'seed',
+                     'importprivkey', 'seed',
                      'deseed','reseed',
                      'freeze','unfreeze',
                      'prioritize','unprioritize']
 
 
-protected_commands = ['payto', 'password', 'mktx', 'signtx', 'seed', 'import','signmessage' ]
+protected_commands = ['payto', 'password', 'mktx', 'signtx', 'seed', 'importprivkey','signmessage' ]
 
 # get password routine
 def prompt_password(prompt, confirm=True):
@@ -139,6 +140,7 @@ def arg_parser():
     parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="show debugging information")
     parser.add_option("-P", "--portable", action="store_true", dest="portable", default=False, help="portable wallet")
     parser.add_option("-L", "--lang", dest="language", default=None, help="defaut language used in GUI")
+    parser.add_option("-u", "--usb", dest="bitkey", action="store_true", help="Turn on support for hardware wallets (EXPERIMENTAL)")
     return parser
 
 
@@ -147,7 +149,6 @@ if __name__ == '__main__':
     parser = arg_parser()
     options, args = parser.parse_args()
     set_verbosity(options.verbose)
-    set_language(options.language)
 
     # config is an object passed to the various constructors (wallet, interface, gui)
     if 'ANDROID_DATA' in os.environ:
@@ -157,6 +158,11 @@ if __name__ == '__main__':
         for k, v in config_options.items():
             if v is None: config_options.pop(k)
 
+    # Wallet migration on Electrum 1.7
+    # Todo: In time we could remove this again
+    if platform.system() == "Windows":
+        util.check_windows_wallet_migration()
+
     config = SimpleConfig(config_options)
     wallet = Wallet(config)
 
@@ -174,6 +180,7 @@ if __name__ == '__main__':
     #right GUI toolkit based the GUI command line option given 
     if cmd == 'gui':
         pref_gui = config.get('gui','classic')
+
         if pref_gui == 'gtk':
             try:
                 import lib.gui as gui
@@ -206,8 +213,10 @@ if __name__ == '__main__':
         interface = Interface(config, True)
         wallet.interface = interface
         interface.start()
-        interface.send([('server.peers.subscribe',[])])
+        if interface.is_connected:
+            interface.send([('server.peers.subscribe',[])])
 
+        set_language(config.get('language'))
         gui = gui.ElectrumGui(wallet, config)
 
         found = config.wallet_file_exists
@@ -218,12 +227,20 @@ if __name__ == '__main__':
             s = gui.network_dialog()
 
             if a =='create':
-                wallet.new_seed(None)
-                wallet.init_mpk( wallet.seed )
+                wallet.init_seed(None)
             else:
                 # ask for seed and gap.
-                if not gui.seed_dialog(): exit()
-                wallet.init_mpk( wallet.seed )
+                sg = gui.seed_dialog()
+                if not sg: exit()
+                seed, gap = sg
+                if not seed: exit()
+                wallet.gap_limit = gap
+                if len(seed) == 128:
+                    wallet.seed = ''
+                    wallet.master_public_key = seed
+                else:
+                    wallet.init_seed(str(seed))
+            
 
             # generate the first addresses, in case we are offline
             if s is None or a == 'create':
@@ -303,10 +320,14 @@ if __name__ == '__main__':
             if not seed:
                 sys.exit("Error: No seed")
 
-            wallet.seed = str(seed)
-            wallet.init_mpk( wallet.seed )
-            if not options.offline:
+            if len(seed) == 128:
+                wallet.seed = None
+                wallet.master_public_key = seed
+            else:
+                wallet.seed = str(seed)
+                wallet.init_mpk( wallet.seed )
 
+            if not options.offline:
                 interface = Interface(config)
                 interface.start()
                 wallet.interface = interface
@@ -327,8 +348,7 @@ if __name__ == '__main__':
             wallet.save()
             print_msg("Wallet saved in '%s'"%wallet.config.path)
         else:
-            wallet.new_seed(None)
-            wallet.init_mpk( wallet.seed )
+            wallet.init_seed(None)
             wallet.synchronize() # there is no wallet thread 
             wallet.save()
             print_msg("Your wallet generation seed is: " + wallet.seed)
@@ -387,23 +407,24 @@ if __name__ == '__main__':
                 exit(1)
             # check password
             try:
-                wallet.pw_decode( wallet.seed, password)
+                seed = wallet.decode_seed(password)
             except:
                 print_msg("Error: This password does not decode this wallet.")
                 exit(1)
         else:
             password = None
+            seed = wallet.seed
 
-    if cmd == 'import':
+    if cmd == 'importprivkey':
         # See if they specificed a key on the cmd line, if not prompt
         if len(args) > 1:
-            keypair = args[1]
+            sec = args[1]
         else:
-            keypair = prompt_password('Enter Address:PrivateKey (will not echo):', False)
+            sec = prompt_password('Enter PrivateKey (will not echo):', False)
         try:
-            wallet.import_key(keypair,password)
+            addr = wallet.import_key(sec,password)
             wallet.save()
-            print_msg("Keypair imported")
+            print_msg("Keypair imported: ", addr)
         except BaseException as e:
             print_msg("Error: Keypair import failed: " + str(e))
 
@@ -418,7 +439,6 @@ if __name__ == '__main__':
             print_msg(known_commands[cmd2])
 
     elif cmd == 'seed':
-        seed = wallet.pw_decode( wallet.seed, password)
         print_msg(seed + ' "' + ' '.join(mnemonic_encode(seed)) + '"')
 
     elif cmd == 'deseed':
@@ -621,11 +641,6 @@ if __name__ == '__main__':
         print_msg(h)
 
     elif cmd == 'password':
-        try:
-            seed = wallet.pw_decode( wallet.seed, password)
-        except ValueError:
-            sys.exit("Error: Password does not decrypt this wallet.")
-
         new_password = prompt_password('New password:')
         wallet.update_password(seed, password, new_password)