Ejemplo n.º 1
0
    def deserialize(self):
        if self._msg_bytes is None or len(
                self._msg_bytes
        ) < eth_common_constants.MDC_LEN + eth_common_constants.SIGNATURE_LEN:
            raise ValueError("Message bytes empty or too short.")

        mdc = self._memory_view[:eth_common_constants.MDC_LEN].tobytes()
        if mdc != eth_common_utils.keccak_hash(
                self._memory_view[eth_common_constants.MDC_LEN:].tobytes()):
            raise WrongMACError("Message hash does not match MDC")

        mdc_sig_len = eth_common_constants.MDC_LEN + eth_common_constants.SIGNATURE_LEN
        signature = self._memory_view[eth_common_constants.
                                      MDC_LEN:mdc_sig_len].tobytes()

        self._msg_type = rlp_utils.safe_ord(self._memory_view[mdc_sig_len])

        encoded_data = self._memory_view[mdc_sig_len:].tobytes()
        signed_data = eth_common_utils.keccak_hash(encoded_data)
        remote_pubkey = crypto_utils.recover_public_key(signed_data, signature)

        self._public_key = remote_pubkey

        if not crypto_utils.verify_signature(remote_pubkey, signature,
                                             signed_data):
            raise InvalidSignatureError(
                "Message signature does not match public key")

        encoded_payload = self._memory_view[mdc_sig_len + eth_common_constants.
                                            MSG_TYPE_LEN:].tobytes()

        self._deserialize_rlp_payload(encoded_payload)

        self._is_deserialized = True
Ejemplo n.º 2
0
    def test_sign_and_verify_signature__valid_signature(self):
        dummy_private_key = crypto_utils.make_private_key(
            helpers.generate_bytearray(111))
        public_key = crypto_utils.private_to_public_key(dummy_private_key)

        # generate random bytes
        msg = helpers.generate_bytearray(222)
        msg_hash = eth_common_utils.keccak_hash(msg)

        signature = crypto_utils.sign(msg_hash, dummy_private_key)

        self.assertTrue(
            crypto_utils.verify_signature(public_key, signature, msg_hash))
Ejemplo n.º 3
0
def verify_eth_transaction_signature(transaction: Transaction) -> bool:
    """
    checks eth transaction signature
    :param transaction:
    :return: if signature matches public key
    """
    try:
        signature = transaction.signature()
        unsigned_msg = transaction.get_unsigned()
        public_key = crypto_utils.recover_public_key(unsigned_msg, signature, keccak_hash)
        return crypto_utils.verify_signature(public_key, signature, keccak_hash(memoryview(unsigned_msg)))
    # pylint: disable=broad-except
    except Exception:
        return False
Ejemplo n.º 4
0
    def verify(self, signature, message):
        """
        Verifies signature for message
        :param signature: signature
        :param message: message
        :return: True if signature is valid, False otherwise
        """

        if len(signature) != eth_common_constants.SIGNATURE_LEN:
            raise ValueError(
                "Signature len of {0} is expected but was {1}".format(
                    eth_common_constants.SIGNATURE_LEN, len(signature)))

        return crypto_utils.verify_signature(self.get_raw_public_key(),
                                             signature, message)