8000 WIP: Fix: Command was too slow to launch by hoh · Pull Request #351 · aleph-im/aleph-client · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

WIP: Fix: Command was too slow to launch #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension All 1 file type selected

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/aleph_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
from importlib.metadata import PackageNotFoundError, version

try:
# Change here if project is renamed and does not equal the package name
__version__ = version("aleph-client")
except PackageNotFoundError:
__version__ = "unknown"

# Deprecation check
moved_types = ["__version__", "AlephClient", "AuthenticatedAlephClient", "synchronous", "asynchronous"]

Expand Down
2 changes: 1 addition & 1 deletion src/aleph_client/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from aleph_client.utils import AsyncTyper

app = AsyncTyper(no_args_is_help=True)
app = AsyncTyper(no_args_is_help=True, pretty_exceptions_enable=False)

app.add_typer(account.app, name="account", help="Manage accounts")
app.add_typer(
Expand Down
4 changes: 2 additions & 2 deletions src/aleph_client/commands/about.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from importlib.metadata import version as importlib_version

import typer

from aleph_client.utils import AsyncTyper
Expand All @@ -10,6 +8,8 @@


def get_version(value: bool):
from importlib.metadata import version as importlib_version

__version__ = "NaN"
dist_name = "aleph-client"
if value:
Expand Down
119 changes: 78 additions & 41 deletions src/aleph_client/commands/account.py
9E7A
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
from __future__ import annotations

import asyncio
import logging
from pathlib import Path
from typing import Annotated, Optional

import aiohttp
import typer
from aleph.sdk.account import _load_account
from aleph.sdk.chains.common import generate_key
from aleph.sdk.chains.solana import parse_private_key as parse_solana_private_key
from aleph.sdk.conf import (
MainConfiguration,
load_main_configuration,
save_main_configuration,
settings,
)
from aleph.sdk.evm_utils import (
get_chains_with_holding,
get_chains_with_super_token,
get_compatible_chains,
)
from aleph.sdk.utils import bytes_from_hex, displayable_amount
from aleph_message.models import Chain
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from rich.table import Table
from rich.text import Text
from typer.colors import GREEN, RED

from aleph_message.models.base import Chain
from aleph_client.commands import help_strings
from aleph_client.commands.utils import (
input_multiline,
setup_logging,
validated_prompt,
yes_no_input,
)
from aleph_client.utils import AsyncTyper, list_unlinked_keys

logger = logging.getLogger(__name__)
Expand All @@ -54,6 +31,10 @@ async def create(
debug: Annotated[bool, typer.Option()] = False,
):
"""Create or import a private key."""
from aleph.sdk.conf import MainConfiguration, save_main_configuration, settings
from aleph_message.models import Chain

from aleph_client.commands.utils import setup_logging, validated_prompt

setup_logging(debug)

Expand Down Expand Up @@ -82,10 +63,18 @@ async def create(
)
)
if chain == Chain.SOL:
from aleph.sdk.chains.solana import (
parse_private_key as parse_solana_private_key,
)

private_key_bytes = parse_solana_private_key(private_key)
else:
from aleph.sdk.utils import bytes_from_hex

private_key_bytes = bytes_from_hex(private_key)
else:
from aleph.sdk.chains.common import generate_key

private_key_bytes = generate_key()
if not private_key_bytes:
typer.secho("An unexpected error occurred!", fg=RED)
Expand Down Expand Up @@ -120,21 +109,26 @@ async def create(

@app.command(name="address")
def display_active_address(
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = settings.PRIVATE_KEY_STRING,
private_key_file: Annotated[
Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)
] = settings.PRIVATE_KEY_FILE,
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = None,
private_key_file: Annotated[Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)] = None,
):
"""
Display your public address(es).
"""
from aleph.sdk.conf import settings
from aleph_message.models import Chain

private_key = private_key or settings.PRIVATE_KEY_STRING
private_key_file = private_key_file or settings.PRIVATE_KEY_FILE

if private_key is not None:
private_key_file = None
elif private_key_file and not private_key_file.exists():
typer.secho("No private key available", fg=RED)
raise typer.Exit(code=1)

from aleph.sdk.account import _load_account

evm_address = _load_account(private_key, private_key_file, chain=Chain.ETH).get_address()
sol_address = _load_account(private_key, private_key_file, chain=Chain.SOL).get_address()

