예제 #1
0
async def create_start_daemon_connection(
        root_path: Path) -> Optional[DaemonProxy]:
    connection = await connect_to_daemon_and_validate(root_path)
    if connection is None:
        print("Starting daemon")
        # launch a daemon
        process = launch_start_daemon(root_path)
        # give the daemon a chance to start up
        if process.stdout:
            process.stdout.readline()
        await asyncio.sleep(1)
        # it prints "daemon: listening"
        connection = await connect_to_daemon_and_validate(root_path)
    if connection:
        passphrase = None
        if await connection.is_keyring_locked():
            passphrase = Keychain.get_cached_master_passphrase()
            if not Keychain.master_passphrase_is_valid(passphrase):
                passphrase = get_current_passphrase()

        if passphrase:
            print("Unlocking daemon keyring")
            await connection.unlock_keyring(passphrase)

        return connection
    return None
예제 #2
0
def initialize_passphrase() -> None:
    if Keychain.has_master_passphrase():
        print("Keyring is already protected by a passphrase")
        print(
            "\nUse 'chia passphrase set' or 'chia passphrase remove' to update or remove your passphrase"
        )
        sys.exit(1)

    # We'll rely on Keyring initialization to leverage the cached passphrase for
    # bootstrapping the keyring encryption process
    print("Setting keyring passphrase")
    passphrase: Optional[str] = None
    # save_passphrase indicates whether the passphrase should be saved in the
    # macOS Keychain or Windows Credential Manager
    save_passphrase: bool = False

    if Keychain.has_cached_passphrase():
        passphrase = Keychain.get_cached_master_passphrase()

    if not passphrase or passphrase == default_passphrase():
        passphrase, save_passphrase = prompt_for_new_passphrase()

    Keychain.set_master_passphrase(current_passphrase=None,
                                   new_passphrase=passphrase,
                                   save_passphrase=save_passphrase)
예제 #3
0
def initialize_passphrase() -> None:
    if Keychain.has_master_passphrase():
        print("Keyring is already protected by a passphrase")
        print(
            "\nUse 'chia passphrase set' or 'chia passphrase remove' to update or remove your passphrase"
        )
        sys.exit(1)

    # We'll rely on Keyring initialization to leverage the cached passphrase for
    # bootstrapping the keyring encryption process
    print("Setting keyring passphrase")
    passphrase = None
    if Keychain.has_cached_passphrase():
        passphrase = Keychain.get_cached_master_passphrase()

    if not passphrase or passphrase == default_passphrase():
        passphrase = prompt_for_new_passphrase()

    Keychain.set_master_passphrase(current_passphrase=None,
                                   new_passphrase=passphrase)
예제 #4
0
async def async_update_daemon_migration_completed_if_running() -> None:
    """
    Attempt to connect to the daemon to notify that keyring migration has completed.
    This allows the daemon to refresh its keyring so that it can stop using the
    legacy keyring.
    """
    ctx: click.Context = click.get_current_context()
    root_path: Path = ctx.obj["root_path"]

    if root_path is None:
        print("Missing root_path in context. Unable to notify daemon")
        return None

    async with acquire_connection_to_daemon(root_path, quiet=True) as daemon:
        if daemon is not None:
            passphrase: str = Keychain.get_cached_master_passphrase()

            print("Updating daemon... ", end="")
            response: WsRpcMessage = await daemon.notify_keyring_migration_completed(
                passphrase)
            success: bool = response.get("data", {}).get("success", False)
            print("succeeded" if success is True else "failed")
예제 #5
0
async def async_update_daemon_passphrase_cache_if_running(
        root_path: Path) -> None:
    """
    Attempt to connect to the daemon and update the cached passphrase
    """
    new_passphrase = Keychain.get_cached_master_passphrase()
    assert new_passphrase is not None

    try:
        async with acquire_connection_to_daemon(root_path,
                                                quiet=True) as daemon:
            if daemon is not None:
                response = await daemon.unlock_keyring(new_passphrase)
                if response is None:
                    raise Exception("daemon didn't respond")

                success: bool = response.get("data", {}).get("success", False)
                if success is False:
                    error = response.get("data",
                                         {}).get("error", "unknown error")
                    raise Exception(error)
    except Exception as e:
        print(f"Failed to notify daemon of updated keyring passphrase: {e}")
예제 #6
0
async def async_update_daemon_passphrase_cache_if_running(
        root_path: Path) -> None:
    from chia.daemon.client import connect_to_daemon_and_validate

    new_passphrase = Keychain.get_cached_master_passphrase()
    assert new_passphrase is not None

    daemon = None
    try:
        daemon = await connect_to_daemon_and_validate(root_path, quiet=True)
        if daemon:
            response = await daemon.unlock_keyring(new_passphrase)

            if not response:
                raise Exception("daemon didn't respond")

            if response["data"].get("success", False) is False:
                error = response["data"].get("error", "unknown error")
                raise Exception(error)
    except Exception as e:
        print(f"Failed to notify daemon of updated keyring passphrase: {e}")

    if daemon:
        await daemon.close()