2013-09-25 20:23:14 -07:00
|
|
|
from decimal import Decimal
|
|
|
|
_ = lambda x:x
|
|
|
|
#from i18n import _
|
2014-08-29 03:27:10 -07:00
|
|
|
from electrum.wallet import WalletStorage, Wallet
|
2015-04-23 03:46:52 -07:00
|
|
|
from electrum.util import format_satoshis, set_verbosity, StoreDict
|
2015-05-31 19:26:22 -07:00
|
|
|
from electrum.bitcoin import is_valid, COIN
|
2013-09-25 20:23:14 -07:00
|
|
|
from electrum.network import filter_protocol
|
|
|
|
import sys, getpass, datetime
|
|
|
|
|
|
|
|
# minimal fdisk like gui for console usage
|
|
|
|
# written by rofl0r, with some bits stolen from the text gui (ncurses)
|
|
|
|
|
|
|
|
class ElectrumGui:
|
|
|
|
|
2015-09-02 20:02:03 -07:00
|
|
|
def __init__(self, config, network, plugins):
|
2013-09-25 20:23:14 -07:00
|
|
|
self.network = network
|
|
|
|
self.config = config
|
2015-05-08 01:58:54 -07:00
|
|
|
storage = WalletStorage(config.get_wallet_path())
|
2013-09-25 20:23:14 -07:00
|
|
|
if not storage.file_exists:
|
|
|
|
print "Wallet not found. try 'electrum create'"
|
|
|
|
exit()
|
|
|
|
|
2014-09-05 08:34:50 -07:00
|
|
|
self.done = 0
|
2013-09-25 20:23:14 -07:00
|
|
|
self.last_balance = ""
|
|
|
|
|
|
|
|
set_verbosity(False)
|
|
|
|
|
|
|
|
self.str_recipient = ""
|
|
|
|
self.str_description = ""
|
|
|
|
self.str_amount = ""
|
|
|
|
self.str_fee = ""
|
|
|
|
|
|
|
|
self.wallet = Wallet(storage)
|
|
|
|
self.wallet.start_threads(network)
|
2015-04-23 03:46:52 -07:00
|
|
|
self.contacts = StoreDict(self.config, 'contacts')
|
2015-09-02 20:02:03 -07:00
|
|
|
|
2013-09-25 20:23:14 -07:00
|
|
|
self.wallet.network.register_callback('updated', self.updated)
|
2015-11-11 22:32:24 -08:00
|
|
|
|
2013-09-25 20:23:14 -07:00
|
|
|
self.wallet.network.register_callback('peers', self.peers)
|
|
|
|
self.wallet.network.register_callback('banner', self.print_banner)
|
|
|
|
self.commands = [_("[h] - displays this help text"), \
|
|
|
|
_("[i] - display transaction history"), \
|
|
|
|
_("[o] - enter payment order"), \
|
|
|
|
_("[p] - print stored payment order"), \
|
|
|
|
_("[s] - send stored payment order"), \
|
|
|
|
_("[r] - show own receipt addresses"), \
|
|
|
|
_("[c] - display contacts"), \
|
|
|
|
_("[b] - print server banner"), \
|
|
|
|
_("[q] - quit") ]
|
|
|
|
self.num_commands = len(self.commands)
|
|
|
|
|
|
|
|
def main_command(self):
|
|
|
|
self.print_balance()
|
|
|
|
c = raw_input("enter command: ")
|
|
|
|
if c == "h" : self.print_commands()
|
|
|
|
elif c == "i" : self.print_history()
|
|
|
|
elif c == "o" : self.enter_order()
|
|
|
|
elif c == "p" : self.print_order()
|
|
|
|
elif c == "s" : self.send_order()
|
|
|
|
elif c == "r" : self.print_addresses()
|
|
|
|
elif c == "c" : self.print_contacts()
|
|
|
|
elif c == "b" : self.print_banner()
|
|
|
|
elif c == "n" : self.network_dialog()
|
|
|
|
elif c == "e" : self.settings_dialog()
|
|
|
|
elif c == "q" : self.done = 1
|
|
|
|
else: self.print_commands()
|
|
|
|
|
|
|
|
def peers(self):
|
|
|
|
print("got peers list:")
|
|
|
|
l = filter_protocol(self.wallet.network.get_servers(), 's')
|
|
|
|
for s in l:
|
|
|
|
print (s)
|
|
|
|
|
|
|
|
def updated(self):
|
|
|
|
s = self.get_balance()
|
|
|
|
if s != self.last_balance:
|
|
|
|
print(s)
|
|
|
|
self.last_balance = s
|
|
|
|
return True
|
|
|
|
|
|
|
|
def print_commands(self):
|
|
|
|
self.print_list(self.commands, "Available commands")
|
|
|
|
|
|
|
|
def print_history(self):
|
|
|
|
width = [20, 40, 14, 14]
|
|
|
|
delta = (80 - sum(width) - 4)/3
|
|
|
|
format_str = "%"+"%d"%width[0]+"s"+"%"+"%d"%(width[1]+delta)+"s"+"%" \
|
|
|
|
+ "%d"%(width[2]+delta)+"s"+"%"+"%d"%(width[3]+delta)+"s"
|
2015-09-02 20:02:03 -07:00
|
|
|
b = 0
|
2013-09-25 20:23:14 -07:00
|
|
|
messages = []
|
|
|
|
|
2015-04-02 06:19:00 -07:00
|
|
|
for item in self.wallet.get_history():
|
|
|
|
tx_hash, confirmations, value, timestamp, balance = item
|
2013-09-25 20:23:14 -07:00
|
|
|
if confirmations:
|
|
|
|
try:
|
2015-04-02 06:19:00 -07:00
|
|
|
time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
|
2013-11-10 12:30:57 -08:00
|
|
|
except Exception:
|
2013-09-25 20:23:14 -07:00
|
|
|
time_str = "unknown"
|
|
|
|
else:
|
|
|
|
time_str = 'pending'
|
|
|
|
|
|
|
|
label, is_default_label = self.wallet.get_label(tx_hash)
|
|
|
|
messages.append( format_str%( time_str, label, format_satoshis(value, whitespaces=True), format_satoshis(balance, whitespaces=True) ) )
|
|
|
|
|
|
|
|
self.print_list(messages[::-1], format_str%( _("Date"), _("Description"), _("Amount"), _("Balance")))
|
|
|
|
|
|
|
|
|
|
|
|
def print_balance(self):
|
|
|
|
print(self.get_balance())
|
|
|
|
|
|
|
|
def get_balance(self):
|
2015-04-02 06:19:00 -07:00
|
|
|
if self.wallet.network.is_connected():
|
2013-09-25 20:23:14 -07:00
|
|
|
if not self.wallet.up_to_date:
|
|
|
|
msg = _( "Synchronizing..." )
|
2015-09-02 20:02:03 -07:00
|
|
|
else:
|
2015-05-05 11:52:14 -07:00
|
|
|
c, u, x = self.wallet.get_balance()
|
2015-05-31 19:26:22 -07:00
|
|
|
msg = _("Balance")+": %f "%(Decimal(c) / COIN)
|
2015-05-05 11:52:14 -07:00
|
|
|
if u:
|
2015-05-31 19:26:22 -07:00
|
|
|
msg += " [%f unconfirmed]"%(Decimal(u) / COIN)
|
2015-05-05 11:52:14 -07:00
|
|
|
if x:
|
2015-05-31 19:26:22 -07:00
|
|
|
msg += " [%f unmatured]"%(Decimal(x) / COIN)
|
2013-09-25 20:23:14 -07:00
|
|
|
else:
|
|
|
|
msg = _( "Not connected" )
|
2015-09-02 20:02:03 -07:00
|
|
|
|
2013-09-25 20:23:14 -07:00
|
|
|
return(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def print_contacts(self):
|
2015-04-23 03:46:52 -07:00
|
|
|
messages = map(lambda x: "%20s %45s "%(x[0], x[1][1]), self.contacts.items())
|
|
|
|
self.print_list(messages, "%19s %25s "%("Key", "Value"))
|
2013-09-25 20:23:14 -07:00
|
|
|
|
|
|
|
def print_addresses(self):
|
|
|
|
messages = map(lambda addr: "%30s %30s "%(addr, self.wallet.labels.get(addr,"")), self.wallet.addresses())
|
|
|
|
self.print_list(messages, "%19s %25s "%("Address", "Label"))
|
|
|
|
|
|
|
|
def print_order(self):
|
|
|
|
print("send order to " + self.str_recipient + ", amount: " + self.str_amount \
|
|
|
|
+ "\nfee: " + self.str_fee + ", desc: " + self.str_description)
|
|
|
|
|
|
|
|
def enter_order(self):
|
|
|
|
self.str_recipient = raw_input("Pay to: ")
|
|
|
|
self.str_description = raw_input("Description : ")
|
|
|
|
self.str_amount = raw_input("Amount: ")
|
|
|
|
self.str_fee = raw_input("Fee: ")
|
|
|
|
|
|
|
|
def send_order(self):
|
|
|
|
self.do_send()
|
|
|
|
|
|
|
|
def print_banner(self):
|
|
|
|
for i, x in enumerate( self.wallet.network.banner.split('\n') ):
|
|
|
|
print( x )
|
|
|
|
|
|
|
|
def print_list(self, list, firstline):
|
|
|
|
self.maxpos = len(list)
|
|
|
|
if not self.maxpos: return
|
|
|
|
print(firstline)
|
|
|
|
for i in range(self.maxpos):
|
|
|
|
msg = list[i] if i < len(list) else ""
|
|
|
|
print(msg)
|
|
|
|
|
2015-09-02 20:02:03 -07:00
|
|
|
|
2015-08-26 08:44:19 -07:00
|
|
|
def main(self):
|
2013-09-25 20:23:14 -07:00
|
|
|
while self.done == 0: self.main_command()
|
|
|
|
|
|
|
|
def do_send(self):
|
|
|
|
if not is_valid(self.str_recipient):
|
|
|
|
print(_('Invalid Bitcoin address'))
|
|
|
|
return
|
|
|
|
try:
|
2015-05-31 19:26:22 -07:00
|
|
|
amount = int(Decimal(self.str_amount) * COIN)
|
2013-11-10 12:30:57 -08:00
|
|
|
except Exception:
|
2013-09-25 20:23:14 -07:00
|
|
|
print(_('Invalid Amount'))
|
|
|
|
return
|
|
|
|
try:
|
2015-05-31 19:26:22 -07:00
|
|
|
fee = int(Decimal(self.str_fee) * COIN)
|
2013-11-10 12:30:57 -08:00
|
|
|
except Exception:
|
2013-09-25 20:23:14 -07:00
|
|
|
print(_('Invalid Fee'))
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.wallet.use_encryption:
|
|
|
|
password = self.password_dialog()
|
|
|
|
if not password:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
password = None
|
|
|
|
|
|
|
|
c = ""
|
|
|
|
while c != "y":
|
|
|
|
c = raw_input("ok to send (y/n)?")
|
|
|
|
if c == "n": return
|
|
|
|
|
|
|
|
try:
|
2015-11-04 10:07:50 -08:00
|
|
|
tx = self.wallet.mktx( [("address", self.str_recipient, amount)], password, self.config, fee)
|
2013-11-09 20:21:02 -08:00
|
|
|
except Exception as e:
|
2013-09-25 20:23:14 -07:00
|
|
|
print(str(e))
|
|
|
|
return
|
2015-09-02 20:02:03 -07:00
|
|
|
|
|
|
|
if self.str_description:
|
2013-09-25 20:23:14 -07:00
|
|
|
self.wallet.labels[tx.hash()] = self.str_description
|
|
|
|
|
|
|
|
h = self.wallet.send_tx(tx)
|
|
|
|
print(_("Please wait..."))
|
|
|
|
self.wallet.tx_event.wait()
|
2014-01-05 00:19:23 -08:00
|
|
|
status, msg = self.wallet.receive_tx( h, tx )
|
2013-09-25 20:23:14 -07:00
|
|
|
|
|
|
|
if status:
|
|
|
|
print(_('Payment sent.'))
|
|
|
|
#self.do_clear()
|
|
|
|
#self.update_contacts_tab()
|
|
|
|
else:
|
|
|
|
print(_('Error'))
|
|
|
|
|
|
|
|
def network_dialog(self):
|
|
|
|
print("use 'electrum setconfig server/proxy' to change your network settings")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def settings_dialog(self):
|
|
|
|
print("use 'electrum setconfig' to change your settings")
|
|
|
|
return True
|
|
|
|
|
|
|
|
def password_dialog(self):
|
|
|
|
return getpass.getpass()
|
2015-09-02 20:02:03 -07:00
|
|
|
|
2013-09-25 20:23:14 -07:00
|
|
|
|
|
|
|
# XXX unused
|
|
|
|
|
|
|
|
def run_receive_tab(self, c):
|
|
|
|
#if c == 10:
|
|
|
|
# out = self.run_popup('Address', ["Edit label", "Freeze", "Prioritize"])
|
|
|
|
return
|
2015-09-02 20:02:03 -07:00
|
|
|
|
2013-09-25 20:23:14 -07:00
|
|
|
def run_contacts_tab(self, c):
|
|
|
|
pass
|