def login_21(): """ Restore wallet to disk and log in to 21. """ mnemonic = os.environ["TWO1_WALLET_MNEMONIC"] provider = TwentyOneProvider() wallet = Two1Wallet.import_from_mnemonic(provider, mnemonic) if not os.path.exists(os.path.dirname(Two1Wallet.DEFAULT_WALLET_PATH)): os.makedirs(os.path.dirname(Two1Wallet.DEFAULT_WALLET_PATH)) wallet.to_file(Two1Wallet.DEFAULT_WALLET_PATH) # login config = Config() machine_auth = machine_auth_wallet.MachineAuthWallet(wallet) username = os.environ["TWO1_USERNAME"] password = os.environ["TWO1_PASSWORD"] rest_client = _rest_client.TwentyOneRestClient(two1.TWO1_HOST, machine_auth, username) machine_auth_pubkey_b64 = base64.b64encode( machine_auth.public_key.compressed_bytes).decode() payout_address = machine_auth.wallet.current_address rest_client.login(payout_address=payout_address, password=password) config.set("username", username) config.set("mining_auth_pubkey", machine_auth_pubkey_b64) config.save()
def sweep(ctx): """ Sweep your machine wallet to your primary wallet. \b Sweep your machine wallet to your primary wallet. $ 21 sell sweep """ manager = ctx.obj['manager'] logger.info(click.style("Sweeping all service balances.", fg=cli_helpers.TITLE_COLOR)) provider = TwentyOneProvider() try: wallet = Two1Wallet.import_from_mnemonic(provider, manager.get_services_mnemonic()) except Exception: logger.info(click.style("Error: unable to import wallet mnemonic. Please check to make " "sure the mnemonic exists in %s " "or contact [email protected]." % Two1Composer.COMPOSE_FILE, fg="magenta")) utxos = wallet.get_utxos(include_unconfirmed=True, accounts=wallet._accounts) utxo_sum = wallet._sum_utxos(utxos) fee_amounts = txn_fees.get_fees() total_value, num_utxos = utxo_sum def fee_calc_small(num_utxos, total_value, fee_amounts): maybe_fee = _fee_calc(num_utxos, total_value, fee_amounts) return int(min([total_value / 2, maybe_fee])) fee = fee_calc_small(num_utxos, total_value, fee_amounts) if click.confirm(click.style("Sweeping %s satoshis to your primary wallet. This will incur a " "fee of approximately %d satoshis.\n" "Would you like to continue?" % (total_value, fee), fg=cli_helpers.PROMPT_COLOR)): master = Two1Wallet(manager.composer.wallet_file, provider) try: wallet.sweep(master.current_address, fee_calculator=fee_calc_small) except WalletBalanceError: cli_helpers.print_str("Sweep", ["Wallet balance (%d satoshis) is less than the dust " "limit. Not Sweeping." % total_value], "FAILED", False) except DustLimitError: cli_helpers.print_str("Sweep", ["Wallet balance (%d satoshis) would be below the " "dust limit when fees are deducted. " "Aborting sweep." % total_value], "FAILED", False) else: cli_helpers.print_str("Sweep", ["Swept %d satoshis, excluding %d satoshis of " "fees" % (total_value - fee, fee)], "SUCCESS", True) else: sys.exit()
def service_balance_check(): """ Check machine balances. """ rest_client = get_rest_client() dollars_per_sat = rest_client.quote_bitcoin_price(1).json()["price"] provider = TwentyOneProvider() payments_server_balance = get_payments_server_balance(provider) # balances = get_balances(services, rest_client) payments_buffer = get_buffer_balance(rest_client) payments_onchain = payments_server_balance["onchain"] payments_channels = payments_server_balance["channels"] print_str_no_label("Server", [build_detail_line("buffer", payments_buffer, dollars_per_sat), build_detail_line("onchain", payments_onchain, dollars_per_sat), build_detail_line("channels", payments_channels, dollars_per_sat)])
def __init__(self): self._connected = ComposerState.DISCONNECTED self.provider = TwentyOneProvider() self.default_wallet = Two1Wallet(self.wallet_file, self.provider)