Standardizing message format and implementing stderr for errors
[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
20
21 try:
22     import ecdsa  
23 except:
24     sys.stderr.write("Error: python-ecdsa does not seem to be installed. Try 'sudo pip install ecdsa'\n")
25     sys.stderr.flush()
26     sys.exit(1)
27
28 try:
29     import aes
30 except:
31     sys.stderr.write("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'\n")
32     sys.stderr.flush()
33     sys.exit(1)
34
35 try:
36     from lib import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password
37 except ImportError:
38     from electrum import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password
39     
40 from optparse import OptionParser
41 from decimal import Decimal
42
43 known_commands = {
44     'help':'Prints this help',
45     'validateaddress':'Check that the address is valid', 
46     'balance': "Display the balance of your wallet or of an address.\nSyntax: balance [<address>]", 
47     'contacts': "Show your list of contacts", 
48     'create':'Create a wallet', 
49     'restore':'Restore a wallet', 
50     'payto':"""Create and broadcast a transaction.
51 Syntax: payto <recipient> <amount> [label]
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             'Broadcasts a transaction to the network. \nSyntax: sendtx <tx>\n<tx> must be in hexadecimal.',
57     'password': 
58             "Changes your password",
59     'addresses':  
60             """Shows your list of addresses.
61 options:
62   -a: show all addresses, including change addresses
63   -k: show private keys
64   -b: show the balance of addresses""",
65
66     'history':"Shows the transaction history",
67     'label':'Assign a label to an item\nSyntax: label <tx_hash> <label>',
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             'Imports a key pair\nSyntax: import <address>:<privatekey>',
77     'signmessage':
78             'Signs a message with a key\nSyntax: signmessage <address> <message>',
79     'verifymessage':
80              'Verifies a signature\nSyntax: verifymessage <address> <signature> <message>',
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="lite", 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 ImportError:
135                 import electrum.gui as gui
136         elif options.gui=='qt':
137             try:
138                 import lib.gui_qt as gui
139             except ImportError:
140                 import electrum.gui_qt as gui
141         elif options.gui == 'lite':
142             try:
143                 import lib.gui_lite as gui
144             except ImportError:
145                 import electrum.gui_lite as gui
146         else:
147             sys.stderr.write("Error: Unknown GUI: " + options.gui + "\n")
148             sys.stderr.flush()
149             exit(1)
150
151         gui = gui.ElectrumGui(wallet)
152         WalletSynchronizer(wallet,True).start()
153
154         try:
155             found = wallet.file_exists
156             if not found:
157                 found = gui.restore_or_create()
158         except SystemExit, e:
159             exit(e)
160         except BaseException, e:
161             import traceback
162             traceback.print_exc(file=sys.stdout)
163             #gui.show_message(e.message)
164             exit(1)
165
166         if not found:
167             exit(1)
168         gui.main(url)
169         wallet.save()
170         sys.exit(0)
171
172     if cmd not in known_commands:
173         cmd = 'help'
174
175     if not wallet.file_exists and cmd not in ['help','create','restore']:
176         sys.stderr.write("Error: Wallet file not found.\n")
177         sys.stderr.write("Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option\n")
178         sys.stderr.flush()
179         sys.exit(0)
180     
181     if cmd in ['create', 'restore']:
182         if wallet.file_exists:
183             sys.stderr.write("Error: Remove the existing wallet first!\n")
184             sys.stderr.flush()
185             sys.exit(0)
186         password = prompt_password("Password (hit return if you do not wish to encrypt your wallet):")
187
188         w_host, w_port, w_protocol = wallet.server.split(':')
189         host = raw_input("server (default:%s):"%w_host)
190         port = raw_input("port (default:%s):"%w_port)
191         protocol = raw_input("protocol [t=tcp;h=http;n=native] (default:%s):"%w_protocol)
192         fee = raw_input("fee (default:%s):"%( str(Decimal(wallet.fee)/100000000)) )
193         gap = raw_input("gap limit (default 5):")
194         if host: w_host = host
195         if port: w_port = port
196         if protocol: w_protocol = protocol
197         wallet.server = w_host + ':' + w_port + ':' +w_protocol
198         if fee: wallet.fee = float(fee)
199         if gap: wallet.gap_limit = int(gap)
200
201         if cmd == 'restore':
202             seed = raw_input("seed:")
203             try:
204                 seed.decode('hex')
205             except:
206                 sys.stderr.write("Warning: Not hex, trying decode.\n")
207                 sys.stderr.flush()
208                 seed = mnemonic.mn_decode( seed.split(' ') )
209             if not seed:
210                 sys.stderr.write("Error: No seed\n")
211                 sys.stderr.flush()
212                 sys.exit(1)
213
214             wallet.seed = str(seed)
215             wallet.init_mpk( wallet.seed )
216             if not options.offline:
217                 WalletSynchronizer(wallet).start()
218                 print "Recovering wallet..."
219                 wallet.up_to_date_event.clear()
220                 wallet.up_to_date = False
221                 wallet.update()
222                 if wallet.is_found():
223                     print "Recovery successful"
224                 else:
225                     sys.stderr.write("Warning: Found no history for this wallet\n")
226                     sys.stderr.flush()
227             wallet.fill_addressbook()
228             wallet.save()
229             sys.stderr.write("Wallet saved in '" + wallet.path + "'\n")
230             sys.stderr.flush()
231         else:
232             wallet.new_seed(None)
233             wallet.init_mpk( wallet.seed )
234             wallet.synchronize() # there is no wallet thread 
235             wallet.save()
236             print "Your wallet generation seed is: " + wallet.seed
237             print "Please keep it in a safe place; if you lose it, you will not be able to restore your wallet."
238             print "Equivalently, your wallet seed can be stored and recovered with the following mnemonic code:"
239             print "\""+' '.join(mnemonic.mn_encode(wallet.seed))+"\""
240             print "Wallet saved in '%s'"%wallet.path
241             
242         if password:
243             wallet.update_password(wallet.seed, None, password)
244
245     # check syntax
246     if cmd in ['payto', 'mktx']:
247         try:
248             to_address = args[1]
249             amount = int( 100000000 * Decimal(args[2]) )
250             change_addr = None
251             label = ' '.join(args[3:])
252             if options.tx_fee: 
253                 options.tx_fee = int( 100000000 * Decimal(options.tx_fee) )
254         except:
255             firstarg = cmd
256             cmd = 'help'
257
258     # open session
259     if cmd not in offline_commands and not options.offline:
260         WalletSynchronizer(wallet).start()
261         wallet.update()
262         wallet.save()
263
264     # check if --from_addr not in wallet (for mktx/payto)
265     is_temporary = False
266     from_addr = None
267     if options.from_addr:
268         from_addr = options.from_addr
269         if from_addr not in wallet.all_addresses():
270             is_temporary = True
271                 
272     # commands needing password
273     if cmd in protected_commands or ( cmd=='addresses' and options.show_keys):
274         password = prompt_password('Password:') if wallet.use_encryption and not is_temporary else None
275         # check password
276         try:
277             wallet.pw_decode( wallet.seed, password)
278         except:
279             sys.stderr.write("Error: This password does not decode this wallet.\n")
280             sys.stderr.flush()
281             exit(1)
282
283     if cmd == 'import':
284         keypair = args[1]
285         try:
286             wallet.import_key(keypair,password)
287             wallet.save()
288             print "Keypair imported"
289         except BaseException, e:
290             sys.stderr.write("Error: Keypair import failed: " + str(e) + "\n")
291             sys.stderr.flush()
292
293     if cmd=='help':
294         cmd2 = firstarg
295         if cmd2 not in known_commands:
296             print "Type 'electrum help <command>' to see the help for a specific command"
297             print "Type 'electrum --help' to see the list of options"
298             print "List of commands:", ', '.join(known_commands)
299         else:
300             print known_commands[cmd2]
301
302     elif cmd == 'seed':
303         seed = wallet.pw_decode( wallet.seed, password)
304         print seed + ' "' + ' '.join(mnemonic.mn_encode(seed)) + '"'
305
306     elif cmd == 'deseed':
307         if not wallet.seed:
308             sys.stderr.write("Error: This wallet has no seed\n")
309             sys.stderr.flush()
310         elif wallet.use_encryption:
311             sys.stderr.write("Error: This wallet is encrypted\n")
312             sys.stderr.flush()
313         else:
314             ns = wallet.path + '.seed'
315             print "Warning: you are going to extract the seed from '%s'\nThe seed will be saved in '%s'"%(wallet.path,ns)
316             if raw_input("Are you sure you want to continue? (y/n) ") in ['y','Y','yes']:
317                 f = open(ns,'w')
318                 f.write(repr({'seed':wallet.seed, 'imported_keys':wallet.imported_keys})+"\n")
319                 f.close()
320                 wallet.seed = ''
321                 for k in wallet.imported_keys.keys(): wallet.imported_keys[k] = ''
322                 wallet.save()
323                 print "Done."
324             else:
325                 sys.stderr.write("Action canceled.\n")
326                 sys.stderr.flush()
327
328     elif cmd == 'reseed':
329         if wallet.seed:
330             print "Warning: This wallet already has a seed", wallet.seed
331         else:
332             ns = wallet.path + '.seed'
333             try:
334                 f = open(ns,'r')
335                 data = f.read()
336                 f.close()
337             except:
338                 sys.stderr.write("Error: Seed file not found\n")
339                 sys.stderr.flush()
340                 sys.exit()
341             try:
342                 import ast
343                 d = ast.literal_eval( data )
344                 seed = d['seed']
345                 imported_keys = d.get('imported_keys',{})
346             except:
347                 sys.stderr.write("Error: Error with seed file\n")
348                 sys.stderr.flush()
349                 sys.exit(1)
350
351             mpk = wallet.master_public_key
352             wallet.seed = seed
353             wallet.imported_keys = imported_keys
354             wallet.use_encryption = False
355             wallet.init_mpk(seed)
356             if mpk == wallet.master_public_key:
357                 wallet.save()
358                 print "Done: " + wallet.path
359             else:
360                 sys.stderr.write("Error: Master public key does not match\n")
361                 sys.stderr.flush()
362
363     elif cmd == 'validateaddress':
364         addr = args[1]
365         print wallet.is_valid(addr)
366
367     elif cmd == 'balance':
368         try:
369             addrs = args[1:]
370         except:
371             pass
372         if addrs == []:
373             c, u = wallet.get_balance()
374             if u:
375                 print Decimal( c ) / 100000000 , Decimal( u ) / 100000000
376             else:
377                 print Decimal( c ) / 100000000
378         else:
379             for addr in addrs:
380                 c, u = wallet.get_addr_balance(addr)
381                 if u:
382                     print "%s %s, %s" % (addr, str(Decimal(c)/100000000), str(Decimal(u)/100000000))
383                 else:
384                     print "%s %s" % (addr, str(Decimal(c)/100000000))
385
386     elif cmd in [ 'contacts']:
387         for addr in wallet.addressbook:
388             print addr, "   ", wallet.labels.get(addr)
389
390     elif cmd == 'eval':
391         print eval(args[1])
392         wallet.save()
393
394     elif cmd in [ 'addresses']:
395         for addr in wallet.all_addresses():
396             if options.show_all or not wallet.is_change(addr):
397
398                 flags = wallet.get_address_flags(addr)
399                 label = wallet.labels.get(addr,'')
400                 
401                 if label: label = "\"%s\""%label
402
403                 if options.show_balance:
404                     h = wallet.history.get(addr,[])
405                     #ni = no = 0
406                     #for item in h:
407                     #    if item['is_input']:  ni += 1
408                     #    else:              no += 1
409                     b = format_satoshis(wallet.get_addr_balance(addr)[0])
410                 else: b=''
411                 m_addr = "%34s"%addr
412                 if options.show_keys:
413                     m_addr += ':' + str(wallet.get_private_key_base58(addr, password))
414                 print flags, m_addr, b, label
415
416     if cmd == 'history':
417         lines = wallet.get_tx_history()
418         b = 0 
419         for line in lines:
420             import datetime
421             v = line['value'] 
422             b += v
423             try:
424                 time_str = str( datetime.datetime.fromtimestamp( line['timestamp']))
425             except:
426                 print line['timestamp']
427                 time_str = 'pending'
428             label = line.get('label')
429             if not label: label = line['tx_hash']
430             else: label = label + ' '*(64 - len(label) )
431
432             print time_str , "  " + label + "  " + format_satoshis(v)+ "  "+ format_satoshis(b)
433         print "# balance: ", format_satoshis(b)
434
435     elif cmd == 'label':
436         try:
437             tx = args[1]
438             label = ' '.join(args[2:])
439         except:
440             sys.stderr.write("Error. Syntax:  label <tx_hash> <text>\n")
441             sys.stderr.flush()
442             sys.exit(1)
443         wallet.labels[tx] = label
444         wallet.save()
445             
446     elif cmd in ['payto', 'mktx']:
447         if from_addr and is_temporary:
448             if from_addr.find(":") == -1:
449                 keypair = from_addr + ":" + prompt_password('Private key:', False)
450             else:
451                 keypair = from_addr
452                 from_addr = keypair.split(':')[0]
453             if not wallet.import_key(keypair,password):
454                 sys.stderr.write("Error: Invalid key pair\n")
455                 sys.stderr.flush()
456                 exit(1)
457             wallet.history[from_addr] = interface.retrieve_history(from_addr)
458             wallet.update_tx_history()
459             change_addr = from_addr
460
461         if options.change_addr:
462             change_addr = options.change_addr
463
464         for k, v in wallet.labels.items():
465             if v == to_address:
466                 to_address = k
467                 print "alias", to_address
468                 break
469             if change_addr and v == change_addr:
470                 change_addr = k
471         try:
472             tx = wallet.mktx( to_address, amount, label, password,
473                 fee = options.tx_fee, change_addr = change_addr, from_addr = from_addr )
474         except:
475             import traceback
476             traceback.print_exc(file=sys.stdout)
477             tx = None
478
479         if tx and cmd=='payto': 
480             r, h = wallet.sendtx( tx )
481             print h
482         else:
483             print tx
484
485         if is_temporary:
486             wallet.imported_keys.pop(from_addr)
487             del(wallet.history[from_addr])
488         wallet.save()
489
490     elif cmd == 'sendtx':
491         tx = args[1]
492         r, h = wallet.sendtx( tx )
493         print h
494
495     elif cmd == 'password':
496         try:
497             seed = wallet.pw_decode( wallet.seed, password)
498         except:
499             sys.stderr.write("Error: Password does not decrypt this wallet.\n")
500             sys.stderr.flush()
501             sys.exit(1)
502
503         new_password = prompt_password('New password:')
504         wallet.update_password(seed, password, new_password)
505
506     elif cmd == 'signmessage':
507         address = args[1]
508         message = ' '.join(args[2:])
509         if len(args) > 3:
510             print "Warning: Message was reconstructed from several arguments:", repr(message)
511         print wallet.sign_message(address, message, password)
512
513     elif cmd == 'verifymessage':
514         try:
515             address = args[1]
516             signature = args[2]
517             message = ' '.join(args[3:])
518         except:
519             sys.stderr.write("Error: Not all parameters were given, displaying help instead.\n")
520             sys.stderr.flush()
521             print known_commands[cmd]
522             sys.exit(1)
523         if len(args) > 4:
524             print "Warning: Message was reconstructed from several arguments:", repr(message)
525         try:
526             wallet.verify_message(address, signature, message)
527             print True
528         except:
529             print False
530
531     elif cmd == 'freeze':
532         addr = args[1]
533         print self.wallet.freeze(addr)
534         
535     elif cmd == 'unfreeze':
536         addr = args[1]
537         print self.wallet.unfreeze(addr)
538
539     elif cmd == 'prioritize':
540         addr = args[1]
541         print self.wallet.prioritize(addr)
542
543     elif cmd == 'unprioritize':
544         addr = args[1]
545         print self.wallet.unprioritize(addr)
546