diff --git a/ape_aws/accounts.py b/ape_aws/accounts.py index 4e8d4ac..2c707ec 100644 --- a/ape_aws/accounts.py +++ b/ape_aws/accounts.py @@ -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 @@ -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]: """ @@ -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) diff --git a/ape_aws/utils.py b/ape_aws/utils.py index 176a6cf..a4290d2 100644 --- a/ape_aws/utils.py +++ b/ape_aws/utils.py @@ -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):