def __init__(self, config, network, daemon, plugins): self.network = network self.config = config storage = WalletStorage(config.get_wallet_path()) if not storage.file_exists: print "Wallet not found. try 'electrum create'" exit() 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(network) self.contacts = StoreDict(self.config, 'contacts') 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 run_offline_command(config, config_options): cmdname = config.get('cmd') cmd = known_commands[cmdname] storage = WalletStorage(config.get_wallet_path()) wallet = Wallet(storage) if cmd.requires_wallet else None # check password if cmd.requires_password and storage.get('use_encryption'): password = config_options.get('password') try: seed = wallet.check_password(password) except InvalidPassword: print "Error: This password does not decode this wallet." sys.exit(1) if cmd.requires_network: print "Warning: running command offline" # arguments passed to function args = map(lambda x: config.get(x), cmd.params) # decode json arguments args = map(json_decode, args) # options args += map(lambda x: config.get(x), cmd.options) cmd_runner = Commands(config, wallet, None, password=config_options.get('password'), new_password=config_options.get('new_password')) func = getattr(cmd_runner, cmd.name) result = func(*args) # save wallet if wallet: wallet.storage.write() return result
def run_offline_command(config, config_options): _ = get_blockchain(config, None) cmdname = config.get('cmd') cmd = Commands.known_commands[cmdname] storage = WalletStorage(config.get_wallet_path()) wallet = Wallet(storage) if cmd.requires_wallet else None # check password if cmd.requires_password and storage.get('use_encryption'): password = config_options.get('password') try: seed = wallet.check_password(password) except InvalidPassword: print "Error: This password does not decode this wallet." sys.exit(1) if cmd.requires_network: print "Warning: running command offline" # arguments passed to function args = map(lambda x: config.get(x), cmd.params) # decode json arguments args = map(json_decode, args) # options args += map(lambda x: config.get(x), cmd.options) cmd_runner = Commands(config, wallet, None, password=config_options.get('password'), new_password=config_options.get('new_password')) func = getattr(cmd_runner, cmd.name) result = func(*args) # save wallet if wallet: wallet.storage.write() return result
def get_wallet(): path = self.config.get_wallet_path() storage = WalletStorage(path) wallet = Wallet(storage) if not storage.file_exists: self.first_run = True seed = wallet.make_seed() wallet.add_seed(seed, None) wallet.create_master_keys(None) wallet.create_main_account() wallet.synchronize() self.wallet = wallet
def __init__(self, lbryum_path): self.config = SimpleConfig() self.config.set_key('chain', 'lbrycrd_main') self.storage = WalletStorage(lbryum_path) self.wallet = Wallet(self.storage) self.cmd_runner = Commands(self.config, self.wallet, None) if not self.wallet.has_seed(): seed = self.wallet.make_seed() self.wallet.add_seed(seed, "derp") self.wallet.create_master_keys("derp") self.wallet.create_main_account() self.wallet.update_password("derp", "") self.network = Network(self.config) self.blockchain = get_blockchain(self.config, self.network) print self.config.get('chain'), self.blockchain self.wallet.storage.write()
def getWallet(path=None): if not path: config = SimpleConfig() path = config.get_wallet_path() storage = WalletStorage(path) if not storage.file_exists: print "Failed to run: No wallet to migrate" sys.exit(1) return Wallet(storage)
def init_cmdline(config_options): config = SimpleConfig(config_options) cmdname = config.get('cmd') cmd = Commands.known_commands[cmdname] # instanciate wallet for command-line storage = WalletStorage(config.get_wallet_path()) if cmd.requires_wallet and not storage.file_exists: log.error("Error: Wallet file not found.") print "Type 'lbryum create' to create a new wallet, or provide a path to a wallet with " \ "the -w option" sys.exit(0) # important warning if cmd.name in ['getprivatekeys']: print "WARNING: ALL your private keys are secret." print "Exposing a single private key can compromise your entire wallet!" print "In particular, DO NOT use 'redeem private key' services proposed by third parties." # commands needing password if cmd.requires_password and storage.get('use_encryption'): if config.get('password'): password = config.get('password') else: password = prompt_password('Password:'******'password'] = password if cmd.name == 'password': new_password = prompt_password('New password:'******'password'] = password config_options['new_password'] = new_password return cmd, password
def run_non_RPC(config): cmdname = config.get('cmd') storage = WalletStorage(config.get_wallet_path()) if storage.file_exists: sys.exit("Error: Remove the existing wallet first!") def password_dialog(): return prompt_password( "Password (hit return if you do not wish to encrypt your wallet):") if cmdname == 'restore': text = config.get('text') password = password_dialog() if Wallet.is_seed(text) or Wallet.is_xprv( text) or Wallet.is_private_key(text) else None try: wallet = Wallet.from_text(text, password, storage) except BaseException as e: sys.exit(str(e)) if not config.get('offline'): network = Network(config) network.start() wallet.start_threads(network) log.info("Recovering wallet...") wallet.synchronize() wallet.wait_until_synchronized() msg = "Recovery successful" if wallet.is_found( ) else "Found no history for this wallet" else: msg = "This wallet was restored offline. It may contain more addresses than displayed." log.info(msg) elif cmdname == 'create': password = password_dialog() wallet = Wallet(storage) seed = wallet.make_seed() wallet.add_seed(seed, password) wallet.create_master_keys(password) wallet.create_main_account() wallet.synchronize() print "Your wallet generation seed is:\n\"%s\"" % seed print "Please keep it in a safe place; if you lose it, you will not be able to restore " \ "your wallet." elif cmdname == 'deseed': wallet = Wallet(storage) if not wallet.seed: log.info("Error: This wallet has no seed") else: ns = wallet.storage.path + '.seedless' print "Warning: you are going to create a seedless wallet'\n" \ "It will be saved in '%s'" % ns if raw_input("Are you sure you want to continue? (y/n) ") in [ 'y', 'Y', 'yes' ]: wallet.storage.path = ns wallet.seed = '' wallet.storage.put('seed', '') wallet.use_encryption = False wallet.storage.put('use_encryption', wallet.use_encryption) for k in wallet.imported_keys.keys(): wallet.imported_keys[k] = '' wallet.storage.put('imported_keys', wallet.imported_keys) print "Done." else: print "Action canceled." wallet.storage.write() else: raise Exception("Unknown command %s" % cmdname) wallet.storage.write() log.info("Wallet saved in '%s'", wallet.storage.path)
class LBRYumWallet(object): def __init__(self, lbryum_path): self.config = SimpleConfig() self.config.set_key('chain', 'lbrycrd_main') self.storage = WalletStorage(lbryum_path) self.wallet = Wallet(self.storage) self.cmd_runner = Commands(self.config, self.wallet, None) if not self.wallet.has_seed(): seed = self.wallet.make_seed() self.wallet.add_seed(seed, "derp") self.wallet.create_master_keys("derp") self.wallet.create_main_account() self.wallet.update_password("derp", "") self.network = Network(self.config) self.blockchain = get_blockchain(self.config, self.network) print self.config.get('chain'), self.blockchain self.wallet.storage.write() def command(self, command_name, *args, **kwargs): cmd_runner = Commands(self.config, self.wallet, None) cmd = known_commands[command_name] func = getattr(cmd_runner, cmd.name) return func(*args, **kwargs) def generate_address(self): address = self.wallet.create_new_address() self.wallet.storage.write() return address
class ElectrumGui: def __init__(self, config, network, daemon, plugins): self.network = network self.config = config storage = WalletStorage(config.get_wallet_path()) if not storage.file_exists: print "Wallet not found. try 'electrum create'" exit() 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(network) self.contacts = StoreDict(self.config, 'contacts') 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 = 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 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" b = 0 messages = [] for item in self.wallet.get_history(): tx_hash, confirmations, value, timestamp, balance = item if confirmations: try: time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3] except Exception: time_str = "unknown" else: time_str = 'pending' 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): 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.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) def main(self): while self.done == 0: self.main_command() def do_send(self): if not is_valid(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.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: 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.hash()] = self.str_description h = self.wallet.send_tx(tx) print(_("Please wait...")) self.wallet.tx_event.wait() status, msg = self.wallet.receive_tx( h, 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
def set_enabled(): wizard.next_button.setEnabled(Wallet.is_xprv(clean_text(text)))
def load_wallet(self, path, get_wizard=None): if path in self.wallets: wallet = self.wallets[path] else: storage = WalletStorage(path) if get_wizard: if storage.file_exists: wallet = Wallet(storage) action = wallet.get_action() else: action = 'new' if action: wizard = get_wizard() wallet = wizard.run(self.network, storage) else: wallet.start_threads(self.network) else: wallet = Wallet(storage) # automatically generate wallet for lbrynet if not storage.file_exists: seed = wallet.make_seed() wallet.add_seed(seed, None) wallet.create_master_keys(None) wallet.create_main_account() wallet.synchronize() wallet.start_threads(self.network) if wallet: self.wallets[path] = wallet return wallet
def run_non_RPC(config): _ = get_blockchain(config, None) cmdname = config.get('cmd') storage = WalletStorage(config.get_wallet_path()) if storage.file_exists: sys.exit("Error: Remove the existing wallet first!") def password_dialog(): return prompt_password("Password (hit return if you do not wish to encrypt your wallet):") if cmdname == 'restore': text = config.get('text') no_password = config.get('no_password') password = None if not no_password and (Wallet.is_seed(text) or Wallet.is_xprv(text) or Wallet.is_private_key(text)): password = password_dialog() try: wallet = Wallet.from_text(text, password, storage) wallet.create_main_account() except BaseException as e: sys.exit(str(e)) if not config.get('offline'): network = Network(config) network.start() wallet.start_threads(network) log.info("Recovering wallet...") wallet.synchronize() wallet.wait_until_synchronized() print "Recovery successful" if wallet.is_found() else "Found no history for this wallet" else: print "This wallet was restored offline. It may contain more addresses than displayed." elif cmdname == 'create': password = password_dialog() wallet = Wallet(storage) seed = wallet.make_seed() wallet.add_seed(seed, password) wallet.create_master_keys(password) wallet.create_main_account() wallet.synchronize() print "Your wallet generation seed is:\n\"%s\"" % seed print "Please keep it in a safe place; if you lose it, you will not be able to restore " \ "your wallet." elif cmdname == 'deseed': wallet = Wallet(storage) if not wallet.seed: log.info("Error: This wallet has no seed") else: ns = wallet.storage.path + '.seedless' print "Warning: you are going to create a seedless wallet'\n" \ "It will be saved in '%s'" % ns if raw_input("Are you sure you want to continue? (y/n) ") in ['y', 'Y', 'yes']: wallet.storage.path = ns wallet.seed = '' wallet.storage.put('seed', '') wallet.use_encryption = False wallet.storage.put('use_encryption', wallet.use_encryption) for k in wallet.imported_keys.keys(): wallet.imported_keys[k] = '' wallet.storage.put('imported_keys', wallet.imported_keys) print "Done." else: print "Action canceled." wallet.storage.write() else: raise Exception("Unknown command %s" % cmdname) wallet.storage.write() log.info("Wallet saved in '%s'", wallet.storage.path)