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

feat: public_key method added to KeyfileAccount class [APE-1466] #1702

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 12 additions & 0 deletions src/ape_accounts/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import click
from eth_account import Account as EthAccount
from eth_keys import keys # type: ignore
from eth_utils import to_bytes
from ethpm_types import HexBytes

Expand Down Expand Up @@ -92,6 +93,17 @@ def __key(self) -> HexBytes:

return key

@property
def public_key(self) -> HexBytes:
Aviksaikat marked this conversation as resolved.
Show resolved Hide resolved
key = self.__key

# Derive the public key from the private key
pk = keys.PrivateKey(key)
Aviksaikat marked this conversation as resolved.
Show resolved Hide resolved
# convert from eth_keys.datatypes.PublicKey to str to make it HexBytes
publicKey = str(pk.public_key)
Aviksaikat marked this conversation as resolved.
Show resolved Hide resolved

return HexBytes(bytes.fromhex(publicKey[2:]))

def unlock(self, passphrase: Optional[str] = None):
if not passphrase:
# Check if environment variable is available
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from eip712.messages import EIP712Message
from eth_account.messages import encode_defunct
from hexbytes import HexBytes

import ape
from ape.api import ImpersonatedAccount
Expand Down Expand Up @@ -565,3 +566,8 @@ def test_prepare_transaction_and_call_using_max_gas(tx_type, ethereum, sender, e

actual = sender.call(tx)
assert not actual.failed


def test_public_key(runner, keyfile_account):
with runner.isolation(input="a\ny\n"):
assert isinstance(keyfile_account.public_key, HexBytes)