-
Notifications
You must be signed in to change notification settings - Fork 272
signature/address: support for high recovery and chain IDs #290
Conversation
Codecov Report
@@ Coverage Diff @@
## master #290 +/- ##
==========================================
+ Coverage 97.10% 97.13% +0.02%
==========================================
Files 11 11
Lines 415 454 +39
Branches 71 79 +8
==========================================
+ Hits 403 441 +38
Misses 3 3
- Partials 9 10 +1
Continue to review full report at Codecov.
|
Really curious about this PR. Is this because of EIP155? Some chainIDs don't fit in a number, so computing the V requires a big number? |
@alcuadrado Yep, this was a problem on the YoloV3 network, which has a chain id of I will add this transaction as test. |
You know what's extra curious about it? In most platforms that's not a BN! It fits in a uint64, but js 🥲 |
Okay, isolated the failing transaction on Yolo and added it as test. Ready for review. |
Yes, also thought after writing that we likely should leave Not sure, I know we want to get this released soon since the client work depends on this, on the other hand we might want to also adopt the other methods as well which are touched by this since this would better justify a release and I think this is something others would directly profit from. Have written together a short list to get an overview on the task:
I think this should be doable, WDYT? 🤔 If you want you can directly integrate here but I could also do tomorrow if you don't find the extra time. For the function overloading cases I guess the most clean way of doing this is likely to always return the same type as the input type? This would then be slightly more work, but I guess it's better than always returning a Monorepo integration: @ryanio has also already suggested this and therefore already did #289 in preparation. 😋 I don't have the capacity right now to do the integration and I also feel that the monorepo is not enough on stable ground right now to do such a heavy integration on top (CI/Coverage often failing and things). But this is definitely planned! 😄 |
(or @ryanio you can also directly pick this up if you want to! 😄 ) |
Yeah this should not be so hard. So what is the plan here? For anything where it would make sense to allow In future PRs we can start to use |
Handling |
I think it makes sense to on this first round stay somewhat narrow here and not do all methods somehow accepting numbers, so e.g. for many of the buffer-related methods ( Yes, I think in most cases, an internal conversion to |
…or ecrecover v and chainId input, added type tests
Phew, this is a lot harder than I expected. Already had various iterations on this, I started by expanding the
So I will introduce 4. on the next push. Hmpfg. |
…tional input value checks
Ok, this 24279bf is finally how I would keep it for this specific function, will continue with the other methods today or tomorrow. |
Have added a planned-breaking-change note to #291 |
…lidChecksumAddress(), expanded tests
…Signature(), expanded tests
Ok, this is now ready for review. 😄 Have left out Otherwise I think the main v and chainId related functionality should be covered. Side note: first planned to also include a release commit to get this out quicker (PR is needed for continued client work), but since this has now gotten so large I've omitted to not overload. |
(or does someone feel uneasy here with opening to so many type inputs? we can alternatively also just limit to (the existing) number + Buffer) |
(wonder since we have lately rather reduced type variety) |
(I have some (but no strong) tendency to think that in this case it is ok) |
src/bytes.ts
Outdated
@@ -112,7 +112,7 @@ export const unpadHexString = function(a: string): string { | |||
*/ | |||
export const toBuffer = function( | |||
v: | |||
| string | |||
| PrefixedHexString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming that originally we expected these strings to be 0x
prefixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah actually PrefixedHexString
is simply a string type right now, maybe in the future typescript will give us some way to enforce that the first two characters are 0x
.
looks great, however instead of converting the types inside each method (seems repetitive) what if we introduced a helper method e.g. Then we can also refactor out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few comments here.
Fun fact, I cannot approve my own PR 😛
Hi @ryanio can you eventually finish this PR? I won't be able to finish today or tomorrow and would be nice if we get this done, I'm fine with all changes. 😀 |
yes for sure I can do in a few hours when I'm back on my computer 👍 |
(so finish === include eventual improvement suggestions, otherwise this should be mostly ready) |
ok I added a commit (243fd47) that adds a I would appreciate some TypeScript guru help, I was able to get the I might also want to add some independent tests for (BTW @holgerd77, do you think it was okay to refactor out the "Performance optimization" here? I can add it back in, but opted for the new simplicity and built in checks introduced by the |
add error if output number is greater than MAX_SAFE_INTEGER
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this lgtm now! I would appreciate another set of 👀 on my latest commits but otherwise 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ryanio,
thanks for these additions, this toType()
method is really genius and a great helper. I have to say that I feel - a bit - uneasy nevertheless to have that much code with such specific functionality and walking on relatively unproven ground somewhat last-minute to a PR.
Maybe on a next round we want to do things like this in a separate PR and in doubt keep out of an outstanding release.
Will give this a go here nevertheless on this turn, I DO think this should be ok after having checked the code twice. Also checked that the types are working when toType
is applied (was also somewhat irritated by these as any
casts at first), so that's great! 😄
Ah, and refactoring out the performance optimization seems to be ok for me, thought this might be faster, but had no real insight here and wast just a precautious measure, eventually overblown.
Ok, will directly prepare a release here. 😄
privateKey: Buffer, | ||
chainId: BN | string | Buffer | ||
): ECDSASignatureBuffer | ||
export function ecsign(msgHash: Buffer, privateKey: Buffer, chainId: BNLike): ECDSASignatureBuffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was intentional to split the BNLike
type to these two overlays and not use the BNLike
type here. Otherwise there is an ambiguity on the number
type.
Update: I just checked in the code, I would have expected for TypeScript
to assume ECDSASignature | ECDSASignatureBuffer
as a return type when used with a number
chainId
, this is not the case though, seems TypeScript
takes the first working signature.
Will therefore let this pass, feeling a little uneasy on this nevertheless.
In ethereumjs/ethereumjs-monorepo#1129 I noticed that
ecrecover
does not have support for very high recovery IDs (which happen on very high chain IDs). This PR allows to pass BNs or Buffers to ecrecover, to allow this support.I will add a test to show that this works for high chain IDs by taking a YoloV3 transaction.