1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-19 14:08:11 +00:00
trezor-firmware/python/src/trezorlib/ethereum.py

103 lines
3.0 KiB
Python
Raw Normal View History

# This file is part of the Trezor project.
#
2019-05-29 16:44:09 +00:00
# Copyright (C) 2012-2019 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from . import exceptions, messages
from .tools import expect, normalize_nfc, session
def int_to_big_endian(value):
2018-08-13 16:21:24 +00:00
return value.to_bytes((value.bit_length() + 7) // 8, "big")
# ====== Client functions ====== #
@expect(messages.EthereumAddress, field="address")
def get_address(client, n, show_display=False, multisig=None):
return client.call(
messages.EthereumGetAddress(address_n=n, show_display=show_display)
)
@expect(messages.EthereumPublicKey)
2019-01-11 12:19:42 +00:00
def get_public_node(client, n, show_display=False):
return client.call(
messages.EthereumGetPublicKey(address_n=n, show_display=show_display)
2019-01-11 12:19:42 +00:00
)
@session
2018-08-13 16:21:24 +00:00
def sign_tx(
client,
n,
nonce,
gas_price,
gas_limit,
to,
value,
data=None,
chain_id=None,
tx_type=None,
):
msg = messages.EthereumSignTx(
address_n=n,
nonce=int_to_big_endian(nonce),
gas_price=int_to_big_endian(gas_price),
gas_limit=int_to_big_endian(gas_limit),
2018-08-13 16:21:24 +00:00
value=int_to_big_endian(value),
2019-08-29 11:53:51 +00:00
to=to,
chain_id=chain_id,
tx_type=tx_type,
2018-08-13 16:21:24 +00:00
)
if data:
msg.data_length = len(data)
data, chunk = data[1024:], data[:1024]
msg.data_initial_chunk = chunk
response = client.call(msg)
while response.data_length is not None:
data_length = response.data_length
data, chunk = data[data_length:], data[:data_length]
response = client.call(messages.EthereumTxAck(data_chunk=chunk))
2018-08-20 13:51:31 +00:00
# https://github.com/trezor/trezor-core/pull/311
2018-08-15 09:31:30 +00:00
# only signature bit returned. recalculate signature_v
if response.signature_v <= 1:
response.signature_v += 2 * chain_id + 35
return response.signature_v, response.signature_r, response.signature_s
@expect(messages.EthereumMessageSignature)
def sign_message(client, n, message):
message = normalize_nfc(message)
return client.call(messages.EthereumSignMessage(address_n=n, message=message))
def verify_message(client, address, signature, message):
message = normalize_nfc(message)
try:
2018-08-13 16:21:24 +00:00
resp = client.call(
messages.EthereumVerifyMessage(
2018-08-13 16:21:24 +00:00
address=address, signature=signature, message=message
)
)
except exceptions.TrezorFailure:
return False
return isinstance(resp, messages.Success)