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

Parse JSON numbers properly to BigNumbers #578

Open
manuelwedler opened this issue Dec 2, 2020 · 0 comments
Open

Parse JSON numbers properly to BigNumbers #578

manuelwedler opened this issue Dec 2, 2020 · 0 comments
Labels
bug Something isn't working

Comments

@manuelwedler
Copy link
Contributor

During the review of #573 @andrevmatos found a potential problem: our losslessParse() function, which is used for parsing JSON API responses, only converts numbers of the type string into BigNumber. This is because the Raiden API does return all numbers as strings. However the PFS API uses the JSON number type for numbers. This is no problem at the moment, because we don't use the numbers from the PFS API, but could be one in the future. We should use a library for parsing.

André's comment from #573:

This losslessParse may work only if the BNs are stringified on the JSONs (since this revive function from JSON.parse is called only after the number already got parsed as such), while it seems the current PFS implementation encodes the capacity and other BNs as JSON number. This may only be working now if the numbers on your test were small (i.e. < 2^53).
We had this issue on the LC, and ended up using json-bigint lib to parse these JSONs. Check here for our implementation.

Notice you need to use a proper decoder/schema validator if you go the way we did, since it'll ensure these values are kept as strings, or use it with your reviver function after using the lib to ensure the BNs are kept as strings or bigint or whatever.. We already had the schemas in place, so went with it, but you can use the reviver as well if you don't care that much with the typesafety there.

@manuelwedler manuelwedler added the bug Something isn't working label Dec 2, 2020
@manuelwedler manuelwedler added this to the Product Backlog milestone Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant