Beispiel #1
0
def get_or_create_wallet(wallet_path):
    """Create a new wallet or return the currently existing one."""
    data_provider = twentyone_provider.TwentyOneProvider(
        two1.TWO1_PROVIDER_HOST)

    if wallet.Two1Wallet.check_wallet_file(wallet_path):
        return wallet.Wallet(wallet_path=wallet_path,
                             data_provider=data_provider)

    # configure wallet with default options
    click.pause(uxstring.UxString.create_wallet)

    wallet_options = dict(data_provider=data_provider, wallet_path=wallet_path)

    if not wallet.Two1Wallet.configure(wallet_options):
        raise click.ClickException(
            uxstring.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.UxString.create_wallet_done %
                click.style(mnemonic, fg='green'))

    if wallet.Two1Wallet.check_wallet_file(wallet_path):
        return wallet.Wallet(wallet_path=wallet_path,
                             data_provider=data_provider)
Beispiel #2
0
def get_user_credentials(two1_dir="~/.two1/two1.json"):
    """ Collect user credentials at CLI.
    """

    with open(os.path.expanduser(two1_dir), "r") as f:
        username = json.load(f)["username"]
    try:
        w = wallet.Wallet()
    except:
        logger.info(
            click.style(
                "A technical error occured. Please try the previous command again.",
                fg="magenta"))
        sys.exit()
    machine_auth = machine_auth_wallet.MachineAuthWallet(w)
    rest_client = _rest_client.TwentyOneRestClient(TWO1_HOST, machine_auth,
                                                   username)
    address = w.current_address

    correct_password = False
    pw = click.prompt(click.style("Please enter your 21 password",
                                  fg=PROMPT_COLOR),
                      hide_input=True)

    while not correct_password:
        try:
            rest_client.login(payout_address=address, password=pw)
            correct_password = True
        except:
            pw = click.prompt(click.style(
                "Incorrect 21 password. Please try again", fg="magenta"),
                              hide_input=True)

    return username, pw
Beispiel #3
0
def get_or_create_wallet(wallet_path):
    """Create a new wallet or return the currently existing one."""
    data_provider = twentyone_provider.TwentyOneProvider(
        two1.TWO1_PROVIDER_HOST)

    if wallet.Two1Wallet.check_wallet_file(wallet_path):
        return wallet.Wallet(wallet_path=wallet_path,
                             data_provider=data_provider)

    # configure wallet with default options
    click.pause(uxstring.UxString.create_wallet)

    wallet_options = dict(data_provider=data_provider, wallet_path=wallet_path)

    if not wallet.Two1Wallet.configure(wallet_options):
        raise click.ClickException(
            uxstring.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.UxString.create_wallet_done %
                click.style(mnemonic, fg='green'))

    # 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 wallet.Two1Wallet.is_configured(
        ) and not os.environ.get("VIRTUAL_ENV") and not d.started():
            d.start()
            if d.started():
                logger.info(uxstring.UxString.wallet_daemon_started)
    except (OSError, exceptions.DaemonizerError):
        pass

    if wallet.Two1Wallet.check_wallet_file(wallet_path):
        return wallet.Wallet(wallet_path=wallet_path,
                             data_provider=data_provider)
Beispiel #4
0
def create_default_rest_client():
    """Return a rest client using default parameters."""
    import two1
    from two1 import wallet
    from two1.commands.util import config
    from two1.server import machine_auth_wallet, rest_client
    auth = machine_auth_wallet.MachineAuthWallet(wallet.Wallet())
    return rest_client.TwentyOneRestClient(two1.TWO1_HOST, auth,
                                           config.Config().username)
Beispiel #5
0
def get_rest_client():
    """ Helper method to create rest_client.
    """
    with open(os.path.expanduser("~/.two1/two1.json"), "r") as f:
        username = json.load(f)["username"]

    try:
        w = wallet.Wallet()
    except:
        logger.info(click.style("A technical error occured. Please try the previous command again.", fg="magenta"))
        sys.exit()

    machine_auth = machine_auth_wallet.MachineAuthWallet(w)
    rest_client = _rest_client.TwentyOneRestClient(TWO1_HOST, machine_auth, username)
    return rest_client
Beispiel #6
0
def wallet(config):
    """ Fixture that injects a Two1Wallet

        Uses py.test's tmpdir to create a temporary directory to store the
        wallet files. If the environment has WALLET_MNEMONIC set, this
        fixture will restore a wallet from the specified mnemonic. This is
        usefull for using the same wallet for testing to manage payouts. If
        WALLET_MNEMONIC is not set then a new wallet is created.

    Returns:
        two1.wallet.Two1Wallet: initialized wallet with a wallet path in a
            temp directory.
    """
    # use standard provider
    data_provider = twentyone_provider.TwentyOneProvider(
        two1.TWO1_PROVIDER_HOST)

    # use mnemonic to create a wallet
    wallet_mnemonic = os.environ[
        'WALLET_MNEMONIC'] if 'WALLET_MNEMONIC' in os.environ else None
    if wallet_mnemonic:
        m = mnemonic.Mnemonic(language='english')

        # ensure mnemonic is valid
        assert m.check(wallet_mnemonic)

        # creates a wallet from mnemonic
        wallet = _wallet.Two1Wallet.import_from_mnemonic(
            data_provider=data_provider, mnemonic=wallet_mnemonic)

        # writes the file to the tempdir
        wallet.to_file(config.wallet_path)

        return wallet
    else:
        # creates a new wallet
        wallet_options = dict(data_provider=data_provider,
                              wallet_path=config.wallet_path)

        # ensures the wallet is configured correctly
        assert _wallet.Two1Wallet.configure(wallet_options)

        return _wallet.Wallet(config.wallet_path, data_provider)