Skip to content

Commit

Permalink
feat(core/ethereum): EIP-712
Browse files Browse the repository at this point in the history
Implemented EIP-712 typed data signatures in Ethereum app.

Add eth_abi into pyproject deps

device test for EIP 712

fixed hex decoding for address

fixup! fixed hex decoding for address

code quality, more pythonic code, removing unused imports

running black and isort on changed files

trezorctl file input for EIP 712 data signing

fixup! code quality, more pythonic code, removing unused imports

fixup! fixup! code quality, more pythonic code, removing unused imports

necessary changes after rebase to master

unit tests for sign_typed_data.py

new protobuf messages, working for nonarray types

simplified and verified solution for our simple data

support for simple arrays, without their confirmation

reverting protobuf value messages to bytes, appropriate changes

showing arrays in Trezor, code quality improvements

data validation on Trezor, minor improvements

using custom types for storing type data instead of dicts, addressing feedback from review

moving helper functions to its own file, tests for decode_data

additional overall tests

support for arrays of structs

adding support for metamask_v4_compat variable

using HashWriter object to collect the final hash continously

minor improvements in code quality

validate_field_type function

streaming values from client without saving them, missing UI

prototype of streamed UI using confirm_properties

accounting for bytes in data, more data types in integration tests

rebase on master, using f-strings

minor fixes and improvements from code review

StructHasher class for the whole hashing process

mypy and style changes

asking users whether to show structs and arrays

protobuf descriptions to fix make defs_check

unifying comments, mypy fix

unit tests for StructHasher class

UI fixtures, skipping device tests for T1

addressing majority of code review comments about code quality and structure

changing file structure - layouts, helpers, sign_typed_data

decode_data renaming and docstring, renaming unit test file

using tuples instead of lists in elifs

layout improvements

excluding core/src/apps/common/confirm.py file from the PR

True/False returning layout with Show more button

code review layout improvements

forgotten br_type argument to should_show_more
  • Loading branch information
grdddj committed Oct 26, 2021
1 parent 6f06914 commit f5afce4
Show file tree
Hide file tree
Showing 19 changed files with 2,269 additions and 21 deletions.
86 changes: 86 additions & 0 deletions common/protob/messages-ethereum.proto
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,92 @@ message EthereumMessageSignature {
required string address = 3; // address used to sign the message
}

/**
* Request: Ask device to sign typed data
* @start
* @next EthereumTypedDataStructRequest
* @next EthereumTypedDataValueRequest
* @next EthereumTypedDataSignature
* @next Failure
*/
message EthereumSignTypedData {
repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
required string primary_type = 2; // name of the root message struct
optional bool metamask_v4_compat = 3 [default=true]; // use MetaMask v4 (see https://github.com/MetaMask/eth-sig-util/issues/106)
}

/**
* Response: Device asks for type information about a struct.
* @next EthereumTypedDataStructAck
*/
message EthereumTypedDataStructRequest {
required string name = 1; // name of the requested struct
}

/**
* Request: Type information about a struct.
* @next EthereumTypedDataStructRequest
*/
message EthereumTypedDataStructAck {
repeated EthereumStructMember members = 1;

message EthereumStructMember {
required EthereumFieldType type = 1;
required string name = 2;
}

message EthereumFieldType {
required EthereumDataType data_type = 1;
optional uint32 size = 2; // for integer types: size in bytes (uint8 has size 1, uint256 has size 32)
// for bytes types: size in bytes, or unset for dynamic
// for arrays: size in elements, or unset for dynamic
// for structs: number of members
// for string, bool and address: unset
optional EthereumFieldType entry_type = 3; // for array types, type of single entry
optional string struct_name = 4; // for structs: its name
}

enum EthereumDataType {
UINT = 1;
INT = 2;
BYTES = 3;
STRING = 4;
BOOL = 5;
ADDRESS = 6;
ARRAY = 7;
STRUCT = 8;
}
}

/**
* Response: Device asks for data at the specific member path.
* @next EthereumTypedDataValueAck
*/
message EthereumTypedDataValueRequest {
repeated uint32 member_path = 1; // member path requested by device
}

/**
* Request: Single value of a specific atomic field.
* @next EthereumTypedDataValueRequest
*/
message EthereumTypedDataValueAck {
required bytes value = 1;
// * atomic types: value of the member.
// Length must match the `size` of the corresponding field type.
// * array types: number of elements, encoded as uint16.
// * struct types: undefined, Trezor will not query a struct field.
}

/**
* Response: Signed typed data
* @end
*/
message EthereumTypedDataSignature {
required bytes signature = 1; // signature of the typed data
required string address = 2; // address used to sign the typed data
}

