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

bigNumberToByteArray: Avoid unexpected behaviour by throwing exception #1066

Merged
merged 2 commits into from
Aug 17, 2020

Conversation

davidyuk
Copy link
Member

@davidyuk davidyuk commented Aug 16, 2020

Implementing #1060 I have found that passing a non-integer amount value looks working, but actually the amount value is gets broken instead of being rounded somehow. BigNumber.toString(16) returns a hex value separated with a dot for non-integer values that get passed to Buffer.from that just ignores the part after .:

> Buffer.from('390cf7311ac9f5ab9746ab.2ab9f559b3d07c84b5dd', 'hex')
<Buffer 39 0c f7 31 1a c9 f5 ab 97 46 ab>

Alignment by adding a leading 0 also may not work properly in this case.

bigNumberToByteArray is handling non-integers not properly, so I propose to throw an exception then.

@davidyuk davidyuk added this to the 7.7.0 milestone Aug 16, 2020
@davidyuk davidyuk requested a review from mradkov August 16, 2020 08:41
@davidyuk davidyuk marked this pull request as ready for review August 16, 2020 08:41
@davidyuk davidyuk force-pushed the feature/validate-to-byte-array-arguments branch from 21ebcb7 to d879411 Compare August 16, 2020 14:06
Copy link
Contributor

@mradkov mradkov left a comment

Choose a reason for hiding this comment

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

Sounds reasonable. Code change looks good. Is the exception being catched somewhere and what is the use-case where this is most likely the user to hit this case?

@davidyuk
Copy link
Member Author

It looks like sdk uses bigNumberToByteArray properly in all cases, but the end-user may ask sdk to transfer for example 123.55 aettos and before this fix the sdk would send 7 aettos (BN(123.55) => 7b.8ccccccccccccccccccd => Buffer.from('07b.8cc...', 'hex') => [07]) and with this fix sdk will reject that attempt. I assume that the user will figure out that he needs to round the token value somehow after seeing the exception.

@davidyuk davidyuk merged commit 70c3a9c into develop Aug 17, 2020
@davidyuk davidyuk deleted the feature/validate-to-byte-array-arguments branch August 17, 2020 01:41
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.

2 participants