Skip to content

Commit

Permalink
feat: add signing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson2427 committed Apr 10, 2024
1 parent 23ac881 commit 2b4ce5d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
19 changes: 14 additions & 5 deletions ape_aws_kms/accounts.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand Down Expand Up @@ -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(<convert message to proper schema>) 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(<convert txn to proper schema>) Gets appended to the message
"""
To be implemented
"""
Empty file removed ape_aws_kms/py.typed
Empty file.
56 changes: 56 additions & 0 deletions ape_aws_kms/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2b4ce5d

Please sign in to comment.