Ejemplo n.º 1
0
    def sign_transaction(self, tx):
        """Sign a provided transaction, either with our private key or a Trezor hardware wallet.

        :param tx: Transaction to sign
        :return: Signed transaction
        """
        logger.info('Signing transaction: %s', tx)
        if self.trezor is not None:
            chain_id = tx['chainId']
            txobj = Transaction(
                nonce=tx['nonce'],
                gasprice=tx['gasPrice'],
                startgas=tx['gas'],
                to=tx['to'],
                value=tx['value'],
                data=bytes.fromhex(tx['data'][2:]),
            )

            v, r, s = trezoreth.sign_tx(
                self.trezor,
                n=self.address_n,
                nonce=txobj.nonce,
                gas_price=txobj.gasprice,
                gas_limit=txobj.startgas,
                to=txobj.to,
                value=txobj.value,
                data=txobj.data,
                chain_id=chain_id,
            )

            r = int.from_bytes(r, byteorder='big')
            s = int.from_bytes(s, byteorder='big')

            return rlp.encode(txobj.copy(v=v, r=r, s=s))
        else:
            return self.w3.eth.account.signTransaction(
                tx, self.priv_key).rawTransaction