use a dict for commands and help texts
[electrum-nvc.git] / electrum
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import re, sys, getpass
20
21 try:
22     import ecdsa  
23 except:
24     print "python-ecdsa does not seem to be installed. Try 'sudo pip install ecdsa'"
25     sys.exit(1)
26
27 try:
28     import aes
29 except:
30     print "AES does not seem to be installed. Try 'sudo pip install slowaes'"
31     sys.exit(1)
32
33 try:
34     import lib as electrum
35 except:
36     import electrum
37     
38 from optparse import OptionParser
39 from decimal import Decimal
40 from electrum import Wallet, WalletSynchronizer, format_satoshis
41
42 known_commands = {
43     'help':'print help',
44     'validateaddress':'check that the address is valid', 
45     'balance': "Display the balance of your wallet or of an address.\nsyntax: balance [<address>]", 
46     'contacts': "Show your list of contacts", 
47     'create':'create wallet', 
48     'restore':'restore wallet', 
49     'payto':""" 
50             payto <recipient> <amount> [label]
51             create and broadcast a transaction.
52             <recipient> can be a bitcoin address or a label
53             options:\n--fee, -f: set transaction fee\n--fromaddr, -s: send from address -\n--changeaddr, -c: send change to address
54             """,
55     'sendtx':
56             """sendtx <tx>
57             broadcast a transaction to the network. <tx> must be in hexadecimal""",
58     'password': 
59             "change your password",
60     'addresses':  
61             """show your list of addresses.
62             options:
63              -a: show all addresses, including change addresses
64              -k: show private keys
65              -b: show the balance of addresses""",
66     'history':"show the transaction history",
67     'label':"assign a label to an item",
68     'mktx':
69         """create a signed transaction. password protected
70            syntax: mktx <recipient> <amount> [label]
71            options:\n--fee, -f: set transaction fee\n--fromaddr, -s: send from address -\n--changeaddr, -c: send change to address
72         """,
73     'seed':
74             "print the generation seed of your wallet.",
75     'import': 
76             "import key pair",
77     'signmessage':
78             'sign a message with a key',
79     'verifymessage':
80              'verify signature',
81     'eval':  
82              "Run python eval() on an object\nSyntax: eval <expression>\nExample: eval \"wallet.aliases\"",
83     'deseed':
84             "remove seed from the wallet. The seed is stored in a file that has the name of the wallet plus '.seed'",
85     'reseed':
86             "restore seed of the wallet. The wallet must have no seed, and the seed must match the wallet's master public key.",
87     'freeze':'',
88     'unfreeze':'',
89     'prioritize':'',
90     'unprioritize':'',
91     }
92
93
94
95 offline_commands = [ 'password', 'mktx', 'label', 'contacts', 'help', 'validateaddress', 'signmessage', 'verifymessage', 'eval', 'create', 'addresses', 'import', 'seed','deseed','reseed','freeze','unfreeze','prioritize','unprioritize']
96
97 protected_commands = ['payto', 'password', 'mktx', 'seed', 'import','signmessage' ]
98
99 if __name__ == '__main__':
100
101     usage = "usage: %prog [options] command\nCommands: "+ (', '.join(known_commands))
102     parser = OptionParser(usage=usage)
103     parser.add_option("-g", "--gui", dest="gui", default="qt", help="gui")
104     parser.add_option("-w", "--wallet", dest="wallet_path", help="wallet path (default: electrum.dat)")
105     parser.add_option("-o", "--offline", action="store_true", dest="offline", default=False, help="remain offline")
106     parser.add_option("-a", "--all", action="store_true", dest="show_all", default=False, help="show all addresses")
107     parser.add_option("-b", "--balance", action="store_true", dest="show_balance", default=False, help="show the balance at listed addresses")
108     parser.add_option("-k", "--keys",action="store_true", dest="show_keys",default=False, help="show the private keys of listed addresses")
109     parser.add_option("-f", "--fee", dest="tx_fee", default="0.005", help="set tx fee")
110     parser.add_option("-s", "--fromaddr", dest="from_addr", default=None, help="set source address for payto/mktx. if it isn't in the wallet, it will ask for the private key unless supplied in the format public_key:private_key. It's not saved in the wallet.")
111     parser.add_option("-c", "--changeaddr", dest="change_addr", default=None, help="set the change address for payto/mktx. default is a spare address, or the source address if it's not in the wallet")
112     parser.add_option("-r", "--remote", dest="remote_url", default=None, help="URL of a remote wallet")
113     options, args = parser.parse_args()
114
115     wallet = Wallet()
116     wallet.set_path(options.wallet_path)
117     wallet.read()
118     wallet.remote_url = options.remote_url
119
120     if len(args)==0:
121         url = None
122         cmd = 'gui'
123     elif len(args)==1 and re.match('^bitcoin:', args[0]):
124         url = args[0]
125         cmd = 'gui'
126     else:
127         cmd = args[0]
128         firstarg = args[1] if len(args) > 1 else ''
129         
130     if cmd == 'gui':
131         if options.gui=='gtk':
132             try:
133                 import lib.gui as gui
134             except:
135                 import electrum.gui as gui
136         elif options.gui=='qt':
137             try:
138                import lib.gui_qt as gui
139             except:
140                 import electrum.gui_qt as gui
141         else:
142             print "unknown gui", options.gui
143             exit(1)
144
145         gui = gui.ElectrumGui(wallet)
146         WalletSynchronizer(wallet,True).start()
147
148         try:
149             found = wallet.file_exists
150             if not found:
151                 found = gui.restore_or_create()
152         except SystemExit, e:
153             exit(e)
154         except BaseException, e:
155             import traceback
156             traceback.print_exc(file=sys.stdout)
157             #gui.show_message(e.message)
158             exit(1)
159
160         if not found: exit(1)
161         gui.main(url)
162         wallet.save()
163         sys.exit(0)
164
165     if cmd not in known_commands:
166         cmd = 'help'
167
168     if not wallet.file_exists and cmd not in ['help','create','restore']:
169         print "Wallet file not found."
170         print "Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option"
171         sys.exit(0)
172     
173     if cmd in ['create', 'restore']:
174         from electrum import mnemonic
175         if wallet.file_exists:
176             print "remove the existing wallet first!"
177             sys.exit(0)
178         password = getpass.getpass("Password (hit return if you do not wish to encrypt your wallet):")
179         if password:
180             password2 = getpass.getpass("Confirm password:")
181             if password != password2:
182                 print "error"
183                 sys.exit(1)
184         else:
185             password = None
186
187         w_host, w_port, w_protocol = wallet.server.split(':')
188         host = raw_input("server (default:%s):"%w_host)
189         port = raw_input("port (default:%s):"%w_port)
190         protocol = raw_input("protocol [t=tcp;h=http;n=native] (default:%s):"%w_protocol)
191         fee = raw_input("fee (default:%s):"%( str(Decimal(wallet.fee)/100000000)) )
192         gap = raw_input("gap limit (default 5):")
193         if host: w_host = host
194         if port: w_port = port
195         if protocol: w_protocol = protocol
196         wallet.server = w_host + ':' + w_port + ':' +w_protocol
197         if fee: wallet.fee = float(fee)
198         if gap: wallet.gap_limit = int(gap)
199
200         if cmd == 'restore':
201             seed = raw_input("seed:")
202             try:
203                 seed.decode('hex')
204             except:
205                 print "not hex, trying decode"
206                 seed = mnemonic.mn_decode( seed.split(' ') )
207             if not seed:
208                 print "no seed"
209                 sys.exit(1)
210
211             wallet.seed = str(seed)
212             wallet.init_mpk( wallet.seed )
213             if not options.offline:
214                 WalletSynchronizer(wallet).start()
215                 print "recovering wallet..."
216                 wallet.up_to_date_event.clear()
217                 wallet.up_to_date = False
218                 wallet.update()
219                 if wallet.is_found():
220                     print "recovery successful"
221                 else:
222                     print "found no history for this wallet"
223             wallet.fill_addressbook()
224             wallet.save()
225             print "Wallet saved in '%s'"%wallet.path
226         else:
227             wallet.new_seed(None)
228             wallet.init_mpk( wallet.seed )
229             wallet.synchronize() # there is no wallet thread 
230             wallet.save()
231             print "Your wallet generation seed is: " + wallet.seed
232             print "Please keep it in a safe place; if you lose it, you will not be able to restore your wallet."
233             print "Equivalently, your wallet seed can be stored and recovered with the following mnemonic code:"
234             print "\""+' '.join(mnemonic.mn_encode(wallet.seed))+"\""
235             print "Wallet saved in '%s'"%wallet.path
236             
237         if password:
238             wallet.update_password(wallet.seed, None, password)
239
240     # check syntax
241     if cmd in ['payto', 'mktx']:
242         try:
243             to_address = args[1]
244             amount = int( 100000000 * Decimal(args[2]) )
245             change_addr = None
246             label = ' '.join(args[3:])
247             if options.tx_fee: 
248                 options.tx_fee = int( 100000000 * Decimal(options.tx_fee) )
249         except:
250             firstarg = cmd
251             cmd = 'help'
252
253     # open session
254     if cmd not in offline_commands and not options.offline:
255         WalletSynchronizer(wallet).start()
256         wallet.update()
257         wallet.save()
258
259     # check if --from_addr not in wallet (for mktx/payto)
260     is_temporary = False
261     from_addr = None
262     if options.from_addr:
263         from_addr = options.from_addr
264         if from_addr not in wallet.all_addresses():
265             is_temporary = True
266                 
267     # commands needing password
268     if cmd in protected_commands or ( cmd=='addresses' and options.show_keys):
269         password = getpass.getpass('Password:') if wallet.use_encryption and not is_temporary else None
270         # check password
271         try:
272             wallet.pw_decode( wallet.seed, password)
273         except:
274             print "invalid password"
275             exit(1)
276
277     if cmd == 'import':
278         keypair = args[1]
279         try:
280             wallet.import_key(keypair,password)
281             wallet.save()
282             print "keypair imported"
283         except BaseException, e:
284             print( 'Error:' + str(e) )
285
286     if cmd=='help':
287         cmd2 = firstarg
288         if cmd2 not in known_commands:
289             print "type 'electrum help <command>' to see the help for a specific command"
290             print "type 'electrum --help' to see the list of options"
291             print "list of commands:", ', '.join(known_commands)
292         else:
293             print known_commands[cmd2]
294
295     elif cmd == 'seed':
296         from electrum import mnemonic
297         seed = wallet.pw_decode( wallet.seed, password)
298         print seed + ' "' + ' '.join(mnemonic.mn_encode(seed)) + '"'
299
300     elif cmd == 'deseed':
301         if not wallet.seed:
302             print "Error: This wallet has no seed"
303         elif wallet.use_encryption:
304             print "Error: This wallet is encrypted"
305         else:
306             ns = wallet.path + '.seed'
307             print "Warning: you are going to extract the seed from '%s'\nThe seed will be saved in '%s'"%(wallet.path,ns)
308             if raw_input("Are you sure you want to continue? (y/n) ") in ['y','Y','yes']:
309                 f = open(ns,'w')
310                 f.write(repr({'seed':wallet.seed, 'imported_keys':wallet.imported_keys})+"\n")
311                 f.close()
312                 wallet.seed = ''
313                 for k in wallet.imported_keys.keys(): wallet.imported_keys[k] = ''
314                 wallet.save()
315                 print "Done."
316             else:
317                 print "Action canceled."
318
319     elif cmd == 'reseed':
320         if wallet.seed:
321             print "This wallet already has a seed", wallet.seed
322         else:
323             ns = wallet.path + '.seed'
324             try:
325                 f = open(ns,'r')
326                 data = f.read()
327                 f.close()
328             except:
329                 print "seed file not found"
330                 sys.exit()
331             try:
332                 import ast
333                 d = ast.literal_eval( data )
334                 seed = d['seed']
335                 imported_keys = d.get('imported_keys',{})
336             except:
337                 print "error with seed file"
338                 sys.exit(1)
339
340             mpk = wallet.master_public_key
341             wallet.seed = seed
342             wallet.imported_keys = imported_keys
343             wallet.use_encryption = False
344             wallet.init_mpk(seed)
345             if mpk == wallet.master_public_key:
346                 wallet.save()
347                 print "Done: " + wallet.path
348             else:
349                 print "error: master public key does not match"
350
351     elif cmd == 'validateaddress':
352         addr = args[1]
353         print wallet.is_valid(addr)
354
355     elif cmd == 'balance':
356         try:
357             addrs = args[1:]
358         except:
359             pass
360         if addrs == []:
361             c, u = wallet.get_balance()
362             if u:
363                 print Decimal( c ) / 100000000 , Decimal( u ) / 100000000
364             else:
365                 print Decimal( c ) / 100000000
366         else:
367             for addr in addrs:
368                 c, u = wallet.get_addr_balance(addr)
369                 if u:
370                     print "%s %s, %s" % (addr, str(Decimal(c)/100000000), str(Decimal(u)/100000000))
371                 else:
372                     print "%s %s" % (addr, str(Decimal(c)/100000000))
373
374     elif cmd in [ 'contacts']:
375         for addr in wallet.addressbook:
376             print addr, "   ", wallet.labels.get(addr)
377
378     elif cmd == 'eval':
379         print eval(args[1])
380         wallet.save()
381
382     elif cmd in [ 'addresses']:
383         for addr in wallet.all_addresses():
384             if options.show_all or not wallet.is_change(addr):
385                 label = wallet.labels.get(addr)
386                 _type = ''
387                 if wallet.is_change(addr): _type = "[change]"
388                 if addr in wallet.imported_keys.keys(): _type = "[imported]"
389                 if label is None: label = ''
390                 if options.show_balance:
391                     h = wallet.history.get(addr,[])
392                     ni = no = 0
393                     for item in h:
394                         if item['is_input']:  ni += 1
395                         else:              no += 1
396                     b = "%d %d %s"%(no, ni, str(Decimal(wallet.get_addr_balance(addr)[0])/100000000))
397                 else: b=''
398                 if options.show_keys:
399                     addr += ':' + str(wallet.get_private_key_base58(addr, password))
400                 print addr, b, _type, label
401
402     if cmd == 'history':
403         lines = wallet.get_tx_history()
404         b = 0 
405         for line in lines:
406             import datetime
407             v = line['value'] 
408             b += v
409             try:
410                 time_str = str( datetime.datetime.fromtimestamp( line['timestamp']))
411             except:
412                 print line['timestamp']
413                 time_str = 'pending'
414             label = line.get('label')
415             if not label: label = line['tx_hash']
416             else: label = label + ' '*(64 - len(label) )
417
418             print time_str , "  " + label + "  " + format_satoshis(v)+ "  "+ format_satoshis(b)
419         print "# balance: ", format_satoshis(b)
420
421     elif cmd == 'label':
422         try:
423             tx = args[1]
424             label = ' '.join(args[2:])
425         except:
426             print "syntax:  label <tx_hash> <text>"
427             sys.exit(1)
428         wallet.labels[tx] = label
429         wallet.save()
430             
431     elif cmd in ['payto', 'mktx']:
432         if from_addr and is_temporary:
433             if from_addr.find(":") == -1:
434                 keypair = from_addr + ":" + getpass.getpass('Private key:')
435             else:
436                 keypair = from_addr
437                 from_addr = keypair.split(':')[0]
438             if not wallet.import_key(keypair,password):
439                 print "invalid key pair"
440                 exit(1)
441             wallet.history[from_addr] = interface.retrieve_history(from_addr)
442             wallet.update_tx_history()
443             change_addr = from_addr
444
445         if options.change_addr:
446             change_addr = options.change_addr
447
448         for k, v in wallet.labels.items():
449             if v == to_address:
450                 to_address = k
451                 print "alias", to_address
452                 break
453             if change_addr and v == change_addr:
454                 change_addr = k
455         try:
456             tx = wallet.mktx( to_address, amount, label, password,
457                 fee = options.tx_fee, change_addr = change_addr, from_addr = from_addr )
458         except:
459             import traceback
460             traceback.print_exc(file=sys.stdout)
461             tx = None
462
463         if tx and cmd=='payto': 
464             r, h = wallet.sendtx( tx )
465             print h
466         else:
467             print tx
468
469         if is_temporary:
470             wallet.imported_keys.pop(from_addr)
471             del(wallet.history[from_addr])
472         wallet.save()
473
474     elif cmd == 'sendtx':
475         tx = args[1]
476         r, h = wallet.sendtx( tx )
477         print h
478
479     elif cmd == 'password':
480         try:
481             seed = wallet.pw_decode( wallet.seed, password)
482         except:
483             print "sorry"
484             sys.exit(1)
485         new_password = getpass.getpass('New password:')
486         if new_password == getpass.getpass('Confirm new password:'):
487             wallet.update_password(seed, password, new_password)
488         else:
489             print "error: mismatch"
490
491     elif cmd == 'signmessage':
492         address, message = args[1:3]
493         print wallet.sign_message(address, message, password)
494
495     elif cmd == 'verifymessage':
496         address, signature, message = args[1:4]
497         try:
498             wallet.verify_message(address, signature, message)
499             print True
500         except:
501             print False
502
503     elif cmd == 'freeze':
504         addr = args[1]
505         if addr in self.wallet.all_addresses() and addr not in self.wallet.frozen_addresses:
506             self.wallet.frozen_addresses.append(addr)
507             self.wallet.save()
508             print True
509         else:
510             print False
511
512     elif cmd == 'unfreeze':
513         addr = args[1]
514         if addr in self.wallet.all_addresses() and addr in self.wallet.frozen_addresses:
515             self.wallet.frozen_addresses.remove(addr)
516             self.wallet.save()
517             print True
518         else:
519             print False
520
521     elif cmd == 'prioritize':
522         addr = args[1]
523         if addr in self.wallet.all_addresses() and addr not in self.wallet.frozen_addresses:
524             self.wallet.prioritized_addresses.append(addr)
525             self.wallet.save()
526             print True
527         else:
528             print False
529
530     elif cmd == 'unprioritize':
531         addr = args[1]
532         if addr in self.wallet.all_addresses() and addr in self.wallet.frozen_addresses:
533             self.wallet.prioritized_addresses.remove(addr)
534             self.wallet.save()
535             print True
536         else:
537             print False
538