Skip to content

Commit

Permalink
Allow specifying password when saving an account (#1216)
Browse files Browse the repository at this point in the history
* Allow specifying password when saving an account

* lint

* more lint

* add type
  • Loading branch information
BlinkyStitt committed Aug 24, 2021
1 parent 8b60ca1 commit 9e75a6b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions brownie/network/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,13 +842,14 @@ def __init__(self, address: str, account: Account, priv_key: Union[int, bytes, s
self.public_key = eth_keys.keys.PrivateKey(HexBytes(priv_key)).public_key
super().__init__(address)

def save(self, filename: str, overwrite: bool = False) -> str:
def save(self, filename: str, overwrite: bool = False, password: Optional[str] = None) -> str:
"""Encrypts the private key and saves it in a keystore json.
Attributes:
filename: path to keystore file. If no folder is given, saved in
~/.brownie/accounts
overwrite: if True, will overwrite an existing file.
password: Password used to encrypt the account. If none, you will be prompted
Returns the absolute path to the keystore file as a string.
"""
Expand All @@ -863,9 +864,11 @@ def save(self, filename: str, overwrite: bool = False) -> str:
json_file = Path(filename).expanduser().resolve()
if not overwrite and json_file.exists():
raise FileExistsError("Account with this identifier already exists")
encrypted = web3.eth.account.encrypt(
self.private_key, getpass("Enter the password to encrypt this account with: ")
)

if password is None:
password = getpass("Enter the password to encrypt this account with: ")

encrypted = web3.eth.account.encrypt(self.private_key, password)
with json_file.open("w") as fp:
json.dump(encrypted, fp)
return str(json_file)
Expand Down

0 comments on commit 9e75a6b

Please sign in to comment.