/**
* Request: Ask device to verify message
* @start
Expand Down
6 changes: 6 additions & 0 deletions common/protob/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ enum MessageType {
MessageType_EthereumSignMessage = 64 [(wire_in) = true];
MessageType_EthereumVerifyMessage = 65 [(wire_in) = true];
MessageType_EthereumMessageSignature = 66 [(wire_out) = true];
MessageType_EthereumSignTypedData = 464 [(wire_in) = true];
MessageType_EthereumTypedDataStructRequest = 465 [(wire_out) = true];
MessageType_EthereumTypedDataStructAck = 466 [(wire_in) = true];
MessageType_EthereumTypedDataValueRequest = 467 [(wire_out) = true];
MessageType_EthereumTypedDataValueAck = 468 [(wire_in) = true];
MessageType_EthereumTypedDataSignature = 469 [(wire_out) = true];

// NEM
MessageType_NEMGetAddress = 67 [(wire_in) = true];
Expand Down
8 changes: 6 additions & 2 deletions core/src/all_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@
import trezor.enums.CardanoTxSigningMode
trezor.enums.CardanoTxWitnessType
import trezor.enums.CardanoTxWitnessType
trezor.enums.EthereumDataType
import trezor.enums.EthereumDataType
trezor.enums.NEMImportanceTransferMode
import trezor.enums.NEMImportanceTransferMode
trezor.enums.NEMModificationType
Expand Down Expand Up @@ -502,12 +504,12 @@
import apps.eos.writers
apps.ethereum
import apps.ethereum
apps.ethereum.address
import apps.ethereum.address
apps.ethereum.get_address
import apps.ethereum.get_address
apps.ethereum.get_public_key
import apps.ethereum.get_public_key
apps.ethereum.helpers
import apps.ethereum.helpers
apps.ethereum.keychain
import apps.ethereum.keychain
apps.ethereum.layout
Expand All @@ -520,6 +522,8 @@
import apps.ethereum.sign_tx
apps.ethereum.sign_tx_eip1559
import apps.ethereum.sign_tx_eip1559
apps.ethereum.sign_typed_data
import apps.ethereum.sign_typed_data
apps.ethereum.tokens
import apps.ethereum.tokens
apps.ethereum.verify_message
Expand Down
71 changes: 71 additions & 0 deletions core/src/apps/ethereum/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from ubinascii import hexlify, unhexlify

from trezor import wire
from trezor.enums import EthereumDataType
from trezor.messages import EthereumFieldType

if False:
from .networks import NetworkInfo
Expand Down Expand Up @@ -53,3 +55,72 @@ def bytes_from_address(address: str) -> bytes:

raise wire.ProcessError("Ethereum: Invalid address length")


def get_type_name(field: EthereumFieldType) -> str:
"""Create a string from type definition (like uint256 or bytes16)."""
data_type = field.data_type
size = field.size

TYPE_TRANSLATION_DICT = {
EthereumDataType.UINT: "uint",
EthereumDataType.INT: "int",
EthereumDataType.BYTES: "bytes",
EthereumDataType.STRING: "string",
EthereumDataType.BOOL: "bool",
EthereumDataType.ADDRESS: "address",
}

if data_type == EthereumDataType.STRUCT:
assert field.struct_name is not None # validate_field_type
return field.struct_name
elif data_type == EthereumDataType.ARRAY:
assert field.entry_type is not None # validate_field_type
type_name = get_type_name(field.entry_type)
if size is None:
return f"{type_name}[]"
else:
return f"{type_name}[{size}]"
elif data_type in (EthereumDataType.UINT, EthereumDataType.INT):
assert size is not None # validate_field_type
return TYPE_TRANSLATION_DICT[data_type] + str(size * 8)
elif data_type == EthereumDataType.BYTES:
if size:
return TYPE_TRANSLATION_DICT[data_type] + str(size)
else:
return TYPE_TRANSLATION_DICT[data_type]
else:
# all remaining types can use the name directly
# if the data_type is left out, this will raise KeyError
return TYPE_TRANSLATION_DICT[data_type]


def decode_typed_data(data: bytes, type_name: str) -> str:
"""Used by sign_typed_data module to show data to user."""
if type_name.startswith("bytes"):
return hexlify(data).decode()
elif type_name == "string":
return data.decode()
elif type_name == "address":
return address_from_bytes(data)
elif type_name == "bool":
return "true" if data == b"\x01" else "false"
elif type_name.startswith("uint"):
return str(int.from_bytes(data, "big"))
elif type_name.startswith("int"):
# Micropython does not implement "signed" arg in int.from_bytes()
return str(from_bytes_bigendian_signed(data))

raise ValueError # Unsupported data type for direct field decoding


def from_bytes_bigendian_signed(b: bytes) -> int:
negative = b[0] & 0x80
if negative:
neg_b = bytearray(b)
for i in range(len(neg_b)):
neg_b[i] = ~neg_b[i] & 0xFF
result = int.from_bytes(neg_b, "big")
return -result - 1
else:
return int.from_bytes(b, "big")
>>>>>>> theirs
2 changes: 2 additions & 0 deletions core/src/apps/ethereum/keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
EthereumSignMessage,
EthereumSignTx,
EthereumSignTxEIP1559,
EthereumSignTypedData,
)

from apps.common.keychain import MsgOut, Handler, HandlerWithKeychain
Expand All @@ -23,6 +24,7 @@
EthereumGetPublicKey,
EthereumSignTx,
EthereumSignMessage,
EthereumSignTypedData,
]
MsgIn = TypeVar("MsgIn", bound=EthereumMessages)

Expand Down
129 changes: 124 additions & 5 deletions core/src/apps/ethereum/layout.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from ubinascii import hexlify

from trezor import ui
from trezor.enums import ButtonRequestType
from trezor.strings import format_amount
from trezor.enums import ButtonRequestType, EthereumDataType
from trezor.messages import EthereumFieldType, EthereumStructMember
from trezor.strings import format_amount, format_plural
from trezor.ui.layouts import (
confirm_address,
confirm_amount,
confirm_blob,
confirm_output,
confirm_text,
should_show_more,
)
from trezor.ui.layouts.tt.altcoin import confirm_total_ethereum

from . import networks, tokens
from .address import address_from_bytes
from .helpers import address_from_bytes, decode_typed_data, get_type_name

if False:
from typing import Awaitable

from typing import Awaitable, Iterable, Optional
from trezor.wire import Context


Expand Down Expand Up @@ -107,6 +109,115 @@ def require_confirm_data(ctx: Context, data: bytes, data_total: int) -> Awaitabl
)


async def confirm_hash(ctx: Context, message_hash: bytes) -> None:
await confirm_blob(
ctx,
"confirm_hash",
title="Confirm hash",
data="0x" + hexlify(message_hash).decode(),
hold=True,
ask_pagination=True,
)


async def should_show_domain(ctx: Context, name: bytes, version: bytes) -> bool:
domain_name = decode_typed_data(name, "string")
domain_version = decode_typed_data(version, "string")

para = (
(ui.NORMAL, "Name and version"),
(ui.BOLD, domain_name),
(ui.BOLD, domain_version),
)
return await should_show_more(
ctx,
title="Confirm domain",
para=para,
button_text="Show full domain",
br_type="should_show_domain",
)


async def should_show_struct(
ctx: Context,
description: str,
data_members: list[EthereumStructMember],
title: str = "Confirm struct",
button_text: str = "Show full struct",
) -> bool:
para = (
(ui.BOLD, description),
(
ui.NORMAL,
"Contains " + format_plural("{count} {plural}", len(data_members), "key"),
),
(ui.NORMAL, ", ".join(field.name for field in data_members)),
)
return await should_show_more(
ctx,
title=title,
para=para,
button_text=button_text,
br_type="should_show_struct",
)


async def should_show_array(
ctx: Context,
parent_objects: Iterable[str],
data_type: str,
size: int,
) -> bool:
para = (
(ui.NORMAL, "Array of " + format_plural("{count} {plural}", size, data_type)),
)
return await should_show_more(
ctx,
title=limit_str(".".join(parent_objects)),
para=para,
button_text="Show full array",
br_type="should_show_array",
)


async def confirm_typed_value(
ctx: Context,
name: str,
value: bytes,
parent_objects: list[str],
field: EthereumFieldType,
array_index: Optional[int] = None,
) -> None:
type_name = get_type_name(field)

if array_index is not None:
title = limit_str(".".join(parent_objects + [name]))
description = f"[{array_index}] ({type_name})"
else:
title = limit_str(".".join(parent_objects))
description = f"{name} ({type_name})"

data = decode_typed_data(value, type_name)

if field.data_type in (EthereumDataType.ADDRESS, EthereumDataType.BYTES):
await confirm_blob(
ctx,
"confirm_typed_value",
title=title,
data=data,
description=description,
ask_pagination=True,
)
else:
await confirm_text(
ctx,
"confirm_typed_value",
title=title,
data=data,
description=description,
)


def format_ethereum_amount(
value: int, token: tokens.TokenInfo | None, chain_id: int
) -> str:
Expand All @@ -123,3 +234,11 @@ def format_ethereum_amount(
decimals = 0

return f"{format_amount(value, decimals)} {suffix}"


def limit_str(s: str, limit: int = 16) -> str:
"""Shortens string to show the last <limit> characters."""
if len(s) <= limit + 2:
return s

return ".." + s[-limit:]
Loading

0 comments on commit f5afce4

Please sign in to comment.