-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds account generation and import API functions (#1887)
* feat: adds ape_accounts.generate_account for programattic account creation * refactor: review feedback; code hygiene * fix: missing file * feat: warn on simple passwords * fix: make prompt order the same as it was originally * fix(test): handle prompt difference in CLI integration account tests * test: use correct test account, fix passwords and output parsing * style: doulbe spaces are for typewriters and old people * docs: document generate_account use in accounts user guide * feat: adds import_account_from_mnemonic and import_account_from_private_key * fix: remove unused arg from import_account_from_private_key() * docs: document use of import_account_from_mnemonic() and import_account_from_private_key() * style: review feedback, docstrings and cleanup * chore: include space in special char validator * docs: clarity fix Co-authored-by: antazoey <[email protected]> * docs: use class def notation for docstring Co-authored-by: antazoey <[email protected]> * style(lint): line too long * fix(docs): incorrect import in example Co-authored-by: El De-dog-lo <[email protected]> * docs: adds ape_accounts autodoc, improves accounts userguide additions * docs: wording review feedback * test: use eth_tester_provider fixture in test_default_sender_account * docs: link to ape_accounts autodoc page from index ToC * test: use different alias in test_import_account_from_mnemonic * test: adds delete_account_after fixture, providing context manager that deletes account files after closing --------- Co-authored-by: antazoey <[email protected]> Co-authored-by: El De-dog-lo <[email protected]>
- Loading branch information
1 parent
fe636bb
commit 7c5b10b
Showing
13 changed files
with
486 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# ape-accounts | ||
|
||
```{eval-rst} | ||
.. automodule:: ape_accounts | ||
:members: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Base non-pydantic validator utils""" | ||
import re | ||
from warnings import warn | ||
|
||
from eth_utils import is_hex | ||
|
||
from ape.exceptions import AccountsError, AliasAlreadyInUseError | ||
from ape.utils.basemodel import ManagerAccessMixin | ||
|
||
MIN_PASSPHRASE_LENGTH = 6 | ||
|
||
|
||
def _has_num(val: str): | ||
return re.search(r"\d{1}", val) is not None | ||
|
||
|
||
def _has_special(val: str): | ||
return re.search(r"[\!@#$%\^&\*\(\) ]{1}", val) is not None | ||
|
||
|
||
def _validate_account_alias(alias: str) -> str: | ||
"""Validate an account alias""" | ||
if alias in ManagerAccessMixin.account_manager.aliases: | ||
raise AliasAlreadyInUseError(alias) | ||
elif not isinstance(alias, str): | ||
raise AccountsError(f"Alias must be a str, not '{type(alias)}'.") | ||
elif is_hex(alias) and len(alias) >= 42: | ||
# Prevents private keys from accidentally being stored in plaintext | ||
# Ref: https://github.com/ApeWorX/ape/issues/1525 | ||
raise AccountsError("Longer aliases cannot be hex strings.") | ||
|
||
return alias | ||
|
||
|
||
def _validate_account_passphrase(passphrase: str) -> str: | ||
"""Make sure given passphrase is valid for account encryption""" | ||
if not passphrase or not isinstance(passphrase, str): | ||
raise AccountsError("Account file encryption passphrase must be provided.") | ||
|
||
if len(passphrase) < MIN_PASSPHRASE_LENGTH: | ||
warn("Passphrase length is extremely short. Consider using something longer.") | ||
|
||
if not (_has_num(passphrase) or _has_special(passphrase)): | ||
warn("Passphrase complexity is simple. Consider using numbers and special characters.") | ||
|
||
return passphrase | ||
|
||
|
||
__all__ = ["_validate_account_alias", "_validate_account_passphrase"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
from ape import plugins | ||
|
||
from .accounts import AccountContainer, KeyfileAccount | ||
from .accounts import ( | ||
AccountContainer, | ||
KeyfileAccount, | ||
generate_account, | ||
import_account_from_mnemonic, | ||
import_account_from_private_key, | ||
) | ||
|
||
|
||
@plugins.register(plugins.AccountPlugin) | ||
def account_types(): | ||
return AccountContainer, KeyfileAccount | ||
|
||
|
||
__all__ = [ | ||
"AccountContainer", | ||
"KeyfileAccount", | ||
"generate_account", | ||
"import_account_from_mnemonic", | ||
"import_account_from_private_key", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.