Expand All @@ -150,6 +144,13 @@ def display_active_chain():
"""
Display the currently active chain.
"""
from aleph.sdk.conf import load_main_configuration, settings
from aleph.sdk.evm_utils import (
get_chains_with_holding,
get_chains_with_super_token,
get_compatible_chains,
)
from aleph_message.models import Chain

config_file_path = Path(settings.CONFIG_FILE)
config = load_main_configuration(config_file_path)
Expand Down Expand Up @@ -180,18 +181,23 @@ def display_active_chain():
@app.command(name="path")
def path_directory():
"""Display the directory path where your private keys, config file, and other settings are stored."""
from aleph.sdk.conf import settings

console.print(f"Aleph Home directory: [yellow]{settings.CONFIG_HOME}[/yellow]")


@app.command()
def show(
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = settings.PRIVATE_KEY_STRING,
private_key_file: Annotated[
Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)
] = settings.PRIVATE_KEY_FILE,
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = None,
private_key_file: Annotated[Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)] = None,
):
"""Display current configuration."""

from aleph.sdk.conf import settings

private_key = private_key or settings.PRIVATE_KEY_STRING
private_key_file = private_key_file or settings.PRIVATE_KEY_FILE

display_active_address(private_key=private_key, private_key_file=private_key_file)
display_active_chain()

Expand All @@ -204,13 +210,16 @@ def export_private_key(
"""
Display your private key.
"""
from aleph_message.models import Chain

if private_key:
private_key_file = None
elif private_key_file and not private_key_file.exists():
typer.secho("No private key available", fg=RED)
raise typer.Exit(code=1)

from aleph.sdk.account import _load_account

evm_pk = _load_account(private_key, private_key_file, chain=Chain.ETH).export_private_key()
sol_pk = _load_account(private_key, private_key_file, chain=Chain.SOL).export_private_key()

Expand All @@ -225,15 +234,23 @@ def export_private_key(
@app.command("sign-bytes")
def sign_bytes(
message: Annotated[Optional[str], typer.Option(help="Message to sign")] = None,
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = settings.PRIVATE_KEY_STRING,
private_key_file: Annotated[
Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)
] = settings.PRIVATE_KEY_FILE,
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = None,
private_key_file: Annotated[Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)] = None,
chain: Annotated[Optional[Chain], typer.Option(help=help_strings.ADDRESS_CHAIN)] = None,
debug: Annotated[bool, typer.Option()] = False,
):
"""Sign a message using your private key."""

import asyncio

from aleph.sdk.account import _load_account
from aleph.sdk.conf import settings

from aleph_client.commands.utils import input_multiline, setup_logging

private_key = private_key or settings.PRIVATE_KEY_STRING
private_key_file = private_key_file or settings.PRIVATE_KEY_FILE

setup_logging(debug)

account = _load_account(private_key, private_key_file, chain=chain)
Expand All @@ -248,9 +265,13 @@ def sign_bytes(


async def get_balance(address: str) -> dict:
from aleph.sdk.conf import settings

balance_data: dict = {}
uri = f"{settings.API_HOST}/api/v0/addresses/{address}/balance"
async with aiohttp.ClientSession() as session:
from aiohttp import ClientSession

async with ClientSession() as session:
response = await session.get(uri)
if response.status == 200:
balance_data = await response.json()
Expand All @@ -264,19 +285,25 @@ async def get_balance(address: str) -> dict:
@app.command()
async def balance(
address: Annotated[Optional[str], typer.Option(help="Address")] = None,
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = settings.PRIVATE_KEY_STRING,
private_key_file: Annotated[
Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)
] = settings.PRIVATE_KEY_FILE,
private_key: Annotated[Optional[str], typer.Option(help=help_strings.PRIVATE_KEY)] = None,
private_key_file: Annotated[Optional[Path], typer.Option(help=help_strings.PRIVATE_KEY_FILE)] = None,
chain: Annotated[Optional[Chain], typer.Option(help=help_strings.ADDRESS_CHAIN)] = None,
):
"""Display your ALEPH balance."""
from aleph.sdk.account import _load_account
from aleph.sdk.conf import settings

private_key = private_key or settings.PRIVATE_KEY_STRING
private_key_file = private_key_file or settings.PRIVATE_KEY_FILE

account = _load_account(private_key, private_key_file, chain=chain)

if account and not address:
address = account.get_address()

if address:
from aleph.sdk.utils import displayable_amount

