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

Wrong padding in toString() function #198

Open
macor161 opened this issue Sep 12, 2018 · 7 comments
Open

Wrong padding in toString() function #198

macor161 opened this issue Sep 12, 2018 · 7 comments
Labels

Comments

@macor161
Copy link

When the length of a number is larger than the requested padding, toString will add an unnecessary padding.

For example:

new BN('123').toString(10, 2)

Should return 123 but returns 0123.

Or

new BN('12345678').toString(10, 6)

Should return 12345678 but returns 000012345678.

A simple fix would be to first check if the length is actually smaller than the requested padding before applying it.

I can submit a pull request to fix this issue.

@dcousens dcousens added the bug label Sep 12, 2018
@dcousens
Copy link
Contributor

I can submit a pull request to fix this issue.

@macor161 please do!

But if you don't have time, please please submit a failing test 👍

@fanatid
Copy link
Collaborator

fanatid commented Sep 12, 2018

wow, currently this looks like not padding at all? 🤔

bn.js/lib/bn.js

Line 474 in e69c617

while (out.length % padding !== 0) {

@macor161
Copy link
Author

I submited a pull request with the fix and a test.
Let me know if you need anything else!

@therightstuff
Copy link

@macor161 I think you're right to bring the #199 conversation back here until we reach some kind of consensus

@fanatid @dcousens

@macor161
Copy link
Author

@therightstuff I like the idea of changing the signature, I think it's better than introducing a new function.

What do you think of those 2 signatures?:

toString(base: number, bytePadding: number);
toString(options: Object);

Internally, it would look something like this:

toString(base: number | options: Object, [bytePadding: number])

And options would be:

base: number = 10
bytePadding: number = 0
padding: number = 0

This keeps the function backward compatible and is easily extendible

@macor161
Copy link
Author

Let me know when you reach a consensus, I will submit a pull request! :)

@macor161
Copy link
Author

I will close this issue as there doesn't seem to be any activity.

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

No branches or pull requests

4 participants