Skip to content
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

Fix importing key file error. #448

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions brownie/_cli/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ def _generate(id_):


def _import(id_, path):
source_path = Path(path).absolute()
if not source_path.suffix:
source_path = source_path.with_suffix(".json")
dest_path = _get_data_folder().joinpath(f"accounts/{id_}.json")
if dest_path.exists():
raise FileExistsError(f"A keystore file already exists with the id '{id_}'")

source_path = Path(path).absolute()
if not source_path.exists():
temp_source = source_path.with_suffix(".json")
if temp_source.exists():
source_path = temp_source
else:
raise FileNotFoundError(f"Cannot find {source_path}")

accounts.load(source_path)
shutil.copy(source_path, dest_path)
notify(
Expand Down
22 changes: 15 additions & 7 deletions brownie/network/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,25 @@ def load(self, filename: str = None) -> Union[List, "LocalAccount"]:

Returns:
Account instance."""
project_path = _get_data_folder().joinpath("accounts")
base_accounts_path = _get_data_folder().joinpath("accounts")
if not filename:
return [i.stem for i in project_path.glob("*.json")]
return [i.stem for i in base_accounts_path.glob("*.json")]

filename = str(filename)
if not filename.endswith(".json"):
filename += ".json"
json_file = Path(filename).expanduser()

if not json_file.exists():
json_file = project_path.joinpath(filename)
if not json_file.exists():
raise FileNotFoundError(f"Cannot find {json_file}")
temp_json_file = json_file.with_suffix(".json")
if temp_json_file.exists():
json_file = temp_json_file
else:
json_file_name = json_file.name
json_file = base_accounts_path.joinpath(json_file_name)
if json_file.suffix != ".json":
json_file = json_file.with_suffix(".json")
if not json_file.exists():
raise FileNotFoundError(f"Cannot find {json_file}")

with json_file.open() as fp:
priv_key = web3.eth.account.decrypt(
json.load(fp), getpass("Enter the password to unlock this account: ")
Expand Down