Skip to content

Commit

Permalink
Merge pull request #448 from Skyge/feature-fix-import-key-file
Browse files Browse the repository at this point in the history
Fix importing key file error. fixes #440
  • Loading branch information
iamdefinitelyahuman authored Apr 26, 2020
2 parents 082e168 + 7276ec0 commit a70ba6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
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

0 comments on commit a70ba6c

Please sign in to comment.