Skip to content

Commit

Permalink
test: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Oct 29, 2024
1 parent f8a81bd commit 5a4415b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/ape_accounts/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,14 @@ def ask_for_passphrase():
confirmation_prompt=True,
)

account_module = import_module("ape_accounts.accounts")
if import_from_mnemonic:
from eth_account import Account as EthAccount

mnemonic = click.prompt("Enter mnemonic seed phrase", hide_input=True)
EthAccount.enable_unaudited_hdwallet_features()
try:
passphrase = ask_for_passphrase()
account = account_module.import_account_from_mnemonic(
alias, passphrase, mnemonic, custom_hd_path
)
account = _account_from_mnemonic(alias, passphrase, mnemonic, hd_path=custom_hd_path)
except Exception as error:
error_msg = f"{error}".replace(mnemonic, HIDDEN_MESSAGE)
cli_ctx.abort(f"Seed phrase can't be imported: {error_msg}")
Expand All @@ -165,7 +162,7 @@ def ask_for_passphrase():
key = click.prompt("Enter Private Key", hide_input=True)
try:
passphrase = ask_for_passphrase()
account = account_module.import_account_from_private_key(alias, passphrase, key)
account = _account_from_key(alias, passphrase, key)
except Exception as error:
cli_ctx.abort(f"Key can't be imported: {error}")

Expand All @@ -180,6 +177,18 @@ def _load_account_type(account: "AccountAPI") -> bool:
return isinstance(account, module.KeyfileAccount)


def _account_from_mnemonic(
alias: str, passphrase: str, mnemonic: str, hd_path: str = ETHEREUM_DEFAULT_PATH
) -> "KeyfileAccount":
account_module = import_module("ape_accounts.accounts")
return account_module.import_account_from_mnemonic(alias, passphrase, mnemonic, hd_path=hd_path)


def _account_from_key(alias: str, passphrase: str, key: str) -> "KeyfileAccount":
account_module = import_module("ape_accounts.accounts")
return account_module.import_account_from_private_key(alias, passphrase, key)


@cli.command(short_help="Export an account private key")
@ape_cli_context()
@existing_alias_argument(account_type=_load_account_type)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cli/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def invoke_import():

@run_once
def test_import_account_instantiation_failure(mocker, ape_cli, runner):
eth_account_from_key_patch = mocker.patch("ape_accounts._cli.EthAccount.from_key")
eth_account_from_key_patch = mocker.patch("ape_accounts._cli._account_from_key")
eth_account_from_key_patch.side_effect = Exception("Can't instantiate this account!")
result = runner.invoke(
ape_cli,
Expand Down

0 comments on commit 5a4415b

Please sign in to comment.