mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
* Rename synchronous_get to synchronous_send This makes it more inline with the method 'send' of which synchronous_send is the, well, synchronous version. * Move protocol strings from scripts to network This is again a small step in the right direction. The network module is going to accumulate more and more of these simple methods. Once everything is moved into that module, that module is going to be split. Note that I've left the scripts which use scripts/util.py alone. I suspect the same functionality can be reached when using just lib/network.py and that scripts/util.py is obsolete. * Remove protocol string from verifier and websocket Websocket still has some references, that'll take more work to remove. Once the network module has been split this should be easy. I took the liberty to rename a variable to better show what it is. * Remove protocol strings from remainder The naming scheme I'm following for the newly introduced methods in the network module is: 'blockchain.<subject>.<action>' -> def <action>_(for|to)_<subject> * Move explicit protocol calls closer to each other This makes it easier to keep track of the methods which are due to be extracted. * Remove `send` when using `get_transaction` This is the final step to formalize (the informal) interface of the network module. A chance of note is changed interface for async/sync calls. It is no longer required to use the `synchronous_send` call. Merely NOT passing a callback makes the call synchronous. I feel this makes the API more intuitive to work with and easier to replace with a different network module. * Remove send from get_merkle_for_transaction The pattern which emerged for calling the lambda yielded an slight refactor. I'm not happy with the name for the `__invoke` method. * Remove explict send from websockets * Remove explicit send from scripts * Remove explicit send from wallet * Remove explicit sync_send from commands, scripts * Remove optional timeout parameter This parameter doesn't seem to be used a lot and removing it makes the remaining calls easier. Potentionally a contentious choice! * Rename `broadcast` to `broadcast_transaction` Doing so makes the method name consistent with the other ElectrumX protocol method names. * Remove synchronous_send Now every method is intuitive in what it does, no special handling required. The `broadcast_transaction` method is weird. I've opted not to change the return type b/c I found it hard to know what the exact consequences are. But ideally this method should just works as all the other ElectrumX related messages. On the other hand this shows nicely how you _can_ do something differnt quite easy. * Rename the awkwardly name `__invoke` method The new name reflects what it does. * Process the result of linter feedback I've used flake8-diff (and ignored a couple of line length warnings). * Rename tx_response to on_tx_response This fell through the cracks when this branch was rebased. * subscript_to_scripthash should be get_balance An oversight while refactoring. * Add missing return statement Without this statement the transaction would have been broadcasted twice. * Pass list of tuples to send not single tuple * Add @staticmethod decorator * Fix argument to be an array
231 lines
7.4 KiB
Python
231 lines
7.4 KiB
Python
from decimal import Decimal
|
|
_ = lambda x:x
|
|
#from i18n import _
|
|
from electrum import WalletStorage, Wallet
|
|
from electrum.util import format_satoshis, set_verbosity
|
|
from electrum.bitcoin import is_address, COIN, TYPE_ADDRESS
|
|
import getpass, datetime
|
|
|
|
# minimal fdisk like gui for console usage
|
|
# written by rofl0r, with some bits stolen from the text gui (ncurses)
|
|
|
|
class ElectrumGui:
|
|
|
|
def __init__(self, config, daemon, plugins):
|
|
self.config = config
|
|
self.network = daemon.network
|
|
storage = WalletStorage(config.get_wallet_path())
|
|
if not storage.file_exists:
|
|
print("Wallet not found. try 'electrum create'")
|
|
exit()
|
|
if storage.is_encrypted():
|
|
password = getpass.getpass('Password:', stream=None)
|
|
storage.decrypt(password)
|
|
|
|
self.done = 0
|
|
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(self.network)
|
|
self.contacts = self.wallet.contacts
|
|
|
|
self.network.register_callback(self.on_network, ['updated', '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 on_network(self, event, *args):
|
|
if event == 'updated':
|
|
self.updated()
|
|
elif event == 'banner':
|
|
self.print_banner()
|
|
|
|
def main_command(self):
|
|
self.print_balance()
|
|
c = 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 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"
|
|
messages = []
|
|
|
|
for item in self.wallet.get_history():
|
|
tx_hash, height, conf, timestamp, delta, balance = item
|
|
if conf:
|
|
try:
|
|
time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
|
|
except Exception:
|
|
time_str = "unknown"
|
|
else:
|
|
time_str = 'unconfirmed'
|
|
|
|
label = self.wallet.get_label(tx_hash)
|
|
messages.append( format_str%( time_str, label, format_satoshis(delta, 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):
|
|
if self.wallet.network.is_connected():
|
|
if not self.wallet.up_to_date:
|
|
msg = _( "Synchronizing..." )
|
|
else:
|
|
c, u, x = self.wallet.get_balance()
|
|
msg = _("Balance")+": %f "%(Decimal(c) / COIN)
|
|
if u:
|
|
msg += " [%f unconfirmed]"%(Decimal(u) / COIN)
|
|
if x:
|
|
msg += " [%f unmatured]"%(Decimal(x) / COIN)
|
|
else:
|
|
msg = _( "Not connected" )
|
|
|
|
return(msg)
|
|
|
|
|
|
def print_contacts(self):
|
|
messages = map(lambda x: "%20s %45s "%(x[0], x[1][1]), self.contacts.items())
|
|
self.print_list(messages, "%19s %25s "%("Key", "Value"))
|
|
|
|
def print_addresses(self):
|
|
messages = map(lambda addr: "%30s %30s "%(addr, self.wallet.labels.get(addr,"")), self.wallet.get_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 = input("Pay to: ")
|
|
self.str_description = input("Description : ")
|
|
self.str_amount = input("Amount: ")
|
|
self.str_fee = 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, lst, firstline):
|
|
lst = list(lst)
|
|
self.maxpos = len(lst)
|
|
if not self.maxpos: return
|
|
print(firstline)
|
|
for i in range(self.maxpos):
|
|
msg = lst[i] if i < len(lst) else ""
|
|
print(msg)
|
|
|
|
|
|
def main(self):
|
|
while self.done == 0: self.main_command()
|
|
|
|
def do_send(self):
|
|
if not is_address(self.str_recipient):
|
|
print(_('Invalid Bitcoin address'))
|
|
return
|
|
try:
|
|
amount = int(Decimal(self.str_amount) * COIN)
|
|
except Exception:
|
|
print(_('Invalid Amount'))
|
|
return
|
|
try:
|
|
fee = int(Decimal(self.str_fee) * COIN)
|
|
except Exception:
|
|
print(_('Invalid Fee'))
|
|
return
|
|
|
|
if self.wallet.has_password():
|
|
password = self.password_dialog()
|
|
if not password:
|
|
return
|
|
else:
|
|
password = None
|
|
|
|
c = ""
|
|
while c != "y":
|
|
c = input("ok to send (y/n)?")
|
|
if c == "n": return
|
|
|
|
try:
|
|
tx = self.wallet.mktx([(TYPE_ADDRESS, self.str_recipient, amount)], password, self.config, fee)
|
|
except Exception as e:
|
|
print(str(e))
|
|
return
|
|
|
|
if self.str_description:
|
|
self.wallet.labels[tx.txid()] = self.str_description
|
|
|
|
print(_("Please wait..."))
|
|
status, msg = self.network.broadcast_transaction(tx)
|
|
|
|
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()
|
|
|
|
|
|
# XXX unused
|
|
|
|
def run_receive_tab(self, c):
|
|
#if c == 10:
|
|
# out = self.run_popup('Address', ["Edit label", "Freeze", "Prioritize"])
|
|
return
|
|
|
|
def run_contacts_tab(self, c):
|
|
pass
|