diff --git a/ape_aws_kms/accounts.py b/ape_aws_kms/accounts.py index a50b870..eafe5c2 100644 --- a/ape_aws_kms/accounts.py +++ b/ape_aws_kms/accounts.py @@ -1,14 +1,15 @@ import boto3 -import json +from hashlib import sha256 from datetime import datetime from typing import Any, Iterator, List, Optional +from eth_account.messages import encode_defunct from eth_utils import keccak, to_checksum_address from pydantic import BaseModel, Field from ape.api.accounts import AccountContainerAPI, AccountAPI, TransactionAPI -from ape.types import AddressType, MessageSignature, TransactionSignature +from ape.types import AddressType, MessageSignature from ape.utils import cached_property @@ -29,7 +30,6 @@ def kms_client(self): def raw_aliases(self) -> List[AliasResponse]: paginator = self.kms_client.get_paginator('list_aliases') pages = paginator.paginate() - # return [AliasResponse(**page) for page in pages for alias_data['Aliases'] in page] return [ AliasResponse(**page) for alias_data in pages @@ -74,7 +74,16 @@ def address(self) -> AddressType: return to_checksum_address(keccak(self.public_key)[:20]) def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]: - ... # self.kms_client.sign() And convert to a message signature + message = sha256(msg.encode()).digest() + response = self.kms_client.sign( + KeyId=self.key_id, + Message=message, + MessageType='DIGEST', + SigningAlgorithm='ECDSA_SHA_256', + ) + return response def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[TransactionAPI]: - ... # self.kms_client.sign() Gets appended to the message + """ + To be implemented + """ diff --git a/ape_aws_kms/py.typed b/ape_aws_kms/py.typed deleted file mode 100644 index e69de29..0000000 diff --git a/ape_aws_kms/utils.py b/ape_aws_kms/utils.py new file mode 100644 index 0000000..b8f33c2 --- /dev/null +++ b/ape_aws_kms/utils.py @@ -0,0 +1,56 @@ +import botocore +from typing import Any, Optional + +from pydantic import ( + BaseModel, + Field, +) +from hexbytes import HexBytes + +from ape.types import SignableMessage + +from eip712.messages import EIP712Message + + +class Transaction(BaseModel): + nonce: int + to: str + value: int + data: str = Field(default='0x00') + gas: int = Field(default=160000) + gas_price: str = Field(alias='gasPrice', default='0x0918400000') + + +class Message(BaseModel): + msg: Any + key_id: str + client: botocore.client.KMS + + def sign(self) -> Optional[Any]: + message = None + if isinstance(self.msg, str): + message = self.msg + + elif isinstance(self.msg, int): + message = HexBytes(self.msg).hex() + + elif isinstance(self.msg, bytes): + message = self.msg.hex() + + elif isinstance(self.msg, SignableMessage): + message = self.msg.body + + elif isinstance(self.msg, EIP712Message): + message = self.msg._body_ + + if not message: + return None + + response = self.client.sign( + KeyId=self.key_id, + Message=self.msg, + MessageType='DIGEST', + SigningAlgorithm='ECDSA_SHA_256', + ) + return response +