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

Return r, s, and v without leading 0's #1288

Merged
merged 2 commits into from
Jan 15, 2018
Merged

Conversation

carver
Copy link
Contributor

@carver carver commented Jan 11, 2018

These values are quantities, not data. Other nodes return r, s, and v as hex-encoded quantites.

See #1170

@coveralls
Copy link

coveralls commented Jan 11, 2018

Coverage Status

Coverage increased (+0.04%) to 85.736% when pulling ac28015 on carver:r-and-s-quantities into 746e440 on ethereum:1.0.

@frozeman
Copy link
Contributor

frozeman commented Jan 12, 2018

I would actually like to get a third opinion on that. As bytes are technically invalid if they are uneven.
@chriseth, @axic and @holiman any opinion?

@@ -42,14 +42,14 @@ var isNot = function(value) {
};

var trimLeadingZero = function (hex) {
while (hex && hex.startsWith('0x00')) {
hex = '0x' + hex.slice(4);
while (hex && hex.startsWith('0x0')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is what if the rs, value is 0x00001234 Then a even number is made uneven

rawTx[7] = trimLeadingZero(rawTx[7]);
rawTx[8] = trimLeadingZero(rawTx[8]);
rawTx[7] = makeEven(trimLeadingZero(rawTx[7]));
rawTx[8] = makeEven(trimLeadingZero(rawTx[8]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And are you sure passing a modified value into RLP.decode is not going to break anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean RLP.encode?

It looks like it dynamically calculates the length, so I don't see any problems: https://github.com/MaiaVictor/eth-lib/blob/master/lib/rlp.js#L27

In fact, I believe leaving out 0 bytes at the beginning of an int in the RLP-encoded data is a more standard approach. (not sure if it's required though)

@frozeman
Copy link
Contributor

frozeman commented Jan 12, 2018

The tests pass here.
But this would actually be an argument for padding the r,s values at all times with leading 0s

@frozeman
Copy link
Contributor

Additionally allowing the odd numbers again will make this problem reappear: #1258

Though its very important that signatures are correctly validated!

@carver
Copy link
Contributor Author

carver commented Jan 12, 2018

But this would actually be an argument for padding the r,s values at all times with leading 0s

Additionally allowing the odd numbers again will make this problem reappear: #1258

Let's separate the two places where r and s values are used:

  1. Just before rlp encoding: https://github.com/ethereum/web3.js/pull/1288/files/ac28015f159b8bece403bcac356008cc19cb037c#diff-7bb2a20126193b9ecfe4723f83429c49R168
  2. When returning from signTransaction: https://github.com/ethereum/web3.js/pull/1288/files/ac28015f159b8bece403bcac356008cc19cb037c#diff-7bb2a20126193b9ecfe4723f83429c49R177

This PR intends to not change the functionality of # 1 - the change there was only a refactor. That means that the #1258 problem should not reappear, and I agree that r and s values should be provided with an even number of hex characters. (because that's what this rlp library implementation expects)

This PR only changes the functionality of # 2, trimming all leading 0s. This brings it in line with what the nodes themselves return when returning (non-rlp-encoded) values of r and s.

@frozeman
Copy link
Contributor

Ill merge this.
@malissa and @jellegerbrandy can you test the 1.0 branch when its merged?

@frozeman frozeman merged commit e58052d into web3:1.0 Jan 15, 2018
@frozeman
Copy link
Contributor

frozeman commented Jan 15, 2018

@carver this also should fix #1289. Can you confirm?

@carver carver deleted the r-and-s-quantities branch January 15, 2018 16:32
@carver
Copy link
Contributor Author

carver commented Jan 15, 2018

@frozeman No, some more work would have to be done, maybe in web3 or maybe in eth-lib. #1290 is the test that should pass.

Some options I see, highest preference first:

  • web3 could zero-pad r and s to 32 bytes before concatenating and handing off the signature to eth-lib
  • eth-lib could add a new recovery option that accepts r, s, and v as individual arguments (splitting them is the only thing it has trouble with IIRC)
  • (more of a strawman than a good idea:) eth-lib could detect short r and s, maybe by doing some modified RLP decoding

@carver
Copy link
Contributor Author

carver commented Jan 15, 2018

On further investigation, it looks like eth-lib itself is responsible for encoding the signature. So the best solution is to fix eth-lib's Account.encodeSignature()

@carver
Copy link
Contributor Author

carver commented Jan 15, 2018

Submitted a PR at VictorTaelin/eth-lib#4

That repo has 3 open pull requests that go back to October, and has never merged or closed an incoming pull request. Maybe eth-lib needs to be forked into the ethereum org to be maintained?

Edit: BTW, I confirmed that PR 4 does cause this PR's test to pass.

@frozeman
Copy link
Contributor

I will talk to @MaiaVictor, but we could also leftpad this before sending it to eth-lib

@frozeman
Copy link
Contributor

I merged eth-lib and the tests pass. Ill create a new version soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants