def stopdaemon(ctx): """ Stops the daemon """ # Check to sere if we're in a venv and don't do anything if we are if os.environ.get("VIRTUAL_ENV"): click.echo("Not stopping any daemons from within a virtualenv.") return try: d = get_daemonizer() except OSError as e: logger.debug(str(e)) click.echo("Error: %s" % e) return msg = "" try: if d.stop(): msg = "walletd successfully stopped." else: msg = "walletd not stopped." except exceptions.DaemonizerError as e: msg = "Error: %s" % e logger.debug(msg) click.echo(msg)
def restore(ctx): """ Restore a wallet from a mnemonic \b If you accidently deleted your wallet file or the file became corrupted, use this command to restore your wallet. You must have your 12 word phrase (mnemonic) that was displayed when you created your wallet. """ # Stop daemon if it's running. d = None try: d = get_daemonizer() except OSError as e: pass if d: try: d.stop() except exceptions.DaemonizerError as e: click.echo("ERROR: Couldn't stop daemon: %s" % e) ctx.exit(code=4) # Check to see if the current wallet path exists if os.path.exists(ctx.obj['wallet_path']): if click.confirm("Wallet file already exists and may have a balance. Do you want to delete it?"): os.remove(ctx.obj['wallet_path']) else: click.echo("Not continuing.") ctx.exit(code=4) # Ask for mnemonic mnemonic = click.prompt("Please enter the wallet's 12 word mnemonic") # Sanity check the mnemonic m = Mnemonic(language='english') if not m.check(mnemonic): click.echo("ERROR: Invalid mnemonic.") ctx.exit(code=5) if click.confirm("Did the wallet have a passphrase?"): passphrase = get_passphrase() else: passphrase = '' # Try creating the wallet click.echo("\nRestoring...") wallet = Two1Wallet.import_from_mnemonic( data_provider=ctx.obj['data_provider'], mnemonic=mnemonic, passphrase=passphrase) wallet.to_file(ctx.obj['wallet_path']) if Two1Wallet.check_wallet_file(ctx.obj['wallet_path']): click.echo("Wallet successfully restored.") else: click.echo("Wallet not restored.") ctx.exit(code=6)
def startdaemon(ctx): """ Starts the daemon """ # Check to sere if we're in a venv and don't do anything if we are if os.environ.get("VIRTUAL_ENV"): click.echo("Not starting daemon while inside a virtualenv. It can be manually started by doing 'walletd' and backgrounding the process.") return # Check if the wallet path exists if not Two1Wallet.check_wallet_file(ctx.obj['wallet_path']): click.echo("ERROR: Wallet does not exist! Not starting daemon.") ctx.exit(code=7) try: d = get_daemonizer() except OSError as e: logger.debug(str(e)) click.echo("Error: %s" % e) return if d.started(): click.echo("walletd already running.") return if not d.installed(): if isinstance(ctx.obj['data_provider'], TwentyOneProvider): dpo = dict(provider='twentyone') elif isinstance(ctx.obj['data_provider'], ChainProvider): dp_params = ctx.obj['data_provider_params'] dpo = dict(provider='chain', api_key_id=dp_params['chain_api_key_id'], api_key_secret=dp_params['chain_api_key_secret']) try: d.install(dpo) except exceptions.DaemonizerError as e: logger.debug(str(e)) click.echo("Error: %s" % e) return msg = "" try: if d.start(): msg = "walletd successfully started." else: msg = "walletd not started." except exceptions.DaemonizerError as e: msg = "Error: %s" % e logger.debug(msg) click.echo(msg)
def startdaemon(ctx): """ Starts the daemon """ # Check to sere if we're in a venv and don't do anything if we are if os.environ.get("VIRTUAL_ENV"): click.echo("Not starting daemon while inside a virtualenv. It can be manually started by doing 'walletd' and backgrounding the process.") return # Check if the wallet path exists if not Two1Wallet.check_wallet_file(ctx.obj['wallet_path']): click.echo("ERROR: Wallet does not exist! Not starting daemon.") ctx.exit(code=7) try: d = get_daemonizer() except OSError as e: logger.debug(str(e)) click.echo("Error: %s" % e) return if d.started(): click.echo("walletd already running.") return if not d.installed(): if isinstance(ctx.obj['data_provider'], TwentyOneProvider): dpo = dict(provider='twentyone') try: d.install(dpo) except exceptions.DaemonizerError as e: logger.debug(str(e)) click.echo("Error: %s" % e) return msg = "" try: if d.start(): msg = "walletd successfully started." else: msg = "walletd not started." except exceptions.DaemonizerError as e: msg = "Error: %s" % e logger.debug(msg) click.echo(msg)
def stop_walletd(): """Stops the walletd process if it is running. """ from two1.lib.wallet import daemonizer from two1.lib.wallet.exceptions import DaemonizerError failed = False try: d = daemonizer.get_daemonizer() if d.started(): if not d.stop(): failed = True except OSError: pass except DaemonizerError: failed = True return not failed
def uninstalldaemon(ctx): """ Uninstalls the daemon from the init system """ try: d = get_daemonizer() except OSError as e: logger.debug(str(e)) click.echo("Error: %s" % e) return try: d.stop() if d.installed(): rv = d.uninstall() if rv: msg = "walletd successfully uninstalled from init system." else: msg = "Unable to uninstall walletd!" except exceptions.DaemonizerError as e: msg = "Error: %s" % e logger.debug(msg) click.echo(msg)
def __init__(self, config_file=TWO1_CONFIG_FILE, config=None, create_wallet=True): if not os.path.exists(TWO1_USER_FOLDER): os.makedirs(TWO1_USER_FOLDER) self.file = path(config_file).expand().abspath() self.dir = self.file.parent self.defaults = {} # TODO: Rename this var. Those are not the defaults but the actual values. self.json_output = False # output in json # actual config. self.load() # override config variables if config: if self.verbose: self.vlog("Applied manual config.") for k, v in config: self.defaults[k] = v if self.verbose: self.vlog("\t{}={}".format(k, v)) # add wallet object if self.defaults.get('testwallet', None) == 'y': self.wallet = test_wallet.TestWallet() elif create_wallet: dp = TwentyOneProvider(TWO1_PROVIDER_HOST) wallet_path = self.defaults.get('wallet_path') if not Two1Wallet.check_wallet_file(wallet_path): # configure wallet with default options click.pause(UxString.create_wallet) wallet_options = { 'data_provider': dp, 'wallet_path': wallet_path } if not Two1Wallet.configure(wallet_options): raise click.ClickException(UxString.Error.create_wallet_failed) # Display the wallet mnemonic and tell user to back it up. # Read the wallet JSON file and extract it. with open(wallet_path, 'r') as f: wallet_config = json.load(f) mnemonic = wallet_config['master_seed'] click.pause(UxString.create_wallet_done % (mnemonic)) # Start the daemon, if: # 1. It's not already started # 2. It's using the default wallet path # 3. We're not in a virtualenv try: d = daemonizer.get_daemonizer() if Two1Wallet.is_configured() and \ wallet_path == Two1Wallet.DEFAULT_WALLET_PATH and \ not os.environ.get("VIRTUAL_ENV") and \ not d.started(): d.start() if d.started(): click.echo(UxString.wallet_daemon_started) except (OSError, DaemonizerError): pass self.wallet = Wallet(wallet_path=wallet_path, data_provider=dp) self.machine_auth = MachineAuthWallet(self.wallet) self.channel_client = PaymentChannelClient(self.wallet) else: # This branch is hit when '21 help' or '21 update' is invoked pass
def __init__(self, config_file=TWO1_CONFIG_FILE, config=None, create_wallet=True): if not os.path.exists(TWO1_USER_FOLDER): os.makedirs(TWO1_USER_FOLDER) self.file = path(config_file).expand().abspath() self.dir = self.file.parent self.defaults = { } # TODO: Rename this var. Those are not the defaults but the actual values. self.json_output = False # output in json # actual config. self.load() # override config variables if config: if self.verbose: self.vlog("Applied manual config.") for k, v in config: self.defaults[k] = v if self.verbose: self.vlog("\t{}={}".format(k, v)) # add wallet object if self.defaults.get('testwallet', None) == 'y': self.wallet = test_wallet.TestWallet() elif create_wallet: dp = TwentyOneProvider(TWO1_PROVIDER_HOST) wallet_path = self.defaults.get('wallet_path') if not Two1Wallet.check_wallet_file(wallet_path): # configure wallet with default options click.pause(UxString.create_wallet) wallet_options = { 'data_provider': dp, 'wallet_path': wallet_path } if not Two1Wallet.configure(wallet_options): raise click.ClickException( UxString.Error.create_wallet_failed) # Display the wallet mnemonic and tell user to back it up. # Read the wallet JSON file and extract it. with open(wallet_path, 'r') as f: wallet_config = json.load(f) mnemonic = wallet_config['master_seed'] click.pause(UxString.create_wallet_done % (mnemonic)) # Start the daemon, if: # 1. It's not already started # 2. It's using the default wallet path # 3. We're not in a virtualenv try: d = daemonizer.get_daemonizer() if Two1Wallet.is_configured() and \ wallet_path == Two1Wallet.DEFAULT_WALLET_PATH and \ not os.environ.get("VIRTUAL_ENV") and \ not d.started(): d.start() if d.started(): click.echo(UxString.wallet_daemon_started) except (OSError, DaemonizerError): pass self.wallet = Wallet(wallet_path=wallet_path, data_provider=dp) self.machine_auth = MachineAuthWallet(self.wallet) self.channel_client = PaymentChannelClient(self.wallet) else: # This branch is hit when '21 help' or '21 update' is invoked pass