def eth_sign_msg(
        self, msg: bytes, keypath: Sequence[int], coin: eth.ETHCoin = eth.ETH
    ) -> bytes:
        """
        Signs message, the msg will be prefixed with "\x19Ethereum message\n" + len(msg) in the
        hardware
        """
        request = eth.ETHRequest()
        # pylint: disable=no-member
        request.sign_msg.CopyFrom(eth.ETHSignMessageRequest(coin=coin, keypath=keypath, msg=msg))

        supports_antiklepto = self.version >= semver.VersionInfo(9, 5, 0)
        if supports_antiklepto:
            host_nonce = os.urandom(32)

            request.sign_msg.host_nonce_commitment.commitment = antiklepto_host_commit(host_nonce)
            signer_commitment = self._eth_msg_query(
                request, expected_response="antiklepto_signer_commitment"
            ).antiklepto_signer_commitment.commitment

            request = eth.ETHRequest()
            request.antiklepto_signature.CopyFrom(
                antiklepto.AntiKleptoSignatureRequest(host_nonce=host_nonce)
            )

            signature = self._eth_msg_query(request, expected_response="sign").sign.signature
            antiklepto_verify(host_nonce, signer_commitment, signature[:64])

            if self.debug:
                print(f"Antiklepto nonce verification PASSED")

            return signature

        return self._eth_msg_query(request, expected_response="sign").sign.signature
Example #2
0
 def eth_sign_msg(self, msg: bytes, keypath: List[int], coin: eth.ETHCoin = eth.ETH) -> bytes:
     """
     Signs message, the msg will be prefixed with "\x19Ethereum message\n" + len(msg) in the
     hardware
     """
     request = eth.ETHRequest()
     # pylint: disable=no-member
     request.sign_msg.CopyFrom(eth.ETHSignMessageRequest(coin=coin, keypath=keypath, msg=msg))
     return self._eth_msg_query(request, expected_response="sign").sign.signature
Example #3
0
    def eth_sign_msg(self,
                     msg: bytes,
                     keypath: Sequence[int],
                     chain_id: int = 1) -> bytes:
        """
        Signs message, the msg will be prefixed with "\x19Ethereum message\n" + len(msg) in the
        hardware. 27 is added to the recID to denote an uncompressed pubkey.
        """
        def format_as_uncompressed(sig: bytes) -> bytes:
            # 27 is the magic constant to add to the recoverable ID to denote an uncompressed
            # pubkey.
            modified_signature = list(sig)
            modified_signature[64] += 27
            return bytes(modified_signature)

        request = eth.ETHRequest()
        # pylint: disable=no-member
        request.sign_msg.CopyFrom(
            eth.ETHSignMessageRequest(coin=self._eth_coin(chain_id),
                                      chain_id=chain_id,
                                      keypath=keypath,
                                      msg=msg))

        supports_antiklepto = self.version >= semver.VersionInfo(9, 5, 0)
        if supports_antiklepto:
            host_nonce = os.urandom(32)

            request.sign_msg.host_nonce_commitment.commitment = antiklepto_host_commit(
                host_nonce)
            signer_commitment = self._eth_msg_query(
                request, expected_response="antiklepto_signer_commitment"
            ).antiklepto_signer_commitment.commitment

            request = eth.ETHRequest()
            request.antiklepto_signature.CopyFrom(
                antiklepto.AntiKleptoSignatureRequest(host_nonce=host_nonce))

            signature = self._eth_msg_query(
                request, expected_response="sign").sign.signature
            antiklepto_verify(host_nonce, signer_commitment, signature[:64])

            if self.debug:
                print("Antiklepto nonce verification PASSED")

            return format_as_uncompressed(signature)

        signature = self._eth_msg_query(
            request, expected_response="sign").sign.signature
        return format_as_uncompressed(signature)