Skip to content

Commit e44214f

Browse files
committed
refactor(KMS): return HexBytes for .public_key; handle error better
1 parent 29f702f commit e44214f

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

ape_aws/kms/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from itertools import chain
44
from typing import TYPE_CHECKING, ClassVar
55

6-
from ape.types import AddressType
6+
from ape.types import AddressType, HexBytes
77
from botocore.exceptions import BotoCoreError # type: ignore[import-untyped]
88
from pydantic import BaseModel, Field, SecretStr, field_validator
99

@@ -104,8 +104,12 @@ def set_alias(self, alias: str):
104104
self.cached_alias = alias
105105

106106
@property
107-
def public_key(self):
108-
return self.kms_client.get_public_key(KeyId=self.id)["PublicKey"]
107+
def public_key(self) -> HexBytes:
108+
try:
109+
return HexBytes(self.kms_client.get_public_key(KeyId=self.id)["PublicKey"][-64:])
110+
except (self.kms_client.exceptions.KMSInvalidStateException, BotoCoreError) as e:
111+
# NOTE: Handle here since `.keys` is the main access point for the external API
112+
raise AwsAccessError(e) from e
109113

110114
@property
111115
def address(self) -> AddressType:
@@ -114,7 +118,7 @@ def address(self) -> AddressType:
114118

115119
from eth_utils import keccak, to_checksum_address
116120

117-
return AddressType(to_checksum_address(keccak(self.public_key[-64:])[-20:].hex().lower()))
121+
return AddressType(to_checksum_address(keccak(self.public_key)[-20:]))
118122

119123
def add_tags(self, tags: list[dict[str, str]]):
120124
self.kms_client.tag_resource(KeyId=self.id, Tags=tags)

0 commit comments

Comments
 (0)