Skip to content

Commit

Permalink
feat: move functionality around to clean things up
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson2427 committed Apr 29, 2024
1 parent d31cec0 commit f435515
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
41 changes: 23 additions & 18 deletions ape_aws/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,23 @@ def address(self) -> AddressType:
keccak(self.public_key[-64:])[-20:].hex().lower()
)

def sign_raw_msghash(self, msghash: HexBytes) -> Optional[bytes]:
def sign_raw_msghash(self, msghash: HexBytes) -> Optional[MessageSignature]:
if len(msghash) != 32:
return None

if signature := self.sign_raw_hash(msghash):
return MessageSignature(**_convert_der_to_rsv(signature, 27))

return None

def sign_raw_hash(self, msghash: HexBytes) -> Optional[bytes]:
response = self.kms_client.sign(
KeyId=self.key_id,
Message=msghash,
MessageType='DIGEST',
SigningAlgorithm='ECDSA_SHA_256',
)
return response['Signature']
return response.get('Signature')

def sign_message(
self, msg: Any, **signer_options
Expand All @@ -91,14 +100,12 @@ def sign_message(
message = encode_defunct(text=msg)
if isinstance(msg, bytes):
message = encode_defunct(primitive=msg)
signature = self.sign_raw_msghash(_hash_eip191_message(message))
r, s = _convert_der_to_rsv(signature)
for v in [signature[0] + 27, signature[0] + 28]:
if self.check_signature(
msg,
message_signature := MessageSignature(v=v, r=r, s=s),
):
return message_signature
msg_sig = self.sign_raw_msghash(_hash_eip191_message(message))
# Note: check here until we figure out v
if self.check_signature(msg, msg_sig):
return msg_sig
else:
return MessageSignature(v=msg_sig.v + 1, r=msg_sig.r, s=msg_sig.s)

def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[TransactionAPI]:
"""
Expand All @@ -120,11 +127,9 @@ def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[Tr
data=txn.data
)
).hash()
signature = self.sign_raw_msghash(unsigned_txn)
r, s = _convert_der_to_rsv(signature)
for v in [signature[0] + 27, signature[0] + 28]:
if self.check_signature(
unsigned_txn,
message_signature := MessageSignature(v=v, r=r, s=s),
):
return message_signature
msg_sig = self.sign_raw_msghash(unsigned_txn)
breakpoint()
if self.check_signature(unsigned_txn, msg_sig):
return msg_sig
else:
return MessageSignature(v=msg_sig.v + 1, r=msg_sig.r, s=msg_sig.s)
7 changes: 5 additions & 2 deletions ape_aws/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ def create_signable_message(msg):
return encode_defunct(text=msg)


def _convert_der_to_rsv(signature: bytes) -> dict:
def _convert_der_to_rsv(
signature: bytes, v_adjustment_factor: int = 0
) -> dict:
r, s = ecdsa.util.sigdecode_der(signature, ecdsa.SECP256k1.order)
if s > SECP256_K1_N / 2:
s = SECP256_K1_N - s
r = r.to_bytes(32, byteorder='big')
s = s.to_bytes(32, byteorder='big')
return r, s
v = signature[0] + v_adjustment_factor
return dict(v=v, r=r, s=s)


class AliasResponse(BaseModel):
Expand Down

0 comments on commit f435515

Please sign in to comment.