try:
balance_data = await get_balance(address)
infos = [
Expand Down Expand Up @@ -325,6 +352,9 @@ async def balance(
async def list_accounts():
"""Display available private keys, along with currenlty active chain and account (from config file)."""

from aleph.sdk.conf import load_main_configuration, settings
from aleph.sdk.evm_utils import get_chains_with_holding, get_chains_with_super_token

config_file_path = Path(settings.CONFIG_FILE)
config = load_main_configuration(config_file_path)
unlinked_keys, _ = await list_unlinked_keys()
Expand Down Expand Up @@ -354,6 +384,8 @@ async def list_accounts():

active_address = None
if config and config.path and active_chain:
from aleph.sdk.account import _load_account

account = _load_account(private_key_path=config.path, chain=active_chain)
active_address = account.get_address()

Expand All @@ -375,6 +407,9 @@ async def configure(
chain: Annotated[Optional[Chain], typer.Option(help="New active chain")] = None,
):
"""Configure current private key file and active chain (default selection)"""
from aleph.sdk.conf import MainConfiguration, save_main_configuration, settings

from aleph_client.commands.utils import yes_no_input

unlinked_keys, config = await list_unlinked_keys()

Expand Down Expand Up @@ -423,6 +458,8 @@ async def configure(

# Configure active chain
if not chain and config and hasattr(config, "chain"):
from aleph_message.models import Chain

if not yes_no_input(
f"Active chain: [bright_cyan]{config.chain}[/bright_cyan]\n[yellow]Keep current active chain?[/yellow]",
default="y",
10000 Expand Down
18 changes: 14 additions & 4 deletions src/aleph_client/commands/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
from typing import Annotated, Optional

import typer
from aiohttp import ClientResponseError, ClientSession
from aleph.sdk.account import _load_account
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.conf import settings
# from aleph.sdk.conf import settings
from aleph.sdk.types import AccountFromPrivateKey
from aleph.sdk.utils import extended_json_encoder
from aleph_message.models import Chain, MessageType
Expand Down Expand Up @@ -57,8 +54,11 @@ async def forget(
) -> bool:
"""Delete an aggregate by key or subkeys"""

from aleph.sdk.client import AuthenticatedAlephHttpClient

setup_logging(debug)

from aleph.sdk.account import _load_account
account: AccountFromPrivateKey = _load_account(private_key, private_key_file)
address = account.get_address() if address is None else address

Expand Down Expand Up @@ -129,9 +129,11 @@ async def post(
debug: bool = False,
) -> bool:
"""Create or update an aggregate by key or subkey"""
from aleph.sdk.client import AuthenticatedAlephHttpClient

setup_logging(debug)

from aleph.sdk.account import _load_account
account: AccountFromPrivateKey = _load_account(private_key, private_key_file)
address = account.get_address() if address is None else address

Expand Down Expand Up @@ -191,6 +193,9 @@ async def get(
debug: bool = False,
) -> Optional[dict]:
"""Fetch an aggregate by key or subkeys"""
from aiohttp.client_exceptions import ClientResponseError
from aleph.sdk.account import _load_account
from aleph.sdk.client import AuthenticatedAlephHttpClient

setup_logging(debug)

Expand Down Expand Up @@ -227,6 +232,8 @@ async def list_aggregates(
debug: bool = False,
) -> Optional[dict]:
"""Display all aggregates associated to an account"""
from aiohttp.client import ClientSession
from aleph.sdk.account import _load_account

setup_logging(debug)

Expand Down Expand Up @@ -302,6 +309,7 @@ async def authorize(
):
"""Grant specific publishing permissions to an address to act on behalf of this account"""

from aleph.sdk.account import _load_account
setup_logging(debug)

account: AccountFromPrivateKey = _load_account(private_key, private_key_file)
Expand Down Expand Up @@ -376,6 +384,7 @@ async def revoke(
):
"""Revoke all publishing permissions from an address acting on behalf of this account"""

from aleph.sdk.account import _load_account
setup_logging(debug)

account: AccountFromPrivateKey = _load_account(private_key, private_key_file)
Expand Down Expand Up @@ -431,6 +440,7 @@ async def permissions(
) -> Optional[dict]:
"""Display all permissions emitted by an account"""

from aleph.sdk.account import _load_account
setup_logging(debug)

account: AccountFromPrivateKey = _load_account(private_key, private_key_file)
Expand Down
Loading
Loading
0