Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core,legacy): add support for Ethereum 64-bit chain_id #1771

Merged

Conversation

arbitrarylink
Copy link
Contributor

Adds support for Ethereum 64-bit chain_id This also makes sure that chain_id of 0 is rejected on core the same way it is rejected in legacy.

Test-Results.txt

@arbitrarygbg
Copy link

This supersedes #1741

@prusnak
Copy link
Member

prusnak commented Aug 24, 2021

I think we could use this opportunity to make MAX_CHAIN_ID do what the name suggests. That is, we should throw an error when provided value in protobuf exceeds the maximum and simplify this:

if msg.chain_id > MAX_CHAIN_ID:
req.signature_v -= 27
elif msg.chain_id:
req.signature_v += 2 * msg.chain_id + 8

into this:

if msg.chain_id: 
  req.signature_v += 2 * msg.chain_id + 8 

and this:

if (chain_id > MAX_CHAIN_ID) {
msg_tx_request.signature_v = v;
} else if (chain_id) {
msg_tx_request.signature_v = v + 2 * chain_id + 35;
} else {
msg_tx_request.signature_v = v + 27;
}

into this:

msg_tx_request.signature_v = v + 27;
if (chain_id) {
  msg_tx_request.signature_v = v + 2 * chain_id + 8; 
}

What do you think @matejcik?

@matejcik
Copy link
Contributor

My thoughts were in the opposite direction: always require chain_id, and then just send a single bit for signature_v and let the host compute the correct value. (both Connect and trezorlib already do this).

I'm not sure if there are any Ethereum networks out there not using EIP-155, but we certainly don't support any, seeing as our Ethereum network database is keyed on chain_id.

At that point, perhaps we could go the extra mile and introduce chain_id_bytes field in case someone decides that 64 bits are not enough. (Ethereum seems to internally use 256-bit numbers, and all other numeric values in the message are bytes already.)

@prusnak
Copy link
Member

prusnak commented Aug 24, 2021

My thoughts were in the opposite direction: always require chain_id

I let you decide on this. Either seems fine to me. I just want to decrease the number of source lines in our codebases.

At that point, perhaps we could go the extra mile and introduce chain_id_bytes field in case someone decides that 64 bits are not enough.

I prefer not to do this, because this would break all existing implementations. OTOH uint32 and uint64 look exactly the same on the wire.

@matejcik matejcik added this to To be reviewed in Firmware Pull Requests via automation Aug 24, 2021
@matejcik matejcik moved this from To be reviewed to In progress in Firmware Pull Requests Aug 24, 2021
@arbitrarylink
Copy link
Contributor Author

Would it make sense for me to just remove the check_chain_id function, revert the tests to no longer set chain_id=1, add the two tests for MAX_CHAIN_ID and MAX_CHAIN_ID+1 and then leave all of the other suggestions for another future PR?

@matejcik
Copy link
Contributor

@arbitrarylink yes, that makes sense. Please do that.

@andrewkozlik andrewkozlik removed their request for review August 25, 2021 14:35
@arbitrarylink
Copy link
Contributor Author

@arbitrarylink yes, that makes sense. Please do that.

The updates have been made.

@matejcik matejcik changed the base branch from master to matejcik/ethereum-typing August 31, 2021 09:28
@matejcik matejcik changed the title Adds support for Ethereum 64-bit chain_id feat(core,legacy): add support for Ethereum 64-bit chain_id Aug 31, 2021
@matejcik matejcik merged commit 816868e into trezor:matejcik/ethereum-typing Aug 31, 2021
Firmware Pull Requests automation moved this from In progress to Merged Aug 31, 2021
@matejcik
Copy link
Contributor

thanks for your work. I'm merging this to a separate Ethereum branch for now, I want to tweak it a little and make it a part of a larger change set (see also #1781)

@0xMarto
Copy link

0xMarto commented Aug 31, 2021

@matejcik quick question, are you planning to merge ethereum-typing into master for the next firmware release?

@matejcik
Copy link
Contributor

matejcik commented Sep 1, 2021

yes

@matejcik matejcik mentioned this pull request Sep 2, 2021
3 tasks
@matejcik
Copy link
Contributor

matejcik commented Sep 7, 2021

@arbitrarylink could you share more details about chain_id==0 ?
Reading the EIP-155 spec, it seems that the value is technically allowed, so I'm inclined to unify the behavior, instead of disallowing zero in core, allowing it in legacy.

Do you know of any reason why that would be a bad idea?

@arbitrarylink
Copy link
Contributor Author

@arbitrarylink could you share more details about chain_id==0 ?
Reading the EIP-155 spec, it seems that the value is technically allowed, so I'm inclined to unify the behavior, instead of disallowing zero in core, allowing it in legacy.

Do you know of any reason why that would be a bad idea?

Currently Trezor One (Legacy) disallows chain_id of 0. It returns an error of "Error: DataError: ChainId out of bounds". This comes from the code ./legacy/firmware/ethereum.c: 444.

The code in this PR I had earlier was to disallow chain_id 0 in Trezor T (core) was to make the core behave like the legacy. This code has been reverted as requested.

If you want both devices to support a chain_id of 0 you will have to modify the Legacy code.

@matejcik
Copy link
Contributor

matejcik commented Sep 7, 2021

i'm well aware.

I'm asking if you know something about one or the other behavior being "more correct" in some sense.

@arbitrarylink
Copy link
Contributor Author

There is an EIP that is not yet accepted that will explicitly make chain_id of 0 invalid. https://eips.ethereum.org/EIPS/eip-3788

@matejcik
Copy link
Contributor

matejcik commented Sep 7, 2021

Thanks for the info!

This means that currently transactions with chain_id 0 are valid and actually can be used.

@arbitrarylink arbitrarylink deleted the 64bit-chainId branch September 7, 2021 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants